-
Notifications
You must be signed in to change notification settings - Fork 923
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
JS_Eval
UBSAN Error: Null pointer passed to memcpy
#225
Comments
QuickJS may pass NULL pointers to memcpy with zero size. The C spec tells it is an undefined behavior but most C code do it, so the spec should be fixed instead. |
I'm not sure about this. See these commits from ffmpeg: And cpython: And curl: And openssl: And qemu: (These were just projects off the top of my head in which I searched "memcpy null" in the commit logs.) GCC currently optimizes around the assumption that the src and dst arguments to memcpy are not NULL, which leads to unintuitive optimizations like this: (this is from #include <string.h>
int f(char *src) {
memcpy(NULL, src, 0);
return src == NULL; // Note that this gets optimized into "return 0"
} f:
sub rsp, 8
mov rsi, rdi
xor edx, edx
xor edi, edi
call memcpy
xor eax, eax
add rsp, 8
ret
There is a proposal to change the standard here if you're interested in contributing. |
@kenballus: the example is compelling! A blatant case of compiler perversion. I did not find such a potential problems in the source code, but we should probably either:
|
There is a flag to disable this optimization, but UB can manifest itself in strange ways, so I wouldn't be 100% confident that the flag eliminates all potential weird behavior. OpenSSL (at least in some cases) uses wrappers: https://github.com/openssl/openssl/blob/62ecad5378067ab1f702ef2381c2f4a279d15250/ssl/ssl_asn1.c#L243 |
Programs with
JS_Eval
that are compiled with-fsanitize=undefined
throw an error due to a null pointer being passed tomemcpy
in this line.You can replicate using:
Compiled and called:
Results in:
Reproduces on linux (also on
debian:sid-slim
docker container)The text was updated successfully, but these errors were encountered: