Quuxplusone / LLVMBugzillaTest

0 stars 0 forks source link

Can __builtin_memcpy be given a null pointer argument? #49084

Open Quuxplusone opened 3 years ago

Quuxplusone commented 3 years ago
Bugzilla Link PR50115
Status NEW
Importance P enhancement
Reported by David Stone (davidfromonline@gmail.com)
Reported on 2021-04-24 18:35:01 -0700
Last modified on 2021-04-25 04:34:54 -0700
Version trunk
Hardware PC Linux
CC david.bolvansky@gmail.com, llvm-bugs@lists.llvm.org, richard-llvm@metafoo.co.uk
Fixed by commit(s)
Attachments
Blocks
Blocked by
See also

The C function memcpy cannot be given null pointers as arguments, even if the size is 0. This is checked by UBSan.

It is unclear whether __builtin_memcpy has this same restriction. UBSan accepts calls to __builtin_memcpy with null pointer arguments as long as the size is 0. The documentation does not say whether this is guaranteed behavior.

I hope that it is defined to call it with null pointer arguments, since the entire reason I'm using it is to avoid that check.

Quuxplusone commented 3 years ago

__builtin_memcpy does give undefined behavior with gcc when passed null pointer arguments.

Quuxplusone commented 3 years ago

__builtin_memcpy(null, null, C) where C is > 0 is UB.

If we know that C is value > 0, LLVM infers that dst and src ptrs must be nonnull.

__builtin_memcpy(dst, src, dynamicsize) - LLVM in general case does nothing fancy here, in same cases LLVM can prove that dynamicsize is > 0, so it is a same story as C > 0 case.