Commit graph

34 commits

Author SHA1 Message Date
Sergey Bugaev
839ae82d66 LibC: Ensure abort() doesn't return
It's not enough to send ourselves a SIGABRT, as it may be ignored or handled
differently. We really, really want abort() to never return, as that will mess
up the assumptions of the calling code big time. So, if raise(SIGABRT) returns,
kill ourselves with SIGKILL, and if that somehow returns too, call _exit().

An alternative approach, which glibc apparently follows, is to reset SIGABRT
disposition to its default value and then send SIGABRT to yourself a second
time. That would also work, but I believe SIGKILL + _exit() to be a simpler
approach that is less likely to break in extremely weird situations.

Note that this only guarantees that abort() never returns, not that the process
actually gets killed. It's still possible to install a SIGABRT handler that
simply never returns (such as by longjmp'ing out, endlessly looping, or exec'ing
another image). That is a legitimate use case we want to support; at the same
time most software doesn't use that functionality and would benefit from hard
guarantees that abort() terminates the program. The following commit is going to
introduce means for ensuring SIGABRT handler is never reset to something
unexpected.
2020-05-26 14:35:10 +02:00
Sergey Bugaev
ac4a31e057 LibC: Mark _exit() as noreturn
We already do this for exit().
2020-05-26 14:35:10 +02:00
AnotherTest
6b1ed26e6a LibC: Always assign the offset pointer to endptr in strto{u,}ll()
This patch makes strto{u,}l{l,}() behave more to-spec about endptr.
"If endptr is not NULL, strtoull stores the address of the first invalid
character in *endptr."
2020-05-12 13:28:57 +02:00
Ben Wiederhake
71fd752289 LibC: Implement strtoull correctly
This fixes the behavior for several inputs:
- '-0' (shouldn't work but was accepted)
- '+3' (shouldn't work but was accepted)
- '13835058055282163712' (should work but returned 9223372036854775807 with errno=ERANGE)
2020-05-11 10:52:24 +02:00
Ben Wiederhake
1f9dcdc41c LibC: Use more flexible digit parsing code, deduplicate 2020-05-11 10:52:24 +02:00
Ben Wiederhake
bc5d5bf75d LibC: Implement new strtod, accurate up to 8 eps
This strtod implementation is not perfectly accurate, as evidenced by the test
(accuracy_strtod.cpp), but it is sufficiently close (up to 8 eps).

The main sources of inaccuracy are:
- Highly repeated division/multiplication by 'base'
  (Each operation brings a multiplicative error of 1+2^-53.)
- Loss during the initial conversion from long long to double (most prominently,
  69294956446009195 should first be rounded to 69294956446009200 and then
  converted to 69294956446009200.0 and then divided by ten, yielding
  6929495644600920.0. Currently, it converts first to double, can't represent
  69294956446009195.0, and instead represents 69294956446009190, which
  eventually yields 6929495644600919.0. Close, but technically wrong.)

I believe that these issues can be fixed by rewriting the part at and after
    double value = digits.number();
and that the loss before that is acceptable.

Specifically, losing the exact exponent on overflow is obviously fine.
The only other loss occurs when the significant digits overflow a 'long long'.
But these store 64(-7ish) bits, and the mantissa of a double only stores 52 bits.
With a bit more thinking, one could probably get the error down to 1 or 2 eps.
(But not better.)

Fixes #1979.
2020-05-11 10:52:24 +02:00
Stephan Unverwerth
b82a2239c6
LibC: Fix strtod() parsing of negative exponents (#1645)
Before this fix negative exponents were interpreted as positive.
2020-04-05 15:32:57 +02:00
Andreas Kling
f2a087126c LibC: Add posix_openpt(), grantpt() and unlockpt()
This makes getting a pseudoterminal pair a little bit more portable.
Note that grantpt() and unlockpt() are currently no-ops, since we've
already granted the pseudoterminal slave to the calling user.

We also accept O_CLOEXEC to posix_openpt(), unlike some systems. :^)
2020-02-05 21:17:41 +01:00
Andreas Kling
545e2ba065 LibC: Use the templated type consistently in strtol_impl<T> 2020-01-18 11:41:04 +01:00
Andreas Kling
94ca55cefd Meta: Add license header to source files
As suggested by Joshua, this commit adds the 2-clause BSD license as a
comment block to the top of every source file.

For the first pass, I've just added myself for simplicity. I encourage
everyone to add themselves as copyright holders of any file they've
added or modified in some significant way. If I've added myself in
error somewhere, feel free to replace it with the appropriate copyright
holder instead.

Going forward, all new source files should include a license header.
2020-01-18 09:45:54 +01:00
Andreas Kling
24c736b0e7 Kernel: Use the Syscall string and buffer types more
While I was updating syscalls to stop passing null-terminated strings,
I added some helpful struct types:

    - StringArgument { const char*; size_t; }
    - ImmutableBuffer<Data, Size> { const Data*; Size; }
    - MutableBuffer<Data, Size> { Data*; Size; }

The Process class has some convenience functions for validating and
optionally extracting the contents from these structs:

    - get_syscall_path_argument(StringArgument)
    - validate_and_copy_string_from_user(StringArgument)
    - validate(ImmutableBuffer)
    - validate(MutableBuffer)

There's still so much code around this and I'm wondering if we should
generate most of it instead. Possible nice little project.
2020-01-11 12:47:47 +01:00
Andreas Kling
7c916b9fe9 Kernel: Make realpath() take path+length, get rid of SmapDisabler 2020-01-06 11:32:25 +01:00
Shannon Booth
47276a09dd LibC: Remove dubious String ends_with usage
As mentioned in #917, the String destructor could potentially be
clobbering the errno. Use memcpy so that we do not need String at all.
2020-01-06 10:43:00 +01:00
Andreas Kling
9d681beaf0 LibC: Oops x2, we can't use "bool" in stdlib.h either 2019-12-26 10:30:34 +01:00
Shannon Booth
f6bd4f8691 LibC: Use shared functon to generate unique filenames
Move some dupliated code into __generate_unique_filename()
2019-12-26 10:05:59 +01:00
Andrew Kaster
824bbc7462 LibC: Support exit time destructors per the Itanium C++ ABI
Implement __cxa_atexit and __cxa_finalize per the Itanium spec,
and convert stdlib's atexit and exit() to to call them instead of
a custom 'C-only' atexit implementation.
2019-12-22 10:47:39 +01:00
Valtteri Koskivuori
fe1df9e9fb LibC: Implement strtod() 2019-12-10 16:24:47 +01:00
William McPherson
680fd3999e LibC: Implement bsearch
Nothing fancy, just a simple implementation of bsearch(3).
2019-11-29 11:04:01 +01:00
Andreas Kling
196b64c0ae LibC: Move realpath() to <stdlib.h> 2019-11-16 17:29:09 +01:00
Brandon Scott
bda36853c9 LibC: Implemented mkstemp in stdlib
Implemented mkstemp method in stdlib.
2019-11-16 12:50:50 +01:00
Emanuel Sprung
e7affa24dc LibC, LibM: Add functions needed to compile python3 2019-11-11 22:04:16 +01:00
Andreas Kling
4f27745136 LibC: Implement a very naive mbtowc()
This just copies the short char into the wide char without any decoding
whatsoever. A proper implementation should look at the current LC_CTYPE
and implement multi-byte decoding.
2019-11-10 13:46:52 +01:00
Jesse Buhagiar
70fb92fa0e LibC: Implemented atof()
`atof()` has now been implemented as part of the standard C library.
It supports scientific notation such as `1.2e-3` etc, ala the version
found as part of `glibc`.

It's a bit chunky, so there's probably room for optimisations here
and there, however, for now it works as intended (and allows Quake
to run).
2019-11-04 12:34:46 +01:00
Calvin Buckley
5050f7b5ee Kernel: Use word-sized entropy as much as possible in syscall 2019-10-13 18:03:21 +02:00
Calvin Buckley
aa42f56210 LibC: add arc4random* using new getrandom syscall
Serenity is really not production ready; I shouldn't have to warn
you not to trust the RNG here. This is for compatibility with
software expecting the interface.

arc4random does expose an annoying flaw with the syscall I want
to discuss with Kling though.
2019-10-13 18:03:21 +02:00
Calvin Buckley
ef40ebbe6d LibC: Add some wchar functions
These are basically copy and pasted from the regular string version.
Also add some more multi-byte/wide conversion stub.

libarchive wanted these. There's a lot more, but we can add them
one at a time.
2019-10-13 08:44:47 +02:00
Sergey Bugaev
89bf38864a LibC: Misc additions 2019-10-03 08:18:05 +02:00
Andreas Kling
0b59c0d0dc LibC: Make div() and ldiv() behave according to the C standard 2019-09-27 10:15:42 +02:00
Andreas Kling
b009f8522c LibC: Make system() behave according to POSIX
- system(nullptr) returns non-zero to indicate the presence of a shell
- Failure to fork() returns -1
- Failure to exec() in the child returns 127
2019-09-27 10:15:42 +02:00
Mauri de Souza Nunes
2d24b12a34 LibC: Implement mkdtemp library function 2019-09-14 12:05:50 +02:00
Andreas Kling
73fdbba59c AK: Rename <AK/AKString.h> to <AK/String.h>
This was a workaround to be able to build on case-insensitive file
systems where it might get confused about <string.h> vs <String.h>.

Let's just not support building that way, so String.h can have an
objectively nicer name. :^)
2019-09-06 15:36:54 +02:00
Andreas Kling
d3a8fe70a2 LibC: Fix strtol() handling of invalid characters
Rewrite this function to go from left-to-right instead of right-to-left
since this allows us to accumulate valid characters before encountering
any invalid ones.

This fixes parsing of strings emitted by GCC, like "1, %0|%0, %1}". :^)
2019-08-24 12:37:58 +02:00
Andreas Kling
266b9cb654 LibC: Fix strtol() not populating `endptr' for valid strings
We were not writing anything out to the `endptr` pointer if a number
was successfully parsed from the input string.

Fixes #460.
2019-08-17 22:07:48 +02:00
Andreas Kling
04b9dc2d30 Libraries: Create top level directory for libraries.
Things were getting a little crowded in the project root, so this patch
moves the Lib*/ directories into Libraries/.
2019-07-04 16:16:50 +02:00
Renamed from LibC/stdlib.cpp (Browse further)