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 cross-compilation of C++ interpreter API to 32-bit architecture #3810

Merged
merged 5 commits into from
Oct 31, 2023

Conversation

tronical
Copy link
Member

@tronical tronical commented Oct 30, 2023

cbindgen doesn't reliably tranfser the 64-bit/32-bit #ifdefs around ValueOpaque.
Instead, move Value onto the heap by using a Box.

Fixes #3653

@tronical
Copy link
Member Author

Draft, not ready for review yet. One CI test run, then patch with fix coming up.

cbindgen doesn't reliably tranfser the 64-bit/32-bit #ifdefs around ValueOpaque.
Instead, move Value onto the heap by using a Box.
@tronical tronical marked this pull request as ready for review October 30, 2023 16:15
@tronical tronical changed the title Re-enable 32-bit interpreter cross-compilation in the CI Fix cross-compilation of C++ interpreter API to 32-bit architecture Oct 30, 2023
@tronical
Copy link
Member Author

This requires a very very careful review. I might have botched up the ownership....

Copy link
Member

@ogoffart ogoffart left a comment

Choose a reason for hiding this comment

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

this looks good from what i can see. Have you ran valgrind on the test to check for leak?

}

#[no_mangle]
pub extern "C" fn slint_interpreter_value_eq(a: &ValueOpaque, b: &ValueOpaque) -> bool {
a.as_value() == b.as_value()
pub extern "C" fn slint_interpreter_value_eq(a: &Box<Value>, b: &Box<Value>) -> bool {
Copy link
Member

Choose a reason for hiding this comment

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

i think that works, but i'd pass the value by reference instead of Box

Suggested change
pub extern "C" fn slint_interpreter_value_eq(a: &Box<Value>, b: &Box<Value>) -> bool {
pub extern "C" fn slint_interpreter_value_eq(a: &Value, b: &Value) -> bool {

passing a reference to a box is one indirection too many.

This is valid for most calls that don't take ownership

Copy link
Member Author

Choose a reason for hiding this comment

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

You're right. Will change.

val: &ValueOpaque,
out: *mut SharedVector<ValueOpaque>,
val: &Box<Value>,
out: *mut SharedVector<Box<Value>>,
Copy link
Member

Choose a reason for hiding this comment

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

SharedVector<Box<Value>>

this is not ideal because the destructor of SharedVector in C++ will not destroy the Value :-(
But since the only caller actualy re-interprets-cast this to the right thing, this is probably fine.

stru.as_struct()
.get_field(std::str::from_utf8(&name).unwrap())
.and_then(|v| unsafe { (v as *const Value as *const ValueOpaque).as_ref() })
) -> *mut Value {
Copy link
Member

Choose a reason for hiding this comment

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

(could be Option<Box<Value>>, but i don't know if cbingen would generate the right stuff)

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 tried that as well, but unfortunately it's only available for C. (See the box test in cbindgen repo)

let args = std::mem::transmute::<Slice<ValueOpaque>, Slice<Value>>(args);
args: Slice<Box<Value>>,
) -> *mut Value {
let args = args.iter().map(|vb| vb.as_ref().clone()).collect::<Vec<_>>();
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 unfortunate that we need to clone everything, but i can't think of a better way.

@tronical
Copy link
Member Author

Good point about valgrind. I'll run that tomorrow.

The memory the box allocated for the out data was never freed, instead the pointer was just overwritten.
Simplify by returning an owned pointer.
@tronical tronical merged commit 1441719 into master Oct 31, 2023
30 checks passed
@tronical tronical deleted the simon/32bit-cross branch October 31, 2023 16:33
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.

C++ interpreter cross-compilation from 64-bit host to 32-bit target broken
2 participants