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(bindings/c): use ManuallyDrop instead of forget to make sure pointer is valid #5166

Merged
merged 1 commit into from
Oct 7, 2024

Conversation

ethe
Copy link
Contributor

@ethe ethe commented Oct 7, 2024

Which issue does this PR close?

Closes #5165.

Rationale for this change

forget does not guarantee that pointers to this memory will remain valid. The need comes up in some specialized use cases for FFI or unsafe code, but even then, ManuallyDrop is typically preferred.

refer to https://doc.rust-lang.org/std/mem/fn.forget.html

the implementation of Vec::into_raw_parts
https://doc.rust-lang.org/1.81.0/src/alloc/vec/mod.rs.html#883

Also, transmute *const _ to * mut _ is an undefined behavior in any cases, it is fixed in this commit.

@ethe ethe force-pushed the main branch 3 times, most recently from 2bb9fc9 to 86b9a0b Compare October 7, 2024 04:46
@ethe ethe changed the title Fix https://github.com/apache/opendal/issues/5165 fix: https://github.com/apache/opendal/issues/5165 Oct 7, 2024
@ethe ethe force-pushed the main branch 9 times, most recently from 10884ab to 77193ff Compare October 7, 2024 06:45
@ethe
Copy link
Contributor Author

ethe commented Oct 7, 2024

This is ready to be reviewed, cc @Xuanwo @PsiACE , I also fixed the bug in Go bindings

Copy link
Member

Choose a reason for hiding this comment

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

So this is the real reason my previous patch failed, lol.

This can be a hidden knowledge that Go binding's struct should be synced with C binding's ..

Copy link
Member

@tisonkun tisonkun left a comment

Choose a reason for hiding this comment

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

Thanks for your contribution! I believe we can have a better PR title ..

drop(Vec::from_raw_parts(data_mut, (*ptr).len, (*ptr).len));
drop(Box::from_raw(ptr));
// transmuting `*const u8` to `*mut u8` is undefined behavior in any cases
// however, fields type of `opendal_bytes` is already related to the zig binding
Copy link
Member

Choose a reason for hiding this comment

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

already related to the zig binding

Do you mean that the Zig binding should use non-const pointer also?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, I think so

Comment on lines +64 to +66
// it is too weird that call `Box::new` outside `opendal_bytes::new` but dealloc it here
// also, boxing `opendal_bytes` might not be necessary
// `data` points to heap, so `opendal_bytes` could be passed as a stack value
Copy link
Member

Choose a reason for hiding this comment

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

Reasonable. But IIRC other bindings rely on this interface also. We can update them all in a follow-up.

Then the free method is guarded by !self.data.is_null().

Copy link
Member

@tisonkun tisonkun Oct 8, 2024

Choose a reason for hiding this comment

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

Try in #5171

@ethe ethe changed the title fix: https://github.com/apache/opendal/issues/5165 fix: use ManuallyDrop instead of forget to make sure pointer is valid Oct 7, 2024
Copy link
Member

@Xuanwo Xuanwo left a comment

Choose a reason for hiding this comment

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

Thanks a lot for helping fix this! Also thanks for @tisonkun's review.

@Xuanwo Xuanwo changed the title fix: use ManuallyDrop instead of forget to make sure pointer is valid fix(bindings/c): use ManuallyDrop instead of forget to make sure pointer is valid Oct 7, 2024
@Xuanwo Xuanwo merged commit ccf6801 into apache:main Oct 7, 2024
35 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

bug: Binding C didn't construct opendal_bytes correctly
3 participants