Commit graph

48 commits

Author SHA1 Message Date
Andrew Kelley
017228de89 libc malloc: introduce a canary
Instead of padding, use static entropy to detect corrupted header.

* 64-bit, safe modes: 10 canary bits
* 64-bit, unsafe modes: 0 canary bits
* 32-bit: 27 canary bits

A further enhancement, not done here, would be to upgrade from canary to
parity.
2026-02-14 09:25:41 +01:00
kj4tmp@gmail.com
3dc2a1f9ac zig libc: acosf 2026-02-13 11:30:14 -08:00
kineye
f36d0573cd zig libc: add rint 2026-02-12 21:42:38 -08:00
Neel
5132d78e83 libc: implement insque and remque in Zig
Implements insque() and remque() doubly-linked list functions
in lib/c/search.zig and removes musl C implementation.
Contributes to #30978
2026-02-12 21:35:36 -08:00
Andrew Kelley
6d52678a6c zig libc malloc: skip export when unit testing
These functions can only be exported when external libc components are
available due to the errno location dependency. Note that even when zig
libc is complete, on Windows, errno location will always be external (in
ucrtbase.dll).
2026-02-12 13:14:51 -08:00
Andrew Kelley
ec02571a30 zig libc malloc: set errno when returning null 2026-02-12 13:14:51 -08:00
Andrew Kelley
6ccabbd4e5 std: brk allocator for single-threaded mode 2026-02-12 13:14:51 -08:00
Andrew Kelley
6744160211 zig libc: implement malloc 2026-02-12 13:14:51 -08:00
Andrew Kelley
b600b6e5e0 std.posix: remove close function 2026-02-11 23:37:31 +01:00
Pivok
d4217e2119 zig libc: exp10, exp10f, pow10, pow10f (#31163)
See #30978.

Commands i run:
```
$ stage3/bin/zig build -p stage4 -Denable-llvm -Dno-lib

$ stage4/bin/zig build test-libc -Dlibc-test-path=../../libc-test -Dtest-filter=exp10 -fqemu -fwasmtime --summary line
Build Summary: 725/737 steps succeeded (12 skipped)
```

Reviewed-on: https://codeberg.org/ziglang/zig/pulls/31163
Reviewed-by: Andrew Kelley <andrew@ziglang.org>
Co-authored-by: Pivok <pivoc@protonmail.com>
Co-committed-by: Pivok <pivoc@protonmail.com>
2026-02-11 19:39:40 +01:00
Andrew Kelley
96bd268c8c zig libc: simplify implementation
- use symbol export helper
- move all declarations from common.zig into c.zig
- correct documentation
- delete dead code
2026-02-11 07:58:11 +01:00
Pivok
f1b2554023 libzigc: hypotf, hypotl (#31150)
Implements hypotf and hypotl for libzigc #30978.

Continuation of #31104

Commands i run:

```
$ stage3/bin/zig build -p stage4 -Denable-llvm -Dno-lib

$ stage4/bin/zig build test-libc -Dlibc-test-path=../../libc-test -Dtest-filter=hypotf -fqemu -fwasmtime --summary line
Build Summary: 365/369 steps succeeded (4 skipped)

$ stage4/bin/zig build test-modules -Dtest-target-filter=windows -Dtest-filter=hypotf --summary line
Build Summary: 53/101 steps succeeded (48 skipped)
```

Reviewed-on: https://codeberg.org/ziglang/zig/pulls/31150
Reviewed-by: Andrew Kelley <andrew@ziglang.org>
Co-authored-by: Pivok <pivoc@protonmail.com>
Co-committed-by: Pivok <pivoc@protonmail.com>
2026-02-09 07:59:40 +01:00
Pivok
d0b39c7f2b libzigc: hypot (#31104)
First time contribution.

Implements hypot for libzigc #30978.

Commands i run:
```
$ stage3/bin/zig build -p stage4 -Denable-llvm -Dno-lib

$ stage4/bin/zig build test-libc -Dlibc-test-path=../../libc-test -Dtest-filter=hypot --summary line -fqemu -fwasmtime
Build Summary: 725/737 steps succeeded (12 skipped)
```

I also changed std.math.hypot becuase some libc-tests raised fp exceptions. Example:
```
../../libc-test/src/math/special/hypot.h:8: bad fp exception: RN hypot(0x1p-1074,0x0p+0)=0x1p-1074, want 0 got INEXACT|UNDERFLOW
../../libc-test/src/math/special/hypot.h:9: bad fp exception: RN hypot(0x1p-1074,-0x0p+0)=0x1p-1074, want 0 got INEXACT|UNDERFLOW
```

I also run this command as a quick sanity check:
```
$ stage4/bin/zig build test-std -Dtest-filter=hypot -Dtest-target-filter=x86_64-linux-musl --summary line
Build Summary: 5/5 steps succeeded; 136/136 tests passed
```

Reviewed-on: https://codeberg.org/ziglang/zig/pulls/31104
Reviewed-by: Andrew Kelley <andrew@ziglang.org>
Co-authored-by: Pivok <pivoc@protonmail.com>
Co-committed-by: Pivok <pivoc@protonmail.com>
2026-02-05 21:57:32 +01:00
Ivel
fa3228ae42 libc: reimplement swab in Zig (#31130)
This PR replaces the bundled musl swab() implementation with zig's one.

Contributes towards #30978.

It looks like there are not test cases for swab() in test-libc.

Reviewed-on: https://codeberg.org/ziglang/zig/pulls/31130
Reviewed-by: Andrew Kelley <andrew@ziglang.org>
Co-authored-by: Ivel <ivel.santos@proton.me>
Co-committed-by: Ivel <ivel.santos@proton.me>
2026-02-05 20:21:41 +01:00
Ivel
7aae7dd3f4 libzigc: pow 2026-02-03 20:14:22 +01:00
Jeff Anderson
4aadb5e4a5 libzigc: cbrtf 2026-02-01 00:09:54 -08:00
Jeff Anderson
0aae9768aa libzigc: cbrt 2026-02-01 00:09:45 -08:00
Andrew Kelley
380ea6fb5e Merge pull request 'Compilation: make libzigc share zcu if possible' (#31037) from GasInfinity/zig:libc-share-zcu into master
Reviewed-on: https://codeberg.org/ziglang/zig/pulls/31037
2026-01-30 08:16:39 +01:00
lzm-build
ccd82ae7cc Add f16, f80 and f128 support for atan 2026-01-30 06:20:14 +01:00
GasInfinity
ed93f0d70f
fix(libzigc): always apply strong linkage, even when testing
* libzigc may be linked into a different test compilation

Co-authored-by: Matthew Lugg <mlugg@mlugg.co.uk>
2026-01-29 10:39:38 +01:00
GasInfinity
73ed351022 fix(libzigc): export mincore 2026-01-27 00:06:18 +01:00
GasInfinity
9cf34a8d81
feat(libzigc): move over some linux syscalls
* does not move all of them, only those which map almost 1:1
* also removes their musl implementation
2026-01-24 20:41:15 +01:00
Andrew Kelley
a2ea36a517 zig libc: modify errno helper to eliminate @intCast
The vast majority of libc functions return `c_int` for the return value,
when setting errno. This utility function is for those cases.

Other cases can hand-roll the logic, or additional helpers can be added.
2026-01-24 20:18:18 +01:00
GasInfinity
1ab6bf59a6
feat(libzigc): add common linux errno syscall helper 2026-01-21 12:02:08 +01:00
GasInfinity
4f652fb4e3
feat(libzigc): use common implementations for sys/utsname.h
* and remove their musl/wasi implementation
2026-01-21 00:06:42 +01:00
Daggerfall-is-the-best-TES-game
21fc85f035 Add acos to ziglibc (#30888)
Since this is my first time contributing I wanted to keep it simple and I added just one function
I tested with
```bash
stage3/bin/zig build -p stage4 -Dno-lib -Dno-langref -Denable-llvm=true --search-prefix ~/repos/zig-boostrap/out/native-linux-musl-baseline/
stage4/bin/zig build test-libc -Dlibc-test-path=../libc-test -Dtest-target-filter=x86_64-linux-musl
```
and the tests passed.
I'm planning on doing more once I get the hang of it.

Co-authored-by: david <davidc.fried@gmail.com>
Reviewed-on: https://codeberg.org/ziglang/zig/pulls/30888
Reviewed-by: Andrew Kelley <andrew@ziglang.org>
Co-authored-by: Daggerfall-is-the-best-TES-game <daggerfall-is-the-best-tes-game@noreply.codeberg.org>
Co-committed-by: Daggerfall-is-the-best-TES-game <daggerfall-is-the-best-tes-game@noreply.codeberg.org>
2026-01-19 22:43:57 +01:00
GasInfinity
f0649dd978
feat(libzigc): use common implementations for strings.h and string.h
* forwards all functions to their equivalent `std`
* removes their musl, wasi-libc and mingw implementations
2026-01-17 22:37:29 +01:00
GasInfinity
06d05decae
feat(libzigc): add wcstok and reuse more std.mem code
* also makes `wcpncpy`/`wcsncpy` pad the buffer with zeroes as required
  by the standard
2026-01-17 21:42:40 +01:00
GasInfinity
e3309aaf4f
fix(libzigc): workaround LLVM bug
* looks like we get stuck inside this loop? 2078da43e2/llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp (L245-L298)
2026-01-16 01:58:35 +01:00
GasInfinity
e22bf480aa
feat(libzigc): use common implementation for wchar.h
* but does not fully implement it (missing locale/unicode tables)
* also removes their musl/mingw implementation
2026-01-15 18:01:20 +01:00
GasInfinity
4df6119335 feat(libzigc): avoid division/multiplication in drand48 and erand48 2026-01-14 23:48:07 +01:00
GasInfinity
e10a325410
feat(libzigc): implement the drand48 family
* also remove their musl implementation
2026-01-14 13:14:33 +01:00
GasInfinity
ad3828db45
feat(libzigc): use common rand implementations
* also remove their musl implementation
2026-01-14 12:47:58 +01:00
GasInfinity
65092d6e4c fix(libzigc): properly handle EOF in ctype
* EOF is defined as a negative integer, thus it doesn't fit in an `u8`!
2026-01-13 05:58:41 +01:00
GasInfinity
8e69ab8a31 fix(libzigc): negative bases are also invalid 2026-01-13 05:58:13 +01:00
GasInfinity
514f6e589c feat(libzigc): use common integer ato* and strto* implementations
* also removes their musl implementation
2026-01-12 00:55:11 +01:00
rpkak
813ae89208
libc -> libzigc: copysign 2026-01-10 00:09:54 +01:00
GasInfinity
6abb1dcd35
feat(libzigc): add common ctype implementation
* implements all functions in the standard `ctype.h` header
* also removes their musl implementations
2026-01-09 02:51:29 +01:00
GasInfinity
7f6eab2704 feat(libzigc): add nan, nanf, nanl and bsearch
* also remove musl implementation
2026-01-09 00:26:20 +01:00
GasInfinity
335c0fcba1 feat(libzigc): add div, ldiv, lldiv and imaxdiv
* also remove musl implementation
2026-01-07 21:53:40 +01:00
GasInfinity
fa625e878f feat(libzigc): add qsort
* also remove musl implementation
2026-01-07 00:06:57 +01:00
Alex Rønne Petersen
e9ac2ce116
compiler-rt: move strlen from libzigc to here
LLVM 21 has started recognizing strlen-like idioms and optimizing them to strlen
calls, so we need this function provided in compiler-rt for libc-less
compilations.
2025-08-30 06:36:41 +02:00
David Senoner
9399fcddce libc: use zig isnan and derivates for mingw 2025-08-27 03:58:32 +02:00
David
55848363fd
libc: implement common abs for various integer sizes (#23893)
* libc: implement common `abs` for various integer sizes

* libc: move imaxabs to inttypes.zig and don't use cInclude

* libc: delete `fabs` c implementations because already implemented in compiler_rt

* libc: export functions depending on the target libc

Previously all the functions that were exported were handled equally,
though some may exist and some not inside the same file. Moving the
checks inside the file allows handling different functions differently

* remove empty ifs in inttypes

Co-authored-by: Alex Rønne Petersen <alex@alexrp.com>

* remove empty ifs in stdlib

Co-authored-by: Alex Rønne Petersen <alex@alexrp.com>

* libc: use `@abs` for the absolute value calculation

---------

Co-authored-by: Alex Rønne Petersen <alex@alexrp.com>
2025-05-21 00:57:38 +02:00
Alex Rønne Petersen
092352ec63
compiler-rt, zigc: Use hidden visibility except when testing.
This prevents symbols from these libraries from polluting the dynamic symbol
tables of binaries built with Zig. The downside is that we no longer deduplicate
the symbols at run time due to weak linkage.

Closes #7935.
Closes #13303.
Closes #19342.
2025-05-19 00:22:03 +02:00
Bryson Miller
08d534e8d8
Introduce common strcasecmp and strncasecmp implementations (#23840) 2025-05-15 10:58:33 +02:00
David
2c241b263c
Introduce common bzero libc implementation. (#23812)
* Introduce common `bzero` libc implementation.

* Update test name according to review

Co-authored-by: Linus Groh <mail@linusgroh.de>

* address code review

- import common implementation when musl or wasi is included
- don't use `c_builtins`, use `@memset`

* bzero calling conv to .c

* Apply review

Co-authored-by: Veikka Tuominen <git@vexu.eu>

---------

Co-authored-by: Linus Groh <mail@linusgroh.de>
Co-authored-by: Veikka Tuominen <git@vexu.eu>
2025-05-10 10:37:21 +02:00
Alex Rønne Petersen
1f896c1bf8
Introduce libzigc for libc function implementations in Zig.
This lays the groundwork for #2879. This library will be built and linked when a
static libc is going to be linked into the compilation. Currently, that means
musl, wasi-libc, and MinGW-w64. As a demonstration, this commit removes the musl
C code for a few string functions and implements them in libzigc. This means
that those libzigc functions are now load-bearing for musl and wasi-libc.

Note that if a function has an implementation in compiler-rt already, libzigc
should not implement it. Instead, as we recently did for memcpy/memmove, we
should delete the libc copy and rely on the compiler-rt implementation.

I repurposed the existing "universal libc" code to do this. That code hadn't
seen development beyond basic string functions in years, and was only usable-ish
on freestanding. I think that if we want to seriously pursue the idea of Zig
providing a freestanding libc, we should do so only after defining clear goals
(and non-goals) for it. See also #22240 for a similar case.
2025-04-11 17:12:31 +02:00