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

Detect std by checking if the crate defines #[lang = "start"] rather than string comparison #1823

Merged
merged 5 commits into from Jun 6, 2021

Conversation

ghost
Copy link

@ghost ghost commented Jun 3, 2021

I also considered to compare the crate name with sym::std, but it's easy to name any crate std by using --crate-name std, so I don't think that is robust enough.

Note that this only checks the crate, it does not check whether the call is in sys::unix or sys::windows, unlike the previous implementation, but I think it's already robust enough.

Fixes #1821.

src/helpers.rs Outdated
fn in_std(&self) -> bool {
let this = self.eval_context_ref();
this.tcx.def_path(this.frame().instance.def_id()).krate
== this.tcx.def_path(this.tcx.lang_items().start_fn().unwrap()).krate
Copy link
Member

Choose a reason for hiding this comment

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

There may not be a #[lang = "start"] when using #![no_std] in combination with #[start], so please don't unwrap.

Copy link
Author

Choose a reason for hiding this comment

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

There is already an unwrap() in create_ecx() 🤔

let start_id = tcx.lang_items().start_fn().unwrap();

Copy link
Member

Choose a reason for hiding this comment

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

using #![no_std] in combination with #[start]

That sounds like a test case we should have, then. ;)

Copy link
Author

Choose a reason for hiding this comment

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

using #![no_std] in combination with #[start]

Note that it's currently broken even before the unwrap():

error: internal compiler error: src/tools/miri/src/eval.rs:110:9: main function must not take any arguments

thread 'rustc' panicked at 'Box<Any>', compiler/rustc_errors/src/lib.rs:1007:9
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

note: the compiler unexpectedly panicked. this is a bug.

note: we would appreciate a bug report: https://github.com/rust-lang/rust/issues/new?labels=C-bug%2C+I-ICE%2C+T-compiler&template=ice.md

note: rustc 1.54.0-nightly (c79419af0 2021-06-04) running on x86_64-unknown-linux-gnu

https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=b867b78dd787c573bdfb44c88fa856dc

bug!("main function must not take any arguments");

Copy link
Member

Choose a reason for hiding this comment

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

Hm, the start item in Rust has no arguments so I am not sure if it makes sense to support other start items that do.

But this sounds like a separate discussion then, could you open an issue?

@RalfJung
Copy link
Member

RalfJung commented Jun 3, 2021

Note that the check does not have to be extremely robust. If someone wants to go out of their way to use these incomplete Miri shims, I'm fine with that, but they don't get to complain about any bugs they encounter. ;)

So I'm in favor of your code changes, but I am worried about the test size blowup.^^ tests/run-pass/std_only_foreign_function.rs is not a supported use-case, so we shouldn't test it IMO. For the compile-fail test, I think having just one is enough -- just to make sure that in_std does not always return true. So I'm in favor of also removing tests/compile-fail/unsupported_get_process_heap.rs.

src/helpers.rs Outdated Show resolved Hide resolved
@bors

This comment has been minimized.

@ghost
Copy link
Author

ghost commented Jun 6, 2021

tests/run-pass/std_only_foreign_function.rs is not a supported use-case, so we shouldn't test it IMO.

(I have removed it now, but) I added it to test that in_std() does not always return false, which is not explicitly tested in other tests.

@RalfJung
Copy link
Member

RalfJung commented Jun 6, 2021

Thanks @hyd-dev :)
@bors r+

@bors
Copy link
Collaborator

bors commented Jun 6, 2021

📌 Commit 0ece55d has been approved by RalfJung

@bors
Copy link
Collaborator

bors commented Jun 6, 2021

⌛ Testing commit 0ece55d with merge 28fbf81...

@RalfJung
Copy link
Member

RalfJung commented Jun 6, 2021

(I have removed it now, but) I added it to test that in_std() does not always return false, which is not explicitly tested in other tests.

Fair -- however, it is implicitly tested in every single test, which IMO is good enough.

@bors
Copy link
Collaborator

bors commented Jun 6, 2021

☀️ Test successful - checks-actions
Approved by: RalfJung
Pushing 28fbf81 to master...

@bors bors merged commit 28fbf81 into rust-lang:master Jun 6, 2021
@ghost ghost mentioned this pull request Jun 6, 2021
@ghost ghost deleted the extern-crate-std branch June 6, 2021 08:55
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

Successfully merging this pull request may close these issues.

extern crate std; in a #![no_std] crate confuses Miri unless it's in the root module
3 participants