rust-lang / rustc_codegen_gcc

libgccjit AOT codegen for rustc
Apache License 2.0
913 stars 60 forks source link

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

Open RalfJung opened 4 months ago

RalfJung commented 4 months ago

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.

hhamud commented 4 months ago

@antoyo Can I try this one?

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

antoyo commented 4 months ago

@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.

GrigorenkoPV commented 1 month ago

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