From: John Reiser <jreiser@bitwagon.com>
To: musl@lists.openwall.com
Subject: Re: [PATCH] Add comments to i386 assembly source
Date: Mon, 1 Jan 2018 14:57:02 -0800 [thread overview]
Message-ID: <5caf910a-dd98-6836-c70f-6a98cf8a9d22@bitwagon.com> (raw)
In-Reply-To: <20180101195224.tpkl5g5w66rzwzz3@voyager>
There's a bug. clone() is a user-level function that can be used
independently of the musl internal implementation of threads.
Thus when clone() in musl/src/linux/clone.c calls
return __syscall_ret(__clone(func, stack, flags, arg, ptid, tls, ctid));
then the i386 implementation of __clone has no guarantee about
the value in %gs, and it is a bug to assume that (%gs >> 3)
fits in 8 bits.
The code in musl/src/thread/i386/clone.s wastes up to 12 bytes
when aligning the new stack, by aligning before [pre-]allocating
space for the one argument to the thread function.
This code fixes the %gs bug, wastes no stack space in the new thread,
and is 6 bytes smaller (83 ==> 77; -7.2%):
===== musl/src/thread/i386/clone.s
__NR_clone = 120
NBPW = 4 /* Number of Bytes Per Word */
.text
.global __clone
.type __clone,@function
__clone: /* clone(func, stack, flags, arg, ptid, tls, ctid) */
push %esi /* non-standard; save .text space */
lea 2*NBPW(%esp),%esi /* &func */
push %ebx
push %ebp
push %edi
/* 'cld' must be in effect upon entry to a .globl function */
lodsl; xchg %eax,%ebp /* func (save) */
lodsl; lea -NBPW(%eax),%ecx /* stack; pre-allocate space for 1 arg */
lodsl; xchg %eax,%ebx /* flags */
and $-16,%ecx /* 16-byte align new stack */
lodsl; mov %eax,(%ecx) /* arg to new thread */
lodsl; xchg %eax,%edx /* ptid */
push $0x51 /* flags */
push $0xffff /* limit */
lodsl; push %eax /* tls */
xor %eax,%eax; mov %gs,%ax; shr $3,%eax; push %eax /* segment # */
mov (%esi),%edi /* ctid */
mov %esp,%esi /* &segment_descriptor on current stack */
push $__NR_clone; pop %eax
int $128
test %eax,%eax
jnz 1f
mov %ebp,%eax /* func */
xor %ebp,%ebp /* end chain of stack frames */
call *%eax /* func(arg) */
mov %eax,%ebx /* rv is arg1 to syscall */
xor %eax,%eax
inc %eax /* __NR_exit */
int $128
hlt
1: add $16,%esp
pop %edi
pop %ebp
pop %ebx
pop %esi
ret
=====
--
next prev parent reply other threads:[~2018-01-01 22:57 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-12-23 9:45 Markus Wichmann
2017-12-31 4:15 ` John Reiser
2017-12-31 6:54 ` Markus Wichmann
2017-12-31 15:49 ` Rich Felker
2018-01-01 19:52 ` Markus Wichmann
2018-01-01 22:57 ` John Reiser [this message]
2018-01-02 1:49 ` Rich Felker
2018-01-02 3:15 ` John Reiser
2018-01-02 19:49 ` Rich Felker
2018-01-02 18:24 ` a third bug in musl clone() John Reiser
2018-01-02 19:58 ` Rich Felker
2018-01-02 22:09 ` Florian Weimer
2018-01-03 2:51 ` Rich Felker
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=5caf910a-dd98-6836-c70f-6a98cf8a9d22@bitwagon.com \
--to=jreiser@bitwagon.com \
--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).