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

Support allocations with non-Box<[u8]> bytes #108022

Merged
merged 7 commits into from
Mar 3, 2023

Conversation

CraftSpider
Copy link
Contributor

This is prep work for allowing miri to support passing pointers to C code, which will require Allocations to be correctly aligned. Currently, it just makes Allocation generic and plumbs the necessary changes through the right places.

The follow-up to this will be adding a type in the miri interpreter which correctly aligns the bytes, using that for the Miri engine, then allowing Miri to pass pointers into these allocations to C calls.

Based off of #100467, credit to @emarteca for the code

@rustbot
Copy link
Collaborator

rustbot commented Feb 14, 2023

r? @nagisa

(rustbot has picked a reviewer for you, use r? to override)

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Feb 14, 2023
@rustbot
Copy link
Collaborator

rustbot commented Feb 14, 2023

Some changes occurred to the CTFE / Miri engine

cc @rust-lang/miri

Some changes occurred to the CTFE / Miri engine

cc @rust-lang/miri

@rust-log-analyzer

This comment has been minimized.

@rustbot
Copy link
Collaborator

rustbot commented Feb 14, 2023

The Miri subtree was changed

cc @rust-lang/miri

@CraftSpider
Copy link
Contributor Author

Baffled by the tidy failure - x tidy passes locally

@rust-log-analyzer

This comment has been minimized.

@oli-obk
Copy link
Contributor

oli-obk commented Feb 15, 2023

looks like a rustfmt difference between your system and CI. Try rebasing over latest master and running x test tidy again

@nagisa
Copy link
Member

nagisa commented Feb 26, 2023

r? rust-lang/miri

@rustbot rustbot assigned oli-obk and unassigned nagisa Feb 26, 2023
@rust-log-analyzer

This comment has been minimized.

@oli-obk
Copy link
Contributor

oli-obk commented Mar 2, 2023

@bors r+

@bors
Copy link
Contributor

bors commented Mar 2, 2023

📌 Commit f26b0a2 has been approved by oli-obk

It is now in the queue for this repository.

@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 Mar 2, 2023
compiler-errors added a commit to compiler-errors/rust that referenced this pull request Mar 2, 2023
Support allocations with non-Box<[u8]> bytes

This is prep work for allowing miri to support passing pointers to C code, which will require `Allocation`s to be correctly aligned. Currently, it just makes `Allocation` generic and plumbs the necessary changes through the right places.

The follow-up to this will be adding a type in the miri interpreter which correctly aligns the bytes, using that for the Miri engine, then allowing Miri to pass pointers into these allocations to C calls.

Based off of rust-lang#100467, credit to `@emarteca` for the code
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Mar 2, 2023
Support allocations with non-Box<[u8]> bytes

This is prep work for allowing miri to support passing pointers to C code, which will require `Allocation`s to be correctly aligned. Currently, it just makes `Allocation` generic and plumbs the necessary changes through the right places.

The follow-up to this will be adding a type in the miri interpreter which correctly aligns the bytes, using that for the Miri engine, then allowing Miri to pass pointers into these allocations to C calls.

Based off of rust-lang#100467, credit to ``@emarteca`` for the code
bors added a commit to rust-lang-ci/rust that referenced this pull request Mar 3, 2023
…iaskrgr

Rollup of 8 pull requests

Successful merges:

 - rust-lang#108022 (Support allocations with non-Box<[u8]> bytes)
 - rust-lang#108367 (Re-apply "switch to the macos-12-xl builder")
 - rust-lang#108557 (Point error span at Some constructor argument when trait resolution fails)
 - rust-lang#108573 (Explain compile-time vs run-time difference in env!() error message)
 - rust-lang#108584 (Put backtick content from rustdoc search errors into a `<code>` elements)
 - rust-lang#108624 (Make `ExprKind` the first field in `thir::Expr`)
 - rust-lang#108644 (Allow setting hashmap toml values in `./configure`)
 - rust-lang#108672 (Feed queries on impl side for RPITITs when using lower_impl_trait_in_trait_to_assoc_ty)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit f75f440 into rust-lang:master Mar 3, 2023
@rustbot rustbot added this to the 1.69.0 milestone Mar 3, 2023
/// It is up to the caller to take sufficient care when using this address:
/// there could be provenance or uninit memory in there, and other memory
/// accesses could invalidate the exposed pointer.
pub fn alloc_base_addr(&self, id: AllocId) -> InterpResult<'tcx, *const u8> {
Copy link
Member

@RalfJung RalfJung Mar 13, 2023

Choose a reason for hiding this comment

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

It is very confusing to talk about getting an "address" when the return type is a pointer types. I would expect an "address" to be usize-typed, like ptr.addr().

Also the docs don't say what the caller is allowed to do with this pointer: for sure no mutation of the pointed-to memory is allowed; for how long may that memory be read? The provenance of the returned raw pointer will become invalid at some point.

Why do we need such an extremely unsafe-to-use function deep inside the interpreter?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is used in FFI code to get a pointer to the base address, however, it likely isn't necessary - we can get a byte slice through other methods that are a lot clearer and actually check various things we probably want to check anyways. I'd be in favor of removing this if I can alter my current draft PR to not use it, to confirm it's practical to avoid.

Copy link
Member

Choose a reason for hiding this comment

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

It being used for FFI does not absolve it from following the usual Rust pointer rules, so all the questions about mutability, lifetime, and provenance still remain.

Also I don't see why FFI support would need this as a new functionality inside the core interpreter. As you said the existing slice methods could be used, or alternatively we might want a function that gives access to the underling M::Bytes inside an allocation, and then Miri can do whatever it has to do directly with that type.

Clone + fmt::Debug + Eq + PartialEq + Hash + Deref<Target = [u8]> + DerefMut<Target = [u8]>
{
/// Adjust the bytes to the specified alignment -- by default, this is a no-op.
fn adjust_to_align(self, _align: Align) -> Self;
Copy link
Member

Choose a reason for hiding this comment

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

That's a strange API. Shouldn't alignment be set when the Self is created?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is called in adjust_from_tcx, it would be necessary if it could accept another bytes type that may not be aligned, but as is I think you're right - we always create the item with the desired alignment and don't need to re-align it later.

Copy link
Member

Choose a reason for hiding this comment

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

adjust_from_tcx will always have to convert a Box<[u8]> into an aligned byte storage, so that is probably the API we want in the trait.

fn from_bytes<'a>(slice: impl Into<Cow<'a, [u8]>>, _align: Align) -> Self;

/// Create a zeroed `AllocBytes` of the specified size and alignment;
/// call the callback error handler if there is an error in allocating the memory.
Copy link
Member

Choose a reason for hiding this comment

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

Which "callback error handler"?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This function used to return a Result and generate the error using a callback - this was effectively the same as returning an Option and calling ok_or_else on it, so it was changed to do that, but I missed updating the docs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants