summaryrefslogtreecommitdiff
path: root/libc/stdio/stdio.cpp
AgeCommit message (Collapse)Author
2021-02-09Make fd overflow an abort.Elliott Hughes
On LP32, just abort if we're asked to handle an fd that's too big for the `short` field in `struct FILE`. This is unreachable anyway because the ulimit is 32Ki, and this will make issues far more noticeable if we ever do increase that limit (which seems unlikely for LP32 devices). Also rename __finit() to __FILE_init() to match __FILE_close(). Test: treehugger Change-Id: I5db4d6c4529a1f558aff135b4dea071d73666be5
2021-02-04Fix freopen() where the path is null.Elliott Hughes
This has been in the standard since C99, but we've never supported it before. It's apparently used by SPIRV-Tools. I tried implementing this the other way (with fcntl(2)) first, but eventually realized that that's more complicated and gives worse results. This implementation assumes that /proc is mounted, but so much of libc relies on that at this point that I don't think there's any realistic case where the fcntl(2) implementation would be preferable, and there are many where it's not. The fact that no-one's mentioned this until now suggests that it's not a heavily used feature anyway. I've also replaced AssertCloseOnExec() with a CloseOnExec() boolean-valued function instead, because it's really annoying getting assertion failures that don't point you at the test line in question, and instead point to some common helper code. Test: treehugger Change-Id: Ia2e53bf2664a4f782581042054ecd492830e2aed
2021-01-11Fewer copies of ALIGN()/ALIGNBYTES.Elliott Hughes
Noticed while updating fts.c. Bug: http://b/177003648 Test: treehugger Change-Id: Ic3625c1c3af47c4dafb8ad686bbbddbc82b69b70
2020-08-12Various coverage improvements.Elliott Hughes
Mostly from extra test cases, but also: * Move the fgets size < 0 assertion into fgets. * Use ELF aliases for strtoq/strtouq rather than duplicating code. * Don't check uname() succeeded, since it can't fail. Test: treehugger Change-Id: I2e6b3b88b0a3eb16bd68be68b9bc9f40d8043291
2020-07-21Changes for #inclusivefixit.Elliott Hughes
Test: treehugger Change-Id: I7ff0496c5c2792a41781e74634247f55b0548213
2019-06-19Remove the ___ hack.Elliott Hughes
Plain __ for generated syscalls didn't mean it was a hidden symbol, it just meant "please don't use this". We added ___ to signify that a hidden symbol should be generated, but then we added the map files anyway so you now have to explicitly export symbols. Given that, this convention serves no particular purpose so we may as well just use the nicer names have everything look the same. Test: treehugger Change-Id: If424e17a49c36f4be545f5d283c4561a6ea9c7ea
2019-05-04Revert fwalk/sfp locking to fix concurrent readsRyan Prichard
The locking can fail in a couple of ways: - A concurrent fread from an unbuffered or line-buffered file flushes the output of other line-buffered files, and if _fwalk locks every file, then the fread blocks until other file reads have completed. - __sfp can initialize a file lock while _fwalk is locking/unlocking it. For now, revert to the behavior Bionic had in previous releases. This commit reverts the file locking parts of commit 468efc80da2504f4ae7de8b5e137426d44dda9d7. Bug: http://b/131251441 Bug: http://b/130189834 Test: bionic unit tests Change-Id: I9e20b9cd8ccd14e7962f7308e174f08af72b56c6
2019-03-26Merge "Typo fix in comment. O_CLOEXEC is e, not x."Treehugger Robot
2019-03-26Typo fix in comment. O_CLOEXEC is e, not x.Dan Albert
Test: None Bug: None Change-Id: I061fe1d3cac6307d878155f1fabdba70da8e9fd5
2019-03-26Merge "Fix internal uses of _PATH_BSHELL."Treehugger Robot
2019-03-25Fix internal uses of _PATH_BSHELL.Elliott Hughes
We regressed on this recently: code under the upstream-* directories has _PATH_BSHELL defined as a call to __bionic_get_shell_path(). In our own code, we may as well just call it directly. Bug: https://issuetracker.google.com/129030706 Test: ran tests Change-Id: Ic2423f521272be95e67f94771772fe8072636ef0
2019-03-25popen: stop using _fwalk.Elliott Hughes
We don't need this now that popen always uses O_CLOEXEC, and it's unsafe because _fwalk takes a lock. (In <= P, the equivalent code walked the list without a lock in the child.) Bug: http://b/129156634 Test: ran tests Change-Id: Ic9cee7eb59cfc9397f370d1dc47ea3d3326179ca
2018-10-01Make fclose/pclose distinct.Elliott Hughes
I failed to convince the compiler/linker to just refrain (via factoring out, attribute `noinline`, or meddling with `--icf=none`), but luckily I noticed that we should have CHECK_FP in each function for a better error message, and the distinct error messages keep the two functions apart. (Also add a missing CHECK_FP to `clearerr_unlocked`.) Bug: http://b/116969207 Test: manual with a modified `crasher` Change-Id: Ic122e90e94f7e22f486be57d3dac7137a225d682
2018-08-03Make all popen(3) file descriptors O_CLOEXEC.Elliott Hughes
POSIX says "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". It doesn't appear to disallow all popen(3) file descriptors from being O_CLOEXEC, and it's not obvious why anyone would want them inherited. Let's see if we can make the stricter guarantee... Bug: N/A Test: ran tests Change-Id: I2c85170d730b211637afb8ba10df150ca3237262
2018-07-19Introduce api to track fd ownership in libc.Josh Gao
Add two functions to allow objects that own a file descriptor to enforce that only they can close their file descriptor. Use them in FILE* and DIR*. Bug: http://b/110100358 Test: bionic_unit_tests Test: aosp/master boots without errors Test: treehugger Change-Id: Iecd6e8b26c62217271e0822dc3d2d7888b091a45
2018-07-11Reimplement popen(3)/pclose(3).Elliott Hughes
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-02-06Remove __overloadable/__RENAME_CLANGGeorge Burgess IV
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
2017-12-20Speed up __sfileext initialization.Elliott Hughes
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-11-10Move public scanf functions away from implementation.Elliott Hughes
Bug: N/A Test: ran tests Change-Id: Ifb8bd91132865f8c6d0b95baf1021af3b3b3c353
2017-11-07Allow 32-bit fseeko/fseeko64 SEEK_CUR/SEEK_SET to exceed 2 GiBRyan Prichard
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-02More printf de-duplication.Elliott Hughes
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-10-31More missing _unlocked <stdio.h> functions.Elliott Hughes
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-23Add explicit null checks to some stdio functions.Josh Gao
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-07-25Use O_APPEND for stdio append mode.Elliott Hughes
"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-24Remove the HASLB and FREEUB macros.Elliott Hughes
Because we hate macros. Bug: N/A Test: ran tests Change-Id: I2c94085ff502ec5ce6d8598ec6b3c10e7a4b5510
2016-10-20Fix stdin/stdout/stderr for pre-M.Dan Albert
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-08-10Fortify vsnprintf in more cases.Elliott Hughes
Bug: http://b/30445072 Change-Id: I1893890f0e3b56533eef053eda1bd96a0b9a5119
2016-08-09Remove more stdio copy/paste.Elliott Hughes
Change-Id: Ia92629b75d2c153ecf1cec711e2f9575eef604ab
2016-08-05Reimplement remove(3) without the lstat(2).Elliott Hughes
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-01More stdio one-liners.Elliott Hughes
This actually turns up a bug in fmemopen, so I guess that's what I'll look at next... Change-Id: I2971ecd1b5a3a3c7f43c22d985f88e389af89e97
2016-07-29More stdio cleanup.Elliott Hughes
Time to get back to cleaning up stdio, so start with a bunch of easy one-liners... Change-Id: I8df5fdc72500a89b977bfaa6c64c3639198d4e3e
2016-06-09Add ctermid.Elliott Hughes
Change-Id: I7c7c815c2725df222932db923632c8b6419741ab
2016-05-03Fix google-explicit-constructor warnings.Chih-Hung Hsieh
Bug: 28341362 Change-Id: I84effbdfa1b9b39328a909b7f70fe17e7ee316c8
2016-02-03Add fopen64/freopen64/tmpfile64 aliases.Elliott Hughes
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-01-26Fix a sign extension bug in stdio.Elliott Hughes
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-26Implement funopen64.Elliott Hughes
Bug: http://b/24807045 Change-Id: I161920978161389be34b707cc6ce8e05f760d552
2016-01-26Support _FILE_OFFSET_BITS=64 for most of <stdio.h>.Elliott Hughes
This doesn't address funopen, but does add fgetpos/fsetpos/fseeko/ftello. Bug: http://b/24807045 Change-Id: Ibff6f00df5fb699c8e8f13b91a75caf024540b73
2016-01-22Add _seek64 to FILE.Elliott Hughes
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-22Put struct FILE back how NDK-built apps expect it.Elliott Hughes
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-21Simplify fseek/ftell.Elliott Hughes
Another step towards _FILE_OFFSET_BITS=64 support. Bug: http://b/24807045 Change-Id: I00b83c81a7b108176c4d9437bc32611f73b7e967
2016-01-20Move stdio implementation details around a little.Elliott Hughes
Change-Id: I24594426d5479bdd55cbef0ab1b7d76c249dbd0c