Commit graph

357 commits

Author SHA1 Message Date
Andreas Kling
bded472ec4 Revert "AK+LibC: Move non-placement new/delete into LibC"
This reverts commit 2c82347393.
2020-05-20 16:24:26 +02:00
Andreas Kling
ef776c1e68 Revert "LibC: Implement Itanium C++ ABI for static variable guards"
This reverts commit cdbbe14062.
2020-05-20 16:24:26 +02:00
Andreas Kling
250c3b363d Revert "Build: Include headers from LibC, LibM, and LibPthread with -isystem"
This reverts commit c1eb744ff0.
2020-05-20 16:24:26 +02:00
Sergey Bugaev
36dcbce161 LibC: Claim some copyright for stdio
I've written a large part of the new stdio, so I'm (partly) to blame for it now.
2020-05-20 14:11:13 +02:00
Sergey Bugaev
776275a747 LibC: Handle fgets(size = 0)
I accidentally broke this in the recent rewrite. This reinstantiates the
behavior implemented in 6571468525.
2020-05-20 14:11:13 +02:00
Andrew Kaster
c1eb744ff0 Build: Include headers from LibC, LibM, and LibPthread with -isystem
Make sure that userspace is always referencing "system" headers in a way
that would build on target :). This means removing the explicit
include_directories of Libraries/LibC in favor of having it export its
headers as SYSTEM. Also remove a redundant include_directories of
Libraries in the 'serenity build' part of the build script. It's already
set at the top.

This causes issues for the Kernel, and for crt0.o. These special cases
are handled individually.
2020-05-20 08:37:50 +02:00
Andrew Kaster
cdbbe14062 LibC: Implement Itanium C++ ABI for static variable guards
This is __cxa_guard_acquire, __cxa_guard_release, and __cxa_guard_abort.

We put these symbols in a 'fake' libstdc++ to trick gcc into thinking it
has libstdc++. These symbols are necessary for C++ programs and not C
programs, so, seems file. There's no way to tell gcc that, for example,
the standard lib it should use is libc++ or libc. So, this is what we
have for now.

When threaded code enters a block that is trying to call the constructor
for a block-scope static, the compiler will emit calls to these methods
to handle the "call_once" nature of block-scope statics.

The compiler creates a 64-bit guard variable, which it checks the first
byte of to determine if the variable should be intialized or not.

If the compiler-generated code reads that byte as a 0, it will call
__cxa_guard_acquire to try and be the thread to call the constructor for
the static variable. If the first byte is 1, it will assume that the
variable's constructor was called, and go on to access it.

__cxa_guard_acquire uses one of the 7 implementation defined bytes of
the guard variable as an atomic 8 bit variable. To control a state
machine that lets each entering thread know if they gained
'initialization rights', someone is working on the varaible, someone is
working on the varaible and there's at least one thread waiting for it
to be intialized, or if the variable was initialized and it's time to
access it. We only store a 1 to the byte the compiler looks at in
__cxa_guard_release, and use a futex to handle waiting.
2020-05-20 08:37:50 +02:00
Andrew Kaster
2c82347393 AK+LibC: Move non-placement new/delete into LibC
This allows operator new and operator delete to be available to anyone
that links -lc (everyone) rather than just people that include
kmalloc.h (almost no one).
2020-05-20 08:37:50 +02:00
Sergey Bugaev
db30a2549e LibC: Rewrite stdio
The new version uses buffering much more prominently, and hopefully performs
better. It also uses something resembling C++ rather than plain C.
2020-05-20 08:31:31 +02:00
Sergey Bugaev
7541122206 Kernel+LibC: Switch isatty() to use a fcntl()
We would want it to work with only stdio pledged.
2020-05-20 08:31:31 +02:00
Andreas Kling
b3f6b43d3c LibC: Don't let ctype isfoo() helpers access array out of bounds 2020-05-17 22:35:25 +02:00
AnotherTest
8582a06899 Kernel + LibC: Handle running processes in do_waitid() 2020-05-17 11:58:08 +02:00
Andreas Kling
21d5f4ada1 Kernel: Absorb LibBareMetal back into the kernel
This was supposed to be the foundation for some kind of pre-kernel
environment, but nobody is working on it right now, so let's move
everything back into the kernel and remove all the confusion.
2020-05-16 12:00:04 +02:00
Andreas Kling
204fb27333 Kernel: Remove now-unused KernelInfoPage.h 2020-05-16 11:34:54 +02:00
Andreas Kling
2dc051c866 Kernel: Remove sys$getdtablesize()
I'm not sure why this was a syscall. If we need this we can add it in
LibC as a wrapper around sysconf(_SC_OPEN_MAX).
2020-05-16 11:34:01 +02:00
Andreas Kling
3a92d0828d Kernel: Remove the "kernel info page" used for fast gettimeofday()
We stopped using gettimeofday() in Core::EventLoop a while back,
in favor of clock_gettime() for monotonic time.

Maintaining an optimization for a syscall we're not using doesn't make
a lot of sense, so let's go back to the old-style sys$gettimeofday().
2020-05-16 11:33:59 +02:00
Sergey Bugaev
888329233b LibC: Fix execvp() errno
Of course, using dbg() in the middle will change errno (most likely, reset
it to zero).
2020-05-15 11:43:58 +02:00
Sergey Bugaev
450a2a0f9c Build: Switch to CMake :^)
Closes https://github.com/SerenityOS/serenity/issues/2080
2020-05-14 20:15:18 +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
Yonatan Goldschmidt
b03fefd9f8 LibC: Add minimal <netinet/ip.h>
It's based on <netinet/in.h> and more definitions might be required
here (e.g, IP header).
2020-05-11 09:50:42 +02:00
Yonatan Goldschmidt
d3bb6cf5a9 LibC: Add missing <sys/time.h> include in <utmp.h>
This file uses 'struct timeval' without including relevant header.
2020-05-11 09:50:42 +02:00
Yonatan Goldschmidt
6c4fe69899 LibC: Add inet_aton, based on inet_pton 2020-05-11 09:50:42 +02:00
Yonatan Goldschmidt
107c83bd42 Kernel+LibC: Add AF_MAX
Will be updated as we add more protocols (e.g AF_INET6)
2020-05-11 09:50:42 +02:00
Yonatan Goldschmidt
6571468525 LibC: Return nullptr in fgets for size=0
I had this assert trigger, I believe in a legitimate case.
This is the behavior glic and musl follow.
2020-05-11 09:50:42 +02:00
Yonatan Goldschmidt
2ac3d33c63 LibC: Fix get{sock,peer}name to match their kernel-side prototypes
In f4302b58fb, the kernel-side syscalls (e.g Process::sys$getsockname)
were updated to use SC_get{sock,peer}name_params, but the libc
functions were not updated.
2020-05-10 01:04:10 +02:00
Linus Groh
358694567f Meta: Add script to enforce license headers & run it on Travis 2020-05-09 23:55:58 +02:00
Itamar
dca0483e9a LibC: Log calls to getrusage 2020-05-07 23:32:11 +02:00
Itamar
2cafc36d9c LibC: Remove ASSERT_NOT_REACHED in getrusage
The gcc port was hitting that assertion for some reason.

This patch fixes it.
2020-05-05 11:01:36 +02:00
Ed Rochenski
861eb8d295 LibC: added F_SETLK and SO_TYPE defs 2020-05-02 14:12:07 +02:00
Andreas Kling
888e35f0fe AK: Add ALWAYS_INLINE, NEVER_INLINE and FLATTEN macros
It's tedious to write (and look at) [[gnu::always_inline]] etc. :^)
2020-04-30 11:43:25 +02:00
Sergey Bugaev
1b36ddce1d LibC: Hint the compiler that assertions rarely fail
Also, rewrite the macro to expand to an if statement instead of
a weird ternary operator with a (void)0 banch.
2020-04-30 11:30:27 +02:00
Andreas Kling
385dacce05 Kernel: Fix integer overflow in framebuffer resolution handling
This made it possible to map the E1000 MMIO range into userspace and
mess with the registers.

Thanks to @grigoritchy for finding this!

Fixes #2015.
2020-04-29 09:37:36 +02:00
Andreas Kling
50c1eca9d4 AK: Add a simple and inefficient Base64 decoder 2020-04-26 22:57:00 +02:00
Luke Payne
f191b84b50 Kernel: Added the ability to set the hostname via new syscall
Userland/hostname: Now takes parameter to set the hostname
LibC/unistd: Added sethostname function
2020-04-26 12:59:09 +02:00
Read H
ee5816b9c8 LibC: getprotoent() family of functions
This commit implements the getprotoent() family of functions, including

getprotoent()
getprotobyname()
getprotobynumber()
setprotoent()
endprotoent()

This implementation is very similar to the getservent family of functions,
which is what led to the discovery of a bug in the process of reading the aliases.
The ByteBuffer for the alias strings didn't include a null terminating character,
this was fixed for both the protoent and servent family of functions by appending a
null character to the end of them before adding them to the alias lists.
2020-04-18 10:11:55 +02:00
Read H
1f4e3dd073 LibC netdb: Requested Changes
fix all requested changes including:

- remove explicit vector initialization
- change keep_service_file_open to boolean
- closing service file on seek error
- change C level char allocation to use ByteBuffer instead
- simplified getservby* loops to a single loop
- change fill_getserv_buffers return to early-return style
2020-04-16 15:55:45 +02:00
Read H
844e5faf84 LibC: Service entry API
This commit implements the getservent family of functions:
getservent()
setservent()
endservent()
getservbyname()
getservbyport()

for accessing the /etc/services file.

This commit has addressed the issues identified by utilizing more C++ structures,
addressing unchecked error possibilities, strncpy bounds checking, and other issues
found in the initial pull request
2020-04-16 15:55:45 +02:00
Andreas Kling
d1ffdea550 LibC: Fix truncated strncpy() in getlogin() 2020-04-13 12:27:05 +02:00
Andreas Kling
dda9ffe906 LibC: Fix truncated strncpy() in /etc/group parsing 2020-04-13 12:27:05 +02:00
Andreas Kling
e3df925e48 LibC: Fix strncpy() overflow in /etc/passwd parsing 2020-04-13 12:27:05 +02:00
Andreas Kling
c1607dc41f LibC: Fix strncpy() overflow in gethostbyname() 2020-04-13 12:27:05 +02:00
Andreas Kling
038fdc2017 LibC: Simplify ASSERT() to reduce code size
Instead of pushing the message, file name, line# and function name
separately, we now mash the message, file name and line# into a string
constant and pass that.

This means that the failure path only has to push a single address onto
the stack, reducing the code size and causing the compiler to inline
many more functions containing an assertions (e.g RefPtr::operator*())

Obviously if you wanted minimal size, you could turn assertions off
entirely, but I really like running with assertions, so let's make
a little effort to reduce their impact. :^)
2020-04-13 12:27:05 +02:00
Andreas Kling
a0592912c3 LibC: Simplify the gettid() cache by just clearing the cache in fork()
It's hilarious how much better this is.

Thanks to Sergey for suggesting it! :^)
2020-04-13 10:27:14 +02:00
Itamar
50fd2cabff ptrace: Report error in PT_PEEK via errno
The syscall wrapper for ptrace needs to return the peeked value when
using  PT_PEEK.
Because of this, the user has to check errno to detect an error in
PT_PEEK.

This commit changes the actual syscall's interface (only for PT_PEEK) to
allow the syscall wrapper to detect an error and change errno.
2020-04-13 00:53:22 +02:00
Itamar
9e51e295cf ptrace: Add PT_SETREGS
PT_SETTREGS sets the regsiters of the traced thread. It can only be
used when the tracee is stopped.

Also, refactor ptrace.
The implementation was getting long and cluttered the alraedy large
Process.cpp file.

This commit moves the bulk of the implementation to Kernel/Ptrace.cpp,
and factors out peek & poke to separate methods of the Process class.
2020-04-13 00:53:22 +02:00
Itamar
b306ac9b2b ptrace: Add PT_POKE
PT_POKE writes a single word to the tracee's address space.

Some caveats:
- If the user requests to write to an address in a read-only region, we
temporarily change the page's protections to allow it.

- If the user requests to write to a region that's backed by a
SharedInodeVMObject, we replace the vmobject with a PrivateIndoeVMObject.
2020-04-13 00:53:22 +02:00
Itamar
984ff93406 ptrace: Add PT_PEEK
PT_PEEK reads a single word from the tracee's address space and returns
it to the tracer.
2020-04-13 00:53:22 +02:00