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

Fix ICEs on invalid vtable size/alignment const UB errors #86245

Merged
merged 2 commits into from
Jun 13, 2021

Conversation

lqd
Copy link
Member

@lqd lqd commented Jun 12, 2021

The invalid vtable size/alignment errors from InterpCx::read_size_and_align_from_vtable were "freeform const UB errors", causing ICEs when reaching validation. This PR turns them into const UB hard errors to catch them during validation and avoid that.

Fixes #86193

r? @RalfJung

(It seemed cleaner to have 2 variants but they can be merged into one variant with a message payload if you prefer that ?)

@rust-highfive
Copy link
Collaborator

Some changes occured to the CTFE / Miri engine

cc @rust-lang/miri

Some changes occured to the CTFE / Miri engine

cc @rust-lang/miri

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jun 12, 2021
@rust-log-analyzer

This comment has been minimized.

@lqd lqd force-pushed the const-ub-align branch 2 times, most recently from c29f599 to b38e376 Compare June 12, 2021 13:50
/// Invalid size in a vtable: too large.
InvalidSize,
/// Invalid alignment in a vtable: too large, or not a power of 2.
InvalidAlignment(String),
Copy link
Member

Choose a reason for hiding this comment

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

I feel like InvalidVtable{Size,Alignment,DropFn} would be clearer names for these variants. What do you think?

Copy link
Member Author

Choose a reason for hiding this comment

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

sure, I'll do that ! I initially did name those specifically for vtables but then InvalidDropFn "convinced me" not to do so ^^

Comment on lines 10 to 11
// ICEs as tracked by #86193. So we also use the transparent wrapper to verify actual const UB hard
// errors are emitted now instead of ICEs.
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
// ICEs as tracked by #86193. So we also use the transparent wrapper to verify actual const UB hard
// errors are emitted now instead of ICEs.
// ICEs as tracked by #86193. So we also use the transparent wrapper to verify proper validation
// errors are emitted instead of ICEs.

|
LL | / const INVALID_VTABLE_SIZE_UB: W<&dyn Trait> =
LL | | unsafe { std::mem::transmute((&92u8, &(drop_me as fn(*mut usize), usize::MAX, 1usize))) };
| |______________________________________________________________________________________________^ type validation failed: encountered invalid vtable: size is bigger than largest supported object at .0
Copy link
Member

Choose a reason for hiding this comment

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

@oli-obk reading this, I feel like we should reorder these error messages a bit, and make them more like

type validation failed at .0: encountered invalid vtable: size is bigger than largest supported object

What do you think? (@lqd certainly not for this PR though)

Copy link
Member Author

Choose a reason for hiding this comment

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

if you and oli want this, I can certainly do that in this PR

Copy link
Member

@RalfJung RalfJung Jun 13, 2021

Choose a reason for hiding this comment

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

This will affect many tests, so it should be a separate PR. Reviewing is also much easier if we keep PRs small. But sure, if you want to help with this that'd be great. :)

Copy link
Member Author

@lqd lqd Jun 13, 2021

Choose a reason for hiding this comment

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

Agreed, I'll do that in another PR.

Would something like this work for you ? Changing ValidationFailure's content from a String message to a path: Option<String>, mesg: String so that

macro_rules! throw_validation_failure {
($where:expr, { $( $what_fmt:expr ),+ } $( expected { $( $expected_fmt:expr ),+ } )?) => {{
let msg = rustc_middle::ty::print::with_no_trimmed_paths(|| {
let mut msg = String::new();
msg.push_str("encountered ");
write!(&mut msg, $($what_fmt),+).unwrap();
let where_ = &$where;
if !where_.is_empty() {
msg.push_str(" at ");
write_path(&mut msg, where_);
}
$(
msg.push_str(", but expected ");
write!(&mut msg, $($expected_fmt),+).unwrap();
)?
msg
});
throw_ub!(ValidationFailure(msg))
}};
}
can handle the empty "where", and the formatting you want can be implemented at
ValidationFailure(ref err) => write!(f, "type validation failed: {}", err),


(I don't know when I'll get to this though: I'm currently having trouble executing some 32bits tests of the master branch, which fail with syntax errors when executed in the test runner but not by themselves, making blessing tests rather ... difficult. Thankfully it did not happen on the test in this PR 😓 )

Copy link
Member

Choose a reason for hiding this comment

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

Let's discuss the implementation details elsewhere (possibly on Zulip), if/when @oli-obk agrees that we even want to do this. :)

Copy link
Contributor

Choose a reason for hiding this comment

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

I agree that the proposed ordering is better than the current one

@lqd lqd force-pushed the const-ub-align branch from b38e376 to 9c7133c Compare June 13, 2021 10:47
@RalfJung
Copy link
Member

This looks great, thanks a lot. :-)
@bors r+

@bors
Copy link
Contributor

bors commented Jun 13, 2021

📌 Commit 9c7133c95bdfaf3b14798f61d40edeb1564b2ed3 has been approved by RalfJung

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jun 13, 2021
@rust-log-analyzer

This comment has been minimized.

@RalfJung
Copy link
Member

@bors r-

@bors bors added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Jun 13, 2021
lqd added 2 commits June 13, 2021 13:11
They were "freeform const UB" error message, but could reach validation
and trigger ICEs there. We now catch them during validation to avoid
that.
@lqd lqd force-pushed the const-ub-align branch from 9c7133c to e29f3e8 Compare June 13, 2021 11:11
@lqd
Copy link
Member Author

lqd commented Jun 13, 2021

CI is green after actually calling rustfmt...

@bors r=RalfJung

@bors
Copy link
Contributor

bors commented Jun 13, 2021

📌 Commit e29f3e8 has been approved by RalfJung

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Jun 13, 2021
@bors
Copy link
Contributor

bors commented Jun 13, 2021

⌛ Testing commit e29f3e8 with merge fb3ea63...

@bors
Copy link
Contributor

bors commented Jun 13, 2021

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

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Jun 13, 2021
@bors bors merged commit fb3ea63 into rust-lang:master Jun 13, 2021
@rustbot rustbot added this to the 1.55.0 milestone Jun 13, 2021
@lqd lqd deleted the const-ub-align branch June 13, 2021 15:35
Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this pull request Jun 14, 2021
Improve CTFE UB validation error messages

As mentioned in rust-lang#86245 (comment) this PR slightly improves the formatting of validation errors, to move the path to the error prefix.

From:
`type validation failed: encountered invalid vtable: size is bigger than largest supported object at .0`

To:
`type validation failed at .0: encountered invalid vtable: size is bigger than largest supported object`.
bors added a commit to rust-lang-ci/rust that referenced this pull request Jun 14, 2021
Improve CTFE UB validation error messages

As mentioned in rust-lang#86245 (comment) this PR slightly improves the formatting of validation errors, to move the path to the error prefix.

From:
`type validation failed: encountered invalid vtable: size is bigger than largest supported object at .0`

To:
`type validation failed at .0: encountered invalid vtable: size is bigger than largest supported object`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
merged-by-bors This PR was explicitly merged by bors. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ICE on invalid size/alignment in CTFE validation
7 participants