Problem with IR code instruction

Hi...

I'm having some trouble with an IR instruction. I'm triying to modify it.

I was trying to create a BinaryOperator "op" with some operands from Instruction "inst":

op = BinaryOperator::Create(Instruction::Add,cast<Value>(inst->getOperand(0)),cast<Value>(r),inst->getNameStr(),inst);

But when executed, my pass throw a segfault and a stacktrace:

../build/Release/bin/clang -emit-llvm -c -I./testprof/ -I./src/headers/ -I../libtommath-0.42.0/ -Wall Wsign-compare -W -Wshadow -Wno-unused-parameter -DLTC_SOURCE -DLTC_NO_ASM -DUSE_LTM -DLTM_DESC -DENCRYPT_ONLY -c src/ciphers/aes/aes.c -o src/ciphers/aes/aes_enc.bc
../build/Release/bin/opt -load ../build/Release/lib/LLVMobfuscation.so -substitution src/ciphers/aes/aes_enc.bc -o src/ciphers/aes/aes_enc.bc -stats
0 opt 0x00000000007f8faf
1 opt 0x00000000007f9419
2 libpthread.so.0 0x00007f2e08fb07e0
3 opt 0x000000000078da7c llvm::BinaryOperator::Create(llvm::Instruction::BinaryOps, llvm::Value*, llvm::Value*, llvm::Twine const&, llvm::Instruction*) + 44
4 LLVMobfuscation.so 0x00007f2e093d7de9
5 opt 0x00000000007a483a llvm::FPPassManager::runOnFunction(llvm::Function&) + 346
6 opt 0x00000000007a4aeb llvm::FPPassManager::runOnModule(llvm::Module&) + 139
7 opt 0x00000000007a4cab llvm::MPPassManager::runOnModule(llvm::Module&) + 315
8 opt 0x00000000007a5247 llvm::PassManagerImpl::run(llvm::Module&) + 279
9 opt 0x00000000007a575a llvm::PassManager::run(llvm::Module&) + 10
10 opt 0x00000000004a67a2 main + 6210
11 libc.so.6 0x00007f2e082bb17d __libc_start_main + 237
12 opt 0x00000000004a09f9
Stack dump:
0. Program arguments: ../build/Release/bin/opt -load ../build/Release/lib/LLVMobfuscation.so -substitution src/ciphers/aes/aes_enc.bc -o src/ciphers/aes/aes_enc.bc -stats
1. Running pass 'Function Pass Manager' on module 'src/ciphers/aes/aes_enc.bc'.
2. Running pass 'operators substitution' on function '@rijndael_enc_setup'
make: *** [src/ciphers/aes/aes_enc.o] Segmentation fault

I looked my code and figured out that inst->getOperand(0) was equal to NULL, so I guess the segfault come from that. errs() << *inst; throw a segfault too :frowning:

So I looked in the ll file and figured it out that the instruction creating the problem was this instruction: %30 = sub nsw i32 %29, 2 (so, getOperand(0) should return %30 and not NULL)

I don't understand why this instruction won't work both with my code and errs()... According to the docs (http://llvm.org/docs/LangRef.html#i_sub) this instruction is perfectly normal...

Thx, ++ (and sorry for my bad english :frowning: )

Hi Rinaldini,

I was trying to create a BinaryOperator "op" with some operands from Instruction "inst":

op = BinaryOperator::Create(Instruction::Add,cast<Value>(inst->getOperand(0)),cast<Value>(r),inst->getNameStr(),inst);

you should remove the casts. In LLVM if you seem to need this kind of cast
then you are doing something wrong.

Ciao, Duncan.