-
Notifications
You must be signed in to change notification settings - Fork 5
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
Problem of the interoperability with Wasm. #9
Comments
The motivation of making the read-only ability on the ArrayBuffer itself instead of on the view is cause I think it allows the engine to optimize based on the immutable binary. If the ArrayBuffer cannot be guaranteed to be frozen, (I guess) the engine cannot do those optimizations. But yes, limit it to the view can make things better. |
I don't see how this is different from issue #6? It doesn't particularly matter how it's being mutated, or who is mutating it, rather what matters is that it's being mutated at all. I raised the same idea, that the ArrayBuffer itself should be mutable/immutable here: #8. The API doesn't look great once you consider that there are currently a bunch of other proposals that are trying to add new ArrayBuffer types too. Oh yeah, imho, any freezing mechanism should throw when used on detached ArrayBuffers, and on Wasm heaps and/or views to these, at least, in strict mode they should. |
On the contrary, I think it's important to make sure who or which part is responsible to mutate the underlying Arraybuffer, the responsibility boundary should be clear. To me, the Arraybuffer itself is only a bunch of continuous memory without any other specific attributes or functionalities. You can regard the relationship between Arraybuffer and ArrayBufferView in analogous to the relationship of pointer/reference and the pointed value in C/C++/Rust. For pointers, we have the concepts like bottom-level constness and top-level constness, so, you can think of the view just like a pointer which points to the underlying data. And we can maybe even enable the below pattern to make a "frozen" ArrayBufferView just behaves like what we expected: people cannot mutate the underlying data through it (this particular view).
In this case, the internal Arraybuffer would be safe as long as there is no external reference on it, meaning no access and no return value assignment on the buffer attribute of the view before calling the "freeze" method. It's a little bit similar to the |
@Becavalier For clarification, by "who," I was referring to JS vs Wasm, and by "how," I was referring to JS' property assignment vs Wasm's TxN.store operations. Also, slightly off-topic, I understand the reference to Rust's void reassign(
unsigned int&& x
) {
x = 0u;
} |
By having frozen array buffers, the view is guaranteed to be frozen too, but not vice verse, therefore this idea seems to support frozen arraybuffers, better than oppose the idea. |
Yes, this is valid, but it just doesn't make any practical sense. The rvalue passed in will be placed on the stack without any accessible external reference. So, the re-assignment to this rvalue doesn't make it live longer. Anyway, the purpose of the analogies of pointers/references is that I want to demonstrate the relationship between Arraybuffer (analogous to rvalue) and ArrayBufferView (analogous to pointers/references). The mutability of the pointers/references will not affect the mutability of the rvalue itself. |
Yes, this is an idea being proposed here, but as an extra feature, see #6 (comment); the primary mechanism for creating frozen ArrayBuffers/ArrayBufferViews should copy once to create a frozen buffer/view. |
Engines might still be able to optimize if all mutable reference to an ArrayBuffer is dropped. function ro() {
const x = new ArrayBuffer(2048)
fillData(x)
return new ReadonlyArrayBuffer(x)
// x the read-write ref is lost
} But here is a question, does line 4 |
Well, consider what you had said before: #6 (comment), a copy is almost definitely required. But maybe an engine could attempt to implement copy-on-write semantics under the hood? (Doesn't sound ideal) The only problems that I have with a .freeze method are that it requires mutating objects to make it immutable, which is somewhat counter-intuitive? Yes, The other reason is that freezing a Wasm heap makes no sense, and should definitely fail. |
Yes, when only generating an immutable value from a mutable reference. If you freeze the original value there is no need to copy it.
The freezing feature is not prepared for WASM. It is used for other security scenarios. |
This issue is fixed in the new API design in #13. Host integration guide in the new API design suggests:
|
Since JavaScript and WebAssembly can communicate with each other by accessing the same Arraybuffer, and the
WebAssembly.Memory
object has the ability to export its internal Arraybuffer by calling thebuffer
method on it. If so, what would be the behavior if we freeze the exported Arraybuffer and mutate the corresponding memory of this Arraybuffer by the Wasm instruction likei32.store
?Without messing things up, I think the better ways are:In conclusion, I think ArrayBuffer itself should not take the responsibility of the mutability, this ability should be controlled by the view lying on it. How do you think?
The text was updated successfully, but these errors were encountered: