From mboxrd@z Thu Jan 1 00:00:00 1970 X-Msuck: nntp://news.gmane.org/gmane.linux.lib.musl.general/9096 Path: news.gmane.org!not-for-mail From: Alexander Cherepanov Newsgroups: gmane.linux.lib.musl.general Subject: Re: string word-at-a-time and atomic.h FAQ on twitter Date: Tue, 12 Jan 2016 15:41:57 +0300 Message-ID: <5694F495.3010203@openwall.com> References: <20160105164640.GL23362@port70.net> <20160105175040.GA238@brightrain.aerifal.cx> <5690315B.5010206@openwall.com> <20160108220535.GO238@brightrain.aerifal.cx> <56903A8E.90802@openwall.com> <20160108225951.GP238@brightrain.aerifal.cx> Reply-To: musl@lists.openwall.com NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Trace: ger.gmane.org 1452602535 12792 80.91.229.3 (12 Jan 2016 12:42:15 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Tue, 12 Jan 2016 12:42:15 +0000 (UTC) To: musl@lists.openwall.com Original-X-From: musl-return-9109-gllmg-musl=m.gmane.org@lists.openwall.com Tue Jan 12 13:42:11 2016 Return-path: Envelope-to: gllmg-musl@m.gmane.org Original-Received: from mother.openwall.net ([195.42.179.200]) by plane.gmane.org with smtp (Exim 4.69) (envelope-from ) id 1aIyH5-0001su-52 for gllmg-musl@m.gmane.org; Tue, 12 Jan 2016 13:42:11 +0100 Original-Received: (qmail 17970 invoked by uid 550); 12 Jan 2016 12:42:09 -0000 Mailing-List: contact musl-help@lists.openwall.com; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-ID: Original-Received: (qmail 17952 invoked from network); 12 Jan 2016 12:42:09 -0000 X-Enigmail-Draft-Status: N1110 In-Reply-To: <20160108225951.GP238@brightrain.aerifal.cx> Xref: news.gmane.org gmane.linux.lib.musl.general:9096 Archived-At: On 2016-01-09 01:59, Rich Felker wrote: > On Sat, Jan 09, 2016 at 01:39:10AM +0300, Alexander Cherepanov wrote: >>>>>> this takes care of oob access, but the bytes outside the passed >>>>>> object might change concurrently i.e. strlen might introduce a >>>>>> data race: again this is a problem on the abstract c language >>>>>> level that may be solved e.g. by making all accesses to those >>>>>> bytes relaxed atomic, but user code is not under libc control. >>>>>> in practice the code works if HASZERO reads the word once so it >>>>>> does arithmetics with a consistent value (because the memory >>>>>> model of the underlying machine does not treat such race >>>>>> undefined and it does not propagate unspecified value bits nor >>>>>> has trap representations). >>>>> >>>>> Indeed, this seems like less of a practical concern. >>>> >>>> HASZERO reads the word twice so this should be a problem for >>>> unoptimized code on big-endian platforms. >>> >>> The number of abstract-machine reads is irrelevant unless we use >>> volatile here. A good compiler will always reduce it to one read, and >>> a bad compiler is always free to turn it into multiple reads. >> >> Ok, I'll reformulate: is compiling musl on a big-endian platform >> with optimizations turned off officially supported? > > Yes, and I don't see why you expect this case to break due to data > race issues. Right, I was too fast. I checked that outside bytes affect computations on inside bytes (on BE platforms) but the effect is not strong enough to break the code. Sorry for the noise. Perhaps a comment is warranted in this and other hairy cases (like qsort)? As for a list of affected functions, the first approximation: $ grep -rF HASZERO | grep -v '#define' src/string/stpcpy.c: for (; !HASZERO(*ws); *wd++ = *ws++); src/string/memchr.c: for (w = (const void *)s; n>=SS && !HASZERO(*w^k); w++, n-=SS); src/string/strlen.c: for (w = (const void *)s; !HASZERO(*w); w++); src/string/strchrnul.c: for (w = (void *)s; !HASZERO(*w) && !HASZERO(*w^k); w++); src/string/memccpy.c: for (; n>=sizeof(size_t) && !HASZERO(*ws^k); src/string/strlcpy.c: for (; n>=sizeof(size_t) && !HASZERO(*ws); src/string/stpncpy.c: for (; n>=sizeof(size_t) && !HASZERO(*ws); HASZERO is not guarded by a size check in strpcpy, strlen and strchrnul. -- Alexander Cherepanov