From mboxrd@z Thu Jan 1 00:00:00 1970 X-Msuck: nntp://news.gmane.org/gmane.linux.lib.musl.general/1182 Path: news.gmane.org!not-for-mail From: Rich Felker Newsgroups: gmane.linux.lib.musl.general Subject: Re: Silly question about strncpy(), strlen() and related funcs Date: Mon, 18 Jun 2012 17:48:21 -0400 Message-ID: <20120618214821.GG163@brightrain.aerifal.cx> References: <20120619025409.4ec1ed49@sibserver.ru> Reply-To: musl@lists.openwall.com NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: dough.gmane.org 1340056355 28807 80.91.229.3 (18 Jun 2012 21:52:35 GMT) X-Complaints-To: usenet@dough.gmane.org NNTP-Posting-Date: Mon, 18 Jun 2012 21:52:35 +0000 (UTC) To: musl@lists.openwall.com Original-X-From: musl-return-1183-gllmg-musl=m.gmane.org@lists.openwall.com Mon Jun 18 23:52:34 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 1Sgjs9-0004ml-AR for gllmg-musl@plane.gmane.org; Mon, 18 Jun 2012 23:52:33 +0200 Original-Received: (qmail 6066 invoked by uid 550); 18 Jun 2012 21:52:33 -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 6057 invoked from network); 18 Jun 2012 21:52:33 -0000 Content-Disposition: inline In-Reply-To: <20120619025409.4ec1ed49@sibserver.ru> User-Agent: Mutt/1.5.21 (2010-09-15) Xref: news.gmane.org gmane.linux.lib.musl.general:1182 Archived-At: 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