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

Use a union for VMContext for functions #1753

Merged
merged 3 commits into from
Oct 31, 2020
Merged

Conversation

MarkMcCaskey
Copy link
Contributor

@MarkMcCaskey MarkMcCaskey commented Oct 22, 2020

This makes the code more self documenting and correctly uses unsafe
to communicate that it's the caller's responsibility to ensure that the
code paths that can lead to the access can only write the value they
expect to be there.

spun off of #1739

  • There's still some inconsistent use of vmctx and extra_data might be good to unify this more.
  • Improve docs on the new type

This makes the code more self documenting and correctly uses `unsafe`
to communicate that it's the user's responsibility to ensure that the
code paths that can lead to the access can only write the value they
expect to be there.
lib/api/src/externals/function.rs Outdated Show resolved Hide resolved
lib/vm/src/vmcontext.rs Outdated Show resolved Hide resolved
lib/vm/src/vmcontext.rs Outdated Show resolved Hide resolved
lib/vm/src/vmcontext.rs Outdated Show resolved Hide resolved
lib/vm/src/vmcontext.rs Outdated Show resolved Hide resolved
/// We stop lying about what this daat is
/// TODO:
#[derive(Copy, Clone)]
pub union VMFunctionExtraData {
Copy link
Contributor

Choose a reason for hiding this comment

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

Optional: consider the name VMFunctionContextParameter or VMFunctionEnvParameter, or possibly CallbackParameter. Whether it's a wasm function or a host function this is giving that function its context/env.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm down for renaming, but I'll wait for more feedback before renaming! I like your suggestions though

Copy link
Contributor

Choose a reason for hiding this comment

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

I also like VMFunctionContextParameter. I would have use VMFunctionEnvironment first, but it creates a naming conflict with “host environment” or similar.

Copy link
Contributor

Choose a reason for hiding this comment

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

Don't forget to rename the extra_data field everywhere accordingly too :-).

Copy link
Member

Choose a reason for hiding this comment

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

I like VMFunctionEnvironment

lib/vm/src/vmcontext.rs Show resolved Hide resolved
MarkMcCaskey pushed a commit that referenced this pull request Oct 22, 2020
/// We stop lying about what this daat is
/// TODO:
#[derive(Copy, Clone)]
pub union VMFunctionExtraData {
Copy link
Contributor

Choose a reason for hiding this comment

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

I also like VMFunctionContextParameter. I would have use VMFunctionEnvironment first, but it creates a naming conflict with “host environment” or similar.

/// We stop lying about what this daat is
/// TODO:
#[derive(Copy, Clone)]
pub union VMFunctionExtraData {
Copy link
Contributor

Choose a reason for hiding this comment

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

Don't forget to rename the extra_data field everywhere accordingly too :-).

/// Wasm functions take a pointer to [`VMContext`].
pub vmctx: *mut VMContext,
/// Host functions can have custom environments.
pub host_env: *mut std::ffi::c_void,
Copy link
Contributor

Choose a reason for hiding this comment

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

As far as I understand this PR (which backport and enhance (!) a pre-1.0 feature), host_env can hold 2 kind of pointers: VMContextWithoutEnv and VMContextWithEnv. To clarify things, wouldn't it be possible to write this union as follows:

pub enum Either<L, R> {
    Left(L),
    Right(R),
}

pub union VMFunctionContextParameter {
    pub vmctx: *mut VMContext,
    pub host_env: Either<*mut VMContextWithEnv, *mut VMContextWithoutEnv>,
}

mem::size_of::<VMFunctionContextParameter> would be around 16 bytes, which is quite “heavy”. Without Either, it's 8 bytes.

If we don't really care about the size of this pointer, I would suggest to use Either or a similar enum, to be super explicity about what kind of pointer host_env can hold.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

My understanding is that there are 3 states that can exist on this single pointer:

  1. The pointer is used in the context of a compiled Wasm function. The pointer is a VMContext.
  2. The pointer is used in the context of a host function with an environment. The pointer is a pointer to user data.
  3. The pointer is used in the context of a host function without an environment. The pointer is never used and we usually set it to null.

I don't think we need to represent the difference between 2 and 3 really, but I think the difference between 1 and 2 can be quite important. Please let me know if my mental model of this is not the same as yours!

Copy link
Contributor

Choose a reason for hiding this comment

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

Your mental model matches mine. I don't know if that's a good news though 🤔.

😄

@Hywan Hywan added 🎉 enhancement New feature! 📦 lib-vm About wasmer-vm labels Oct 27, 2020
@MarkMcCaskey
Copy link
Contributor Author

bors r+

@bors
Copy link
Contributor

bors bot commented Oct 31, 2020

@bors bors bot merged commit a5281ea into master Oct 31, 2020
@bors bors bot deleted the feature/vmctx-in-func-is-a-union branch October 31, 2020 01:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🎉 enhancement New feature! 📦 lib-vm About wasmer-vm
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants