Commit Graph

1906 Commits

Author SHA1 Message Date
Rich Felker 83966b369d add ether_aton[_r] and ether_ntoa[_r] functions
based on patch by Strake with minor stylistic changes, and combined
into a single file. this patch remained open for a long time due to
some question as to whether ether_aton would be better implemented in
terms of sscanf, and it's time something was committed, so here it is.
2013-06-25 21:15:27 -04:00
Rich Felker ef5507867b fix scanf %c conversion wrongly storing a terminating null byte
this seems to have been a regression from the refactoring which added
the 'm' modifier.
2013-06-22 17:23:45 -04:00
Rich Felker c20804500d fix major scanf breakage with unbuffered streams, fmemopen, etc.
the shgetc api, used internally in scanf and int/float scanning code
to handle field width limiting and pushback, was designed assuming
that pushback could be achieved via a simple decrement on the file
buffer pointer. this only worked by chance for regular FILE streams,
due to the linux readv bug workaround in __stdio_read which moves the
last requested byte through the buffer rather than directly back to
the caller. for unbuffered streams and streams not using __stdio_read
but some other underlying read function, the first character read
could be completely lost, and replaced by whatever junk happened to be
in the unget buffer.

to fix this, simply have shgetc, when it performs an underlying read
operation on the stream, store the character read at the -1 offset
from the read buffer pointer. this is valid even for unbuffered
streams, as they have an unget buffer located just below the start of
the zero-length buffer. the check to avoid storing the character when
it is already there is to handle the possibility of read-only buffers.
no application-exposed FILE types are allowed to use read-only
buffers, but sscanf and strto* may use them internally when calling
functions which use the shgetc api.
2013-06-22 17:11:17 -04:00
Rich Felker a494171a5a fix invalid access in aio notification
issue found and patch provided by Jens Gustedt. after the atomic store
to the error code field of the aiocb, the application is permitted to
free or reuse the storage, so further access is invalid. instead, use
the local copy that was already made.
2013-06-16 10:39:02 -04:00
Rich Felker 8600849d74 fix uninitialized variable in lio (aio) code 2013-06-16 01:40:17 -04:00
Rich Felker 20d01d83b5 improve the quality of output from rand_r
due to the interface requirement of having the full state contained in
a single object of type unsigned int, it is difficult to provide a
reasonable-quality implementation; most good PRNGs are immediately
ruled out because they need larger state. the old rand_r gave very
poor output (very short period) in its lower bits; normally, it's
desirable to throw away the low bits (as in rand()) when using a LCG,
but this is not possible since the state is only 32 bits and we need
31 bits of output.

glibc's rand_r uses the same LCG as musl's, but runs it for 3
iterations and only takes 10-11 bits from each iteration to construct
the output value. this partially fixes the period issue, but
introduces bias: not all outputs have the same frequency, and many do
not appear at all. with such a low period, the bias is likely to be
observable.

I tried many approaches to "fix" rand_r, and the simplest I found
which made it pass the "dieharder" tests was applying this
transformation to the output. the "temper" function is taken from
mersenne twister, where it seems to have been chosen for some rigorous
properties; here, the only formal property I'm using is that it's
one-to-one and thus avoids introducing bias.

should further deficiencies in rand_r be reported, the obvious "best"
solution is applying a 32-bit cryptographic block cipher in CTR mode.
I identified several possible ciphers that could be used directly or
adapted, but as they would be a lot slower and larger, I do not see a
justification for using them unless the current rand_r proves
deficient for some real-world use.
2013-06-12 18:20:48 -04:00
Rich Felker 4191d24476 add clock id macros for a number of new(ish) Linux-specific clocks
arguably CLOCK_MONOTONIC should be redirected to CLOCK_BOOTTIME with a
fallback for old kernels that don't support it, since Linux's
CLOCK_BOOTTIME semantics seem to match the spirit of the POSIX
requirements for CLOCK_MONOTONIC better than Linux's version of
CLOCK_MONOTONIC does. however, this is a change that would require
further discussion and research, so for now, I'm simply making them
all available.
2013-06-08 11:42:52 -04:00
Rich Felker 0173990284 fix the type of CLOCKS_PER_SEC to match new clock_t type
originally it was right on 32-bit archs and wrong on 64-bit, but after
recent changes it was wrong everywhere. with this commit, it's now
right everywhere.
2013-06-08 11:40:27 -04:00
Rich Felker ea200e38bd support cputime clocks for processes/threads other than self
apparently these features have been in Linux for a while now, so it
makes sense to support them. the bit twiddling seems utterly illogical
and wasteful, especially the negation, but that's how the kernel folks
chose to encode pids/tids into the clock id.
2013-06-08 11:36:41 -04:00
Szabolcs Nagy 0996faa3d7 prng: make rand_r have 2^32 period instead of 2^31
this is a minor fix to increase the period of the obsolete rand_r a bit.
an include header in __rand48_step.c is fixed as well.
2013-06-08 13:49:12 +00:00
Szabolcs Nagy c79cd27e9e prng: fix rand() to give good sequence with small state
some applications rely on the low bits of rand() to be reasonably good
quality prng, so now it fixed by using the top bits of a 64 bit LCG,
this is simple, has small state and passes statistical tests.
D.E. Knuth attributes the multiplier to C.E. Haynes in TAOCP Vol2 3.3.4
2013-06-08 13:31:10 +00:00
Rich Felker fd1d7be35f fix mixup in previous change to gcc wrapper 2013-06-07 10:18:07 -04:00
Rich Felker c161356002 make gcc-specific headers (intrinsics, etc.) available with wrapper
they are intentionally listed after the libc include directory so that
the gcc float.h, etc. don't get used in place of the libc ones.
2013-06-07 10:13:07 -04:00
Rich Felker f7244d205f improve handling of nonstandard fields in struct tm
defining tm_gmtoff and tm_zone as macros was breaking some application
code that used these names for its own purposes.
2013-06-07 09:54:45 -04:00
Rich Felker e039db27c2 implement 'm' modifier for wide scanf variants 2013-06-06 00:26:17 -04:00
Rich Felker 16a1e0365d implement the 'm' (malloc) modifier for scanf
this commit only covers the byte-based scanf-family functions. the
wide functions still lack support for the 'm' modifier.
2013-06-05 18:18:41 -04:00
Rich Felker de80ea9f1c refactor wide-char scanf string handling
this brings the wide version of the code into alignment with the
byte-based version, in preparation for adding support for the m
(malloc) modifier.
2013-06-05 16:53:26 -04:00
Rich Felker 1ab59de81e simplify some logic in scanf and remove redundant invalid-format check 2013-06-04 16:22:02 -04:00
Rich Felker f18846dd3a refactor scanf core to use common code path for all string formats
the concept here is that %s and %c are essentially special-cases of
%[, with some minimal additional special-casing.

aside from simplifying the code and reducing the number of complex
code-paths that would need changing to make optimizations later, the
main purpose of this change is to simplify addition of the 'm'
modifier which causes scanf to allocate storage for the string being
read.
2013-06-04 16:09:36 -04:00
Rich Felker a6d272127b align stack properly for calling global ctors/dtors on x86[_64]
failure to do so was causing crashes on x86_64 when ctors used SSE,
which was first observed when ctors called variadic functions due to
the SSE prologue code inserted into every variadic function.
2013-06-03 17:32:42 -04:00
Rich Felker 44b4d09fc0 ensure that thread dtv pointer is never null to optimize __tls_get_addr 2013-06-03 16:35:59 -04:00
Rich Felker d926565355 Merge remote-tracking branch 'nsz/review' 2013-05-26 18:22:12 -04:00
Szabolcs Nagy 31ff797787 fix the prototype of settimeofday to follow the original BSD declaration 2013-05-26 16:01:38 +00:00
Szabolcs Nagy 41c34d188a fix ioctl _IOR, _IOW, etc macros to avoid signed overflow (2<<30) 2013-05-26 15:49:08 +00:00
Szabolcs Nagy a6367a17d5 on x86_64 use long instead of long long for 64bit posix types
following glibc use the lowest rank 64bit integer type for ino_t etc.
this is eg. useful for printf format compatibility
2013-05-26 15:43:17 +00:00
Rich Felker 5e642b5a23 change underlying type of clock_t to be uniform and match ABI
previously we were using an unsigned type on 32-bit systems so that
subtraction would be well-defined when it wrapped, but since wrapping
is non-conforming anyway (when clock() overflows, it has to return -1)
the only use of unsigned would be to buy a little bit more time before
overflow. this does not seem worth having the type vary per-arch
(which leads to more arch-specific bugs) or disagree with the ABI musl
(mostly) follows.
2013-05-23 20:38:51 -04:00
Rich Felker 05453b37fc fix overflow behavior of clock() function
per Austin Group interpretation for issue #686, which cites the
requirements of ISO C, clock() cannot wrap. if the result is not
representable, it must return (clock_t)-1. in addition, the old code
was performing wrapping via signed overflow and thus invoking
undefined behavior.

since it seems impossible to accurately check for overflow with the
old times()-based fallback code, I have simply dropped the fallback
code for now, thus always returning -1 on ancient systems. if there's
a demand for making it work and somebody comes up with a way, it could
be reinstated, but the clock() function is essentially useless on
32-bit system anyway (it overflows in less than an hour).

it should be noted that I used LONG_MAX rather than ULONG_MAX, despite
32-bit archs using an unsigned type for clock_t. this discrepency with
the glibc/LSB type definitions will be fixed now; since wrapping of
clock_t is no longer supported, there's no use in it being unsigned.
2013-05-23 14:31:02 -04:00
Szabolcs Nagy 1e5eb73545 math: add fma TODO comments about the underflow issue
The underflow exception is not raised correctly in some
cornercases (see previous fma commit), added comments
with examples for fmaf, fmal and non-x86 fma.

In fmaf store the result before returning so it has the
correct precision when FLT_EVAL_METHOD!=0
2013-05-19 14:43:32 +00:00
Szabolcs Nagy ffd8ac2dd5 math: fix two fma issues (only affects non-nearest rounding mode, x86)
1) in downward rounding fma(1,1,-1) should be -0 but it was 0 with
gcc, the code was correct but gcc does not support FENV_ACCESS ON
so it used common subexpression elimination where it shouldn't have.
now volatile memory access is used as a barrier after fesetround.

2) in directed rounding modes there is no double rounding issue
so the complicated adjustments done for nearest rounding mode are
not needed. the only exception to this rule is raising the underflow
flag: assume "small" is an exactly representible subnormal value in
double precision and "verysmall" is a much smaller value so that
	(long double)(small plus verysmall) == small
then
	(double)(small plus verysmall)
raises underflow because the result is an inexact subnormal, but
	(double)(long double)(small plus verysmall)
does not because small is not a subnormal in long double precision
and it is exact in double precision.
now this problem is fixed by checking inexact using fenv when the
result is subnormal
2013-05-19 12:13:08 +00:00
Rich Felker 83af1dd65a Merge remote-tracking branch 'nsz/review' 2013-05-18 12:06:42 -04:00
Szabolcs Nagy bfda379358 math: sin cos cleanup
* use unsigned arithmetics
* use unsigned to store arg reduction quotient (so n&3 is understood)
* remove z=0.0 variables, use literal 0
* raise underflow and inexact exceptions properly when x is small
* fix spurious underflow in tanl
2013-05-18 14:40:22 +00:00
Rich Felker 69ee9b2cb1 make err.h functions print __progname
patch by Strake. previously is was not feasible to duplicate this
functionality of the functions these were modeled on, since argv[0]
was not saved at program startup, but now that it's available it's
easy to use.
2013-05-18 10:20:42 -04:00
Szabolcs Nagy 1d5ba3bb5a math: tan cleanups
* use unsigned arithmetics on the representation
* store arg reduction quotient in unsigned (so n%2 would work like n&1)
* use different convention to pass the arg reduction bit to __tan
  (this argument used to be 1 for even and -1 for odd reduction
  which meant obscure bithacks, the new n&1 is cleaner)
* raise inexact and underflow flags correctly for small x
  (tanl(x) may still raise spurious underflow for small but normal x)
  (this exception raising code increases codesize a bit, similar fixes
  are needed in many other places, it may worth investigating at some
  point if the inexact and underflow flags are worth raising correctly
  as this is not strictly required by the standard)
* tanf manual reduction optimization is kept for now
* tanl code path is cleaned up to follow similar logic to tan and tanf
2013-05-18 12:34:00 +00:00
Rich Felker 22730d6560 add FLT_TRUE_MIN, etc. macros from C11
there was some question as to how many decimal places to use, since
one decimal place is always sufficient to identify the smallest
denormal uniquely. for now, I'm following the example in the C
standard which is consistent with the other min/max macros we already
had in place.
2013-05-17 18:38:42 -04:00
Rich Felker ec9f5353b1 remove the __STDC_FORMAT_MACROS nonsense from inttypes.h
somehow I missed this when removing the corresponding
__STDC_LIMIT_MACROS and __STDC_CONSTANT_MACROS nonsense from stdint.h.
these were all attempts by the C committee to guess what the C++
committee would want, and the guesses turned out to be wrong.
2013-05-17 14:23:41 -04:00
Rich Felker 1a70198b3e fix mknod and mknodat to accept large dev_t values
support for these was recently added to sysmacros.h. note that the
syscall argument is a long, despite dev_t being 64-bit, so on 32-bit
archs the high bits will be lost. it appears the high bits are just
glibc silliness and not part of the kernel api, anyway, but it's nice
that we have them there for future expansion if needed.
2013-05-16 16:27:37 -04:00
Szabolcs Nagy e216951f50 math: use double_t for temporaries to avoid stores on i386
When FLT_EVAL_METHOD!=0 (only i386 with x87 fp) the excess
precision of an expression must be removed in an assignment.
(gcc needs -fexcess-precision=standard or -std=c99 for this)

This is done by extra load/store instructions which adds code
bloat when lot of temporaries are used and it makes the result
less precise in many cases.
Using double_t and float_t avoids these issues on i386 and
it makes no difference on other archs.

For now only a few functions are modified where the excess
precision is clearly beneficial (mostly polynomial evaluations
with temporaries).

object size differences on i386, gcc-4.8:
             old   new
__cosdf.o    123    95
__cos.o      199   169
__sindf.o    131    95
__sin.o      225   203
__tandf.o    207   151
__tan.o      605   499
erff.o      1470  1416
erf.o       1703  1649
j0f.o       1779  1745
j0.o        2308  2274
j1f.o       1602  1568
j1.o        2286  2252
tgamma.o    1431  1424
math/*.o   64164 63635
2013-05-15 23:08:52 +00:00
Rich Felker 2c184264ea support full range of dev_t major/minor numbers in makedev, etc. macros 2013-05-15 16:15:50 -04:00
Szabolcs Nagy 2897bfdd60 remove compound literals from math.h to please c++
__FLOAT_BITS and __DOUBLE_BITS macros used union compound literals,
now they are changed into static inline functions. A good C compiler
generates the same code for both and the later is C++ conformant.
2013-05-06 17:52:48 +00:00
Rich Felker da49b872f5 fix incorrect clock tick scaling in fallback case of clock()
since CLOCKS_PER_SEC is 1000000 (required by XSI) and the times
syscall reports values in 1/100 second units (Linux), the correct
scaling factor is 10000, not 100. note that only ancient kernels which
lack clock_gettime are affected.
2013-05-05 14:51:25 -04:00
Rich Felker 9293b765c4 do not interpret errors in return value of times() syscall
all return values are valid, and on 32-bit systems, values that look
like errors can and will occur. since the only actual error this
function could return is EFAULT, and it is only returnable when the
application has invoked undefined behavior, simply ignore the
possibility that the return value is actually an error code.
2013-05-05 14:19:37 -04:00
Rich Felker 2c074b0d6c transition to using functions for internal signal blocking/restoring
there are several reasons for this change. one is getting rid of the
repetition of the syscall signature all over the place. another is
sharing the constant masks without costly GOT accesses in PIC.

the main motivation, however, is accurately representing whether we
want to block signals that might be handled by the application, or all
signals.
2013-04-26 19:48:01 -04:00
Rich Felker d53c92c972 optimize/debloat raise
use __syscall rather than syscall when failure is not possible or not
to be considered.
2013-04-26 19:02:23 -04:00
Rich Felker d674f8582a prevent code from running under a thread id which already gave ESRCH 2013-04-26 17:51:22 -04:00
Rich Felker 47d2bf5103 synccall signal handler need not handle dead threads anymore
they have already blocked signals before decrementing the thread
count, so the code being removed is unreachable in the case where the
thread is no longer counted.
2013-04-26 17:46:58 -04:00
Rich Felker 082fb4e9bf fix clobbering of signal mask when creating thread with sched attributes
this was simply a case of saving the state in the wrong place.
2013-04-26 17:30:32 -04:00
Rich Felker d0ba09837b make last thread's pthread_exit give exit(0) a consistent state
the previous few commits ended up leaving the thread count and signal
mask wrong for atexit handlers and stdio cleanup.
2013-04-26 16:16:04 -04:00
Rich Felker c3a6839ce9 use atomic decrement rather than cas in pthread_exit thread count
now that blocking signals prevents any application code from running
while the last thread is exiting, the cas logic is no longer needed to
prevent decrementing below zero.
2013-04-26 16:05:39 -04:00
Rich Felker 6e531f999a add comments on some of the pthread_exit logic 2013-04-26 16:04:30 -04:00
Rich Felker 23f21c304f always block signals in pthread_exit before decrementing thread count
the thread count (1+libc.threads_minus_1) must always be greater than
or equal to the number of threads which could have application code
running, even in an async-signal-safe sense. there is at least one
dangerous race condition if this invariant fails to hold: dlopen could
allocate too little TLS for existing threads, and a signal handler
running in the exiting thread could claim the allocated TLS for itself
(via __tls_get_addr), leaving too little for the other threads it was
allocated for and thereby causing out-of-bounds access.

there may be other situations where it's dangerous for the thread
count to be too low, particularly in the case where only one thread
should be left, in which case locking may be omitted. however, all
such code paths seem to arise from undefined behavior, since
async-signal-unsafe functions are not permitted to be called from a
signal handler that interrupts pthread_exit (which is itself
async-signal-unsafe).

this change may also simplify logic in __synccall and improve the
chances of making __synccall async-signal-safe.
2013-04-26 15:47:44 -04:00