[LLVMdev] BasicAliasAnalysis and out-of-bound GEP indices

Duncan Sands baldrick at free.fr
Thu Nov 15 01:18:32 PST 2007


Hi,

> Sadly, this will break a very common idiom.  In GCC, we discovered it
> to be common enough that it broke a *bunch* of C code.
> 
> In particular, you will break
> 
> struct foo {
> int a;
> char name[0];
> }
> 
> bar = malloc(sizeof (struct foo) + strlen("thisismyname") + 1);
> strcpy(bar->name, "thisismyname");
> 
> 
> It only started turning up when we started doing higher level loop
> opts and used alias info in dependence testing.  It would end up
> reversing or interchanging loops around these things which while
> legal, broke enough software that we got yelled at.
> 
> So we special case the [0] at end of struct case.

as noted in LangRef,

"Note that 'variable sized arrays' can be implemented in LLVM with a zero
length array. Normally, accesses past the end of an array are undefined in
LLVM (e.g. it is illegal to access the 5th element of a 3 element array). As
a special case, however, zero length arrays are recognized to be variable
length. This allows implementation of 'pascal style arrays' with the LLVM
type "{ i32, [0 x float]}", for example."

so this example should work fine (it wouldn't work if it was char name[1]
though).

Ciao,

Duncan.



More information about the llvm-dev mailing list