简体   繁体   中英

How to check overflow through LLVM IR

I have no knowledge about compiler and very very limited knowledge in C++ and LLVM, now I got stuck on an important stage.

My question is : how to use llvm with C++ to check whether there is overflow?

This is the source code:

int
main(int argc, char **argv) {
  char buffer[4] = { 0, 0, 0, 0 };
  return buffer[2];
}

This is the IR code:

define i32 @main(i32, i8**) #0 !dbg !6 {
  %3 = alloca [4 x i8], align 1
  call void @llvm.dbg.value(metadata i32 %0, i64 0, metadata !13, metadata !14), !dbg !15
  call void @llvm.dbg.value(metadata i8** %1, i64 0, metadata !16, metadata !14), !dbg !17
  call void @llvm.dbg.declare(metadata [4 x i8]* %3, metadata !18, metadata !14), !dbg !22
  %4 = bitcast [4 x i8]* %3 to i8*, !dbg !22
  call void @llvm.memset.p0i8.i64(i8* %4, i8 0, i64 4, i32 1, i1 false), !dbg !22
  %5 = getelementptr inbounds [4 x i8], [4 x i8]* %3, i64 0, i64 2, !dbg !23
  %6 = load i8, i8* %5, align 1, !dbg !23
  %7 = sext i8 %6 to i32, !dbg !23
  ret i32 %7, !dbg !24
}

I think in order to do this check, need to use CallSite getInstructions(), and it seems that the answer is in that load instruction, but load is in %6, while the parameters are in %5, I don't know:

  1. How to find an instruction is load

  2. Even if I could find load instruction, how can I jump to %5 to compare parameters and decide whether it's overflow?

  1. To check if instruction is load, you can do LoadInst* load = dyn_cast<LoadInst>(instruction) if (load != nullptr) {...}

  2. To get %5, call getPointerOperand() on the LoadInst, it will return %5 in this case

我建议您看一下伊利诺伊大学的SAFECode项目。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM