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

Allow #[cppgc] &mut T in sync ops #791

Merged
merged 4 commits into from
Jun 21, 2024
Merged

Allow #[cppgc] &mut T in sync ops #791

merged 4 commits into from
Jun 21, 2024

Conversation

littledivy
Copy link
Member

@littledivy littledivy commented Jun 20, 2024

Allow #[cppgc] _: &mut T and #[cppgc] _: Option<&mut T>

Closes #787

Trying to use it with async op gives a compiler error:

error[E0308]: mismatched types
    --> core/runtime/ops.rs:1911:3
     |
1911 |   #[op2(async)]
     |   ^^^^^^^^^^^^^
     |   |
     |   types differ in mutability
     |   arguments to this function are incorrect
     |
     = note: expected mutable reference `&mut TestResource`
                        found reference `&TestResource`
note: associated function defined here
    --> core/runtime/ops.rs:1911:3
     |
1911 |   #[op2(async)]
     |   ^^^^^^^^^^^^^
1912 |   pub async fn op_test_set_cppgc_resource(#[cppgc] resource: &mut TestResource, value: u32) {

Comment on lines +44 to +48
#[repr(C)]
struct InnerMember {
inner: [usize; 2],
ptr: *mut (),
}
Copy link
Member Author

Choose a reason for hiding this comment

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

TODO: add v8::cppgc::InnerMember::as_mut in rusty_v8

Copy link
Member

@bartlomieju bartlomieju left a comment

Choose a reason for hiding this comment

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

LGTM

@littledivy littledivy merged commit 3cc4464 into main Jun 21, 2024
18 checks passed
@littledivy littledivy deleted the cppgc_mut_ref branch June 21, 2024 03:02
Copy link
Member

@lucacasonato lucacasonato left a comment

Choose a reason for hiding this comment

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

I am not clear as to the safety of this. For example, this is currently valid but very unsafe:

#[op2(async)]
async fn op_use_cppgc_object_mut_async(#[cppgc] _wrap: &mut Wrap, #[cppgc] _wrap2: &mut Wrap) {}

When called with the same object for first and second argument.

Comment on lines 54 to -49
pub fn try_unwrap_cppgc_object<'sc, T: GcResource + 'static>(
val: v8::Local<'sc, v8::Value>,
) -> Option<&'sc T> {
Copy link
Member

Choose a reason for hiding this comment

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

This is not safe anymore. Please now mark as unsafe. The caller has to guarantee that there are not more than one user currently called try_unwrap_cppgc_object on this object.

Copy link
Member Author

Choose a reason for hiding this comment

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

I'll add an unsafe variant that returns &mut

}
(Position::Arg, Type::Reference(of)) => match &*of.elem {
Type::Path(of) => Ok(Arg::CppGcResource(stringify_token(&of.path))),
_ => Err(ArgError::InvalidCppGcType(stringify_token(ty))),
Copy link
Member

Choose a reason for hiding this comment

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

      _ => Err(ArgError::InvalidCppGcType(stringify_token(*of.elem))),

Copy link
Member

@lucacasonato lucacasonato left a comment

Choose a reason for hiding this comment

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

This should also not be allowed on #[op2(reentrant)] (that would be unsafe).

I think we should revert this for now - it's not clear to me how to make this safe yet, and what restrictions we need. We should talk about this more.

Also, for such fundamental issues we need more than one review. These are incredibly delicate components of our stack, and they need to be very well audited, especially when involving unsafe code.

littledivy added a commit that referenced this pull request Jun 21, 2024
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.

Allow #[cppgc] &mut T in sync ops
3 participants