From: orc <orc@sibserver.ru>
To: musl@lists.openwall.com
Subject: Re: Silly question about strncpy(), strlen() and related funcs
Date: Tue, 19 Jun 2012 14:44:23 +0800 [thread overview]
Message-ID: <20120619144423.548c5400@sibserver.ru> (raw)
In-Reply-To: <20120618214821.GG163@brightrain.aerifal.cx>
On Mon, 18 Jun 2012 17:48:21 -0400
Rich Felker <dalias@aerifal.cx> 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.
prev parent reply other threads:[~2012-06-19 6:44 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2012-06-18 18:54 orc
2012-06-18 20:35 ` Szabolcs Nagy
2012-06-19 6:44 ` orc
2012-06-18 21:48 ` Rich Felker
2012-06-19 6:44 ` orc [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20120619144423.548c5400@sibserver.ru \
--to=orc@sibserver.ru \
--cc=musl@lists.openwall.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://git.vuxu.org/mirror/musl/
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).