Commit Graph
39 Commits
Author SHA1 Message Date
Alexander MonakovandRich Felker 4554f155dd setjmp: optimize longjmp prologues
Use a branchless sequence that is one byte shorter on 64-bit, same size
on 32-bit. Thanks to Pete Cawley for suggesting this variant.
2020-08-12 21:52:56 -04:00
Alexander MonakovandRich Felker 59b64ff686 setjmp: optimize x86 longjmp epilogues 2020-08-11 14:43:20 -04:00
Alexander MonakovandRich Felker c6a6fe4ccd setjmp: avoid useless REX-prefix on xor %eax, %eax 2020-08-11 14:43:12 -04:00
Alexander MonakovandRich Felker 21431a0e43 setjmp: fix x86-64 longjmp argument adjustment
longjmp 'val' argument is an int, but the assembly is referencing 64-bit
registers as if the argument was a long, or the caller was responsible
for extending the argument. Though the psABI is not clear on this, the
interpretation in GCC is that high bits may be arbitrary and the callee
is responsible for sign/zero-extending the value as needed (likewise for
return values: callers must anticipate that high bits may be garbage).

Therefore testing %rax is a functional bug: setjmp would wrongly return
zero if longjmp was called with val==0, but high bits of %rsi happened
to be non-zero.

Rewrite the prologue to refer to 32-bit registers. In passing, change
'test' to use %rsi, as there's no advantage to using %rax and the new
form is cheaper on processors that do not perform move elimination.
2020-08-11 14:42:54 -04:00
Alexander MonakovandRich Felker 19f870c3a6 math: add x86_64 remquol 2020-03-24 16:31:36 -04:00
Alexander MonakovandRich Felker bc87299ce7 math: move x87-family fmod functions to C with inline asm 2020-03-24 16:31:36 -04:00
Alexander MonakovandRich Felker b173e4262f math: move x87-family remainder functions to C with inline asm 2020-03-24 16:31:36 -04:00
Alexander MonakovandRich Felker 51f4f8c512 math: move x87-family rint functions to C with inline asm 2020-03-24 16:31:36 -04:00
Alexander MonakovandRich Felker 9443f1b5cf math: move x87-family lrint functions to C with inline asm 2020-03-24 16:31:36 -04:00
Alexander MonakovandRich Felker 6bbdbfdcde math: move x86_64 (l)lrint(f) functions to C with inline asm 2020-03-24 16:31:36 -04:00
Alexander MonakovandRich Felker acfe6d033e math: move i386 sqrt to C with inline asm 2020-03-24 16:31:36 -04:00
Alexander MonakovandRich Felker 29adaeb2c0 math: move i386 sqrtf to C with inline asm 2020-03-24 16:31:36 -04:00
Alexander MonakovandRich Felker 41b290ba39 math: move trivial x86-family sqrt functions to C with inline asm 2020-03-24 16:27:38 -04:00
Alexander MonakovandRich Felker c24a992386 math: move x87-family fabs functions to C with inline asm 2020-03-24 16:27:38 -04:00
Alexander MonakovandRich Felker 87026f6843 math: move x86_64 fabs, fabsf to C with inline asm 2020-03-24 16:27:06 -04:00
Alexander MonakovandRich Felker 526df238d0 remove redundant condition in memccpy
Commit d9bdfd164 ("fix memccpy to not access buffer past given size")
correctly added a check for 'n' nonzero, but made the pre-existing test
'*s==c' redundant: n!=0 implies *s==c. Remove the unnecessary check.

Reported by Alexey Izbyshev.
2020-03-20 15:45:08 -04:00
Alexander MonakovandRich Felker ff5b8ad386 math/x32: correct lrintl.s for 32-bit long 2020-01-27 13:02:19 -05:00
Alexander MonakovandRich Felker 00bd3b7d30 __libc_start_main: slightly simplify stage2 pointer setup
Use "+r" in the asm instead of implementing a non-transparent copy by
applying "0" constraint to the source value. Introduce a typedef for
the function type to avoid spelling it out twice.
2018-11-02 12:13:53 -04:00
Alexander MonakovandRich Felker b0d2b3a1e5 optimize explicit_bzero for size
Avoid saving/restoring the incoming argument by reusing memset return
value.
2018-07-02 17:14:54 -04:00
Alexander MonakovandRich Felker ce7ae11acf ldso, malloc: implement reclaim_gaps via __malloc_donate
Split 'free' into unmap_chunk and bin_chunk, use the latter to introduce
__malloc_donate and use it in reclaim_gaps instead of calling 'free'.
2018-04-17 19:23:00 -04:00
Alexander MonakovandRich Felker d889cc3463 malloc: fix an over-allocation bug
Fix an instance where realloc code would overallocate by OVERHEAD bytes
amount. Manually arrange for reuse of memcpy-free-return exit sequence.
2018-04-17 19:23:00 -04:00
Alexander MonakovandRich Felker 424eab2225 optimize malloc0
Implementation of __malloc0 in malloc.c takes care to preserve zero
pages by overwriting only non-zero data. However, malloc must have
already modified auxiliary heap data just before and beyond the
allocated region, so we know that edge pages need not be preserved.

For allocations smaller than one page, pass them immediately to memset.
Otherwise, use memset to handle partial pages at the head and tail of
the allocation, and scan complete pages in the interior. Optimize the
scanning loop by processing 16 bytes per iteration and handling rest of
page via memset as soon as a non-zero byte is found.
2018-04-11 15:37:44 -04:00
Alexander MonakovandRich Felker e53296f889 re-fix child reaping in wordexp
Do not retry waitpid if the child was terminated by a signal. Do not
examine status: since we are not passing any flags, we will not receive
stop or continue notifications.
2018-02-05 11:40:03 -05:00
Alexander MonakovandRich Felker 51bdcdc424 fix OOB reads in Xbyte_memmem
Reported by Leah Neukirchen.
2017-09-04 16:38:03 -04:00
Alexander MonakovandRich Felker cc0dbd5f09 free allocations in clearenv
This aligns clearenv with the Linux man page by setting 'environ'
rather than '*environ' to NULL, and stops it from leaking entries
allocated by the libc.
2017-09-04 15:55:21 -04:00
Alexander MonakovandRich Felker 8e932792c9 overhaul environment functions
Rewrite environment access functions to slim down code, fix bugs and
avoid invoking undefined behavior.

* avoid using int-typed iterators where size_t would be correct;
* use strncmp instead of memcmp consistently;
* tighten prologues by invoking __strchrnul;
* handle NULL environ.

putenv:
* handle "=value" input via unsetenv too (will return -1/EINVAL);
* rewrite and simplify __putenv; fix the leak caused by failure to
  deallocate entry added by preceding setenv when called from putenv.

setenv:
* move management of libc-allocated entries to this translation unit,
  and use no-op weak symbols in putenv/unsetenv;

unsetenv:
* rewrite; this fixes UB caused by testing a free'd pointer against
  NULL on entry to subsequent loops.

Not changed:
Failure to extend allocation tracking array (previously __env_map, now
env_alloced) is ignored rather than causing to report -1/ENOMEM to the
caller; the worst-case consequence is leaking this allocation when it
is removed or replaced in a subsequent environment access.

Initially UB in unsetenv was reported by Alexander Cherepanov.
Using a weak alias to avoid pulling in malloc via unsetenv was
suggested by Rich Felker.
2017-09-04 15:55:05 -04:00
Alexander MonakovandRich Felker c7f56b4d2f __init_libc: add fallbacks for __progname setup
It is possible for argv[0] to be a null pointer, but the __progname
variable is used to implement functions in src/legacy/err.c that do not
expect it to be null. It is also available to the user via the
program_invocation_name alias as a GNU extension, and the implementation
in Glibc initializes it to a pointer to empty string rather than NULL.

Since argv[0] is usually non-null and it's preferable to keep those
variables in BSS, implement the fallbacks in __init_libc, which also
allows to have an intermediate fallback to AT_EXECFN.
2017-08-29 19:50:48 -04:00
Alexander MonakovandRich Felker 2e6e08423b remove ineffective compiler assist from printf
The switch statement has no 'default:' case and the function ends
immediately following the switch, so the extra comparison did not
communicate any extra information to the compiler.
2017-07-04 18:04:52 -04:00
Alexander MonakovandRich Felker bc42dcbfd9 fix undefined behavior in ptrace 2017-07-04 17:53:58 -04:00
Alexander MonakovandRich Felker 60ab365cae fix undefined behavior in free 2017-07-04 17:11:33 -04:00
Alexander MonakovandRich Felker f688884061 reapply va_arg hacks removal to wprintf
commit 58e2396a9a missed that the same
code was duplicated in implementation of vfwprintf.
2017-07-04 17:04:43 -04:00
Alexander MonakovandRich Felker bd00cc8781 remove useless declarations in string.h
The two functions str{,n}casecmp_l are specified to be declared in
<strings.h> which is already included from <string.h> under _GNU_SOURCE.
2017-07-04 17:04:21 -04:00
Alexander MonakovandRich Felker 9543656cc3 env: avoid leaving dangling pointers in __env_map
This is the minimal fix for __putenv leaving a pointer to freed heap
storage in __env_map array, which could later on lead to errors such
as double-free.
2016-03-06 13:33:52 -05:00
Alexander MonakovandRich Felker 8f08a58c63 dynlink.c: pass gnu-hash table pointer to gnu_lookup
The callers need to check the value of the pointer anyway, so make
them pass the pointer to gnu_lookup instead of reloading it there.

Reorder gnu_lookup arguments so that always-used ones are listed
first. GCC can choose a calling convention with arguments in registers
(e.g. up to 3 arguments in eax, ecx, edx on x86), but cannot reorder
the arguments for static functions.
2015-06-28 02:30:12 +00:00
Alexander MonakovandRich Felker 5b4286e12c dynlink.c: slim down gnu_lookup
Do not reference dso->syms and dso->strings until point of use.
Check 'h1 == (h2|1)', the simplest condition, before the others.
2015-06-28 01:51:15 +00:00
Alexander MonakovandRich Felker 84389c6456 dynlink.c: use bloom filter in gnu hash lookup
Introduce gnu_lookup_filtered and use it to speed up symbol lookups in
find_sym (do_dlsym is left as is, based on an expectation that
frequently dlsym queries will use a dlopen handle rather than
RTLD_NEXT or RTLD_DEFAULT, and will not need to look at more than one
DSO).
2015-06-28 01:49:53 +00:00
Alexander MonakovandRich Felker 66d45787c8 dynlink.c: use a faster expression in gnu_hash
With -Os, GCC uses a multiply rather than a shift and addition for 'h*33'.
Use a more efficient expression explicitely.
2015-06-27 21:48:19 -04:00
Alexander MonakovandRich Felker fa80787698 add missing 'void' in prototypes of internal pthread functions 2015-04-18 16:59:00 -04:00
Alexander MonakovandRich Felker 20cbd60775 getloadavg: use sysinfo() instead of /proc/loadavg
Based on a patch by Szabolcs Nagy.
2015-02-25 15:37:55 -05:00