Commit graph

173 commits

Author SHA1 Message Date
Elliott Hughes
81c54c72b3 Upstream sync.
Also clean up some obsolete cruft from openbsd-compat.h...

Test: treehugger
Change-Id: I9eae0f8304e701d032045617427289002d234cd8
2024-04-29 23:41:39 +00:00
Elliott Hughes
7d13666b53 riscv64: fix ODR violations.
Rather than do the work to fix the ODR violations while preserving non-V,
let's just remove the non-V code. Android will require V anyway, and
anyone trying to work on a non-V system in the meantime already needs
a bunch of patches to the build system and ART, so one more shouldn't
hurt too much.

Test: treehugger
Change-Id: Iab43d8a80d99a4d045b0008dbea4e7e8696d1167
2023-10-27 16:42:52 -07:00
Elliott Hughes
307ff340db Sync upstream OpenBSD stdio.
"""
__swsetup: set error flag and errno on error.

Previously, we set errno to EBADF if the cantwrite() macro (which calls
__swsetup()) returns true for POSIX compliance.  However, we neglected
to also set the error flag, __SERR.  Rather than set the error flag in
all callers of cantwrite(), set both errno and the error flag in
__swsetup().  This matches what FreeBSD does and makes it possible
to choose a proper errno value for the second error condition in
__swsetup().  OK deraadt@
"""
fc99cf9338

Bug: http://b/302742247
Test: treehugger
Change-Id: If3be4905fc21e513cb8718cca671eae3885e411a
2023-10-09 16:53:31 -07:00
Elliott Hughes
ac496ec2ba Sync upstream OpenBSD.
A couple of minor optimizations since the last sync.

Test: treehugger
Change-Id: Ibba2cd6749f989a24b74a64d0db180bd0458fdc8
2023-07-19 13:36:44 -07:00
Elliott Hughes
2bd4316bd6 Expose tzalloc()/localtime_rz()/mktime_z()/tzfree().
* Rationale

The question often comes up of how to use multiple time zones in C code.
If you're single-threaded, you can just use setenv() to manipulate $TZ.
toybox does this, for example. But that's not thread-safe in two
distinct ways: firstly, getenv() is not thread-safe with respect to
modifications to the environment (and between the way putenv() is
specified and the existence of environ, it's not obvious how to fully
fix that), and secondly the _caller_ needs to ensure that no other
threads are using tzset() or any function that behaves "as if" tzset()
was called (which is neither easy to determine nor easy to ensure).

This isn't a bigger problem because most of the time the right answer
is to stop pretending that libc is at all suitable for any i18n, and
switch to icu4c instead. (The NDK icu4c headers do not include ucal_*,
so this is not a realistic option for most applications.)

But what if you're somewhere in between? Like the rust chrono library,
for example? What then?

Currently their "least worst" option is to reinvent the entire wheel and
read our tzdata files. Which isn't a great solution for anyone, for
obvious maintainability reasons.

So it's probably time we broke the catch-22 here and joined NetBSD in
offering a less broken API than standard C has for the last 40 years.
Sure, any would-be caller will have to have a separate "is this
Android?" and even "is this API level >= 35?" path, but that will fix
itself sometime in the 2030s when developers can just assume "yes, it
is", whereas if we keep putting off exposing anything, this problem
never gets solved.

(No-one's bothered to try to implement the std::chrono::time_zone
functionality in libc++ yet, but they'll face a similar problem if/when
they do.)

* Implementation

The good news is that tzcode already implements these functions, so
there's relatively little here.

I've chosen not to expose `struct state` because `struct __timezone_t`
makes for clearer error messages, given that compiler diagnostics will
show the underlying type name (`struct __timezone_t*`) rather than the
typedef name (`timezone_t`) that's used in calling code.

I've moved us over to FreeBSD's wcsftime() rather than keep the OpenBSD
one building --- I've long wanted to only have one implementation here,
and FreeBSD is already doing the "convert back and forth, calling the
non-wide function in the middle" dance that I'd hoped to get round to
doing myself someday. This should mean that our strftime() and
wcsftime() behaviors can't easily diverge in future, plus macOS/iOS are
mostly FreeBSD, so any bugs will likely be interoperable with the other
major mobile operating system, so there's something nice for everyone
there!

The FreeBSD wcsftime() implementation includes a wcsftime_l()
implementation, so that's one stub we can remove. The flip side of that
is that it uses mbsrtowcs_l() and wcsrtombs_l() which we didn't
previously have. So expose those as aliases of mbsrtowcs() and
wcsrtombs().

Bug: https://github.com/chronotope/chrono/issues/499
Test: treehugger
Change-Id: Iee1b9d763ead15eef3d2c33666b3403b68940c3c
2023-06-16 08:10:47 -07:00
Elliott Hughes
53b6dfbc64 Sync with upstream openbsd.
Upstream made a change similar to our %s change, so we don't need that
difference any more. (But they didn't seem interested in our GNU
extensions, even though they're sensible ones that just ensure symmetry
between strftime() and strptime().)

Bug: http://b/167569813
Test: treehugger
Change-Id: I1d86d69c87b51719f0583341fafa7802869cd37e
2023-03-06 23:32:48 +00:00
Elliott Hughes
d3627a444f Sync strptime.c with upstream.
We still have local differences, but this minimizes (and documents) them.

Bug: http://b/167569813
Test: treehugger
Change-Id: Ib90e6ccc5ec1224e7ee89224a51b87fc48c9931f
2022-12-12 20:56:12 +00:00
Elliott Hughes
1f462dec34 Add %b and %B support to the scanf/wscanf and strto*/wcsto* families.
Coming to C23 via WG14 N2630.

This one is a little interesting, because it actually changes existing
behavior. Previously "0b101" would be parsed as "0", "b", "101" by these
functions. I'm led to believe that glibc plans to actually have separate
versions of these functions for C23 and pre-C23, so callers can have the
behavior they (implicitly) specify by virtue of which -std= they compile
with. Android has never really done anything like that, and I'm pretty
sure app developers have more than enough to worry about with API levels
without having to deal with the cartesian product of API level and C
standard.

Therefore, my plan A is "if you're running on Android >= U, you get C23
behavior". My plan B in the (I think unlikely) event that that actually
causes trouble for anyone is "if you're _targeting_ Android >= U, you
get C23 behavior". I don't think we'd actually want to have two versions
of each of these functions under any circumstances --- that seems by far
the most confusing option.

Test: treehugger
Change-Id: I0bbb30315d3fabd306905ad1484361f5d8745935
2022-08-11 00:25:08 +00:00
Elliott Hughes
fbac9af484 Sync with upstream openbsd.
Note that this is only a partial update; some other files have changes
upstream that aren't here, but they're changes that seem to require a
bit more thought, whereas these seem easy (and the base64.c and fputws.c
C2x compatibility changes were the motivation to sync right now).

Test: treehugger
Change-Id: I2f86708e25bcb9e779ecb6f9643b769cd4f83240
2022-07-12 13:32:02 -07:00
Colin Cross
048f24ed2a Export fts as a static library for use with musl
musl libc doesn't provide fts, but elfutils and libabigail need it.
Export bionic's fts as a staic library that can be linked into elfutils
and libabigail when compiling against musl.

fts uses recallocarray, which musl doesn't provide, so also include
recallocarray.c in libfts.a.

Requires minor tweaks to fts.c and a wrapper around fts.h to make them
compatible with musl, primarily by providing local definitions of macros
provided in bionic's sys/cdefs.h.

Bug: 190084016
Test: m libfts
Change-Id: Ifac9a59e7504c0c1f5f8a3a5bd3c19a13980b83c
2021-09-08 15:53:10 -07:00
Elliott Hughes
e1dc4f62eb Fewer copies of ALIGN()/ALIGNBYTES.
Noticed while updating fts.c.

Bug: http://b/177003648
Test: treehugger
Change-Id: Ic3625c1c3af47c4dafb8ad686bbbddbc82b69b70
2021-01-11 11:51:29 -08:00
Elliott Hughes
439ebbd349 Simplify and improve tempnam() and tmpnam().
They're both obsolescent in POSIX.1-2008, and you really shouldn't be
using them, but since we can't actually delete them...

This change makes them both obey $TMPDIR if set, and fall back to
/data/local/tmp otherwise. That's as good as we've managed for anything
else such as tmpfile(3).

Also add some tests.

Bug: http://b/174682340
Test: treehugger
Change-Id: Ieef99dcc2062f84b2b7cbae046787fdfe975e772
2020-12-08 22:26:06 -08:00
Elliott Hughes
5633caa285 Switch to musl memmem (via OpenBSD).
Similar to the musl strstr. This patch also increases test coverage for
memmem, again similar to the strstr tests.

Test: treehugger
Change-Id: I7f4a2ab93a610cb692994d06d2512976e657ae9f
2020-08-06 14:33:48 -07:00
Elliott Hughes
26b06073f6 Sync with upstream OpenBSD.
Test: treehugger
Change-Id: I1fc649ba5d79a3d95242c6b2240dbb05c85d30e9
2020-07-31 13:01:38 -07:00
Elliott Hughes
cdb4a26d29 Update upstream OpenBSD gdtoa.
Also add a test for the bug that this fixes.

Bug: http://b/152588929
Test: treehugger
Change-Id: I58055b3ebaef457721bb4f5d8a8710025122b2e7
2020-06-11 12:57:37 -07:00
Elliott Hughes
bb575d93c4 arc4random.h: remove some cruft.
This hasn't been used since the code was rewritten years ago.

Test: builds
Change-Id: I2c4bccb3fffb15115083afbb178519bd133c64de
2020-04-16 23:14:38 +00:00
Christopher Ferris
1cc755c8cf Fully disable clang format where needed.
Even with formatting off, clang still tries to rearrange the include
files or the using statements, so disable that too.

Test: Verified that the include directories are not rearranged.
Change-Id: I991a1b2bfa94a8202c5a486664658d654f1c7811
2020-01-30 08:10:17 -08:00
Christopher Ferris
fdaf82f96b Link .clang-format file to system/core one.
Use the .clang-format-2 found in system/core instead of this which is
not actually being used.

Also, enable clang-format running by default.

All upstream directories are marked as ignoring formatting so that
their source files are not modified.

Test: NA
Change-Id: Icee6030f373fa5f072df162f97e6f34320e3d89a
2020-01-29 15:14:20 -08:00
Elliott Hughes
c6b38aefa7 Sync with upstream OpenBSD strstr().
Bug: http://b/124855136
Test: treehugger
Change-Id: I6cbeb82bc0e418f50e6c171ac4e38e335c448db8
2019-11-22 11:16:23 -08:00
Elliott Hughes
9a4b68e20d Take a bunch of trivial patches from upstream OpenBSD.
Test: treehugger
Change-Id: Ie18a94cddada926eff23b53ac9e4d5e5cabd91ed
2019-11-20 14:57:01 -08:00
Elliott Hughes
80c99e30c0 Merge "Clean up some obsolete OpenBSD portability cruft." 2019-10-30 17:20:58 +00:00
Elliott Hughes
5c5152bff5 Clean up some obsolete OpenBSD portability cruft.
Test: treehugger
Change-Id: Ic76a55cf28b862d51fa3f96549eb99e992e72954
2019-10-29 19:59:28 -07:00
Peter Collingbourne
900d07d6a1 Add arm64 string.h function implementations for use with hardware supporting MTE.
As it turns out, our "generic" arm64 implementations of certain string.h
functions are not actually generic, since they will eagerly read memory
possibly outside of the bounds of an MTE granule, which may lead to a segfault
on MTE-enabled hardware. Therefore, move the implementations into a "default"
directory and use ifuncs to select between them and a new set of "mte"
implementations, conditional on whether the hardware and kernel support MTE.

The MTE implementations are currently naive implementations written in C
but will later be replaced with a set of optimized assembly implementations.

Bug: 135772972
Change-Id: Ife37c4e0e6fd60ff20a34594cc09c541af4d1dd7
2019-10-29 16:18:31 -07:00
Elliott Hughes
a4959aa6f8 Reimplement the <ctype.h> is* functions.
Following on from the towlower()/towupper() changes, add benchmarks for
most of <ctype.h>, rewrite the tests to cover the entire defined range
for all of these functions, and then reimplement most of the functions.

The old table-based implementation is mostly a bad idea on modern
hardware, with only ispunct() showing a significant benefit compared to
any other way I could think of writing it, and isalnum() a marginal but
still convincingly genuine benefit.

My new benchmarks make an effort to test an example from each relevant
range of characters to avoid, say, accidentally optimizing the behavior
of `isalnum('0')` at the expense of `isalnum('z')`.

Interestingly, clang is able to generate what I believe to be the
optimal implementations from the most readable code, which is
impressive. It certainly matched or beat all my attempts to be clever!

The BSD table-based implementations made a special case of EOF despite
having a `_ctype_` table that's offset by 1 to include EOF at index 0.
I'm not sure why they didn't take advantage of that, but removing the
explicit check for EOF measurably improves the generated code on arm and
arm64, so even the two functions that still use the table benefit from
this rewrite.

Here are the benchmark results:

arm64 before:
  BM_ctype_isalnum_n                 3.73 ns         3.73 ns    183727137
  BM_ctype_isalnum_y1                3.82 ns         3.81 ns    186383058
  BM_ctype_isalnum_y2                3.73 ns         3.72 ns    187809830
  BM_ctype_isalnum_y3                3.78 ns         3.77 ns    181383055
  BM_ctype_isalpha_n                 3.75 ns         3.75 ns    189453927
  BM_ctype_isalpha_y1                3.76 ns         3.75 ns    184854043
  BM_ctype_isalpha_y2                4.32 ns         3.78 ns    186326931
  BM_ctype_isascii_n                 2.49 ns         2.48 ns    275583822
  BM_ctype_isascii_y                 2.51 ns         2.51 ns    282123915
  BM_ctype_isblank_n                 3.11 ns         3.10 ns    220472044
  BM_ctype_isblank_y1                3.20 ns         3.19 ns    226088868
  BM_ctype_isblank_y2                3.11 ns         3.11 ns    220809122
  BM_ctype_iscntrl_n                 3.79 ns         3.78 ns    188719938
  BM_ctype_iscntrl_y1                3.72 ns         3.71 ns    186209237
  BM_ctype_iscntrl_y2                3.80 ns         3.80 ns    184315749
  BM_ctype_isdigit_n                 3.76 ns         3.74 ns    188334682
  BM_ctype_isdigit_y                 3.78 ns         3.77 ns    186249335
  BM_ctype_isgraph_n                 3.99 ns         3.98 ns    177814143
  BM_ctype_isgraph_y1                3.98 ns         3.95 ns    175140090
  BM_ctype_isgraph_y2                4.01 ns         4.00 ns    178320453
  BM_ctype_isgraph_y3                3.96 ns         3.95 ns    175412814
  BM_ctype_isgraph_y4                4.01 ns         4.00 ns    175711174
  BM_ctype_islower_n                 3.75 ns         3.74 ns    188604818
  BM_ctype_islower_y                 3.79 ns         3.78 ns    154738238
  BM_ctype_isprint_n                 3.96 ns         3.95 ns    177607734
  BM_ctype_isprint_y1                3.94 ns         3.93 ns    174877244
  BM_ctype_isprint_y2                4.02 ns         4.01 ns    178206135
  BM_ctype_isprint_y3                3.94 ns         3.93 ns    175959069
  BM_ctype_isprint_y4                4.03 ns         4.02 ns    176158314
  BM_ctype_isprint_y5                3.95 ns         3.94 ns    178745462
  BM_ctype_ispunct_n                 3.78 ns         3.77 ns    184727184
  BM_ctype_ispunct_y                 3.76 ns         3.75 ns    187947503
  BM_ctype_isspace_n                 3.74 ns         3.74 ns    185300285
  BM_ctype_isspace_y1                3.77 ns         3.76 ns    187202066
  BM_ctype_isspace_y2                3.73 ns         3.73 ns    184105959
  BM_ctype_isupper_n                 3.81 ns         3.80 ns    185038761
  BM_ctype_isupper_y                 3.71 ns         3.71 ns    185885793
  BM_ctype_isxdigit_n                3.79 ns         3.79 ns    184965673
  BM_ctype_isxdigit_y1               3.76 ns         3.75 ns    188251672
  BM_ctype_isxdigit_y2               3.79 ns         3.78 ns    184187481
  BM_ctype_isxdigit_y3               3.77 ns         3.76 ns    187635540

arm64 after:
  BM_ctype_isalnum_n                 3.37 ns         3.37 ns    205613810
  BM_ctype_isalnum_y1                3.40 ns         3.39 ns    204806361
  BM_ctype_isalnum_y2                3.43 ns         3.43 ns    205066077
  BM_ctype_isalnum_y3                3.50 ns         3.50 ns    200057128
  BM_ctype_isalpha_n                 2.97 ns         2.97 ns    236084076
  BM_ctype_isalpha_y1                2.97 ns         2.97 ns    236083626
  BM_ctype_isalpha_y2                2.97 ns         2.97 ns    236084246
  BM_ctype_isascii_n                 2.55 ns         2.55 ns    272879994
  BM_ctype_isascii_y                 2.46 ns         2.45 ns    286522323
  BM_ctype_isblank_n                 3.18 ns         3.18 ns    220431175
  BM_ctype_isblank_y1                3.18 ns         3.18 ns    220345602
  BM_ctype_isblank_y2                3.18 ns         3.18 ns    220308509
  BM_ctype_iscntrl_n                 3.10 ns         3.10 ns    220344270
  BM_ctype_iscntrl_y1                3.10 ns         3.07 ns    228973615
  BM_ctype_iscntrl_y2                3.07 ns         3.07 ns    229192626
  BM_ctype_isdigit_n                 3.07 ns         3.07 ns    228925676
  BM_ctype_isdigit_y                 3.07 ns         3.07 ns    229182934
  BM_ctype_isgraph_n                 2.66 ns         2.66 ns    264268737
  BM_ctype_isgraph_y1                2.66 ns         2.66 ns    264445277
  BM_ctype_isgraph_y2                2.66 ns         2.66 ns    264327427
  BM_ctype_isgraph_y3                2.66 ns         2.66 ns    264427480
  BM_ctype_isgraph_y4                2.66 ns         2.66 ns    264155250
  BM_ctype_islower_n                 2.66 ns         2.66 ns    264421600
  BM_ctype_islower_y                 2.66 ns         2.66 ns    264341148
  BM_ctype_isprint_n                 2.66 ns         2.66 ns    264415198
  BM_ctype_isprint_y1                2.66 ns         2.66 ns    264268793
  BM_ctype_isprint_y2                2.66 ns         2.66 ns    264419205
  BM_ctype_isprint_y3                2.66 ns         2.66 ns    264205886
  BM_ctype_isprint_y4                2.66 ns         2.66 ns    264440797
  BM_ctype_isprint_y5                2.72 ns         2.72 ns    264333293
  BM_ctype_ispunct_n                 3.52 ns         3.51 ns    198956572
  BM_ctype_ispunct_y                 3.38 ns         3.38 ns    201661792
  BM_ctype_isspace_n                 3.39 ns         3.39 ns    206896620
  BM_ctype_isspace_y1                3.39 ns         3.39 ns    206569020
  BM_ctype_isspace_y2                3.39 ns         3.39 ns    206564415
  BM_ctype_isupper_n                 2.76 ns         2.75 ns    254227134
  BM_ctype_isupper_y                 2.76 ns         2.75 ns    254235314
  BM_ctype_isxdigit_n                3.60 ns         3.60 ns    194418653
  BM_ctype_isxdigit_y1               2.97 ns         2.97 ns    236082424
  BM_ctype_isxdigit_y2               3.48 ns         3.48 ns    200390011
  BM_ctype_isxdigit_y3               3.48 ns         3.48 ns    202255815

arm32 before:
  BM_ctype_isalnum_n                 4.77 ns         4.76 ns    129230464
  BM_ctype_isalnum_y1                4.88 ns         4.87 ns    147939321
  BM_ctype_isalnum_y2                4.74 ns         4.73 ns    145508054
  BM_ctype_isalnum_y3                4.81 ns         4.80 ns    144968914
  BM_ctype_isalpha_n                 4.80 ns         4.79 ns    148262579
  BM_ctype_isalpha_y1                4.74 ns         4.73 ns    145061326
  BM_ctype_isalpha_y2                4.83 ns         4.82 ns    147642546
  BM_ctype_isascii_n                 3.74 ns         3.72 ns    186711139
  BM_ctype_isascii_y                 3.79 ns         3.78 ns    183654780
  BM_ctype_isblank_n                 4.20 ns         4.19 ns    169733252
  BM_ctype_isblank_y1                4.19 ns         4.18 ns    165713363
  BM_ctype_isblank_y2                4.22 ns         4.21 ns    168776265
  BM_ctype_iscntrl_n                 4.75 ns         4.74 ns    145417484
  BM_ctype_iscntrl_y1                4.82 ns         4.81 ns    146283250
  BM_ctype_iscntrl_y2                4.79 ns         4.78 ns    148662453
  BM_ctype_isdigit_n                 4.77 ns         4.76 ns    145789210
  BM_ctype_isdigit_y                 4.84 ns         4.84 ns    146909458
  BM_ctype_isgraph_n                 4.72 ns         4.71 ns    145874663
  BM_ctype_isgraph_y1                4.86 ns         4.85 ns    142037606
  BM_ctype_isgraph_y2                4.79 ns         4.78 ns    145109612
  BM_ctype_isgraph_y3                4.75 ns         4.75 ns    144829039
  BM_ctype_isgraph_y4                4.86 ns         4.85 ns    146769899
  BM_ctype_islower_n                 4.76 ns         4.75 ns    147537637
  BM_ctype_islower_y                 4.79 ns         4.78 ns    145648017
  BM_ctype_isprint_n                 4.82 ns         4.81 ns    147154780
  BM_ctype_isprint_y1                4.76 ns         4.76 ns    145117604
  BM_ctype_isprint_y2                4.87 ns         4.86 ns    145801406
  BM_ctype_isprint_y3                4.79 ns         4.78 ns    148043446
  BM_ctype_isprint_y4                4.77 ns         4.76 ns    145157619
  BM_ctype_isprint_y5                4.91 ns         4.90 ns    147810800
  BM_ctype_ispunct_n                 4.74 ns         4.73 ns    145588611
  BM_ctype_ispunct_y                 4.82 ns         4.81 ns    144065436
  BM_ctype_isspace_n                 4.78 ns         4.77 ns    147153712
  BM_ctype_isspace_y1                4.73 ns         4.72 ns    145252863
  BM_ctype_isspace_y2                4.84 ns         4.83 ns    148615797
  BM_ctype_isupper_n                 4.75 ns         4.74 ns    148276631
  BM_ctype_isupper_y                 4.80 ns         4.79 ns    145529893
  BM_ctype_isxdigit_n                4.78 ns         4.77 ns    147271646
  BM_ctype_isxdigit_y1               4.74 ns         4.74 ns    145142209
  BM_ctype_isxdigit_y2               4.83 ns         4.82 ns    146398497
  BM_ctype_isxdigit_y3               4.78 ns         4.77 ns    147617686

arm32 after:
  BM_ctype_isalnum_n                 4.35 ns         4.35 ns    161086146
  BM_ctype_isalnum_y1                4.36 ns         4.35 ns    160961111
  BM_ctype_isalnum_y2                4.36 ns         4.36 ns    160733210
  BM_ctype_isalnum_y3                4.35 ns         4.35 ns    160897524
  BM_ctype_isalpha_n                 3.67 ns         3.67 ns    189377208
  BM_ctype_isalpha_y1                3.68 ns         3.67 ns    189438146
  BM_ctype_isalpha_y2                3.75 ns         3.69 ns    190971186
  BM_ctype_isascii_n                 3.69 ns         3.68 ns    191029191
  BM_ctype_isascii_y                 3.68 ns         3.68 ns    191011817
  BM_ctype_isblank_n                 4.09 ns         4.09 ns    171887541
  BM_ctype_isblank_y1                4.09 ns         4.09 ns    171829345
  BM_ctype_isblank_y2                4.08 ns         4.07 ns    170585590
  BM_ctype_iscntrl_n                 4.08 ns         4.07 ns    170614383
  BM_ctype_iscntrl_y1                4.13 ns         4.11 ns    171495899
  BM_ctype_iscntrl_y2                4.19 ns         4.18 ns    165255578
  BM_ctype_isdigit_n                 4.25 ns         4.24 ns    165237008
  BM_ctype_isdigit_y                 4.24 ns         4.24 ns    165256149
  BM_ctype_isgraph_n                 3.82 ns         3.81 ns    183610114
  BM_ctype_isgraph_y1                3.82 ns         3.81 ns    183614131
  BM_ctype_isgraph_y2                3.82 ns         3.81 ns    183616840
  BM_ctype_isgraph_y3                3.79 ns         3.79 ns    183620182
  BM_ctype_isgraph_y4                3.82 ns         3.81 ns    185740009
  BM_ctype_islower_n                 3.75 ns         3.74 ns    183619502
  BM_ctype_islower_y                 3.68 ns         3.68 ns    190999901
  BM_ctype_isprint_n                 3.69 ns         3.68 ns    190899544
  BM_ctype_isprint_y1                3.68 ns         3.67 ns    190192384
  BM_ctype_isprint_y2                3.67 ns         3.67 ns    189351466
  BM_ctype_isprint_y3                3.67 ns         3.67 ns    189430348
  BM_ctype_isprint_y4                3.68 ns         3.68 ns    189430161
  BM_ctype_isprint_y5                3.69 ns         3.68 ns    190962419
  BM_ctype_ispunct_n                 4.14 ns         4.14 ns    171034861
  BM_ctype_ispunct_y                 4.19 ns         4.19 ns    168308152
  BM_ctype_isspace_n                 4.50 ns         4.50 ns    156250887
  BM_ctype_isspace_y1                4.48 ns         4.48 ns    155124476
  BM_ctype_isspace_y2                4.50 ns         4.50 ns    155077504
  BM_ctype_isupper_n                 3.68 ns         3.68 ns    191020583
  BM_ctype_isupper_y                 3.68 ns         3.68 ns    191015669
  BM_ctype_isxdigit_n                4.50 ns         4.50 ns    156276745
  BM_ctype_isxdigit_y1               3.28 ns         3.27 ns    214729725
  BM_ctype_isxdigit_y2               4.48 ns         4.48 ns    155265129
  BM_ctype_isxdigit_y3               4.48 ns         4.48 ns    155216846

I've also corrected a small mistake in the documentation for isxdigit().

Test: tests and benchmarks
Change-Id: I4a77859f826c3fc8f0e327e847886882f29ec4a3
2019-10-08 12:04:09 -07:00
Elliott Hughes
1c8a2a99a7 Optimize tolower(3)/toupper(3) from <ctype.h>.
The tables in the BSD tolower/toupper are slower for ASCII than just
doing the bit twiddling.

We can't actually remove the tables on LP32, so move them into the
"cruft" we keep around for backwards compatibility (but remove them for
LP64 where they were never exposed).

I noticed that the new bit-twiddling tolower(3) was performing better
on arm64 than toupper(3). The 0xdf constant was requiring an extra MOV,
and there isn't a BIC that takes an immediate value. Since we've already
done the comparison to check that we're in the right range (where the
bit is always set), though, we can EOR 0x20 to get the same result as
the missing BIC 0x20 in just one instruction.

I've applied that same optimization to towupper(3) too.

Before:

  BM_ctype_tolower_n                 3.30 ns         3.30 ns    212353035
  BM_ctype_tolower_y                 3.31 ns         3.30 ns    211234204
  BM_ctype_toupper_n                 3.30 ns         3.29 ns    214161246
  BM_ctype_toupper_y                 3.29 ns         3.28 ns    207643473

  BM_wctype_towupper_ascii_n         3.53 ns         3.53 ns    195944444
  BM_wctype_towupper_ascii_y         3.48 ns         3.48 ns    199233248

After:

  BM_ctype_tolower_n                 2.93 ns         2.92 ns    242373703
  BM_ctype_tolower_y                 2.88 ns         2.87 ns    245365309
  BM_ctype_toupper_n                 2.93 ns         2.93 ns    243049353
  BM_ctype_toupper_y                 2.89 ns         2.89 ns    245072521

  BM_wctype_towupper_ascii_n         3.34 ns         3.33 ns    212951912
  BM_wctype_towupper_ascii_y         3.29 ns         3.29 ns    214651254

(Why do both the "y" and "n" variants speed up with the EOR
change? Because the compiler transforms the code so that we
unconditionally do the bit twiddling and then use CSEL to decide whether
or not to actually use the result.)

We also save 1028 bytes of data in the LP64 libc.so.

Test: ran the bionic benchmarks and tests
Change-Id: I7829339f8cb89a58efe539c2a01c51807413aa2d
2019-09-27 14:42:39 -07:00
Elliott Hughes
01809e1fd1 Switch to OpenBSD div/ldiv/lldiv.
Test: ran tests
Change-Id: I6ecf5878162d7cee81af40edd4b44406196f49be
2019-02-05 16:48:22 -08:00
Elliott Hughes
fbac97a54c Move NetBSD string routines to OpenBSD.
NetBSD seems to be the least well maintained of our three BSD upstreams,
and it's already the one we use the least. Let's push a little further
in that direction...

Test: new smoke tests
Change-Id: Idfebd11794445fe14cbfa07177a7392a7b36a5e4
2019-02-05 12:15:27 -08:00
Elliott Hughes
b177085ce7 Add reallocarray(3).
Originally a BSD extension, now in glibc too. We've used it internally
for a while.

(cherry-pick of e4b13f7e3ca68edfcc5faedc5e7d4e13c4e8edb9.)

Bug: http://b/112163459
Test: ran tests
Change-Id: I813c3a62b13ddb91ba41e32a5a853d09207ea6bc
Merged-In: I813c3a62b13ddb91ba41e32a5a853d09207ea6bc
2018-09-26 14:24:18 -07:00
Elliott Hughes
99d54656bd Add PR_SET_VMA and PR_SET_VMA_ANON_NAME to <sys/prctl.h>.
We've copied & pasted these to too many places. And if we're going to
have another go at upstreaming these, that's probably yet another reason
to have the *values* in just one place. (Even if upstream wants different
names, we'll likely keep the legacy names around for a while for source
compatibility.)

Bug: http://b/111903542
Test: ran tests
Change-Id: I8ccc557453d69530e5b74f865cbe0b458c84e3ba
2018-08-22 10:36:23 -07:00
Elliott Hughes
468efc80da Reimplement popen(3)/pclose(3).
pclose(3) is now an alias for fclose(3). We could add a FORTIFY check
that you use pclose(3) if and only if you used popen(3), but there seems
little value to that when we can just do the right thing.

This patch also adds the missing locking to _fwalk --- we need to lock
both the global list of FILE*s and also each FILE* we touch. POSIX says
that "The popen() function shall ensure that any streams from previous
popen() calls that remain open in the parent process are closed in the
new child process", which we implement via _fwalk(fclose) in the child,
but we might want to just make *all* popen(3) file descriptors O_CLOEXEC
in all cases.

Ignore fewer errors in popen(3) failure cases.

Improve popen(3) test coverage.

Bug: http://b/72470344
Test: ran tests
Change-Id: Ic937594bf28ec88b375f7e5825b9c05f500af438
2018-07-11 12:15:26 -07:00
Elliott Hughes
da46caee09 Add generic arm non-neon memmove.
From OpenBSD.

Bug: http://b/63992911
Test: ran tests
Change-Id: If7d9166922776cdc9333ff04205f9c6312a812b3
2018-05-24 14:57:15 -07:00
Elliott Hughes
71ba5899ae Rewrite system(3) to use posix_spawn(3).
We saw crashes from pthread_exit+debuggerd on LP32
(https://issuetracker.google.com/72291624), and it seems like the
equivalent problem should exist with system(3). I fixed posix_spawn(3)
as part of that bug, so the easiest fix is probably to reuse that.

Bug: http://b/72470344
Test: ran tests
Change-Id: I05f838706f2b4a14ac3ee21292833e6c8579b0d4
2018-03-05 17:20:12 -08:00
George Burgess IV
9024235005 Remove __overloadable/__RENAME_CLANG
Now that we have a clang that supports transparent overloads, we can
kill all of this cruft, and restore our upstream sources to their
untouched glory. Woohoo!

Bug: 12231437
Test: Built aosp_marlin; no obvious patch-related aosp_mips issues.
Change-Id: I520a19d014f12137f80e43f973dccd6711c571cd
2018-02-06 13:35:56 -08:00
Elliott Hughes
f1515f6408 Clean up the time(2) implementation.
This is also slightly faster for the no VDSO case (56ns vs 66ns).

Bug: N/A
Test: ran tests, benchmarks
Change-Id: I2b0edd06ee6942eb57c32678279278a53ca5ee9b
2018-01-12 15:20:28 -08:00
Mark Salyzyn
4473ccd5b0 bionic: add vdso time()
time() can be a hot call, and it currently uses __vdso_gettimeofday,
which is already pretty fast (~3 times faster than the syscall),
but with a __vdso_time call it is ~3 times even faster, in part
because __vdso_time does not require interlocking with updates,
and the read for just the seconds is atomic.  __vdso_time is
always available, whereas __vdso_gettimeofday is gated on access
to the physical timers.  arm improvement is compelling (x10),
x86 improvement is even more pronounced (x100).

[TL;DR]

w/vdso32 kernel patches, locked cores to MAX, little cores only.

BEFORE:

hikey960 vdso (aarch64):

----------------------------------------------------------------------
Benchmark                               Time           CPU Iterations
----------------------------------------------------------------------
BM_time_clock_gettime                  48 ns         48 ns   15414753
BM_time_clock_gettime_syscall         175 ns        175 ns    4062031
BM_time_clock_gettime_REALTIME         44 ns         44 ns   15897875
BM_time_clock_gettime_BOOTTIME         47 ns         47 ns   14307903
BM_time_clock_gettime_TAI             210 ns        210 ns    3341372
BM_time_clock_gettime_unsupported     100 ns        100 ns    7030649
BM_time_gettimeofday                   47 ns         47 ns   14969643
BM_time_gettimeofday_syscall          163 ns        163 ns    4283542
BM_time_time                           59 ns         59 ns   11815385

hikey960 vdso32 (aarch32):

----------------------------------------------------------------------
Benchmark                               Time           CPU Iterations
----------------------------------------------------------------------
BM_time_clock_gettime                  90 ns         90 ns    7572898
BM_time_clock_gettime_syscall         251 ns        251 ns    2763442
BM_time_clock_gettime_REALTIME         81 ns         80 ns    8699536
BM_time_clock_gettime_BOOTTIME         97 ns         97 ns    7256667
BM_time_clock_gettime_TAI             272 ns        272 ns    2570419
BM_time_clock_gettime_unsupported     160 ns        160 ns    4379819
BM_time_gettimeofday                   73 ns         73 ns    9608922
BM_time_gettimeofday_syscall          200 ns        199 ns    3527957
BM_time_time                          123 ns        123 ns    5651095

x86_64 (glibc):

--------------------------------------------------------------------
Benchmark                             Time           CPU Iterations
--------------------------------------------------------------------
BM_time_clock_gettime                  21 ns         21 ns   28873070
BM_time_clock_gettime_syscall         224 ns        224 ns    3095370
BM_time_clock_gettime_REALTIME         17 ns         17 ns   42083086
BM_time_clock_gettime_BOOTTIME        239 ns        239 ns    2924015
BM_time_clock_gettime_TAI             236 ns        236 ns    2961423
BM_time_clock_gettime_unsupported     221 ns        221 ns    3357696
BM_time_gettimeofday                 22 ns         22 ns   27975154
BM_time_gettimeofday_syscall        238 ns        238 ns    2882032
BM_time_time                          2 ns          2 ns  340354885
BM_time_time_syscall                207 ns        207 ns    3383073

imx7d_pico IOT nyc (w/arm,cpu-registers-not-fw-configured) (armv7a):
(virtual timers)

Benchmark                           Time(ns)    CPU(ns) Iterations
------------------------------------------------------------------
BM_time_clock_gettime                     20        477    1489362
BM_time_clock_gettime_syscall             20        487    1458333
BM_time_clock_gettime_REALTIME            19        464    1400000
BM_time_clock_gettime_BOOTTIME            29        700    1000000
BM_time_clock_gettime_TAI                 29        690    1000000
BM_time_clock_gettime_unsupported          9        227    3043478
BM_time_gettimeofday                      18        444    1555556
BM_time_gettimeofday_syscall              19        456    1555556
BM_time_time                              21        497    1166667

imx7d_pico IOT nyc (wo/arm,cpu-registers-not-fw-configured) (armv7a):
(physical timers)

Benchmark                           Time(ns)    CPU(ns) Iterations
------------------------------------------------------------------
BM_time_clock_gettime                      6        144    4666667
BM_time_clock_gettime_syscall             20        486    1400000
BM_time_clock_gettime_REALTIME             6        136    5000000
BM_time_clock_gettime_BOOTTIME             6        153    4375000
BM_time_clock_gettime_TAI                 31        760    1000000
BM_time_clock_gettime_unsupported         10        233    3043478
BM_time_gettimeofday                       6        140    5000000
BM_time_gettimeofday_syscall              19        450    1555556
BM_time_time                               9        203    3500000

AFTER:

hikey960 vdso (aarch64):

--------------------------------------------------------------------
Benchmark                             Time           CPU Iterations
--------------------------------------------------------------------
BM_time_clock_gettime                48 ns         48 ns   15414753
BM_time_clock_gettime_syscall       175 ns        175 ns    4062031
BM_time_clock_gettime_REALTIME       44 ns         44 ns   15897875
BM_time_clock_gettime_BOOTTIME       47 ns         47 ns   14307903
BM_time_clock_gettime_TAI           210 ns        210 ns    3341372
BM_time_clock_gettime_unsupported   100 ns        100 ns    7030649
BM_time_gettimeofday                 47 ns         47 ns   14975314
BM_time_gettimeofday_syscall        164 ns        164 ns    4278797
BM_time_time                         16 ns         16 ns   42932165

hikey960 vdso32 (aarch32):

--------------------------------------------------------------------
Benchmark                             Time           CPU Iterations
--------------------------------------------------------------------
BM_time_clock_gettime                90 ns         90 ns    7572898
BM_time_clock_gettime_syscall       251 ns        251 ns    2763442
BM_time_clock_gettime_REALTIME       81 ns         80 ns    8699536
BM_time_clock_gettime_BOOTTIME       97 ns         97 ns    7256667
BM_time_clock_gettime_TAI           272 ns        272 ns    2570419
BM_time_clock_gettime_unsupported   160 ns        160 ns    4379819
BM_time_gettimeofday                 73 ns         73 ns    9596230
BM_time_gettimeofday_syscall        199 ns        199 ns    3575428
BM_time_time                         35 ns         35 ns   19798801

imx7d_pico IOT nyc (w/arm,cpu-registers-not-fw-configured) (armv7a):

Benchmark                           Time(ns)    CPU(ns) Iterations
------------------------------------------------------------------
BM_time_clock_gettime                     20        477    1489362
BM_time_clock_gettime_syscall             20        487    1458333
BM_time_clock_gettime_REALTIME            19        464    1400000
BM_time_clock_gettime_BOOTTIME            29        700    1000000
BM_time_clock_gettime_TAI                 29        690    1000000
BM_time_clock_gettime_unsupported          9        227    3043478
BM_time_gettimeofday                      18        444    1555556
BM_time_gettimeofday_syscall              19        456    1555556
BM_time_time                               2         50   11666667

imx7d_pico IOT nyc (wo/arm,cpu-registers-not-fw-configured) (armv7a):

Benchmark                           Time(ns)    CPU(ns) Iterations
------------------------------------------------------------------
BM_time_clock_gettime                      6        144    4666667
BM_time_clock_gettime_syscall             20        486    1400000
BM_time_clock_gettime_REALTIME             6        136    5000000
BM_time_clock_gettime_BOOTTIME             6        153    4375000
BM_time_clock_gettime_TAI                 31        760    1000000
BM_time_clock_gettime_unsupported         10        233    3043478
BM_time_gettimeofday                       6        140    5000000
BM_time_gettimeofday_syscall              19        450    1555556
BM_time_time                               2         50   10000000

Test: bionic-unit-tests --gtest_filter=time.time
      taskset F bionic-benchmarks --bionic_xml=vdso.xml \
          --benchmark_filter='BM_time_(time*|clock_gettime*|gettimeofday*)'
Bug: 63737556
Change-Id: I81b088a12ca41a6c4733d46c5477527777138efa
2018-01-11 15:46:11 +00:00
Elliott Hughes
1921dce886 Refactor the ato* and strto* family.
There are no meaningful changes here, just a minimal conversion to two
C++ templates to make further changes easier.

Bug: N/A
Test: ran tests, benchmarks
Change-Id: I958fbf17a85f19dd8f17bfb4bbb9314d220daa3b
2017-12-19 13:55:54 -08:00
Treehugger Robot
07e412419b Merge "More missing _unlocked <stdio.h> functions." 2017-10-31 22:43:34 +00:00
Elliott Hughes
37ad959783 More missing _unlocked <stdio.h> functions.
Also simplify trivial one-liners like perror/puts/fputs, and clean up
fread/fwrite slightly.

Fix perror to match POSIX.

Add basic perror and *_unlocked tests.

Bug: N/A
Test: ran tests
Change-Id: I63f83c8e0c15c3c4096509d17421ac331b6fc23d
2017-10-31 13:23:41 -07:00
Elliott Hughes
3a589c2362 Fork vfprintf.c/vfwprintf.c.
Future changes will start reducing the duplication...

Bug: http://b/67371539
Test: ran tests
Change-Id: I477afea34f9e1f41817823984bd0548944ee5eec
2017-10-30 11:43:58 -07:00
Dan Albert
1c78cb0fee Add missing includes.
Test: mma
Bug: None
Change-Id: I0221b213e08d07cc5ac0b704a86e98ae8c0f456f
2017-10-11 11:25:52 -07:00
Elliott Hughes
8465e968a8 Add <sys/random.h>.
iOS 10 has <sys/random.h> with getentropy, glibc >= 2.25 has
<sys/random.h> with getentropy and getrandom. (glibc also pollutes
<unistd.h>, but that seems like a bad idea.)

Also, all supported devices now have kernels with the getrandom system
call.

We've had these available internally for a while, but it seems like the
time is ripe to expose them.

Bug: http://b/67014255
Test: ran tests
Change-Id: I76dde1e3a2d0bc82777eea437ac193f96964f138
2017-09-29 05:31:35 +00:00
Elliott Hughes
7b0af7ad82 Always log errno when aborting.
(Where errno is relevant.)

Also consistently use -1 as the fd for anonymous mmaps. (It doesn't matter,
but it's more common, and potentially more intention-revealing.)

Bug: http://b/65608572
Test: ran tests
Change-Id: Ie9a207632d8242f42086ba3ca862519014c3c102
2017-09-15 16:18:49 -07:00
Sandeep Patil
9b1ca569db libc: Add support to allow library calls to find appropriate shell executable for a process
Library calls like system() and popen() invoke the shell executable
pointed to by '_PATH_BSHELL' in order to run the command passed into the
function. The _PATH_BSHELL points to /system/bin/sh by default and thus
breaks any vendor process trying to use system() / popen(), as they are
denied access to system shell by selinux.

This CL make necessary changes, so the implmentations of system() and popen()
can use the appropriate shell (e.g. /vendor/bin/sh for processes running
out of /vendor partition). Also, changes the implementation of system()
and popen().

Bug: 64832610
Test: Manual, Using a test program running from /system/bin and
      /vendor/bin to ensure correct shell is being used.

Change-Id: Ie7168d69decb1ae98284446ae7db34dec930dc33
Merged-In: Ie7168d69decb1ae98284446ae7db34dec930dc33
Signed-off-by: Sandeep Patil <sspatil@google.com>
(cherry picked from commit aa3e32422c)
2017-08-22 10:17:28 -07:00
Elliott Hughes
bff8da308f Merge "Replace fmemopen." 2017-07-24 20:36:16 +00:00
Elliott Hughes
89ff49942b Merge "Apply recent strto* fix, add new tests." 2017-07-24 20:18:34 +00:00
Elliott Hughes
57ad09278f Merge "Replace killpg." 2017-07-24 20:16:40 +00:00
Elliott Hughes
3a4c45499e Replace fmemopen.
A new implementation starting from the FreeBSD fmemopen rather than the
OpenBSD one we used to use.

The tests were arrived at by translating each sentence in
http://pubs.opengroup.org/onlinepubs/9699919799/functions/fmemopen.html,
plus http://man7.org/linux/man-pages/man3/fmemopen.3.html for historical
GNU bugs.

Bug: http://b/31304889
Test: ran tests
Change-Id: Id8b168c9ecde638e9cdedbc3b8a0982fc83c7048
2017-07-24 10:48:42 -07:00
Elliott Hughes
20f3399956 Switch to FreeBSD for wcsstr and wmemcpy.
Almost all of our w* functions come from FreeBSD already. The one downside is
that we can't take all our w* functions from FreeBSD because FreeBSD handles
locales very differently from us.

Bug: N/A
Test: ran tests
Change-Id: I177b4332499992babd5d5afe5b3f469f8c4345a5
2017-07-13 09:45:00 -07:00
Elliott Hughes
f826a37b95 Apply recent strto* fix, add new tests.
The behavior with "0xy" was wrong: we'd swallow the 'x'.

Bug: N/A
Test: ran tests
Change-Id: I2464d22a2408e99880303876306f18a25c390ad9
2017-07-13 09:35:15 -07:00
Elliott Hughes
7532b32627 Replace killpg.
Upstream's killpg is diverging further from glibc behavior, so let's just fork.

Bug: N/A
Test: ran tests
Change-Id: I70a3543018bc0a5c0bbf019ac527043b90568fda
2017-07-11 15:00:17 -07:00