glibc/debug
Siddhesh Poyarekar 9bcd12d223 wcrtomb: Make behavior POSIX compliant
The GNU implementation of wcrtomb assumes that there are at least
MB_CUR_MAX bytes available in the destination buffer passed to wcrtomb
as the first argument.  This is not compatible with the POSIX
definition, which only requires enough space for the input wide
character.

This does not break much in practice because when users supply buffers
smaller than MB_CUR_MAX (e.g. in ncurses), they compute and dynamically
allocate the buffer, which results in enough spare space (thanks to
usable_size in malloc and padding in alloca) that no actual buffer
overflow occurs.  However when the code is built with _FORTIFY_SOURCE,
it runs into the hard check against MB_CUR_MAX in __wcrtomb_chk and
hence fails.  It wasn't evident until now since dynamic allocations
would result in wcrtomb not being fortified but since _FORTIFY_SOURCE=3,
that limitation is gone, resulting in such code failing.

To fix this problem, introduce an internal buffer that is MB_LEN_MAX
long and use that to perform the conversion and then copy the resultant
bytes into the destination buffer.  Also move the fortification check
into the main implementation, which checks the result after conversion
and aborts if the resultant byte count is greater than the destination
buffer size.

One complication is that applications that assume the MB_CUR_MAX
limitation to be gone may not be able to run safely on older glibcs if
they use static destination buffers smaller than MB_CUR_MAX; dynamic
allocations will always have enough spare space that no actual overruns
will occur.  One alternative to fixing this is to bump symbol version to
prevent them from running on older glibcs but that seems too strict a
constraint.  Instead, since these users will only have made this
decision on reading the manual, I have put a note in the manual warning
them about the pitfalls of having static buffers smaller than
MB_CUR_MAX and running them on older glibc.

Benchmarking:

The wcrtomb microbenchmark shows significant increases in maximum
execution time for all locales, ranging from 10x for ar_SA.UTF-8 to
1.5x-2x for nearly everything else.  The mean execution time however saw
practically no impact, with some results even being quicker, indicating
that cache locality has a much bigger role in the overhead.

Given that the additional copy uses a temporary buffer inside wcrtomb,
it's likely that a hot path will end up putting that buffer (which is
responsible for the additional overhead) in a similar place on stack,
giving the necessary cache locality to negate the overhead.  However in
situations where wcrtomb ends up getting called at wildly different
spots on the call stack (or is on different call stacks, e.g. with
threads or different execution contexts) and is still a hotspot, the
performance lag will be visible.

Signed-off-by: Siddhesh Poyarekar <siddhesh@sourceware.org>
2022-05-13 19:15:46 +05:30
..
asprintf_chk.c Update copyright dates with scripts/update-copyrights 2022-01-01 11:40:24 -08:00
backtrace-tst.c Add #include <stdint.h> for uint[32|64]_t usage (except installed headers). 2013-05-16 11:32:54 -05:00
backtrace.c Update copyright dates with scripts/update-copyrights 2022-01-01 11:40:24 -08:00
backtracesyms.c Update copyright dates with scripts/update-copyrights 2022-01-01 11:40:24 -08:00
backtracesymsfd.c Update copyright dates with scripts/update-copyrights 2022-01-01 11:40:24 -08:00
chk_fail.c Update copyright dates with scripts/update-copyrights 2022-01-01 11:40:24 -08:00
confstr_chk.c Update copyright dates with scripts/update-copyrights 2022-01-01 11:40:24 -08:00
Depend * stdlib/bits/stdlib.h: New file. 2005-07-13 06:09:58 +00:00
dprintf_chk.c Update copyright dates with scripts/update-copyrights 2022-01-01 11:40:24 -08:00
execinfo.h Update copyright dates with scripts/update-copyrights 2022-01-01 11:40:24 -08:00
explicit_bzero_chk.c Update copyright dates with scripts/update-copyrights 2022-01-01 11:40:24 -08:00
fdelt_chk.c debug: Improve fdelt_chk error message 2022-03-28 19:10:30 +05:30
fgets_chk.c Update copyright dates with scripts/update-copyrights 2022-01-01 11:40:24 -08:00
fgets_u_chk.c Update copyright dates with scripts/update-copyrights 2022-01-01 11:40:24 -08:00
fgetws_chk.c Update copyright dates with scripts/update-copyrights 2022-01-01 11:40:24 -08:00
fgetws_u_chk.c Update copyright dates with scripts/update-copyrights 2022-01-01 11:40:24 -08:00
fortify_fail.c Update copyright dates with scripts/update-copyrights 2022-01-01 11:40:24 -08:00
fprintf_chk.c Update copyright dates with scripts/update-copyrights 2022-01-01 11:40:24 -08:00
fread_chk.c Update copyright dates with scripts/update-copyrights 2022-01-01 11:40:24 -08:00
fread_u_chk.c Update copyright dates with scripts/update-copyrights 2022-01-01 11:40:24 -08:00
fwprintf_chk.c Update copyright dates with scripts/update-copyrights 2022-01-01 11:40:24 -08:00
getcwd_chk.c Update copyright dates with scripts/update-copyrights 2022-01-01 11:40:24 -08:00
getdomainname_chk.c Update copyright dates with scripts/update-copyrights 2022-01-01 11:40:24 -08:00
getgroups_chk.c Update copyright dates with scripts/update-copyrights 2022-01-01 11:40:24 -08:00
gethostname_chk.c Update copyright dates with scripts/update-copyrights 2022-01-01 11:40:24 -08:00
gets_chk.c Update copyright dates with scripts/update-copyrights 2022-01-01 11:40:24 -08:00
getwd_chk.c Update copyright dates with scripts/update-copyrights 2022-01-01 11:40:24 -08:00
longjmp_chk.c Update copyright dates with scripts/update-copyrights 2022-01-01 11:40:24 -08:00
Makefile debug: Synchronize feature guards in fortified functions [BZ #28746] 2022-01-12 23:34:48 +05:30
mbsnrtowcs_chk.c Update copyright dates with scripts/update-copyrights 2022-01-01 11:40:24 -08:00
mbsrtowcs_chk.c Update copyright dates with scripts/update-copyrights 2022-01-01 11:40:24 -08:00
mbstowcs_chk.c Update copyright dates with scripts/update-copyrights 2022-01-01 11:40:24 -08:00
memcpy_chk.c Update copyright dates with scripts/update-copyrights 2022-01-01 11:40:24 -08:00
memmove_chk.c Update copyright dates with scripts/update-copyrights 2022-01-01 11:40:24 -08:00
mempcpy_chk.c Update copyright dates with scripts/update-copyrights 2022-01-01 11:40:24 -08:00
memset_chk.c Update copyright dates with scripts/update-copyrights 2022-01-01 11:40:24 -08:00
noophooks.c Update copyright dates with scripts/update-copyrights 2022-01-01 11:40:24 -08:00
obprintf_chk.c Update copyright dates with scripts/update-copyrights 2022-01-01 11:40:24 -08:00
pcprofile.c Update copyright dates with scripts/update-copyrights 2022-01-01 11:40:24 -08:00
pcprofiledump.c Update copyright dates not handled by scripts/update-copyrights. 2022-01-01 11:42:26 -08:00
poll_chk.c Update copyright dates with scripts/update-copyrights 2022-01-01 11:40:24 -08:00
ppoll_chk.c Update copyright dates with scripts/update-copyrights 2022-01-01 11:40:24 -08:00
pread64_chk.c Update copyright dates with scripts/update-copyrights 2022-01-01 11:40:24 -08:00
pread_chk.c Update copyright dates with scripts/update-copyrights 2022-01-01 11:40:24 -08:00
printf_chk.c Update copyright dates with scripts/update-copyrights 2022-01-01 11:40:24 -08:00
read_chk.c Update copyright dates with scripts/update-copyrights 2022-01-01 11:40:24 -08:00
readlink_chk.c Update copyright dates with scripts/update-copyrights 2022-01-01 11:40:24 -08:00
readlinkat_chk.c Update copyright dates with scripts/update-copyrights 2022-01-01 11:40:24 -08:00
readonly-area.c Update copyright dates with scripts/update-copyrights 2022-01-01 11:40:24 -08:00
realpath_chk.c Update copyright dates with scripts/update-copyrights 2022-01-01 11:40:24 -08:00
recv_chk.c Update copyright dates with scripts/update-copyrights 2022-01-01 11:40:24 -08:00
recvfrom_chk.c Update copyright dates with scripts/update-copyrights 2022-01-01 11:40:24 -08:00
snprintf_chk.c Update copyright dates with scripts/update-copyrights 2022-01-01 11:40:24 -08:00
sprintf_chk.c Update copyright dates with scripts/update-copyrights 2022-01-01 11:40:24 -08:00
stack_chk_fail_local.c Update copyright dates with scripts/update-copyrights 2022-01-01 11:40:24 -08:00
stack_chk_fail.c Update copyright dates with scripts/update-copyrights 2022-01-01 11:40:24 -08:00
stpcpy_chk.c Update copyright dates with scripts/update-copyrights 2022-01-01 11:40:24 -08:00
stpncpy_chk.c Update copyright dates with scripts/update-copyrights 2022-01-01 11:40:24 -08:00
strcat_chk.c Update copyright dates with scripts/update-copyrights 2022-01-01 11:40:24 -08:00
strcpy_chk.c Update copyright dates with scripts/update-copyrights 2022-01-01 11:40:24 -08:00
strncat_chk.c Update copyright dates with scripts/update-copyrights 2022-01-01 11:40:24 -08:00
strncpy_chk.c Update copyright dates with scripts/update-copyrights 2022-01-01 11:40:24 -08:00
swprintf_chk.c Update copyright dates with scripts/update-copyrights 2022-01-01 11:40:24 -08:00
test-stpcpy_chk.c Update copyright dates with scripts/update-copyrights 2022-01-01 11:40:24 -08:00
test-strcpy_chk.c Update copyright dates with scripts/update-copyrights 2022-01-01 11:40:24 -08:00
tst-backtrace2.c Update copyright dates with scripts/update-copyrights 2022-01-01 11:40:24 -08:00
tst-backtrace3.c Update copyright dates with scripts/update-copyrights 2022-01-01 11:40:24 -08:00
tst-backtrace4.c Update copyright dates with scripts/update-copyrights 2022-01-01 11:40:24 -08:00
tst-backtrace5.c Update copyright dates with scripts/update-copyrights 2022-01-01 11:40:24 -08:00
tst-backtrace6.c Update copyright dates with scripts/update-copyrights 2022-01-01 11:40:24 -08:00
tst-backtrace.h Update copyright dates with scripts/update-copyrights 2022-01-01 11:40:24 -08:00
tst-fortify.c wcrtomb: Make behavior POSIX compliant 2022-05-13 19:15:46 +05:30
tst-longjmp_chk2.c Remove "Contributed by" lines 2021-09-03 22:06:44 +05:30
tst-longjmp_chk3.c Update copyright dates with scripts/update-copyrights 2022-01-01 11:40:24 -08:00
tst-longjmp_chk.c tests: unify fortification handler logic 2014-02-08 06:58:43 -05:00
tst-realpath-chk.c fortify: Fix spurious warning with realpath 2021-12-17 18:49:27 +05:30
tst-ssp-1.c Update copyright dates with scripts/update-copyrights 2022-01-01 11:40:24 -08:00
ttyname_r_chk.c Update copyright dates with scripts/update-copyrights 2022-01-01 11:40:24 -08:00
vasprintf_chk.c Update copyright dates with scripts/update-copyrights 2022-01-01 11:40:24 -08:00
vdprintf_chk.c Update copyright dates with scripts/update-copyrights 2022-01-01 11:40:24 -08:00
Versions New string function explicit_bzero (from OpenBSD). 2016-12-16 16:21:54 -05:00
vfprintf_chk.c Update copyright dates with scripts/update-copyrights 2022-01-01 11:40:24 -08:00
vfwprintf_chk.c Update copyright dates with scripts/update-copyrights 2022-01-01 11:40:24 -08:00
vobprintf_chk.c Update copyright dates with scripts/update-copyrights 2022-01-01 11:40:24 -08:00
vprintf_chk.c Update copyright dates with scripts/update-copyrights 2022-01-01 11:40:24 -08:00
vsnprintf_chk.c Update copyright dates with scripts/update-copyrights 2022-01-01 11:40:24 -08:00
vsprintf_chk.c Update copyright dates with scripts/update-copyrights 2022-01-01 11:40:24 -08:00
vswprintf_chk.c Update copyright dates with scripts/update-copyrights 2022-01-01 11:40:24 -08:00
vwprintf_chk.c Update copyright dates with scripts/update-copyrights 2022-01-01 11:40:24 -08:00
wcpcpy_chk.c Update copyright dates with scripts/update-copyrights 2022-01-01 11:40:24 -08:00
wcpncpy_chk.c Update copyright dates with scripts/update-copyrights 2022-01-01 11:40:24 -08:00
wcrtomb_chk.c wcrtomb: Make behavior POSIX compliant 2022-05-13 19:15:46 +05:30
wcscat_chk.c Update copyright dates with scripts/update-copyrights 2022-01-01 11:40:24 -08:00
wcscpy_chk.c Update copyright dates with scripts/update-copyrights 2022-01-01 11:40:24 -08:00
wcsncat_chk.c Update copyright dates with scripts/update-copyrights 2022-01-01 11:40:24 -08:00
wcsncpy_chk.c Update copyright dates with scripts/update-copyrights 2022-01-01 11:40:24 -08:00
wcsnrtombs_chk.c Update copyright dates with scripts/update-copyrights 2022-01-01 11:40:24 -08:00
wcsrtombs_chk.c Update copyright dates with scripts/update-copyrights 2022-01-01 11:40:24 -08:00
wcstombs_chk.c Update copyright dates with scripts/update-copyrights 2022-01-01 11:40:24 -08:00
wctomb_chk.c Update copyright dates with scripts/update-copyrights 2022-01-01 11:40:24 -08:00
wmemcpy_chk.c Update copyright dates with scripts/update-copyrights 2022-01-01 11:40:24 -08:00
wmemmove_chk.c Update copyright dates with scripts/update-copyrights 2022-01-01 11:40:24 -08:00
wmempcpy_chk.c Update copyright dates with scripts/update-copyrights 2022-01-01 11:40:24 -08:00
wmemset_chk.c Update copyright dates with scripts/update-copyrights 2022-01-01 11:40:24 -08:00
wprintf_chk.c Update copyright dates with scripts/update-copyrights 2022-01-01 11:40:24 -08:00
xtrace.sh Update copyright dates not handled by scripts/update-copyrights. 2022-01-01 11:42:26 -08:00