Avoiding use-after-free
I encountered a use-after-free issue when writing an FFI binding. The code looks like this.
But the user could then do this.
How do I ensure that
example_free_data
is called only when no ArrayBuffer
points to the allocation?1 Reply
Hmm... That is a good question.
I don't think a native way to do this exists. The only option I can think of is to offer the user an API to call that would deregister the original AB and replace it with some other object, possibly any object (not just the new AB they cloned) if they wish to eg. share the AB with workers.
I think it's also fair to just say: "This will lead to a crash, don't do it."
There's a fair chance of leaking memory if you use
Deno.UnsafePointer.of()
on an AB that was created from a foreign pointer that is itself used in a FinalizationRegistry. ... I'll need to think on that.