- Sponsor
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
@memcpy
panics on shared-memory WASM
#15920
Comments
This isn't caused by enabling shared-memory, this is due to enabling @andrewrk I can't really find the best solution here, we could perhaps add a safety check for the WebAssembly targets to check for zero-lengths. I don't think changing the value of the |
But if this is done, changes will have to be done to I think that a check for zero-sized copies on Wasm is the best option (that I can think of, at least). |
The language is correct to allow zero length memcpy with undefined pointer value, even when the target is webassembly. It is each backend's job to make it work when lowering. This is arguably a bug in the wasm runtime or even the specification. Getting those third parties to change their behavior is the first path we should explore. Failing that, there could be a "CPU feature" flag that indicates the wasm runtime can't ignore the address of a length zero memcpy, which the wasm backend can branch on and emit checks for length zero if necessary. |
it is in libc which runtimes may be using still trying to find a spec reference to double check how wasm defines it |
citation needed |
https://man7.org/linux/man-pages/man3/memcpy.3.html and https://linux.die.net/man/3/memcpy are very sparse and do not describe nearly any error conditions explicitly that reasoning was taken from https://en.cppreference.com/w/cpp/string/byte/memcpy which has the line:
|
Note that is the C++ reference. The C one is here: https://en.cppreference.com/w/c/string/byte/memcpy Regardless, Zig is a different language than C, and I am quite certain that len=0 will always be well-defined behavior, regardless of the pointer values of src and dest. |
You can find that in the overview in the original proposal:
It seems that originally it wasn't bound-checked for zero-length, but that was ultimately reverted. Some discussions around this can be found in WebAssembly/bulk-memory-operations#111 and WebAssembly/bulk-memory-operations#124 the main reasoning being simplicity and runtime cost for the runtime itself. Considering it's part of the spec, we can only attempt to have them change the specification with the option 2 being a change to emit a check in the LLVM and Wasm backend for the |
Zig Version
0.11.0-dev.3332+76aa1fffb
Steps to Reproduce and Observed Behavior
The bug was found when executing a WASM library in Deno
Try creating an
ArrayList
, and then reserve capacity withensureTotalCapacityPrecise
.Here is the code, but whith
ensureTotalCapacityPrecise
extractedReplace
@memcpy
withstd.mem.copyForwards
, and the code stops panicking.Expected Behavior
Well, this shoudn't panic!
My theory is that there is something about Wasm shared memory that messes up
@memcpy
when doing zero-sized copies (It seems to work fine when actually copying values, so it may have to do with the high value of the originalundefined
pointer). Apart from that, I have no idea,The text was updated successfully, but these errors were encountered: