Commit graph

189 commits

Author SHA1 Message Date
Elliott Hughes
f23ccdba77 Merge "Speed up __sfileext initialization." 2017-12-21 15:59:21 +00:00
Elliott Hughes
1a56a26f11 Speed up __sfileext initialization.
The internal uses don't need to actually initialize a mutex since they'll
never escape and don't need locking. It's a small saving, but easy.

Before:

  BM_stdio_scanf_d                    465 ns        465 ns    1507891
  BM_stdio_scanf_maps                1836 ns       1836 ns     381082
  BM_stdio_scanf_maps_baseline        846 ns        845 ns     830881
  BM_stdio_scanf_s                    419 ns        419 ns    1671979

After:

  BM_stdio_scanf_d                    434 ns        434 ns    1612930
  BM_stdio_scanf_maps                1815 ns       1815 ns     386470
  BM_stdio_scanf_maps_baseline        875 ns        873 ns     803304
  BM_stdio_scanf_s                    382 ns        382 ns    1833198

Bug: http://b/70862888
Test: ran tests, benchmarks
Change-Id: Ic8822aaca5d8ca1a73390089153d0fe35d91673e
2017-12-20 08:53:49 -08:00
Elliott Hughes
1133fecde3 Use inline ctype functions for ato*/strto* and scanf.
This also let us remove the `unsigned char` casts, since we define our
inlines to work for all values.

Before:

  Benchmark                      Time           CPU Iterations
  -------------------------------------------------------------
  BM_inttypes_strtoimax        112 ns        112 ns    6222193
  BM_inttypes_strtoumax        104 ns        104 ns    6725010
  BM_stdlib_strtol             113 ns        113 ns    6195861
  BM_stdlib_strtoll            113 ns        113 ns    6195633
  BM_stdlib_strtoul            105 ns        105 ns    6691394
  BM_stdlib_strtoull           105 ns        105 ns    6690695

  BM_stdio_scanf_d                    504 ns        503 ns    1385224
  BM_stdio_scanf_maps                1900 ns       1898 ns     369260
  BM_stdio_scanf_maps_baseline       1030 ns       1030 ns     678832
  BM_stdio_scanf_s                    433 ns        432 ns    1619086

After:

  BM_inttypes_strtoimax         91 ns         91 ns    7718194
  BM_inttypes_strtoumax         82 ns         82 ns    8508052
  BM_stdlib_strtol              92 ns         92 ns    7674694
  BM_stdlib_strtoll             91 ns         91 ns    7639228
  BM_stdlib_strtoul             83 ns         82 ns    8500304
  BM_stdlib_strtoull            82 ns         82 ns    8504929

  BM_stdio_scanf_d                    465 ns        465 ns    1507891
  BM_stdio_scanf_maps                1836 ns       1836 ns     381082
  BM_stdio_scanf_maps_baseline        846 ns        845 ns     830881
  BM_stdio_scanf_s                    419 ns        419 ns    1671979

Bug: N/A
Test: ran tests, benchmarks
Change-Id: I44681daf16c4328b060770cf11fc0633157c427f
2017-12-19 16:37:01 -08:00
Elliott Hughes
bf9cb9e404 More scanf cleanup.
Merge CT_CCL and CT_STRING handling before we add %m.

Also fix an accidental scanf/wscanf difference.

Add currently-disabled tests for questionable behavior noticed during
code review that isn't a regression, but should be fixed later.

Bug: http://b/68672236
Bug: https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=202240
Test: ran tests
Change-Id: I3eec9b7dfce84f63c68426406224822c52551d64
2017-12-12 17:10:51 -08:00
Elliott Hughes
0d3ba1f047 Add sscanf %[ tests and fix the bug that fell out.
Strictly, POSIX says "If a '-' is in the scanlist and is not the first
wide character, nor the second where the first wide character is a '^',
nor the last wide character, the behavior is implementation-defined",
but it seems unreasonable for swscanf to interpret `a-c` differently
from sscanf. Make ours behave the same as each other by making swscanf
work the same as sscanf.

Bug: http://b/68672236
Test: ran tests
Change-Id: Ia84805897628d7128e901b468e02504373730e61
2017-12-07 14:13:57 -08:00
Elliott Hughes
b70576b048 Remove more trivial char/wchar_t printf differences.
Bug: http://b/67371539
Test: ran tests
Change-Id: I899458af92517dc21f874519716fa3f3a0f0e3d6
2017-11-13 11:10:05 -08:00
Elliott Hughes
0793e3dd07 Merge "Yet more printf de-duplication." 2017-11-13 18:04:18 +00:00
Elliott Hughes
345b727e96 Move public scanf functions away from implementation.
Bug: N/A
Test: ran tests
Change-Id: Ifb8bd91132865f8c6d0b95baf1021af3b3b3c353
2017-11-10 16:20:43 -08:00
Elliott Hughes
bc27bdc24f Yet more printf de-duplication.
Bug: http://b/67371539
Test: ran tests
Change-Id: Iae38055426596919c0de4f5080c99eba95fac3a9
2017-11-10 15:25:49 -08:00
Elliott Hughes
1f4931751b Move shared parts of the two printfs out.
Bug: http://b/67371539
Test: ran tests
Change-Id: I24eae6a5d2c64ff4fd65c8d508a3709bab5a11c4
2017-11-08 16:27:49 -08:00
Elliott Hughes
93a1f8b1f5 More printf de-duplication.
Just trivial use of macros.

The %s/%ls case in __find_arguments was backwards in the wide copy of
the code, but not problematically so because all pointers are the same
size anyway.

Bug: http://b/67371539
Test: ran tests
Change-Id: I8d34915d75ae5425c56c59510a16c328fc481d20
2017-11-07 22:52:29 -08:00
Ryan Prichard
bf54986d19 Allow 32-bit fseeko/fseeko64 SEEK_CUR/SEEK_SET to exceed 2 GiB
Bug: http://b/68837650
Test: /data/nativetest/bionic-unit-tests/bionic-unit-tests
Test: /data/nativetest/bionic-unit-tests-static/bionic-unit-tests-static
Test: /data/nativetest64/bionic-unit-tests/bionic-unit-tests
Change-Id: I367e0238c31d35f76d8ad89fd0aa27ecfeb7c149
2017-11-07 15:34:23 -08:00
Elliott Hughes
5305a4d4a7 10x printf speedup.
Android is UTF-8. Don't make everyone pay to convert UTF-8 to ASCII just
so we can recognize '%'. With UTF-8 we can just strchr forwards.

Before:

  ---------------------------------------------------------------
  Benchmark                        Time           CPU Iterations
  ---------------------------------------------------------------
  BM_stdio_printf_literal       1290 ns       1290 ns     442554
  BM_stdio_printf_s             1204 ns       1204 ns     582446
  BM_stdio_printf_d             1206 ns       1206 ns     578311
  BM_stdio_printf_1$s           2263 ns       2263 ns     310002

After:

  ---------------------------------------------------------------
  Benchmark                        Time           CPU Iterations
  ---------------------------------------------------------------
  BM_stdio_printf_literal        178 ns        178 ns    3394001
  BM_stdio_printf_s              246 ns        246 ns    2850284
  BM_stdio_printf_d              252 ns        252 ns    2778610
  BM_stdio_printf_1$s            363 ns        363 ns    1929011

Add missing __find_arguments error checking to the wide variant to match
the regular one.

Also replace various char/wchar_t differences with the macro.

Bug: http://b/67371539
Test: ran tests
Change-Id: I18f122009c22699943ab5d666a98ea594a972c40
2017-11-03 14:19:00 -07:00
Elliott Hughes
618303ca4a More printf de-duplication.
Fix the 'j' (intmax_t/uintmax_t) length qualifier in the wide
variant. (With new tests that fail without this fix.)

Fix a typo in the wide support for intmax_t*, which isn't testable because
%n is disabled on Android (and will be removed in a later cleanup pass).

Also move the public vfprintf/vfwprint functions into stdio.cpp.

Bug: http://b/67371539
Test: ran tests
Change-Id: Ib003599b1e9cb789044a068940b59e447f2cb7cb
2017-11-02 16:58:44 -07:00
Elliott Hughes
2f9c8ce38a Start de-duplicating the regular and wide printf implementations.
This patch switches to C++ (in anticipation of needing it later), removes
a little duplication (via a macro for now), and ensures uniform support
for %C/%lc and %S/%ls between regular and wide (with new tests).

Since it's so hard to debug problems in printf (as the time I've wasted
already today will testify), that's all I want to do in this change. The
other 500 lines of diff can wait...

(Also merge "floatio.h" into "local.h" now all the users are in forked
code.)

Bug: http://b/67371539
Test: ran tests
Change-Id: I083353d89c32b9302d759ca6967cc6d8a62cd8a5
2017-11-01 18:13:36 -07: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
c8f2c520fa clang-format the printf/scanf implementation.
Bug: N/A
Test: builds
Change-Id: I490fdc1d568cba901c123b9efe0b9053b0d83b64
2017-10-31 13:07:51 -07:00
Elliott Hughes
d9a7de1b77 Remove some always-on #ifdefery.
Bug: N/A
Test: ran tests
Change-Id: I02d3c0a639f48ae9c415fb59f37ade015e6f46a2
2017-10-31 09:55:40 -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
Elliott Hughes
cc3d04f03a Add null checks to <dirent.h> functions.
Move all the new checks over to the existing __fortify_fatal.

Bug: http://b/67455242
Test: ran tests
Change-Id: Idb899c58c32d52d3b423caf1a91feb7defcba9b3
2017-10-26 16:16:33 -07:00
Josh Gao
d162060928 Add explicit null checks to some stdio functions.
Applications fopening files and then blindly trying to read are
widespread, leading to a recurring problem of SELinux tightening
resulting in segfaults. Add a friendly diagnostic for this case.

Bug: http://b/67455242
Test: bionic-unit-tests32/64 on sailfish
Change-Id: I1734fa94487c4eff9b55a02c6b01baf6b265d236
2017-10-23 12:48:08 -07:00
Elliott Hughes
5ba2c21159 Finish <stdio_ext.h>.
Implement __freading and __fwriting, and clarify the documentation that was
the cause of these not being implemented for years.

Bug: http://b/17157253
Test: ran tests
Change-Id: I89542c8131b13889e2585417a024050ecf2abcb7
2017-08-01 15:27:27 -07:00
Elliott Hughes
4a8de0d7ca Consistently say va_arg rather than __va_arg.
Bug: http://b/64250392
Test: builds
Change-Id: I435242f620245d825b8a68365c71c99a60aba625
2017-08-01 10:48:08 -07:00
Elliott Hughes
ec6850d849 Remove restrict/__restrict.
We've never really used __restrict: only <string.h> and <stdio.h> (which
are still very similar to upstream BSD headers) consistently have these
annotations. Neither clang nor GCC warns for trivial cases, and there's
little obvious documentation benefit.

Bug: http://b/30833514
Test: builds
Change-Id: I3e4384281865475d0c55d764b546d8166419ee31
2017-08-01 08:28:46 -07:00
Elliott Hughes
33a8cb1d1a Use O_APPEND for stdio append mode.
"Although not explicitly required by this volume of POSIX.1-2008, a good
implementation of append (a) mode would cause the O_APPEND flag to be set."

Yeah, about that...

Bug: N/A
Test: ran tests
Change-Id: I23c4bc5c1ebc92e0cb44025d2d313f321f9ffa68
2017-07-25 18:11:23 -07:00
Treehugger Robot
4fce647a5b Merge "Remove the HASLB and FREEUB macros." 2017-07-24 21:05:37 +00:00
Elliott Hughes
80e4c1529d Remove the HASLB and FREEUB macros.
Because we hate macros.

Bug: N/A
Test: ran tests
Change-Id: I2c94085ff502ec5ce6d8598ec6b3c10e7a4b5510
2017-07-24 11:58:23 -07: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
Christopher Ferris
7a3681e5b6 Move libc_log code into libasync_safe.
This library is used by a number of different libraries in the system.
Make it easy for platform libraries to use this library and create
an actual exported include file.

Change the names of the functions to reflect the new name of the library.

Run clang_format on the async_safe_log.cpp file since the formatting is
all over the place.

Bug: 31919199

Test: Compiled for angler/bullhead, and booted.
Test: Ran bionic unit tests.
Test: Ran the malloc debug tests.
Change-Id: I8071bf690c17b0ea3bc8dc5749cdd5b6ad58478a
2017-05-03 08:50:43 -07:00
George Burgess IV
7cc779f15c libc: add clang FORTIFY support
This patch adds clang-style FORTIFY to Bionic. For more information on
FORTIFY, please see https://goo.gl/8HS2dW . This implementation works
for versions of clang that don't support diagnose_if, so please see the
"without diagnose_if" sections. We plan to swap to a diagnose_if-based
FORTIFY later this year (since it doesn't really add any features; it
just simplifies the implementation a lot, and it gives us much prettier
diagnostics)

Bug: 32073964
Test: Builds on angler, bullhead, marlin, sailfish. Bionic CTS tests
pass on Angler and Bullhead.

Change-Id: I607aecbeee81529709b1eee7bef5b0836151eb2b
2017-02-09 15:49:32 -08:00
Dan Albert
3037ea43fc Fix stdin/stdout/stderr for pre-M.
This wasn't an array of pointers, it was an array of structs.
Unfortunately we need a complete type to index into the struct for
stdin/stdout/stderr, so add a phony struct that matches the size and
alignment of `struct __sFILE`. This property is guaranteed by the
static_asserts in libc/bionic/struct_file_test.cpp.

Test: mma
Bug: http://b/30465923
Change-Id: I8ce851dd64a261703bb44f9b5cd23b7caff4dd68
2016-10-20 10:10:45 -07:00
Elliott Hughes
a9209d7a0e Fix some easy cases of __ANDROID__ versus __BIONIC__.
We need to make a clearer distinction for bionic on the host. This patch
doesn't fully address things like "should host bionic try to talk to netd?"
for now, but is a step in the right direction.

Bug: http://b/31559095
Test: bionic tests.
Change-Id: I49812f8b75d9d78c4fd8a8ddf5df1201d63771d6
2016-09-16 18:27:07 -07:00
Elliott Hughes
7f0849fd11 Fix sscanf/wcstod parsing of NaNs.
The parsefloat routines -- which let us pass NaNs and infinities on to
strto(f|d|ld) -- come from NetBSD.

Also fix LP64's strtold to return a NaN, and fix all the architectures
to return quiet NaNs.

Also fix wcstof/wcstod/wcstold to use parsefloat so they support hex
floats.

Lots of new tests.

Bug: http://b/31101647
Change-Id: Id7d46ac2d8acb8770b5e8c445e87cfabfde6f111
2016-09-07 15:01:54 -07:00
Elliott Hughes
fb3873d4db Fortify vsnprintf in more cases.
Bug: http://b/30445072
Change-Id: I1893890f0e3b56533eef053eda1bd96a0b9a5119
2016-08-10 11:50:12 -07:00
Elliott Hughes
53cf348c82 Remove more stdio copy/paste.
Change-Id: Ia92629b75d2c153ecf1cec711e2f9575eef604ab
2016-08-09 16:27:23 -07:00
Elliott Hughes
d1f25a7eb1 Reimplement remove(3) without the lstat(2).
This assumes that it's more likely we're unlinking a file than a directory,
though even if that's not true, as long as a failed unlink(2) is cheaper
than a successful lstat(2) -- which seems likely since there's no data to
copy -- we still win.

Change-Id: I0210e9cd3d31b8cf1813c55c810262ef327382ed
2016-08-05 15:53:03 -07:00
Elliott Hughes
70715da453 More stdio one-liners.
This actually turns up a bug in fmemopen, so I guess that's what I'll
look at next...

Change-Id: I2971ecd1b5a3a3c7f43c22d985f88e389af89e97
2016-08-01 18:06:38 -07:00
Elliott Hughes
cceaf069c7 More stdio cleanup.
Time to get back to cleaning up stdio, so start with a bunch of easy
one-liners...

Change-Id: I8df5fdc72500a89b977bfaa6c64c3639198d4e3e
2016-07-29 16:31:52 -07:00
Elliott Hughes
7b737e43f8 Remove #pragma GCC visibility.
We have better control over visibility now.

Bug: http://b/24767418
Change-Id: Ifa9a1e5af4bd4217db08422050908770247b3b69
2016-07-25 15:57:02 -07:00
Elliott Hughes
20788aec00 Add ctermid.
Change-Id: I7c7c815c2725df222932db923632c8b6419741ab
2016-06-09 15:16:32 -07:00
Chih-Hung Hsieh
62e3a078aa Fix google-explicit-constructor warnings.
Bug: 28341362
Change-Id: I84effbdfa1b9b39328a909b7f70fe17e7ee316c8
2016-05-03 12:08:05 -07:00
Elliott Hughes
f226ee59e0 Add fopen64/freopen64/tmpfile64 aliases.
Our fopen/freopen/tmpfile are already always O_LARGEFILE, but let's add
the aliases for _LARGEFILE_SOURCE compatibility.

Bug: http://b/24807045
Change-Id: I5d99b3ef3c9f27ce70f13313f6a92e96c7f21f80
2016-02-03 11:24:28 -08:00
Elliott Hughes
955426ef79 Fix a sign extension bug in stdio.
This also lets us test the EOVERFLOW behavior, which pointed out that the
fgetpos/fsetpos return on failure has always been wrong...

Bug: http://b/24807045
Change-Id: I35273eb07c8c9155af858adb27569983397580b6
2016-01-26 18:25:52 -08:00
Elliott Hughes
03e65eb03b Implement funopen64.
Bug: http://b/24807045
Change-Id: I161920978161389be34b707cc6ce8e05f760d552
2016-01-26 14:45:13 -08:00
Elliott Hughes
9677fab88c Support _FILE_OFFSET_BITS=64 for most of <stdio.h>.
This doesn't address funopen, but does add fgetpos/fsetpos/fseeko/ftello.

Bug: http://b/24807045
Change-Id: Ibff6f00df5fb699c8e8f13b91a75caf024540b73
2016-01-26 09:14:41 -08:00
Elliott Hughes
e70e0e9267 Move internal FILE flags macros to "local.h" from <stdio.h>.
Bug: http://b/24807045
Change-Id: Ia42ec1907370b4af957ece94a856f71c7ee27cb7
2016-01-25 11:10:47 -08:00
Elliott Hughes
023c3071a0 Add _seek64 to FILE.
Move fdopen/fopen/freopen and change them to initialize _seek64 instead
of the legacy _seek. The in-memory streams can stick with _seek for now,
since you're not going to fit a > 4GiB in-memory stream on a 32-bit device
anyway.

Bug: http://b/24807045
Change-Id: I09dcb426817b571415ce24d4d15f364cdda395b3
2016-01-22 23:54:10 -08:00
Elliott Hughes
b877601bff Put struct FILE back how NDK-built apps expect it.
The first rule of stdio is you never change struct FILE. This broke all
NDK-built apps that used stdin/stdout/stderr. (Which is more than you
might think, given that those streams don't go anywhere useful. Svelte!)

I've added a big code comment because I knew when I removed the field that
doing so was a mistake, but I couldn't think why.

Bug: http://b/24807045
Bug: http://b/26747402
Change-Id: Ie1233586b223bb1cdf8e354c66d5ff23487a833a
2016-01-22 19:23:40 -08:00
Elliott Hughes
2704bd1340 Simplify fseek/ftell.
Another step towards _FILE_OFFSET_BITS=64 support.

Bug: http://b/24807045
Change-Id: I00b83c81a7b108176c4d9437bc32611f73b7e967
2016-01-21 10:34:35 -08:00
Elliott Hughes
021335ebda Move stdio implementation details around a little.
Change-Id: I24594426d5479bdd55cbef0ab1b7d76c249dbd0c
2016-01-20 08:50:51 -08:00
Elliott Hughes
923f165b29 Make FILE*s less usable after fclose(3).
BSD doesn't invalidate the fd stored in struct FILE, which can make
it possible (via fileno(3), for example), to perform operations on
an fd you didn't intend to (rather than just failing with EBADF).

Fixing this makes the code slightly simpler anyway, and might help
catch bad code before it ships.

Bug: http://stackoverflow.com/questions/10816837/fclose-works-differently-on-android-and-linux
Change-Id: I9db74584038229499197a2695c70b58ed0372a87
2016-01-19 15:46:05 -08:00
Elliott Hughes
29ee6397a8 Fix stdio static initializers to make both clang and GCC happy.
Previously only clang was happy. GCC said:

  error: missing initializer for field 'wcio_mbstate_in' of 'struct wchar_io_data'

Change-Id: I25a11b64f4dfa22a5dd5daded152191fe2cfacaf
2015-12-07 11:07:15 -08:00
Elliott Hughes
bb46afd6c4 Revert "Revert "Remove __sinit and __sdidinit.""
This reverts commit c8bae05f3f.

We were breaking init (ueventd) because we initialize system properties
before we initialize stdio. The new system property implementation uses
stdio to read from /property_contexts, so we end up touching stdio data
structures before they've been initialized.

This second attempt takes things further by removing the stdio initialization
function altogether. The data structures for stdin/stdout/stderr can be
statically initialized as data, and -- since we already had to give the
atexit implementation a backdoor for stdio -- we can just admit that we
need to clean up stdio, and that we always do so last.

This patch also removes the 17 statically pre-allocated file structures,
so the first fopen will now allocate a block of 10 (the usual overflow
behavior). I did this just to make my life simpler, but it's not actually
necessary to remove it if we want it back.

Change-Id: I936b2eb5e88e4ebaf5516121872b71fc88e5609c
2015-12-05 07:30:59 -08:00
Elliott Hughes
c8bae05f3f Revert "Remove __sinit and __sdidinit."
This reverts commit 4371961e00.

This broke booting; ueventd crashes with a null pointer dereference
somewhere in __sfp (but the kernel doesn't unwind, so I don't know
what was calling __sfp).

Change-Id: I65375fdfdf1d339a06558b4057b580cacd6324e2
2015-12-04 17:47:20 -08:00
Elliott Hughes
4371961e00 Remove __sinit and __sdidinit.
We're eagerly initializing stdio now, so this can all be simplified.

Change-Id: Icb288f8dd0ee08f02bea0d23670f75e78bed6b99
2015-12-03 13:23:03 -08:00
Yabin Cui
76144aaa63 Change _stdio_handles_locking into _caller_handles_locking.
It is reported by tsan that funlockfile() can unlock an unlocked mutex.
It happens when printf() is called before fopen() or other stdio stuff.
As FLOCKFILE(fp) is called before __sinit(), _stdio_handles_locking is false,
and _FLOCK(fp) will not be locked. But then cantwrite(fp) in __vfprintf()
calls__sinit(), which makes _stdio_handles_locking become true, and
FUNLOCKFILE(fp) unlocks _FLOCK(fp).

Change _stdio_handles_locking into _caller_handles_locking,
so __sinit() won't change its value. Add test due to my previous fault.

Bug: 25392375
Change-Id: I483e3c3cdb28da65e62f1fd9615bf58c5403b4dd
2015-11-20 17:44:26 -08:00
Yabin Cui
fb994f4c04 Add fileno_unlocked to support thread sanitizer.
Bug: 25392375
Change-Id: If3f92a0e08a53f4b59e01397e8efa307d8572349
2015-11-06 16:13:47 -08:00
Christopher Ferris
a818445622 Rename LIBC64_HIDDEN to LIBC32_LEGACY_PUBLIC.
Change-Id: If4da9e46398ca5524f6f0680d70588e3dc7e80b4
2015-10-23 12:32:52 -07:00
Elliott Hughes
f0141dfab1 Make struct FILE opaque.
Bug: http://b/24807045
Change-Id: I3cb4e436325a65bef3b203e3970e4d967101fcce
2015-10-12 12:53:42 -07:00
Elliott Hughes
8ab433df13 Fix stdio read after EOF behavior.
Bug: https://code.google.com/p/android/issues/detail?id=184847
Change-Id: Ia20ce94007c2a09649f0763b1dc7ba959f2f618d
2015-10-09 17:57:26 -07:00
Elliott Hughes
4e8ef698d0 Don't use TEMP_FAILURE_RETRY on close in bionic.
Bug: http://b/20501816
Change-Id: Id64b5109cc2b165fa0351b6edbb865a5e5058008
2015-05-15 16:20:07 -07:00
Yabin Cui
17393b06ba Hide internal of pthread_mutex_t.
Bug: 19249079
Change-Id: Iffb79c8d861b698d474f212dc80c638fc2cf1620
2015-03-25 17:54:17 -07:00
Christopher Ferris
cc9ca1051d Fix fread returning bad data.
Bug: 19172514
Change-Id: I05016577858a02aca7d14e75e6ec28abc925037c
2015-02-27 18:22:45 -08:00
Elliott Hughes
e6bb5a2776 Fix optimized fread.
gcov does writes after reads on the same stream, but the bulk read optimization
was clobbering the FILE _flags, causing fwrite to fail.

Bug: 19129055
Change-Id: I9650cb7de4bb173a706b502406266ed0d2b654d7
2015-01-24 12:35:41 -08:00
Elliott Hughes
8b5df3920f Turn on -Wold-style-cast and fix the errors.
A couple of dodgy cases where we cast away const, but otherwise pretty boring.

Change-Id: Ibc39ebd525377792b5911464be842121c20f03b9
2015-01-21 17:09:58 -08:00
Elliott Hughes
8c4994bbc1 Implement __fsetlocking.
The old __isthreaded hack was never very useful on Android because all user
code runs in a VM where there are lots of threads running. But __fsetlocking
lets a caller say "I'll worry about the locking for this FILE*", which is
useful for the normal case where you don't share a FILE* between threads
so you don't need any locking.

Bug: 17154740
Bug: 18593728
Change-Id: I2a8dddc29d3edff39a3d7d793387f2253608a68d
2015-01-21 10:33:30 -08:00
Elliott Hughes
e69e6458cc Fix signed/unsigned comparison that was upsetting clang.
bionic/libc/stdio/fread.c:86:27: error: comparison of integers of different signs: 'int' and 'size_t' (aka 'unsigned int') [-Werror,-Wsign-compare]

Change-Id: Ia7e1e053e0cb13113e8f2eede820be013acbab82
2015-01-20 16:52:04 -08:00
Elliott Hughes
8885dcc779 Add TEMP_FAILURE_RETRY to stdio's low-level read/write functions.
This is correctness rather than performance, but found while investigating
performance.

Bug: 18593728
Change-Id: Idbdfed89d1931fcfae65db29d662108d4bbd9b65
2015-01-20 16:18:32 -08:00
Elliott Hughes
75b99387dd Optimized fread.
This makes us competitive with glibc for fully-buffered and unbuffered reads,
except in single-threaded situations where glibc avoids locking, but since
we're never really single-threaded anyway, that isn't a priority.

Bug: 18593728
Change-Id: Ib776bfba422ccf46209581fc0dc54f3567645b8f
2015-01-20 15:59:17 -08:00
Elliott Hughes
20841a137b Avoid pathological behavior in OpenBSD's fread.
Bug: https://code.google.com/p/android/issues/detail?id=81155
Bug: 18556607
Change-Id: Idc60976b79610e2202cc42dc393dcb4ca6c42e05
2014-12-02 14:22:02 -08:00
Elliott Hughes
c48c3e4bb3 Fix flockfile(3) and friends for stdin/stdout/stderr too.
stdin/stdout/stderr are special; their mutexes are initialized by
__sinit. There's no unit test for this, because __sinit has already
been called by the time the first unit test runs, but you could
reproduce this failure with a trivial main() that calls flockfile
or ftrylockfile on one of the standard streams before otherwise
using stdio.

Bug: 18208568
Change-Id: I28d232cf05a9f198a2bed61854d8047b23d2091d
2014-11-19 15:16:51 -08:00
Elliott Hughes
168667c972 Add non-macro stdin/stdout/stderr too.
Various C and C++ standards explicitly say that stdin/stdout/stderr
should be macros, but glibc makes them global variables too. This
means it's possible to write code that uses those names as locals,
but that code (toybox being an example) won't build on bionic.

If we'd done this earlier, we could have hidden __sF for LP64, but
it's too late now.

Change-Id: I90cf8c73f52b66e1760b8fa2e135b9f9f9651230
2014-11-14 14:42:59 -08:00
Chih-Hung Hsieh
dc6599e20f Fix warning of missing field initializer.
BUG: 18241135
Change-Id: I91304eb608bc3336ce5fcd5744fbe080d64f1cc2
2014-11-04 12:09:35 -08:00
Elliott Hughes
6a03abcfd2 Ensure we initialize stdin/stdout/stderr's recursive mutexes.
Bug: 18208568
Change-Id: I9da16ce0f9375bc363d1d02be706d73fd3b1e150
2014-11-03 12:32:17 -08:00
Christopher Ferris
78ba823033 Add back symbols to fix Greed for Glory franchise.
Bug: 17813018

(cherry picked from commit c891e24073)

Change-Id: I4e891c2dbad9ea00d2377c5175317d9ae3840153
2014-10-09 22:23:50 -07:00
Elliott Hughes
abefc93ff3 Re-expose more stdio implementation details for LP32.
Keeps a variety of apps running.

(cherry-pick of 5def2f5aecd968e4022b0afbe4441fa7ba3e7c7e.)

Bug: 17047819
Change-Id: I55882ec95f2b59a5df76e5a89c23aa315609e01d
2014-09-24 20:07:10 -07:00
Elliott Hughes
c08c25b4c9 Sync with upstream findfp.c.
Change-Id: Ib298f46de0c4d0fd4ce0a9b456c163e8b8b06342
2014-09-24 10:32:30 -07:00
Elliott Hughes
5f35710fad Expose __swsetup for LP32 binary compatibility.
(cherry-pick of 7e00b44e80d6e38b8ab86d0ebc86b666c0ac2ef6.)

Bug: 17476127
Change-Id: I0ef1355ac913d782c268a638f88642d6cfc236c2
2014-09-11 18:23:46 -07:00
Elliott Hughes
2b021e1066 Add <stdio_ext.h> for elfutils.
Bug: 17139679
Change-Id: I1605ac382dbb6f23b2d874dbb9769f3cde4a6a99
2014-08-20 10:23:52 -07:00
Elliott Hughes
a167eef548 Fix visibility for a bunch more symbols.
Bug: 11156955
Bug: 15291317
Change-Id: I664f25cce7c17085a101d6593d8e01525a1f6a90
2014-07-14 15:13:30 -07:00
Elliott Hughes
f6d2ca2696 Expose __srefill for LP64 until we can fix the NDK.
The LP64 has a duplicate copy of part of stdio, and relies
on bionic supplying this part. We should remove the hack from
the NDK, at least for LP64, and then revert this.

Bug: 15291317
Change-Id: I75e06e130188ca0aeb9d50dfe3a3e48a1d3968b7
2014-05-27 21:23:36 -07:00
Elliott Hughes
aa50585d96 Restore __srefill for LP32.
The NDK apparently includes an android_support.a library that
refers to __srefill in its copy of the vsnprintf implementation.

Bug: 15249361
Change-Id: Ic2cf6f21290b3146c42fbe0624f5e4d54f6194b4
2014-05-27 11:22:39 -07:00
Elliott Hughes
35d90bb6ec Restore __srget and __swbuf for LP32.
Anthony King <anthonydking@slimroms.net> reports that for Grouper the
Nvidia GL blobs need access to __swbuf. This is because the old <stdio.h>
had inline getc and putc implementations that directly referred to these
symbols.

Change-Id: I11a7b5550018ecc93d8f195c99857759669b2906
2014-05-25 10:38:25 -07:00
Elliott Hughes
2899de9b05 Hide various stdio implementation details.
I've left __sF exposed since that's how the OpenBSD stdin, stdout, stderr
are implemented. Other BSDs and glibc use a separate global for each instead
of an array.

Bug: 11156955
Change-Id: I9f3d2d4314a8d4a78c3197b9acd9258820c5f150
2014-05-22 20:06:23 -07:00
Elliott Hughes
4c2da039ba Fix the newly-introduced warning and turn on -Werror.
I cleaned up most of our warnings last week but forgot to turn on -Werror,
so of course we're getting new warnings already. I've left -Werror commented
out in those places where we still have warnings to deal with before we can
turn on -Werror.

Change-Id: Ia58ff8b8c1ada4bf81eec6f19ec1d34e133cf4b1
2014-05-16 16:50:34 -07:00
Dmitriy Ivanov
623b0d05bd Register _cleanup function with atexit
* Register cleanup function with atexit
   instead of calling it explicitly on
   exit()
 * abort() no longer calls _cleanup:
   Flushing stdio buffers on abort is no
   longer required by POSIX.
 * dlmalloc no longer need to reset cleanup
   (see above)
 * Upstream findfp.c makebuf.c setvbuf.cexit.c
   to openbsd versions.

Bug: 14415367
Change-Id: I277058852485a9d3dbb13e5c232db5f9948d78ac
2014-05-15 13:05:21 -07:00
Calin Juravle
7b1a95bc76 Merge "Add include guard to fileext.h" 2014-05-06 13:47:19 +00:00
Elliott Hughes
ad534985ec Merge "Disable %n in printf and vfwprintf." 2014-05-05 21:49:22 +00:00
Elliott Hughes
e2341d08fa Disable %n in printf and vfwprintf.
Bug: 14492135
Change-Id: If190bede29e5f68a65043ddbe8e878c660933d03
2014-05-05 14:43:17 -07:00
Elliott Hughes
792ae72e41 Switch to current upstream OpenBSD fwrite.c.
Change-Id: Ife527aafc1e5438f477d711902efe6e6f59f3f8e
2014-05-02 18:22:23 -07:00
Calin Juravle
9287223674 Add include guard to fileext.h
Change-Id: I89b0e3bf0b15989727b5903b40bec92d49e122d9
2014-05-02 17:34:58 +01:00
Elliott Hughes
01ae00f317 Switch to the OpenBSD implementations of the wide scanf functions.
This also gets us the C99 wcstoimax and wcstoumax, and a working fgetwc and
ungetwc, all of which are needed in the implementation.

This also brings several other files closer to upstream.

Change-Id: I23b025a8237a6dbb9aa50d2a96765ea729a85579
2014-04-29 16:28:56 -07:00
Elliott Hughes
9d3c2dd11f Switch to upstream OpenBSD fread/fvwrite.
Change-Id: I3dc11083693bc8d99edc0cbcc6f70dc9e5dc6565
2014-04-18 13:13:04 -07:00
Elliott Hughes
0549371bd7 Upgrade to current vfprintf.
This gets us various bug fixes and missing C99 functionality.

Bug: https://code.google.com/p/android/issues/detail?id=64886
Change-Id: Ie9f8ac569e9b5eec1e4a1faacfe2c21662eaf895
2014-04-17 17:30:03 -07:00
Elliott Hughes
4bd97cee28 Switch to gdtoa.
This gives us a real strtold for LP64 and fixes various LP64
bugs.

Bug: 13563801
Change-Id: I277858d718ee746e136b6b6308a495ba50dfa488
2014-04-16 15:15:52 -07:00
Elliott Hughes
02c78a3867 Reimplement isinf/isnan/fpclassify.
Also move isinf and isnan into libc like everyone else.

Also move fpclassify to libc like the BSDs (but unlike glibc). We need
this to be able to upgrade our float/double/long double parsing to gdtoa.

Also add some missing aliases. We now have all of:

  isnan, __isnan, isnanf, __isnanf, isnanl, __isnanl,
  isinf, __isinf, isinff, __isinff, isinfl, __isinfl,
  __fpclassify, __fpclassifyd, __fpclassifyf, __fpclassifyl.

Bug: 13469877
Change-Id: I407ffbac06c765a6c5fffda8106c37d7db04f27d
2014-04-14 14:35:47 -07:00
Elliott Hughes
5eb670462a Replace the bionic hack for a vfprintf leak with (roughly) the upstream fix.
Currently in bionic free and freedtoa are equivalent, but that's not true
of gdtoa. This makes it easier to test gdtoa without having to replace
everything. (Yes, I found this bug the hard way.)

Change-Id: I290823a2a0a83329def5f2719b349215ad0dbbde
2014-04-11 18:00:37 -07:00
Alexander Ivchenko
edd7c2ec25 Fix the printf issue for 64 bits. The following case:
printf("%1$s %1$s\n", "test");

would print garbage instead of the second "test". The problem is __find_arguments
and the patch is a backport of two patches from OpenBSD that fix the issue:

Author: tedu <tedu@cvs.openbsd.org>
Date:   Sat Apr 29 23:00:24 2006 +0000

    check mmap for failure.  the helper functions using it return -1, but
    callers do not yet check since printf() for example is not documented
    to return an error.
    some formatting cleanups.
    mostly ok deraadt millert

Author: millert <millert@cvs.openbsd.org>
Date:   Fri May 16 14:28:54 2008 +0000

    C99 says that for each va_copy() there must be a matching va_end().
    Replace the non-portable hackery in __find_arguments() with a union.
    From FreeBSD.

Change-Id: I6ea392ce6fcf4a319ae6a67ec58cc52fe7cbe534
Signed-off-by: Alexander Ivchenko <alexander.ivchenko@intel.com>
2014-04-02 18:30:55 +04:00
Calin Juravle
c20de90287 Remove ALIGNBYTES and ALIGN from <sys/param.h>
There are only three users of bionic definition of ALIGN and keeping it
in sys/param.h polutes the namespace.

I inline the definition in the the three places that's used.

Bug: 13400663
Change-Id: I565008e8426c38ffb07422f42cd8e547d53044e9
2014-03-21 09:59:34 +00:00