Skip to content

add NetBSD shims#5109

Draft
joboet wants to merge 1 commit into
rust-lang:masterfrom
joboet:netbsd
Draft

add NetBSD shims#5109
joboet wants to merge 1 commit into
rust-lang:masterfrom
joboet:netbsd

Conversation

@joboet

@joboet joboet commented Jun 15, 2026

Copy link
Copy Markdown
Member

NetBSD is a very useful test target for std, since it

and at the same time it is a UNIX system and can share most of the existing shims. Hence it'd be very useful for miri to support it. I'm willing to serve as target maintainer, though I should point out that I'm not involved with the platform otherwise.

Most of the changes necessary involve symbol names, which NetBSD uses for versioning. I've added a small check_alias_used_on helper that ensures that the alias is always and only used on the given platforms.

As for the NetBSD specific shims, there are two interesting cases:

  • pthread_setname_np weirdly uses printf like formatting, but with only a single argument. To avoid having to shim the entire printf infrastructure, the current version only supports a %s format string since that's the only thing uses by std.
  • _lwp_park and _lwp_unpark present a new kind of synchronisation API to miri. Their semantics are pretty much identical to the thread::park and Thread::unpark function and method in the standard library.

Currently, all test pass with the exception of two test that utilise thread::park_timeout – those report undefined behaviour within std. This is not caused by the _lwp_park shim but rather an incorrect definition of _lwp_park in libc, which acts together with the implicit immutable reborrow inserted by stacked borrows when coercing a mutable reference to an immutable pointer to cause unsoundness inside std (c.f. rust-lang/libc#5169). I went ahead and added this to the trophy shelf...

@rustbot rustbot added the S-waiting-on-review Status: Waiting for a review to complete label Jun 15, 2026
"getentropy" => {
// This function is non-standard but exists with the same signature and behavior on
// Linux, macOS, FreeBSD and Solaris/Illumos.
// This function is in the POSIX standard, but does not exist

@joboet joboet Jun 15, 2026

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment on lines -69 to -74
"readdir_r" | "readdir_r$INODE64" => {
let [dirp, entry, result] =
this.check_shim_sig_lenient(abi, CanonAbi::C, link_name, args)?;
let result = this.macos_readdir_r(dirp, entry, result)?;
this.write_scalar(result, dest)?;
}

@joboet joboet Jun 15, 2026

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved to the shared UNIX match.

View changes since the review

Comment on lines +1483 to +1487
this.check_alias_used_on(
"__posix_getpwuid_r",
&[Os::Illumos, Os::Solaris],
link_name,
)?;

@joboet joboet Jun 15, 2026

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The helper exists now, so we might as well use it for Illumos and Solaris too.

View changes since the review

@joboet

joboet commented Jun 15, 2026

Copy link
Copy Markdown
Member Author

CC @he32 @0323pin
as the rustc NetBSD target maintainers

@RalfJung

RalfJung commented Jun 15, 2026

Copy link
Copy Markdown
Member

Thanks for the PR!

Please also add a netbsd job to ci/ci.sh to ensure this is actually tested. You can use run_tests_minimal if you need to add filters to run only a part of the test suite.

Also, 500 lines is pretty big as PRs go. If this can be split up into multiple PRs, that would simplify review.

Comment on lines +152 to +153
"unsetenv" | "__unsetenv13" => {
this.check_alias_used_on("__unsetenv13", &[Os::NetBsd], link_name)?;

@RalfJung RalfJung Jun 15, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please move the __unsetenv13 case into the netbsd module. That matches how we handle the same situation on macOS / freeBSD.

View changes since the review

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you sure? The unversioned function has different semantics on NetBSD, it returns void instead of int, so if someone accidentally messes up their function definition and calls it, that's UB, and miri can easily catch that. And additionally, there are versioned functions in this shared match already, so it's not like there isn't any precedent.

@0323pin

0323pin commented Jun 17, 2026

Copy link
Copy Markdown

@joboet First of all, thanks for working on this, truly appreciated 👍

I don't really know if you expect anything from me or @he32 but, I've just tried a local test build on bare metal. Do let me know if you would like further support. For now, here's what I get:

~> git clone -b netbsd https://github.com/joboet/miri
~> cd miri
~> pkg_info | grep rust
rust-1.97.0beta.4   Safe, concurrent, practical language
~> uname -a
NetBSD mybox 11.99.6 NetBSD 11.99.6 (GENERIC) #0: Fri Jun 12 05:36:50 UTC 2026  mkrepro@mkrepro.NetBSD.org:/usr/src/sys/arch/amd64/compile/GENERIC amd64
~> cargo build
[...]
   Compiling miri v0.1.0 (/home/pin/Downloads/miri)
error[E0554]: `#![feature]` may not be used on the stable release channel
 --> src/lib.rs:1:48
  |
1 | #![cfg_attr(all(feature = "native-lib", unix), feature(iter_advance_by))]
  |                                                ^^^^^^^^^^^^^^^^^^^^^^^^

error[E0599]: no method named `metadata` found for struct `Dir` in the current scope
   --> src/shims/files.rs:495:48
    |
495 |         return interp_ok(Either::Left(self.dir.metadata()));
    |                                                ^^^^^^^^ method not found in `Dir`
    |
    = help: items from traits can only be used if the trait is implemented and in scope
note: `FileDescription` defines an item `metadata`, perhaps you need to implement it
   --> src/shims/files.rs:153:1
    |
153 | pub trait FileDescription: std::fmt::Debug + FileDescriptionExt {
    | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error[E0599]: no method named `metadata` found for struct `Dir` in the current scope
   --> src/shims/windows/fs.rs:217:25
    |
217 |                 if !dir.metadata().unwrap().is_dir() {
    |                         ^^^^^^^^ method not found in `Dir`
    |
    = help: items from traits can only be used if the trait is implemented and in scope
note: `FileDescription` defines an item `metadata`, perhaps you need to implement it
   --> src/shims/files.rs:153:1
    |
153 | pub trait FileDescription: std::fmt::Debug + FileDescriptionExt {
    | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Some errors have detailed explanations: E0554, E0599.
For more information about an error, try `rustc --explain E0554`.
error: could not compile `miri` (lib) due to 3 previous errors
Exception: cargo exited with 101

Do I need nightly to compile this?

If it matters:

~> cat .cargo/config.toml
[target.x86_64-unknown-netbsd]
rustflags = ["-C", "link-args=-Wl,-R/usr/pkg/lib,-R/usr/X11R7/lib"]

[env]
MAKE = "gmake"

@joboet

joboet commented Jun 17, 2026

Copy link
Copy Markdown
Member Author

@0323pin there's no specific ask, I just wanted you to be aware of this 😊. Also note that this is about interpreting with NetBSD as a target, not about using NetBSD as a host for miri. Though I'm unsurprised that cargo build fails, you need ./miri toolchain && ./miri build since there's some build setup involved.

@joboet joboet marked this pull request as draft June 18, 2026 10:22
@rustbot rustbot added S-waiting-on-author Status: Waiting for the PR author to address review comments and removed S-waiting-on-review Status: Waiting for a review to complete labels Jun 18, 2026
@joboet

joboet commented Jun 18, 2026

Copy link
Copy Markdown
Member Author

@RalfJung sure thing, I'll split this up.

PRs:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-author Status: Waiting for the PR author to address review comments

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants