Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

provide common implementations where applicable for libc functions #2879

Open
Tracked by #8
andrewrk opened this issue Jul 12, 2019 · 7 comments
Open
Tracked by #8

provide common implementations where applicable for libc functions #2879

andrewrk opened this issue Jul 12, 2019 · 7 comments
Labels
contributor friendly This issue is limited in scope and/or knowledge of Zig internals. enhancement Solving this issue will likely involve adding new logic or components to the codebase.
Milestone

Comments

@andrewrk
Copy link
Member

Some libc functions can obviously share a common implementation. For example, right now, musl, mingw, the zig standard library all have independent implementations of the math functions, such as sqrt.

Instead, the canonical implementation should be in Zig, and even when providing musl or mingw libcs, the zig code should be built and used to fulfill those dependencies.

Other examples of functions would be memcpy and strcmp.

Functions that would be tricky to have common implementations would be, e.g. fopen. But that's worth investigating as well.

@andrewrk andrewrk added the enhancement Solving this issue will likely involve adding new logic or components to the codebase. label Jul 12, 2019
@andrewrk andrewrk added this to the 0.6.0 milestone Jul 12, 2019
@andrewrk andrewrk modified the milestones: 0.6.0, 0.7.0 Feb 10, 2020
@andrewrk
Copy link
Member Author

We now ship libc++ as well and it offers even more opportunities for this.

@marcthe12
Copy link

I think we should focus on musl first especially stuff that does not rely on syscall. That should cover most targets easily as there are very few issues to to use does functions with ucrt for example.

@andrewrk andrewrk modified the milestones: 0.7.0, 0.8.0 Oct 14, 2020
@andrewrk andrewrk modified the milestones: 0.8.0, 0.9.0 Nov 6, 2020
@LewisGaul
Copy link
Contributor

Is there anything that needs doing here that would be somewhat 'newcomer friendly'? Looking for an opportunity to get involved in something a bit bigger than zig fmt fixes :)

@Mouvedia
Copy link

Mouvedia commented Apr 12, 2021

@LewisGaul add *BSD libc issues?
#2876 #2877 #2878

@nektro has a draft PR for FreeBSD: #8327

@andrewrk andrewrk modified the milestones: 0.9.0, 0.10.0 May 19, 2021
@andrewrk andrewrk added the contributor friendly This issue is limited in scope and/or knowledge of Zig internals. label May 20, 2021
@marcthe12
Copy link

I was looking at this for something similar recently. So I have some points so we can create a more concrete plan.

First thing, to what extend are we replacing the functions, are we only going to be doing only ones we have access to source or we can we do for one we only have headers or even no source at all? On nice thing if we can patch header is we can add functions not found in one libc but found in another one (annex k and complex numbers and C2x).

Unless we can do vardic functions in zig, we will not be able to stdio as printf family of function are vardic functions. Alot functions are simply wrapper here so we can share a lot of but it must implemented in c.

We should use the structure implemented in the libc and not reimplement it not to break the ABI. To us they should be an opaque structure. No need reimplement them.

Some stuff need state or a "runtime" like malloc and threads. Locale id the most annoying here. So unless we don't mind short circuiting locales, lets not touch those. We can still implementing supprising amount of functions though.

There are alot of function especially the math one which doesn't even need a libc itself. We really should start implementing this part first along with the wrappers in place like stdio. If done correctly we could even safety link some of these functions to any executable on any target.

@nektro
Copy link
Contributor

nektro commented Jan 13, 2022

is this with the intent to eventually remove some of the header files?

@matu3ba
Copy link
Contributor

matu3ba commented Mar 26, 2022

The "freestanding"/platform-independent bits are covered either by std.math or compiler-rt, see #7265.

What remains are smaller or bigger platform dependencies, which ideally have automatic abi tracking per platform or at least some test coverage for each supported platform.

Some of the shennanigans I did experience on trying to build a sane test system (it is not possible without user input for "non-standard platforms/freestanding"):

There are several things missing in the proposal to estimate the scope:

  1. Who is responsible to provide the correctly sized implementation with macro magic? Zig or the user? There is alot implementation defined behavior, ie char can be signed or unsigned.
  2. What is the scope of bending backwards backwards compatibility with all the macro shennanigans for usage?
  3. Which compilers are the target audience?

Technical side questions:

  1. How are abi sizes defined in clang and/or is there a query system in clang/other compilers we can use for all possible targets?
  2. How should sizes be communicated to the user for usability/no silent breakings?
    2.1 One could either replace the header with a most accurate defined size type or/and
    2.2 one could ship macro assertions with guards per target + a macro for the target selection.
    2.3 Or should we make it like C to be mean and hide all the sizes from the user?

Related: marler8997/ziglibc#1
Also related (to check if upstream breaks abi or allows abi breakage via build scripts etc): #11447
see posix_spawn as example, which can be platform defined (and for some reason glibc doesnt want to upstream the logic).

I played around with C macro introspection here and I believe that C code can not be sanely argued on without assumptions on how symbols may be retrieved for target triplet, all used types (typedefs offer no simple introspection) and a bunch of "platform specifiers" (super basic assumptions like function exit codes etc).

alexrp added a commit to alexrp/zig that referenced this issue Dec 15, 2024
This is, at least today, a very broken target: It doesn't actually build either
musl or wasi-libc even if you use -lc. It does give you musl headers, but that's
it. Those headers are not terribly useful, however, without any implementation
code. You can sort of call some math functions because they just so happen to
have implementations in compiler-rt. But that's only true for a small subset,
and I don't think users should be relying on the ABI surface of a library that
is an implementation detail of the compiler.

Clearly, a freestanding-capable libc of sorts is a useful thing as evidenced by
newlib, picolibc, etc existing. However, calling it "musl" is misleading when it
isn't actually musl-compatible, nor can it ever be because the musl API surface
is inextricably tied to the Linux kernel. In the discussion on ziglang#20690, there was
agreement that once we split up the API and ABI components in the target string,
the API component should be about compatibility, not whether you literally get a
particular implementation of it. Also, we decided that Linux musl and wasi-libc
musl shouldn't use the same API tag precisely because they're not actually
compatible.

(And besides, how would any syscall even be implemented in freestanding? Who or
what would we be calling?)

So I think we should remove this triple for now. If we decide to reintroduce
something like this, especially once ziglang#2879 gets going, we should come up with a
bespoke name for it rather than using "musl".
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
contributor friendly This issue is limited in scope and/or knowledge of Zig internals. enhancement Solving this issue will likely involve adding new logic or components to the codebase.
Projects
None yet
Development

No branches or pull requests

6 participants