我正在创建一种静态编译的编程语言,并使用 LLVM 作为其后端。我希望我的语言在发生整数溢出时捕获/崩溃。
我知道 llvm.sadd.with.overflow 之类的东西,但我认为这不是最佳/有效的解决方案。该函数返回一个包含两个值的结构,而不仅仅是让我直接访问 OF 寄存器标志。理想情况下,在每次算术运算之后,只要发生整数溢出,我就会有一个“JO”汇编指令来捕获。这正是 clang 的 UndefinedBehaviorSanitizer 所做的。但是,我正在编译为 LLVM IR,而不是 C 或 C++。
如何直接在 LLVM IR 中使用 UndefinedBehaviorSanitizer(或完成等效的操作)来处理整数溢出?
最佳答案
UndefinedBehaviorSanitizer 的作用是生成对 llvm.sadd.with.overflow
的调用。您可以通过使用 -fsanitize=undefined
编译以下 C 程序并查看生成的 LLVM 代码来轻松验证这一点:
黑色:
#include <stdio.h>
int main(void){
int x;
scanf("%d", &x);
printf("%d\n", x+1);
return 0;
}
命令行:
clang -fsanitize=undefined -emit-llvm -O2 -S bla.c
bla.ll(摘录):
%5 = call { i32, i1 } @llvm.sadd.with.overflow.i32(i32 %4, i32 1), !nosanitize !8
%6 = extractvalue { i32, i1 } %5, 0, !nosanitize !8
%7 = extractvalue { i32, i1 } %5, 1, !nosanitize !8
br i1 %7, label %8, label %10, !prof !9, !nosanitize !8
; <label>:8: ; preds = %0
%9 = zext i32 %4 to i64, !nosanitize !8
call void @__ubsan_handle_add_overflow(i8* bitcast ({ { [6 x i8]*, i32, i32 }, { i16, i16, [6 x i8] }* }* @1 to i8*), i64 %9, i64 1) #5, !nosanitize !8
sadd.with.overflow
最终将作为常规 incl
指令¹和 br i1 %7
作为 jo
在生成的 x64 程序集中,所以这正是您想要的。¹ 当然,如果我在 C 代码中添加了 1 以外的内容,这将是一个正确的添加指令。
关于llvm - 使用 LLVM 捕获整数溢出?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/56177325/