[llvm-dev] Why is remalloc not marked as noalias?
David Chisnall via llvm-dev
llvm-dev at lists.llvm.org
Mon Jan 8 02:40:32 PST 2018
On 26 Dec 2017, at 10:13, Bhatu via llvm-dev <llvm-dev at lists.llvm.org> wrote:
> According to my understanding, it seems that the result of realloc will not technically alias the original pointer. When the realloc is done in-place the reference says:
> "The original pointer ptr is invalidated and any access to it is undefined behavior (even if reallocation was in-place)."
> Additionally from the C11 standard we have:
> "The value of a pointer becomes indeterminate when the object it points to (or just past) reaches the end of its lifetime"
> Is this enough to infer that we can safely mark realloc as noalias?
Realloc is tricky in practice and has caused us to rethink pointer equality on CHERI. We’ve observed that this is a fairly common idiom:
char *new = realloc(old, someLength);
if (new != old)
// Update some copies of old to use new
This was problematic for us, because old and new had the same address but different bounds information and we were treating comparisons of pointers as equal if they pointed to the same location.
This is problematic with noalias, because if the result of realloc is guaranteed never to alias with any existing pointer then this comparison is trivially true. This won’t break any of the code that I’ve seen, but it will mean that we’re optimising away the fast path and leaving only the slow path (which doesn’t quite accord with how most programmers understand the notion of optimisation).
Note that this code does contain undefined behaviour, but as our prior work has shown, so does pretty much any nontrivial C program.
More information about the llvm-dev