rust-lang/rustc_codegen_gcc

Make sure memcpy/memmove/memset with size 0 behave correctly

RalfJung opened this issue · 3 comments

Zero-sized memory accesses are now always permitted, even if the pointer is NULL or dangling (but it must be aligned still). For codegen this means in particular that memcpy/memmove/memset must be lowered to operations that are never UB when the size is 0 (and the pointer is sufficiently aligned). In LLVM that's easy as LLVM's corresponding intrinsics explicitly allow size 0. However, in C, memcpy/memmove/memset with size 0 is UB on NULL (and dangling pointers are impossible to even mention in C), so GCC may use a different semantics for its builtins. For Rust's GCC backend, it's crucial that we use GCC builtins that allow size 0 with any pointer.

@antoyo Can I try this one?

Also any pointers on how to solve this issue will be helpful

@hhamud: I assigned the issue to you.

You would need to check if the GCC builtins follow the right semantics and if not, adjust the code here so that we follow the right semantics.

However, in C, memcpy/memmove/memset with size 0 is UB on NULL

There are proposals in progress to allow that: https://www.open-std.org/JTC1/SC22/WG14/www/docs/n3261.pdf

Fingers crossed it gets accepted