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

feat(InstanceContext): Allow any type of user data #85

Merged
merged 2 commits into from
Nov 29, 2019

Conversation

AdamSLevy
Copy link
Contributor

Previously the given unsafe.Pointer to user data was passed directly
across the CGo FFI resulting in a panic for any reference types or types
that included any reference types or other Go pointers.

This commit avoids a panic by casting the unsafe.Pointer to a uintptr,
which is not analyzed by the runtime. In order for compatability with
the existing CGo API and bridge code, which expects (void*), a *uintptr
is passed. The memory for the user provided unsafe.Pointer and the
*uintptr is cached in the instance to protect it from garbage
collection.

The tests are expanded to ensure that reference types do not cause
panics.

re #44 #83

Previously the given unsafe.Pointer to user data was passed directly
across the CGo FFI resulting in a panic for any reference types or types
that included any reference types or other Go pointers.

This commit avoids a panic by casting the unsafe.Pointer to a uintptr,
which is not analyzed by the runtime. In order for compatability with
the existing CGo API and bridge code, which expects (void*), a *uintptr
is passed. The memory for the user provided unsafe.Pointer and the
*uintptr is cached in the instance to protect it from garbage
collection.

The tests are expanded to ensure that reference types do not cause
panics.

re wasmerio#44 wasmerio#83
@AdamSLevy
Copy link
Contributor Author

I have added a test for using reference types as well that fails on the master branch but succeeds after this patch.

@losfair Thanks for the suggestion. This is much simpler than the previous approach, and has the advantage of not at all changing the API. However the downside of not changing the API is that when users who implemented some other work around update their dependencies they will not have anything in particular that would alert them to the new functionality, especially given that the documentation for Instance.SetContextData was already misleading. Perhaps the documentation should be updated to be more explicit that any type can be used. This would help signal to users that the package has been improved.

Changing the functions to use an interface{} instead of an unsafe.Pointer might be a friendlier API anyway so that users don't have to import unsafe.

Let me know if you would like to see the documentation for SetContextData to be updated or this API change and I will push those commits on this PR.

@AdamSLevy
Copy link
Contributor Author

@Hywan @losfair any further thoughts on this PR?

Copy link
Contributor

@Hywan Hywan left a comment

Choose a reason for hiding this comment

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

The PR looks good to me!

@Hywan Hywan self-assigned this Nov 25, 2019
@Hywan Hywan added 🎉 enhancement New feature or request 📦 component-runtime About the Wasm runtime 📦 component-extension About the Go extension and removed 📦 component-runtime About the Wasm runtime labels Nov 25, 2019
@Hywan
Copy link
Contributor

Hywan commented Nov 25, 2019

I will double-check with @losfair, but it looks good to me, thanks!

I agree the documentation must be improved. Do you feel brave enough to update it in this PR, or do you prefer to postpone (by opening a new issue for instance)?

@AdamSLevy
Copy link
Contributor Author

Happy to update the documentation. I'll get that pushed today in an additional commit to this PR.

Any thoughts about changing the API to use an interface{} instead of an unsafe.Pointer? That would additionally lead people to review the documentation yet make updating to the new API still very simple. It also allows users of the package to not necessarily need to import unsafe.

…for Data

Instead of an unsafe.Pointer, use an interface{} to allow stronger type
assertions and safer code. Additionally update the documentation for
Instance.SetContextData to explicitly allow data types that include Go
references.

BREAKING CHANGE: Changes the API of Instance.SetContextData and
InstanceContext.Data to use interface{}.
@AdamSLevy
Copy link
Contributor Author

AdamSLevy commented Nov 26, 2019

I added a commit that changes to use an interface{} instead of an unsafe.Pointer. I believe this is actually the better API as it allows runtime type checking for user data. I also updated the documentation for Instance.SetContextData.

@Hywan
Copy link
Contributor

Hywan commented Nov 28, 2019

I'm fine with breaking the compatibility for the moment. We are still on the 0.x versions. So changing for interface{} is really acceptable.

Copy link
Contributor

@Hywan Hywan left a comment

Choose a reason for hiding this comment

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

Looks good to me!

@Hywan
Copy link
Contributor

Hywan commented Nov 29, 2019

Thank you very much!

AdamSLevy added a commit to AdamSLevy/go-ext-wasm that referenced this pull request Dec 3, 2019
Previously an uintptr to user data was passed across the CGo FFI which
would become invalid if any stack frame occurred. This commit avoids
this by never passing any pointers across the CGo FFI. A registry map is
used instead to keep Go data on the Go side, and only an int index into
the map is passed across to the C side.

fix wasmerio#93
re wasmerio#44 wasmerio#85 wasmerio#83
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
📦 component-extension About the Go extension 🎉 enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants