1651 Commits

Author SHA1 Message Date
Rich Felker
14a835b386 implement pthread_getattr_np
this function is mainly (purely?) for obtaining stack address
information, but we also provide the detach state since it's easy to
do anyway.
2013-03-31 23:25:55 -04:00
Rich Felker
ccc7b4c3a1 remove __SYSCALL_SSLEN arch macro in favor of using public _NSIG
the issue at hand is that many syscalls require as an argument the
kernel-ABI size of sigset_t, intended to allow the kernel to switch to
a larger sigset_t in the future. previously, each arch was defining
this size in syscall_arch.h, which was redundant with the definition
of _NSIG in bits/signal.h. as it's used in some not-quite-portable
application code as well, _NSIG is much more likely to be recognized
and understood immediately by someone reading the code, and it's also
shorter and less cluttered.

note that _NSIG is actually 65/129, not 64/128, but the division takes
care of throwing away the off-by-one part.
2013-03-26 23:07:31 -04:00
Rich Felker
00f1521fdd provide emulation of fcntl F_DUPFD_CLOEXEC on old kernels
I'm not entirely happy with the amount of ugliness here, but since
F_DUPFD_CLOEXEC is used elsewhere in code that's expected to work on
old kernels (popen), it seems necessary. reportedly even some modern
kernels went back and broke F_DUPFD_CLOEXEC (making it behave like
plain F_DUPFD), so it might be necessary to add some additional fixup
code later to deal with that issue too.
2013-03-26 22:54:57 -04:00
Rich Felker
ae7399bfd8 in pipe2, use pipe() rather than __syscall(SYS_pipe, ...) for fallback
SYS_pipe is not usable directly in general, since mips has a very
broken calling convention for the pipe syscall. instead, just call the
function, so that the mips-specific ugliness is isolated in
mips/pipe.s and not copied elsewhere.
2013-03-25 11:34:22 -04:00
Rich Felker
9cb6e6ea12 rewrite popen to use posix_spawn instead of fragile vfork hacks 2013-03-24 22:41:38 -04:00
Rich Felker
7914ce9204 remove cruft from pre-posix_spawn version of the system function 2013-03-24 22:40:54 -04:00
Rich Felker
427c0ca79e fix multiple bugs in syslog interfaces
1. as reported by William Haddon, the value returned by snprintf was
wrongly used as a length passed to sendto, despite it possibly
exceeding the buffer length. this could lead to invalid reads and
leaking additional data to syslog.

2. openlog was storing a pointer to the ident string passed by the
caller, rather than copying it. this bug is shared with (and even
documented in) other implementations like glibc, but such behavior
does not seem to meet the requirements of the standard.

3. extremely long ident provided to openlog, or corrupt ident due to
the above issue, could possibly have resulted in buffer overflows.
despite having the potential for smashing the stack, i believe the
impact is low since ident points to a short string literal in typical
application usage (and per the above bug, other usages will break
horribly on other implementations).

4. when used with LOG_NDELAY, openlog was not connecting the
newly-opened socket; sendto was being used instead. this defeated the
main purpose of LOG_NDELAY: preparing for chroot.

5. the default facility was not being used at all, so all messages
without an explicit facility passed to syslog were getting logged at
the kernel facility.

6. setlogmask was not thread-safe; no synchronization was performed
updating the mask. the fix uses atomics rather than locking to avoid
introducing a lock in the fast path for messages whose priority is not
in the mask.

7. in some code paths, the syslog lock was being unlocked twice; this
could result in releasing a lock that was actually held by a different
thread.

some additional enhancements to syslog such as a default identifier
based on argv[0] or similar may still be desired; at this time, only
the above-listed bugs have been fixed.
2013-03-23 18:59:30 -04:00
Rich Felker
da1442c9a8 fix types for wctype_t and wctrans_t
wctype_t was incorrectly "int" rather than "long" on x86_64. not only
is this an ABI incompatibility; it's also a major design flaw if we
ever wanted wctype_t to be implemented as a pointer, which would be
necessary if locales support custom character classes, since int is
too small to store a converted pointer. this commit fixes wctype_t to
be unsigned long on all archs, matching the LSB ABI; this change does
not matter for C code, but for C++ it affects mangling.

the same issue applied to wctrans_t. glibc/LSB defines this type as
const __int32_t *, but since no such definition is visible, I've just
expanded the definition, int, everywhere.

it would be nice if these types (which don't vary by arch) could be in
wctype.h, but the OB XSI requirement in POSIX that wchar.h expose some
types and functions from wctype.h precludes doing so. glibc works
around this with some hideous hacks, but trying to duplicate that
would go against the intent of musl's headers.
2013-03-04 19:22:14 -05:00
Rich Felker
5afc74fbaa fix integer type issue in strverscmp
lenl-lenr is not a valid expression for a signed int return value from
strverscmp, since after implicit conversion from size_t to int this
difference could have the wrong sign or might even be zero. using the
difference for char values works since they're bounded well within the
range of differences representable by int, but it does not work for
size_t values.
2013-02-26 01:42:11 -05:00
Rich Felker
4853c1f7f7 implement non-stub strverscmp
patch by Isaac Dunham.
2013-02-26 01:36:47 -05:00
Rich Felker
e864ddc368 replace stub with working strcasestr 2013-02-21 23:54:25 -05:00
Rich Felker
330fd96213 fix wrong return value from wmemmove on forward copies 2013-02-21 23:19:18 -05:00
Rich Felker
8872e4e481 use memcmp instead of str[n]cmp for temp function XXXXXX checking 2013-02-20 23:01:22 -05:00
Rich Felker
8d2f8064aa fix error cases in mkostemps core
1. wrong return value and missing errno for negative suffix len
2. failure to catch suffix len > strlen
3. remove unwanted clearing of input string in invalid case
2013-02-20 22:58:46 -05:00
Rich Felker
3b00675bf5 remove leftover unused variable in mktemp after refactoring 2013-02-20 22:56:53 -05:00
Rich Felker
2cc63358cd add mkostemp, mkstemps, and mkostemps functions and reorganize temp internals
based on patch contributed by Anthony G. Basile (blueness)

some issues remain with the filename generation algorithm and other
small bugs, but this patch has been sitting around long enough that I
feel it's best to get it committed and then work out any remaining
issues.
2013-02-20 22:43:23 -05:00
Rich Felker
f78cdbe899 remove unused #undef environ now that libc.h no longer #defines it 2013-02-17 14:30:19 -05:00
Rich Felker
23ccb80fcb consistently use the internal name __environ for environ
patch by Jens Gustedt.
previously, the intended policy was to use __environ in code that must
conform to the ISO C namespace requirements, and environ elsewhere.
this policy was not followed in practice anyway, making things
confusing. on top of that, Jens reported that certain combinations of
link-time optimization options were breaking with the inconsistent
references; this seems to be a compiler or linker bug, but having it
go away is a nice side effect of the changes made here.
2013-02-17 14:24:39 -05:00
Rich Felker
cac872957e add fgetgrent function
based on patch by Isaac Dunham, moved to its own file to avoid
increasing bss on static linked programs not using this nonstandard
function but using the standard getgrent function, and vice versa.
2013-02-17 13:21:56 -05:00
Rich Felker
b9e08308f4 fix typo in newly-added error message for EOWNERDEAD 2013-02-07 03:13:35 -05:00
Rich Felker
d75348ddda improve error strings
this definitely has the potential to be a bikeshed topic, so some
justification is in order. most of the changes made fit into one of
the following categories:
1. alignment with text in posix, xsh 2.3
2. eliminating overly-specific text for shared error codes
3. making the message match more closely with the macro name
4. removing extraneous words

in particular, the EAGAIN/EWOULDBLOCK text is updated to match the
description of EAGAIN (which covers both uses) rather than saying the
operation would block, and ENOTSUP/EOPNOTSUPP is updated not to
mention sockets.

the distinction between ENFILE/EMFILE has also been clarified; ENFILE
is aligned with the posix text, and EMFILE, which lacks concise posix
text matching any historic message, is updated to emphasize that the
exhausted resource is not open files/open file descriptions, but
rather the integer 'address space' of file descriptors.

some messages may be further tweaked based on feedback.
2013-02-07 03:05:54 -05:00
Rich Felker
a8799356d5 base system() on posix_spawn
this avoids duplicating the fragile logic for executing an external
program without fork.
2013-02-03 18:21:04 -05:00
Rich Felker
4862864fc1 fix unsigned comparison bug in posix_spawn
read should never return anything but 0 or sizeof ec here, but if it
does, we want to treat any other return as "success". then the caller
will get back the pid and is responsible for waiting on it when it
immediately exits.
2013-02-03 17:09:47 -05:00
Rich Felker
fb6b159d9e overhaul posix_spawn to use CLONE_VM instead of vfork
the proposed change was described in detail in detail previously on
the mailing list. in short, vfork is unsafe because:

1. the compiler could make optimizations that cause the child to
clobber the parent's local vars.

2. strace is buggy and allows the vforking parent to run before the
child execs when run under strace.

the new design uses a close-on-exec pipe instead of vfork semantics to
synchronize the parent and child so that the parent does not return
before the child has finished using its arguments (and now, also its
stack). this also allows reporting exec failures to the caller instead
of giving the caller a child that mysteriously exits with status 127
on exec error.

basic testing has been performed on both the success and failure code
paths. further testing should be done.
2013-02-03 16:42:40 -05:00
Rich Felker
89d3df6e54 streamline old-kernel fallback path of pipe2 to use syscalls directly
also, don't waste code/time on F_GETFL since pipes always have blank
flags initially (at least on old kernels, which are all this fallback
code matters for).
2013-02-03 16:09:05 -05:00
Rich Felker
a47ad3ebce dynamically allocate storage for gethostby* buffers
this change shaves ~1k off libc.so bss size, and also avoids hard
errors in the case where the static buffer was not large enough to
hold the result.

this whole framework is really ugly and might should be replaced or at
least heavily overhauled when some changes/factorizations are made to
getaddrinfo internals in the future.
2013-02-02 02:07:38 -05:00
Rich Felker
c0193550a0 fix blank ai_canonname from getaddrinfo for non-CNAMEs 2013-02-02 01:59:53 -05:00
Rich Felker
b0302863a3 fix memory leak due to double call to getaddrinfo in gethostbyname* 2013-02-02 01:32:12 -05:00
Rich Felker
70b584bc94 fix error returns in gethostby*_r functions
they're supposed to return an error code rather than using errno.
2013-02-02 01:31:10 -05:00
Rich Felker
74025c80ce fix uninitialized map_len being used in munmap failure paths in load_library
this bug seems to have been introduced when the map_library signatures
was changed to return the mapping in a temp dso structure instead of
into separate variables.
2013-02-02 00:59:25 -05:00
Rich Felker
72768ea99e fix stale locks left behind when pthread_create fails
this bug seems to have been around a long time.
2013-02-01 22:25:19 -05:00
Rich Felker
077549e0d4 if pthread_create fails, it must not attempt mmap if there is no mapping
this bug was introduced when support for application-provided stacks
was originally added.
2013-02-01 22:23:24 -05:00
Rich Felker
d5142642b8 pthread stack treatment overhaul for application-provided stacks, etc.
the main goal of these changes is to address the case where an
application provides a stack of size N, but TLS has size M that's a
significant portion of the size N (or even larger than N), thus giving
the application less stack space than it expected or no stack at all!

the new strategy pthread_create now uses is to only put TLS on the
application-provided stack if TLS is smaller than 1/8 of the stack
size or 2k, whichever is smaller. this ensures that the application
always has "close enough" to what it requested, and the threshold is
chosen heuristically to make sure "sane" amounts of TLS still end up
in the application-provided stack.

if TLS does not fit the above criteria, pthread_create uses mmap to
obtain space for TLS, but still uses the application-provided stack
for actual call frame stack. this is to avoid wasting memory, and for
the sake of supporting ugly hacks like garbage collection based on
assumptions that the implementation will use the provided stack range.

in order for the above heuristics to ever succeed, the amount of TLS
space wasted on POSIX TSD (pthread_key_create based) needed to be
reduced. otherwise, these changes would preclude any use of
pthread_create without mmap, which would have serious memory usage and
performance costs for applications trying to create huge numbers of
threads using pre-allocated stack space. the new value of
PTHREAD_KEYS_MAX is the minimum allowed by POSIX, 128. this should
still be plenty more than real-world applications need, especially now
that C11/gcc-style TLS is now supported in musl, and most apps and
libraries choose to use that instead of POSIX TSD when available.

at the same time, PTHREAD_STACK_MIN has been decreased. it was
originally set to PAGE_SIZE back when there was no support for TLS or
application-provided stacks, and requests smaller than a whole page
did not make sense. now, there are two good reasons to support
requests smaller than a page: (1) applications could provide
pre-allocated stacks smaller than a page, and (2) with smaller stack
sizes, stack+TLS+TSD can all fit in one page, making it possible for
applications which need huge numbers of threads with minimal stack
needs to allocate exactly one page per thread. the new value of
PTHREAD_STACK_MIN, 2k, is aligned with the minimum size for
sigaltstack.
2013-02-01 22:10:40 -05:00
rofl0r
c50925071c make some arrays const
this way they'll go into .rodata, decreasing memory pressure.
2013-02-02 03:19:25 +01:00
Rich Felker
facc6acbfd replace __wake function with macro that performs direct syscall
this should generate faster and smaller code, especially with inline
syscalls. the conditional with cnt is ugly, but thankfully cnt is
always a constant anyway so it gets evaluated at compile time. it may
be preferable to make separate __wake and __wakeall macros without a
count argument.

priv flag is not used yet; private futex support still needs to be
done at some point in the future.
2013-02-01 16:41:53 -05:00
Rich Felker
96fbcf7d80 fix up minor misplacement of restrict keyword in spawnattr sched stubs 2013-02-01 15:57:28 -05:00
Rich Felker
ae4b0b96d6 revert regex "cleanup" that seems unjustified and may break backtracking
it's not clear to me at the moment whether the code that was removed
(and which is now being re-added) is needed, but it's far from being a
no-op, and i don't want to risk breaking regex in this release.
2013-02-01 01:10:59 -05:00
Rich Felker
d5c164d5ff fix tm_to_time logic for number of days in november
report/patch by Hiltjo Posthuma <hiltjo@codemadness.org>
2013-01-26 11:40:40 -05:00
Rich Felker
4d07e5521e add support for RTLD_NOLOAD to dlopen
based on patch by Pierre Carrier <pierre@gcarrier.fr> that just added
the flag constant, but with minimal additional code so that it
actually works as documented. this is a nonstandard option but some
major software (reportedly, Firefox) uses it and it was easy to add
anyway.
2013-01-23 22:07:45 -05:00
Rich Felker
637dd2d383 fix regression in dlsym: rejection of special RTLD_* handles as invalid 2013-01-23 20:21:36 -05:00
Rich Felker
4d98280388 fix warning building dynlink.c stub for static libc
struct dso was not defined in this case, and it's not needed in the
code that was using it anyway; void pointers work just as well.
2013-01-16 11:49:00 -05:00
Rich Felker
cdc5c74c6a fix bug in dladdr that prevented resolving addresses in the PLT 2013-01-16 11:47:35 -05:00
Szabolcs Nagy
f05f59b804 remove unused "params" related code from regex
some structs and functions had reference to the params
feature of tre that is not used by the code anymore
2013-01-15 01:05:29 +01:00
Rich Felker
21f01e1bf9 Merge remote-tracking branch 'nsz/math' 2013-01-14 18:04:49 -05:00
Szabolcs Nagy
dd95916382 regex: remove an unused local variable from regexec
pos_start local variable is not used in tre_tnfa_run_backtrack
2013-01-14 00:06:49 +01:00
Szabolcs Nagy
9724defdb7 in crypt_des change unnecessary union keybuf into unsigned char[]
original FreeSec code accessed keybuf as uint32* and uint8* as well
(incorrectly), this got fixed with an union, but then it seems the
uint32* access is no longer needed so the code can be simplified
2013-01-13 23:54:48 +01:00
Szabolcs Nagy
30779ee1aa crypt: fix the prototype of md5_sum, sha256_sum and sha512_sum
the internal sha2 hash sum functions had incorrect array size
in the prototype for the message digest argument, fixed by
using pointer so it is not misleading
2013-01-13 23:18:32 +01:00
Szabolcs Nagy
a753f7fe7a fix lio_listio return value in LIO_WAIT mode 2013-01-13 23:15:39 +01:00
Rich Felker
6468fc9e37 check for invalid handles in dlsym/dlclose
this is wasteful and useless from a standpoint of sane programs, but
it is required by the standard, and the current requirements were
upheld with the closure of Austin Group issue #639:
http://austingroupbugs.net/view.php?id=639
2013-01-10 14:05:40 -05:00
Szabolcs Nagy
121e3a38a1 math: erf and erfc cleanup
common part of erf and erfc was put in a separate function which
saved some space and the new code is using unsigned arithmetics

erfcf had a bug: for some inputs in [7.95,8] the result had
more than 60ulp error: in expf(-z*z - 0.5625f) the argument
must be exact but not enough lowbits of z were zeroed,
-SET_FLOAT_WORD(z, ix&0xfffff000);
+SET_FLOAT_WORD(z, ix&0xffffe000);
fixed the issue
2013-01-07 23:54:13 +01:00