From mboxrd@z Thu Jan 1 00:00:00 1970 X-Msuck: nntp://news.gmane.org/gmane.linux.lib.musl.general/1192 Path: news.gmane.org!not-for-mail From: orc Newsgroups: gmane.linux.lib.musl.general Subject: Re: Silly question about strncpy(), strlen() and related funcs Date: Tue, 19 Jun 2012 14:44:23 +0800 Message-ID: <20120619144423.548c5400@sibserver.ru> References: <20120619025409.4ec1ed49@sibserver.ru> <20120618214821.GG163@brightrain.aerifal.cx> Reply-To: musl@lists.openwall.com NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-Trace: dough.gmane.org 1340088398 2309 80.91.229.3 (19 Jun 2012 06:46:38 GMT) X-Complaints-To: usenet@dough.gmane.org NNTP-Posting-Date: Tue, 19 Jun 2012 06:46:38 +0000 (UTC) To: musl@lists.openwall.com Original-X-From: musl-return-1192-gllmg-musl=m.gmane.org@lists.openwall.com Tue Jun 19 08:46:36 2012 Return-path: Envelope-to: gllmg-musl@plane.gmane.org Original-Received: from mother.openwall.net ([195.42.179.200]) by plane.gmane.org with smtp (Exim 4.69) (envelope-from ) id 1SgsCt-0003gg-1a for gllmg-musl@plane.gmane.org; Tue, 19 Jun 2012 08:46:31 +0200 Original-Received: (qmail 5226 invoked by uid 550); 19 Jun 2012 06:46:30 -0000 Mailing-List: contact musl-help@lists.openwall.com; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: Original-Received: (qmail 5199 invoked from network); 19 Jun 2012 06:46:26 -0000 In-Reply-To: <20120618214821.GG163@brightrain.aerifal.cx> X-Mailer: claws-mail Xref: news.gmane.org gmane.linux.lib.musl.general:1192 Archived-At: On Mon, 18 Jun 2012 17:48:21 -0400 Rich Felker wrote: > On Tue, Jun 19, 2012 at 02:54:09AM +0800, orc wrote: > > Did not reached Rich privately, so I want to ask publicly: > > > > What ALIGN and additional checks like 'if (((uintptr_t)s & ALIGN) == > > ((uintptr_t)d & ALIGN))' {...} are mean in src/string/strpcpy.c and > > similiar functions? > > Hi. Sorry I didn't get back to you earlier. I meant to but lost your > email amidst all the gnulib stuff. > > The point of this test is that we want to copy larger data units at a > time (system word size) instead of single bytes if possible, but this > is only portable if the source and destination of each read and write > is properly aligned. The initial addresses don't have to be aligned as > long as their remainder modulo the alignment is the same; the initial > misaligned part can be copied byte-at-a-time, and as long as the > the source and destination misalignment initially matched, they'll > both be aligned for word-at-a-time copying after the initial segment. > > Some systems, such as x86, would actually allow misaligned > reads/writes in general, but we still need to avoid them for many > functions. Why? Because a misaligned read might cross page boundaries > into an unreadable/nonexistant page, and thereby cause SIGSEGV or > SIGBUS. Reading past the end of a string is no problem as long as we > stay in the same page, so it could work on x86 if we align the source > address and just leave the destination possibly misaligned, but x86 is > about the _only_ arch where that's safe, and if we really want to take > advantage of larger-unit copies in the misaligned case, I think it > should just be done with x86 asm rather than adding special cases in > the C code. With asm, we could also use the string functions (rep > movsd etc.) which give optimal performance on most cpus. > > Rich Thanks for the detailed explanation. Just wondered that BSDs implement only one-byte-at-a-time versions of this functions. Interesting code, always learning something new.