bscarlet / llvm-general

Rich LLVM bindings for Haskell (with transfer of LLVM IR to and from C++, detailed compilation pass control, etc.)
http://hackage.haskell.org/package/llvm-general
132 stars 38 forks source link

Segmentation fault with GEP and null address #104

Closed hberntsen closed 10 years ago

hberntsen commented 10 years ago

When you add the following code to llvm-general/test/LLVM/General/Test/Instructions.hs in the list of tests, the tests will crash with a segmentation fault:

          ("GEP2",
            GetElementPtr {
                inBounds = False,
                address = ConstantOperand $ C.Null (IntegerType 32),
                indices = [ConstantOperand $ C.Int 32 1],
                metadata = []
           },
           "getelementptr i32* null, i32 1"),

(I am using version v3.4.2.2 with LLVM 3.4 on Ubuntu 14.04) I wanted to try the trick described at http://nondot.org/sabre/LLVMNotes/SizeOf-OffsetOf-VariableSizedStructs.txt to let llvm calculate sizes in my compiler. I tested with GHC 7.8.2 and 7.6.3 and both give a segmentation fault for this test.

gdb tells:

Program received signal SIGSEGV, Segmentation fault.
0x00007ffff6a046af in llvm::CompositeType::getTypeAtIndex(llvm::Value const*) () from /usr/lib/x86_64-linux-gnu/libLLVM-3.4.so.1

When using (ArrayType 2 (PointerType (IntegerType 32) (AddrSpace 0)):

Program terminated with signal SIGSEGV, Segmentation fault.
#0  0x00007f9ca0a7ec44 in llvm::PointerType::get(llvm::Type*, unsigned int) () from /usr/lib/x86_64-linux-gnu/libLLVM-3.4.so.1
bscarlet commented 10 years ago

The type used with C.Null needs to be a pointer type - e.g. C.Null (PointerType (IntegerType 32) 0). You might consider using a build of llvm with asserts turned on, at least for development. It'll give you informative messages in some cases like this, rather than just dying.

hberntsen commented 10 years ago

Thanks for you quick response. That indeed solves my problem. I did not expect a segmentation fault with the wrong AST, apparently that is how LLVM works. Makes sense though, all the assertions decrease compiling speed.