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

system constants in Rust are decided at compile time #3

Closed
Jules-Bertholet opened this issue Nov 2, 2023 · 6 comments
Closed

system constants in Rust are decided at compile time #3

Jules-Bertholet opened this issue Nov 2, 2023 · 6 comments

Comments

@Jules-Bertholet
Copy link

In Cosmopolitan, the values of many system constants (syscalls, errno, etc) are different for different systems and only available at runtime. The Rust target definition in this repo specifies "os":"linux", "env": "musl", which is what the libc crate uses (via #[cfg(..)]) to determine what it thinks the values of these constants is. Naturally, these will match only on Linux platforms, so libc's dependents, including std, will be broken everywhere else.

@ahgamut
Copy link
Owner

ahgamut commented Nov 2, 2023

Thanks for the note! For my reference, can you link to the part of the libc crate source that actually decides the system constants? I'd like to know that the fat binary would work on x86-64 linux and aarch64 linux, because those are the envs that are fixed by the config file.

@ahgamut ahgamut changed the title Not actually portable system constants in Rust are decided at compile time Nov 2, 2023
@Jules-Bertholet
Copy link
Author

They are split across many different files, each platform has its own. You can search for them individually: pub const EINVAL/SIGABRT/MAP_INHERIT/…

@ahgamut
Copy link
Owner

ahgamut commented Nov 2, 2023

I just want to link to one of them so I can say the APE is for x86-64 linux and aarch64-linux.

Is it possible for the system constants to be extern values in Rust? I know the C standard doesn't allow extern constants within switch-cases, which is where the errnos are frequently used, so I wrote a small gcc patch to allow that behavior in C.

@ahgamut
Copy link
Owner

ahgamut commented Nov 2, 2023

I shall link to https://github.com/search?q=repo%3Arust-lang%2Flibc%20EINVAL&type=code showing that Rust constants are decided at compile-time.

@ahgamut
Copy link
Owner

ahgamut commented Nov 2, 2023

Updated the README. Thanks for the heads up!

@ahgamut ahgamut closed this as completed Nov 2, 2023
@Jules-Bertholet
Copy link
Author

Jules-Bertholet commented Nov 2, 2023

Is it possible for the system constants to be extern values in Rust? I know the C standard doesn't allow extern constants within switch-cases, which is where the errnos are frequently used, so I wrote a small gcc patch to allow that behavior in C.

std uses these constants as match patterns a lot; Rust does not allow statics to appear in such patterns, so std won't compile if you replace the consts with extern { static ... }s. A year ago, I submitted a Rust RFC that would have fixed this, but it was rejected.

Even if you fix that, IIRC some widely used crates outside std, like nix, use some of these constants as enum discriminant values; that's not something that could likely ever be supported with statics.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants