Commit Graph

122 Commits

Author SHA1 Message Date
Travis Geiselbrecht
2377c3d440 [make] fix three misuses of TOBOOL
Forgot to expand the variable in the call to the TOBOOL function which
would cause the result to always be true. In this case always resulted
in the test code for these three modules to be included.
2025-09-03 15:14:18 -07:00
Matt Schulte
e51cf68c3c [lib][libc] Make errno thread safe using TLS APIs
Before this change, errno was "completely un-threadsafe" as the comment
states.

This changes errno to be threadsafe by making errno a thread local
variable.
2025-07-10 15:37:43 -07:00
Travis Geiselbrecht
73d29b0860 [lib][libc] don't compile the floating point tests if there's no FPU 2025-06-10 23:57:05 -07:00
Travis Geiselbrecht
2865ab6813 [lib][libc][tests] switch floating point test vector to a constructor
This should avoid a problem with older compilers that do not support non
trivial designated initializers.
2025-06-10 23:37:51 -07:00
Travis Geiselbrecht
03bc415b2b [lib][libc] a few little style tweaks in the printf code
No functional change.
2025-06-10 23:10:45 -07:00
Travis Geiselbrecht
c8f011a159 [lib][libc][tests] move the floating point printf test to libc unittests
Complete moving the rest of the previously manual printf tests out of
app/tests and into lib/libc/test as a proper unit test.
2025-06-10 23:10:45 -07:00
Travis Geiselbrecht
433b02ce10 [lib][libc] move all the printf routines into printf.c
Some of the wrapper routines (printf, fprintf, etc) were defined in
stdio.c which is not necessarily compiled with the same compiler flags
concerning floating point support.

On some architectures (arm64, x86-64) this caused the wrapper routines
to clobber the floating point registers prior to getting into the
printf_engine.
2025-06-10 23:10:45 -07:00
Heiko Behrens
dd8210d957 atol handles overflow and accepts leading whitespace and '+' 2025-04-10 21:41:06 -07:00
Travis Geiselbrecht
6d9a0b5c35 [libc][printf] Handle case where snprintf underflows for len = 0
The code would write a null pointer always, even if len = 0, or if the
buffer pointer is null.

Test for this condition and add a unit test.

fixes #407
2025-01-09 23:32:56 -08:00
Travis Geiselbrecht
6a3db09e55 [compiler] GCC 14.1 supports __has_feature
Enable it if not present, not just if its clang.
2024-05-09 18:54:38 -07:00
Travis Geiselbrecht
4401560dd9 [libc][printf] pull in fix from fuchsia that handles 0x prefixes properly
With this change printf passes the fuchsia unit tests, except the lack
of field with and precision support. Disabled those parts of the test
for now.

Original change at
https://fuchsia.googlesource.com/fuchsia/+/db61af0d1fc27c4c807f5da7d0553a4bb422882b

The old code used to pad the output with zeros before the 0x,
so you would end up with a 0000000x10. This is not the intended
behavior.

Original author: pedro.falcato@gmail.com
2024-04-24 00:43:42 -07:00
Travis Geiselbrecht
45155fdbf9 [libc][tests] pull over printf unit tests from fuchsia
The fuchsia printf unit tests are a derivative of the current LK ones in
app/tests, but have been somewhat expanded. Pull in the new copy and add
it to the new libc test module.
2024-04-24 00:01:50 -07:00
Travis Geiselbrecht
253bb8225c [libc] have fputs return the number of bytes written
Not strictly according to spec but it matches the way the unit test was
written.
2024-04-23 23:50:25 -07:00
Travis Geiselbrecht
f92aae9dcf [lib][libc] rework a bit of the stdio code to handle errors more closely to spec
Some of the recent refactoring may cause some of the inner error codes
to be eaten or misinterpreted. Restructure fread/fwrite to handle these
a bit more gracefully.

There's some careful use with ssize_t vs size_t here to try to deal with
negatves values properly.
2024-04-18 23:29:17 -07:00
Chieh-Min Wang
b651168fa1 [lib][stdio] wrap file io APIs with stdio APIs
Implement stdio file io APIs with LK lib/fs APIs

Signed-off-by: Chieh-Min Wang <cmwang@google.com>
2024-04-17 23:59:49 -07:00
Travis Geiselbrecht
8176ae67c8 [libc][cdefs] add a few additional features in cdefs
This seems to only be needed for older gccs (found with 7.5.0).
2024-02-27 00:46:56 -08:00
Travis Geiselbrecht
25ff64d4fd [libc] move sys/cdefs.h out of libm and into the main include path
More code is using it, so try to build a more standardized version of
it. For now, since most of the defines that are needed are similar to
lk/compiler.h, mostly reimplement in terms of those.
2024-02-27 00:12:15 -08:00
Cody Wong
94a15119b2 [libc][string] fix strncpy potential buffer overflow
The wrong placement of the increment for index `i` causes an unexpected
behavior, which the `strncpy` writes an extra '\0'.

For example:
The `src` string is "abc". The buffer size of `dest` is 5.

When we call `strncpy(dest, src, 5)`, the first `for` loop copies the
characters, 'a', 'b', and 'c', to the `dest[0:2]`. In the 4th iteration,
however, the `for` loop breaks due to the termination of `src` whereas
the value of `i` stays 3. At the moment, it has copied 4 bytes,
including the '\0' of `src`.

In the second `for` loop, we have `i = 3` and `count = 5`, so the loop
copies two more '\0' to the `dest`. As a result, the `strncpy` copies 6
bytes to the `dest` buffer, leading to buffer overflow.

Fix the issue by increasing the index `i` before every copy.

Signed-off-by: Cody Wong <codycswong@google.com>
2023-11-04 13:16:24 -07:00
Aaron Odell
9ba1f165cd [libc][string] Add strcasecmp support
Add strcasecmp() for case insensitive string compares. There is no ISO
standard function for this purpose but strcasecmp() is POSIX standard.
2023-04-23 17:34:54 -07:00
Alex Richardson
7c7612225a [libc] Fix -Wincompatible-library-redeclaration for strerror
Return a char * instead of a const char * to silence this Clang warning.
2023-04-23 17:23:31 -07:00
Travis Geiselbrecht
2367686854 [make] add a way for a module to opt into additional warnings
Move -Wmissing-declarations and -Wmissing-prototypes into this bucket.
Opt in most of the core top level modules in the system. More to follow.
2022-10-23 23:16:48 -07:00
Travis Geiselbrecht
4ce2e6a35d [libc] fix a warning squelch for some locally defined routines 2022-10-23 23:03:17 -07:00
Michael Bishop
368b5f69b8 [lib][libc] add missing BE64SWAP 2022-09-25 18:16:06 -07:00
Travis Geiselbrecht
baca46e133 [lib][libc] fix an incorrect include path in a libc file 2022-09-25 18:03:59 -07:00
mni
034a8d0ede [libc] add atof() function 2022-09-06 10:26:41 -07:00
Travis Geiselbrecht
6462cbf51c [arch][fpu] add ability to specify per file or module if code needs fpu
Have the arch define additional compiler flags to explicit support or
not support a floating point unit.

Add ability for modules to per file or for the whole module mark code
as needing floating point support.

Add default flags for arm64, riscv, and x86 toolchains.

Needed because gcc 12 is getting much more aggressive about using vector
instructions for non float code, so getting away with avoiding it was
no longer working.

Still not perfect: printf code is being compiled with float, so it's
possible to use floating point instructions inside core kernel or
interrupt handling code if a printf is used.

Possibly will have problems on architectures where mixing float and non
float code at the linker generates issues, but so far seems to be okay.
2022-07-17 16:32:24 -07:00
Travis Geiselbrecht
2bb32fe813 [libc][assert] add __PRINTFLIKE to assert_fail_msg()
Fix a warning as a result of this.

fixes issue #302
2021-11-12 22:07:14 -08:00
Travis Geiselbrecht
5b1041748e [libc] flesh out inttypes.h a bit more 2021-11-12 20:44:47 -08:00
Travis Geiselbrecht
1550939102 [libc][inittypes] add entry for PRIxPTR 2021-11-11 00:03:39 -08:00
Travis Geiselbrecht
fcb65c9a88 [dev/lib][warnings] fix -Wmissing-declarations warnings in dev/ and lib/ 2021-10-21 23:16:20 -07:00
Travis Geiselbrecht
08aeb5a6ad [libc] fix the extension routine that adds entropy to the random pool
Worked for short entropy but actually would end up zeroing out the new
entropy word.
2021-05-26 02:15:59 -07:00
Travis Geiselbrecht
9a796e868a [assert] update the assert macros
Pull some assert macro improvements back from zircon.

Added new variants that let you pass an arbitrary message.
Move most of the inner routine into a helper, makes for slightly
smaller codegen.
2021-04-02 19:28:58 -07:00
Travis Geiselbrecht
ebdc1ea077 [lib][libcpp] move c++ specific shims out of libc and heap
Consolidate into a single library. Also renames legacy new.h to
the more standard new.

Possible that some C++ code will need to get this added to their
MODULE_DEPS.
2021-04-02 19:28:58 -07:00
Travis Geiselbrecht
c5ef3165e4 [libc] add LLONG_* to limits.h 2021-04-02 19:28:58 -07:00
Michael Bishop
41f651268b [libc] allow arch string rules to be missing
[elf] add the constant for VC4
2021-04-01 21:34:29 -07:00
Brian Swetland
5434847c51 [libc] C style static_assert takes two arguments
Fix this up and fix up the one existening dependency on the
existing incorrect behaviour.
2021-02-06 20:30:34 -08:00
Piotr Tworek
a969cc9c99 [libc] Fix UINT16_MAX, INT16_MIN and INT16_MAX definitions.
The defines from limits.h have SHRT, not SHORT in their names.
2021-01-25 18:49:35 -08:00
Travis Geiselbrecht
917906e4a4 [libc] add abort() implementation
Does nothing but call panic(). The compiler has been seen emitting
this which is highly suspicious.
2020-10-21 02:34:50 -07:00
Travis Geiselbrecht
bf24db407e [endian] update the endian.h file to use builtins and tighten up a macro
Use compiler builtins for these and make the *E32SWAP routines a bit safer.
2020-10-10 01:08:02 -07:00
Travis Geiselbrecht
f7d8e2300c [warnings] add -Wshadow which helps detect local variables that override globals
Nothing particularly bad showed up but cleaned up a bit of code.
2020-07-25 16:49:25 -07:00
Travis Geiselbrecht
9d07949891 [lib][unittest] spiff up the underutilized unittest library
-Stop using 512 bytes of bss and use a proper printf output
routine if the user wants to override it.
-Add a test case for unittest itself to see what the failure printfs
look like and make sure the test registration is working.
2020-07-12 15:37:49 -07:00
Travis Geiselbrecht
213895c69a [libc] build libc with -fno-builtin
Can't remember precisely what this fixes to be honest, but I
remember there was some sort of recursion on one of the targets on
one of the versions of the compiler.
2020-07-11 14:55:09 -07:00
Travis Geiselbrecht
f34164580e [libc][printf] remove wrapper #define macro that calls inner _printf
Wasn't really useful and caused the compiler to not be able to optimize
printf -> puts replacements which is slightly nicer.
2020-04-30 01:02:19 -07:00
Travis Geiselbrecht
ad246760ff [c++] remove WITH_CPP_SUPPORT flag
Just always build with it enabled.
2019-07-13 17:41:25 -07:00
Travis Geiselbrecht
f880ff3615 [libc][c++] move __cxa_atexit into the eabi.c file to always be built
Even if C++ isn't active we still need it.
2019-07-13 17:10:06 -07:00
Travis Geiselbrecht
35a8d555a3 [include] move almost all of the remainder of top level includes into a subdir
Examples are include/platform.h -> platform/include/platform.h
include/target.h -> target/include/target.h

The old model generally considered these to be Always There includes,
but they're starting to stick out more and more so may as well actually
follow the model that most of the rest of the system follows.
2019-07-13 16:09:27 -07:00
Travis Geiselbrecht
3aecdda231 [includes] replace header guards with #pragma once 2019-07-13 15:46:16 -07:00
Travis Geiselbrecht
d10eb9a8f6 [libc] remove a few unused header files
strings.h and alloca.h are unused and a little funny anyway. Probably
were once used in an external library that's no longer in the tree.
2019-07-13 15:46:01 -07:00
Travis Geiselbrecht
cba9e47987 [license] replace the longer full MIT license with a shorter one
Used scripts/replacelic. Everything seems to build fine.
2019-07-05 17:22:23 -07:00
Travis Geiselbrecht
d8fa82cb91 [formatting] run everything through codestyle
Almost nothing changes here except moving braces to the same line as the
function declaration. Everything else is largely whitespace changes and
a few dangling files with tab indents.

See scripts/codestyle
2019-06-19 21:02:24 -07:00