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

Remove undefined behavior from JSTypedArray.GetJSValueForMemory #295

Merged
merged 1 commit into from
May 15, 2024

Conversation

DaZombieKiller
Copy link
Contributor

The GetJSValueForMemory method currently uses unsafe code to access the internals of the Memory<T> type, resulting in undefined behavior.

Contrary to the comments inside, there has always been a public API to access the MemoryManager<T> backing a Memory<T> instance: MemoryMarshal.TryGetMemoryManager<T, TManager>. This PR replaces the implementation with one using that API to remove the undefined behavior.

With this new implementation, it is no longer necessary to strip the high bit from the index (it is an implementation detail that the MemoryMarshal APIs do not expose). Technically it wasn't necessary to begin with as that bit is only set for Memory<T> instances backed by pinned arrays, which are already ruled out by checking for a MemoryManager instance.

@DaZombieKiller
Copy link
Contributor Author

@microsoft-github-policy-service agree

@jasongin
Copy link
Member

Thanks for the PR! This is a great clean-up, as the previous code there (that I wrote) was certainly dubious. I guess I just did not discover the TryGetMemoryManager() API.

src/NodeApi/JSTypedArray.cs Outdated Show resolved Hide resolved
@jasongin jasongin merged commit 25b3b6c into microsoft:main May 15, 2024
24 checks passed
@DaZombieKiller DaZombieKiller deleted the jstypedarray-ub branch May 15, 2024 18:44
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.

2 participants