如何在llvm ir代码文件中找到所有内存分配? 我尝试编译C++代码的片段: void FuncTest() { int* a = new int; int* b = new int[2]; }

如何在llvm ir代码文件中找到所有内存分配? 我尝试编译C++代码的片段: void FuncTest() { int* a = new int; int* b = new int[2]; },llvm,clang,Llvm,Clang,使用: clang test.cpp -S -emit-llvm -o - > test.llvm 并得出如下结论: define void @_Z8FuncTestv() { entry: %a = alloca i32*, align 4 %b = alloca i32*, align 4 %call = call noalias i8* @_Znwj(i32 4) %0 = bitcast i8* %call to i32* store i32* %0, i32

使用:

clang test.cpp -S -emit-llvm -o - > test.llvm
并得出如下结论:

define void @_Z8FuncTestv() {
entry:
  %a = alloca i32*, align 4
  %b = alloca i32*, align 4
  %call = call noalias i8* @_Znwj(i32 4)
  %0 = bitcast i8* %call to i32*
  store i32* %0, i32** %a, align 4
  %call1 = call noalias i8* @_Znaj(i32 8)
  %1 = bitcast i8* %call1 to i32*
  store i32* %1, i32** %b, align 4
  ret void
}

declare noalias i8* @_Znwj(i32)
declare noalias i8* @_Znaj(i32)
我现在想知道的是:
\u Znwj
\u Znaj
符号来自哪里?它们是随机分配的还是有系统的?我想说的是:

%call = call noalias i8* @_Znwj(i32 4)

执行内存分配。但它看起来并不那么有希望


这里有一些LLVM专家有一个想法?

< p>你看到操作员的C++被忽略的名字。使用Demangle符号,或建立一个损坏符号表。new/delete运算符可能会重载,因此符号不是常量。需求可能是最安全的选择

这是通过c++filt传输的函数,c++filt反过来使用:

定义void@FuncTest()(){ 条目: %a=alloca i32*,对齐4 %b=alloca i32*,对齐4 %call=call noalias i8*@运算符新(无符号整数)(i32 4) %0=比特广播i8*%调用i32* 存储i32*%0,i32**%a,对齐4 %call1=调用noalias i8*@运算符new[](无符号整数)(i32 8) %1=比特广播i8*%call1到i32* 存储i32*%1,i32**%b,对齐4 ret void } 声明noalias i8*@运算符new(unsigned int)(i32) 声明noalias i8*@运算符new[](无符号整数)(i32)
您可以通过isAllocationFn检查函数是否正在分配内存。这似乎对我有用

资料来源:


PD:我正在回答标题上的问题,这正是我来到这里的原因。

我可以补充说,如果有一种方法可以从clang/llvm工具链过程中知道这一点(内存分配的位置),那也没问题,请告诉我如何做。
%call1 = call noalias i8* @_Znaj(i32 8)
define void @FuncTest()() { entry: %a = alloca i32*, align 4 %b = alloca i32*, align 4 %call = call noalias i8* @operator new(unsigned int)(i32 4) %0 = bitcast i8* %call to i32* store i32* %0, i32** %a, align 4 %call1 = call noalias i8* @operator new[](unsigned int)(i32 8) %1 = bitcast i8* %call1 to i32* store i32* %1, i32** %b, align 4 ret void } declare noalias i8* @operator new(unsigned int)(i32) declare noalias i8* @operator new[](unsigned int)(i32)