【发布时间】:2019-11-12 07:05:49
【问题描述】:
有没有办法在运行 lli 解释器时禁用异常? 我想禁用以下崩溃,以便我可以进行内存分析 - 它目前在释放后释放错误后中止:
我正在使用的程序是:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int *ptr;
int *ptr2;
int z = 10;
int n = 5;
ptr = (int*)malloc(n * sizeof(int));
// ptr2 = (int*)malloc(z * sizeof(int));
free(ptr);
free(ptr);
return 0;
}
然后我将其转换为 LLVM IR,然后使用 lli 进行解释:
$ /usr/local/opt/llvm/bin/lli example_opt.ll > out.txt
lli(23782,0x7fff9934a380) malloc: *** error for object 0x7f9bee411780: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug
Stack dump:
0. Program arguments: /usr/local/opt/llvm/bin/lli example_opt.ll
0 lli 0x0000000103a76be8 llvm::sys::PrintStackTrace(llvm::raw_ostream&) + 37
1 lli 0x0000000103a76fe6 SignalHandler(int) + 200
2 libsystem_platform.dylib 0x00007fff60c13f5a _sigtramp + 26
3 libsystem_c.dylib 0x00007fff6099290f __sfvwrite + 407
4 libsystem_c.dylib 0x00007fff609b11ae abort + 127
5 libsystem_malloc.dylib 0x00007fff60aaf8a6 free + 521
6 libsystem_malloc.dylib 0x0000000104c4e080 free + 18446603343269325283
7 lli 0x000000010378708d llvm::MCJIT::runFunction(llvm::Function*, llvm::ArrayRef<llvm::GenericValue>) + 861
8 lli 0x00000001037114e7 llvm::ExecutionEngine::runFunctionAsMain(llvm::Function*, std::__1::vector<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::allocator<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > > const&, char const* const*) + 1159
9 lli 0x0000000103407a29 main + 8473
10 libdyld.dylib 0x00007fff60905015 start + 1
11 libdyld.dylib 0x0000000000000002 start + 18446603338896093166
Abort trap: 6
【问题讨论】:
-
您可以 a) 报告错误,b) 提交补丁,c) 使用它或 d) 用更宽容的实现替换免费,例如
void free(void *) {},适合调试但不太适合生产。我不认为选项 a 是富有成效的,b 可能会受到热烈欢迎,c 和 d 我只是提供给您考虑。 -
谢谢!我用过(d)它有效
标签: memory-leaks llvm llvm-clang llvm-ir lli