0

Is there a way to disable exceptions when running the lli interpreter? I would like to disable the following from crashing so I can do memory analysis--it currently aborts after a free-after-free error:

The program I am working with is:

#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;
}

which I then convert to LLVM IR and then interpret using 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
  • You can a) report a bug, b) submit a patch, c) live with it or d) replace free with an more tolerant implementation such as `void free(void *) {}`, suited to debugging but less so to production. I don't think option a is fruitful, b might be warmly welcomed, c and d I simply offer for your consideration. – arnt Nov 12 '19 at 09:19
  • Thank you! I used (d) it works – Sameer Lal Nov 12 '19 at 20:37

1 Answers1

0

The lli tool uses just-in-time compilation as the default method of executing LLVM IR code. This is also the case in your example — see llvm::MCJIT::runFunction() in your callstack.

First step is to use interpreter mode.

lli -force-interpreter example_opt.ll
Paweł Bylica
  • 3,780
  • 1
  • 31
  • 44