-
Notifications
You must be signed in to change notification settings - Fork 824
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 memory leak in C API in some uses of wasm_name_t
#2210
Conversation
Converting from `String` means that the type should auto-destruct to make sure we free that memory. The memory can still be freed manually though. We need thorough review for the changes were the `owned_wasm_name_t` is coming from user code. Is it guaranteed that `wasm_name_t` coming from the user always uses a host allocation? I don't think so, in which case, we have to find some way to handle transfer of ownership where it's unclear who owns it...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm confused by the Box<owned_wasm_name_t>
. Previously we had Box<wasm_name_t>
which means that we own wasm_name_t
. If wasm_name_t
isn't drop with the Box
, it's a bug. I think it can be solved by implementing Drop
on wasm_name_t
, rather than introducing a new non-standard type. Thoughts?
Yeah, I should have written more for you to review this with, wasn't expecting a review so soon though! I'm a bit confused about what exactly ownership means right now but I felt like I fully understood it before, I'll look into more and see if I can figure it out again. One distinction is that I agree that we probably need to reevaluate what we're doing though, so The most important idea in the PR right now is the idea that our conversion function from |
Alright, new hypothesis, Let's walk through how static inline void wasm_name_new_from_string(
own wasm_name_t* out, own const char* s
) {
wasm_name_new(out, strlen(s), s);
} is a common way to make one (note how the pointer is being passed in, the metadata about ptr, len exists on the C-side when we're dealing with
#define wasm_name_new wasm_byte_vec_new Which is created in this macro: WASM_API_EXTERN void wasm_##name##_vec_new( \
own wasm_##name##_vec_t* out, \
size_t, own wasm_##name##_t ptr_or_none const[]); \ And all other uses of WASM_API_EXTERN own wasm_importtype_t* wasm_importtype_new(
own wasm_name_t* module, own wasm_name_t* name, own wasm_externtype_t*);
WASM_API_EXTERN const wasm_name_t* wasm_importtype_module(const wasm_importtype_t*);
WASM_API_EXTERN const wasm_name_t* wasm_importtype_name(const wasm_importtype_t*);
WASM_API_EXTERN own wasm_exporttype_t* wasm_exporttype_new(
own wasm_name_t*, own wasm_externtype_t*);
WASM_API_EXTERN const wasm_name_t* wasm_exporttype_name(const wasm_exporttype_t*); We can see that in all cases with vecs (I also looked at other vecs besides We never return owned vecs meaning that there's no ambiguity here. Also I never noticed this before, but there are comments in the official header file that also express surprise about how vectors work 😄 https://github.com/WebAssembly/wasm-c-api/blob/master/include/wasm.h#L46-L65 The comment seems incorrect though, as we can see with the code from above.
Is not correct unless
takes precedence. But if that's the case, then the first rule never applies. A quick search with |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Despites I'm not super happy with the name owned_wasm_name_t
(I would prefer something like CBox<wasm_name_t>
or Owned<wasm_name_t>
), I agree that this is required and that the fix is correct.
I think something like I don't think the current solution is optimal either but it's easy enough to change in the future if we find something we like better. |
bors r+ |
This PR fixes a bug relating to
wasm_importtype_t
leaking memory, particularly on the path whereString
is converted intowasm_name_t
. It fixes this by introducing a new type,owned_wasm_name_t
, which is identical towasm_name_t
but has RAII and calls the destructor if it goes out of scope.I'm not confident that the use of
owned_wasm_name_t
on the FFI boundary is correct though, we'll have to thoroughly review that before shipping this PR.Review