llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
28.81k stars 11.91k forks source link

opt, llc and clang++ version 18: program crashes when optimizing bitcode file #69110

Closed bluefish43 closed 1 year ago

bluefish43 commented 1 year ago

The opt utlity crashes when optimizing the bitcode file:

$ target/debug/apolloc build main.ap -o main.ll --output-type=llvm-ir -O3 -v
VERBOSE Reading input file `main.ap`
VERBOSE Creating directory `./tmp`
VERBOSE Writing output bitcode to `./tmp/main.bc`
VERBOSE Executing command `opt ./tmp/main.bc -o ./tmp/opt.bc -O3`
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace.
Stack dump:
0.      Program arguments: opt ./tmp/main.bc -o ./tmp/opt.bc -O3
 #0 0x00007f473ea3a446 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/usr/lib/llvm-18/bin/../lib/libLLVM-18.so.1+0xd2c446)
 #1 0x00007f473ea38400 llvm::sys::RunSignalHandlers() (/usr/lib/llvm-18/bin/../lib/libLLVM-18.so.1+0xd2a400)
 #2 0x00007f473ea3ab0b (/usr/lib/llvm-18/bin/../lib/libLLVM-18.so.1+0xd2cb0b)
 #3 0x00007f473d7e2520 (/lib/x86_64-linux-gnu/libc.so.6+0x42520)
 #4 0x00007f473ebccc07 llvm::StructType::getTypeAtIndex(llvm::Value const*) const (/usr/lib/llvm-18/bin/../lib/libLLVM-18.so.1+0xebec07)
 #5 0x00007f473f46801e (/usr/lib/llvm-18/bin/../lib/libLLVM-18.so.1+0x175a01e)
 #6 0x00007f473f460e77 (/usr/lib/llvm-18/bin/../lib/libLLVM-18.so.1+0x1752e77)
 #7 0x00007f473f4615f8 (/usr/lib/llvm-18/bin/../lib/libLLVM-18.so.1+0x17535f8)
 #8 0x00007f473eba3c87 llvm::Module::materializeAll() (/usr/lib/llvm-18/bin/../lib/libLLVM-18.so.1+0xe95c87)
 #9 0x00007f473f458725 llvm::BitcodeModule::getModuleImpl(llvm::LLVMContext&, bool, bool, bool, llvm::ParserCallbacks) (/usr/lib/llvm-18/bin/../lib/libLLVM-18.so.1+0x174a725)
#10 0x00007f473f45f04a llvm::BitcodeModule::parseModule(llvm::LLVMContext&, llvm::ParserCallbacks) (/usr/lib/llvm-18/bin/../lib/libLLVM-18.so.1+0x175104a)
#11 0x00007f473f45f241 llvm::parseBitcodeFile(llvm::MemoryBufferRef, llvm::LLVMContext&, llvm::ParserCallbacks) (/usr/lib/llvm-18/bin/../lib/libLLVM-18.so.1+0x1751241)
#12 0x00007f473ec42cd9 llvm::parseIR(llvm::MemoryBufferRef, llvm::SMDiagnostic&, llvm::LLVMContext&, llvm::ParserCallbacks) (/usr/lib/llvm-18/bin/../lib/libLLVM-18.so.1+0xf34cd9)
#13 0x00007f473ec4326d llvm::parseIRFile(llvm::StringRef, llvm::SMDiagnostic&, llvm::LLVMContext&, llvm::ParserCallbacks) (/usr/lib/llvm-18/bin/../lib/libLLVM-18.so.1+0xf3526d)
#14 0x0000563c2aec7e66 main (/usr/lib/llvm-18/bin/opt+0x29e66)
#15 0x00007f473d7c9d90 __libc_start_call_main ./csu/../sysdeps/nptl/libc_start_call_main.h:58:16
#16 0x00007f473d7c9e40 call_init ./csu/../csu/libc-start.c:128:20
#17 0x00007f473d7c9e40 __libc_start_main ./csu/../csu/libc-start.c:379:5
#18 0x0000563c2aeb5f65 _start (/usr/lib/llvm-18/bin/opt+0x17f65)
VERBOSE Removing directory `./tmp`
Error: opt was terminated by a signal

llc and clang++ crash aswell:

$ target/debug/apolloc build main.ap -o main.ll --output-type=s -O3 -v
VERBOSE Reading input file `main.ap`
VERBOSE Creating directory `./tmp`
VERBOSE Writing output bitcode to `./tmp/main.bc`
VERBOSE Executing command `llc ./tmp/main.bc -o main.ll -O3`
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace.
Stack dump:
0.      Program arguments: llc ./tmp/main.bc -o main.ll -O3
 #0 0x00007fb8d441a446 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/usr/lib/llvm-18/bin/../lib/libLLVM-18.so.1+0xd2c446)
 #1 0x00007fb8d4418400 llvm::sys::RunSignalHandlers() (/usr/lib/llvm-18/bin/../lib/libLLVM-18.so.1+0xd2a400)
 #2 0x00007fb8d441ab0b (/usr/lib/llvm-18/bin/../lib/libLLVM-18.so.1+0xd2cb0b)
 #3 0x00007fb8d31c2520 (/lib/x86_64-linux-gnu/libc.so.6+0x42520)
 #4 0x00007fb8d45acc07 llvm::StructType::getTypeAtIndex(llvm::Value const*) const (/usr/lib/llvm-18/bin/../lib/libLLVM-18.so.1+0xebec07)
 #5 0x00007fb8d4e4801e (/usr/lib/llvm-18/bin/../lib/libLLVM-18.so.1+0x175a01e)
 #6 0x00007fb8d4e40e77 (/usr/lib/llvm-18/bin/../lib/libLLVM-18.so.1+0x1752e77)
 #7 0x00007fb8d4e415f8 (/usr/lib/llvm-18/bin/../lib/libLLVM-18.so.1+0x17535f8)
 #8 0x00007fb8d4583c87 llvm::Module::materializeAll() (/usr/lib/llvm-18/bin/../lib/libLLVM-18.so.1+0xe95c87)
 #9 0x00007fb8d4e38725 llvm::BitcodeModule::getModuleImpl(llvm::LLVMContext&, bool, bool, bool, llvm::ParserCallbacks) (/usr/lib/llvm-18/bin/../lib/libLLVM-18.so.1+0x174a725)
#10 0x00007fb8d4e3f04a llvm::BitcodeModule::parseModule(llvm::LLVMContext&, llvm::ParserCallbacks) (/usr/lib/llvm-18/bin/../lib/libLLVM-18.so.1+0x175104a)
#11 0x00007fb8d4e3f241 llvm::parseBitcodeFile(llvm::MemoryBufferRef, llvm::LLVMContext&, llvm::ParserCallbacks) (/usr/lib/llvm-18/bin/../lib/libLLVM-18.so.1+0x1751241)
#12 0x00007fb8d4622cd9 llvm::parseIR(llvm::MemoryBufferRef, llvm::SMDiagnostic&, llvm::LLVMContext&, llvm::ParserCallbacks) (/usr/lib/llvm-18/bin/../lib/libLLVM-18.so.1+0xf34cd9)
#13 0x00007fb8d462326d llvm::parseIRFile(llvm::StringRef, llvm::SMDiagnostic&, llvm::LLVMContext&, llvm::ParserCallbacks) (/usr/lib/llvm-18/bin/../lib/libLLVM-18.so.1+0xf3526d)
#14 0x0000555bd89a304f main (/usr/lib/llvm-18/bin/llc+0x1004f)
#15 0x00007fb8d31a9d90 __libc_start_call_main ./csu/../sysdeps/nptl/libc_start_call_main.h:58:16
#16 0x00007fb8d31a9e40 call_init ./csu/../csu/libc-start.c:128:20
#17 0x00007fb8d31a9e40 __libc_start_main ./csu/../csu/libc-start.c:379:5
#18 0x0000555bd899f3c5 _start (/usr/lib/llvm-18/bin/llc+0xc3c5)
Error: llc was terminated by a signal
$ target/debug/apolloc build main.ap -o main.ll --output-type=obj -O3 -v
VERBOSE Reading input file `main.ap`
VERBOSE Creating directory `./tmp`
VERBOSE Writing output bitcode to `./tmp/main.bc`
VERBOSE Executing command `clang++ ./tmp/main.bc -o main.ll -O3 -c`
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace, preprocessed source, and associated run script.
Stack dump:
0.      Program arguments: clang++ ./tmp/main.bc -o main.ll -O3 -c
 #0 0x00007f45287ac446 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/usr/lib/llvm-18/bin/../lib/libLLVM-18.so.1+0xd2c446)
 #1 0x00007f45287aa400 llvm::sys::RunSignalHandlers() (/usr/lib/llvm-18/bin/../lib/libLLVM-18.so.1+0xd2a400)
 #2 0x00007f45286fb8a0 (/usr/lib/llvm-18/bin/../lib/libLLVM-18.so.1+0xc7b8a0)
 #3 0x00007f4527552520 (/lib/x86_64-linux-gnu/libc.so.6+0x42520)
 #4 0x00007f452893ec07 llvm::StructType::getTypeAtIndex(llvm::Value const*) const (/usr/lib/llvm-18/bin/../lib/libLLVM-18.so.1+0xebec07)
 #5 0x00007f45291da01e (/usr/lib/llvm-18/bin/../lib/libLLVM-18.so.1+0x175a01e)
 #6 0x00007f45291d2e77 (/usr/lib/llvm-18/bin/../lib/libLLVM-18.so.1+0x1752e77)
 #7 0x00007f45291d35f8 (/usr/lib/llvm-18/bin/../lib/libLLVM-18.so.1+0x17535f8)
 #8 0x00007f4528915c87 llvm::Module::materializeAll() (/usr/lib/llvm-18/bin/../lib/libLLVM-18.so.1+0xe95c87)
 #9 0x00007f45291ca725 llvm::BitcodeModule::getModuleImpl(llvm::LLVMContext&, bool, bool, bool, llvm::ParserCallbacks) (/usr/lib/llvm-18/bin/../lib/libLLVM-18.so.1+0x174a725)
#10 0x00007f45291d104a llvm::BitcodeModule::parseModule(llvm::LLVMContext&, llvm::ParserCallbacks) (/usr/lib/llvm-18/bin/../lib/libLLVM-18.so.1+0x175104a)
#11 0x00007f45291d1241 llvm::parseBitcodeFile(llvm::MemoryBufferRef, llvm::LLVMContext&, llvm::ParserCallbacks) (/usr/lib/llvm-18/bin/../lib/libLLVM-18.so.1+0x1751241)
#12 0x00007f45289b4cd9 llvm::parseIR(llvm::MemoryBufferRef, llvm::SMDiagnostic&, llvm::LLVMContext&, llvm::ParserCallbacks) (/usr/lib/llvm-18/bin/../lib/libLLVM-18.so.1+0xf34cd9)
#13 0x00007f453127d80d clang::CodeGenAction::loadModule(llvm::MemoryBufferRef) (/usr/lib/llvm-18/bin/../lib/libclang-cpp.so.18+0x1e3480d)
#14 0x00007f453127e492 clang::CodeGenAction::ExecuteAction() (/usr/lib/llvm-18/bin/../lib/libclang-cpp.so.18+0x1e35492)
#15 0x00007f4531cc8785 clang::FrontendAction::Execute() (/usr/lib/llvm-18/bin/../lib/libclang-cpp.so.18+0x287f785)
#16 0x00007f4531c42014 clang::CompilerInstance::ExecuteAction(clang::FrontendAction&) (/usr/lib/llvm-18/bin/../lib/libclang-cpp.so.18+0x27f9014)
#17 0x00007f4531d421d0 clang::ExecuteCompilerInvocation(clang::CompilerInstance*) (/usr/lib/llvm-18/bin/../lib/libclang-cpp.so.18+0x28f91d0)
#18 0x0000565440d26191 cc1_main(llvm::ArrayRef<char const*>, char const*, void*) (/usr/lib/llvm-18/bin/clang+0x13191)
#19 0x0000565440d236c5 (/usr/lib/llvm-18/bin/clang+0x106c5)
#20 0x00007f4531903439 (/usr/lib/llvm-18/bin/../lib/libclang-cpp.so.18+0x24ba439)
#21 0x00007f45286fb63c llvm::CrashRecoveryContext::RunSafely(llvm::function_ref<void ()>) (/usr/lib/llvm-18/bin/../lib/libLLVM-18.so.1+0xc7b63c)
#22 0x00007f4531902dae clang::driver::CC1Command::Execute(llvm::ArrayRef<std::optional<llvm::StringRef>>, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>*, bool*) const (/usr/lib/llvm-18/bin/../lib/libclang-cpp.so.18+0x24b9dae)
#23 0x00007f45318cb8a1 clang::driver::Compilation::ExecuteCommand(clang::driver::Command const&, clang::driver::Command const*&, bool) const (/usr/lib/llvm-18/bin/../lib/libclang-cpp.so.18+0x24828a1)
#24 0x00007f45318cbaee clang::driver::Compilation::ExecuteJobs(clang::driver::JobList const&, llvm::SmallVectorImpl<std::pair<int, clang::driver::Command const*>>&, bool) const (/usr/lib/llvm-18/bin/../lib/libclang-cpp.so.18+0x2482aee)
#25 0x00007f45318e74bc clang::driver::Driver::ExecuteCompilation(clang::driver::Compilation&, llvm::SmallVectorImpl<std::pair<int, clang::driver::Command const*>>&) (/usr/lib/llvm-18/bin/../lib/libclang-cpp.so.18+0x249e4bc)
#26 0x0000565440d23024 clang_main(int, char**, llvm::ToolContext const&) (/usr/lib/llvm-18/bin/clang+0x10024)
#27 0x0000565440d300f2 main (/usr/lib/llvm-18/bin/clang+0x1d0f2)
#28 0x00007f4527539d90 __libc_start_call_main ./csu/../sysdeps/nptl/libc_start_call_main.h:58:16
#29 0x00007f4527539e40 call_init ./csu/../csu/libc-start.c:128:20
#30 0x00007f4527539e40 __libc_start_main ./csu/../csu/libc-start.c:379:5
#31 0x0000565440d200d5 _start (/usr/lib/llvm-18/bin/clang+0xd0d5)
clang++: error: clang frontend command failed with exit code 139 (use -v to see invocation)
Ubuntu clang version 18.0.0 (++20231014042238+64e7207ea5c7-1~exp1~20231014042353.1244)
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/lib/llvm-18/bin
clang++: note: diagnostic msg: Error generating preprocessed source(s) - no preprocessable inputs.
VERBOSE Removing directory `./tmp`
Error: clang++ exited with non-zero exit code: 1

And this is the input file, "main.ap", that is outputting the bitcode that makes opt, llc and clang++ crash:

extern "C" function printf(_format: string, ...): i32
extern "C" function strlen(__s: string): u64
extern "C" function malloc(size: u64): *none
extern "C" function free(ptr: *none): *none

struct String {
    ptr: *u8,
    len: u32,
    cap: u32,
}

function disposeString(input: String): none {
    free(input.ptr as *none);
}

struct Person {
    name: string,
    age: i32,
}

function main(): i32 {
    var b = Person {
        name: "Hello",
        age: -13,
    };
    printf("Hello, world!\n");
    return 0;
}

This seems to be a problem specifically about using GEP on a struct field, because if you use the code below as the input file instead:

extern "C" function printf(_format: string, ...): i32

function main(): i32 {
    printf("Hello, world!\n");
    return 0;
}

It runs successfully:

$ target/debug/apolloc build main.ap -o main.ll --output-type=llvm-ir -O3 -v
VERBOSE Reading input file `main.ap`
VERBOSE Creating directory `./tmp`
VERBOSE Writing output bitcode to `./tmp/main.bc`
VERBOSE Executing command `opt ./tmp/main.bc -o ./tmp/opt.bc -O3`
VERBOSE Executing command `llvm-dis ./tmp/opt.bc -o main.ll`
VERBOSE Removing directory `./tmp`

And generates this output file:

; ModuleID = './tmp/opt.bc'
source_filename = "main.ap"
target triple = "x86_64-linux-gnu"

@0 = private unnamed_addr constant [15 x i8] c"Hello, world!\0A\00", align 1

declare i32 @__gxx_personality_v0(...)

declare void @exit(i32) local_unnamed_addr

; Function Attrs: nofree nounwind
declare noundef i32 @printf(ptr nocapture noundef readonly, ...) local_unnamed_addr #0

define i32 @main() local_unnamed_addr {
  %1 = tail call i32 @printf(ptr nonnull @0)
  ret i32 0
}

; Function Attrs: noreturn
define i32 @__apolloc_rt_entrypoint(i32 %0, ptr nocapture readnone %1, ptr nocapture readnone %2) local_unnamed_addr #1 personality ptr @__gxx_personality_v0 {
  %4 = tail call i32 @printf(ptr nonnull @0)
  tail call void @exit(i32 0)
  unreachable
}

attributes #0 = { nofree nounwind }
attributes #1 = { noreturn }

And this is the minimal code example that generates that error:

extern "C" function free(ptr: *none): *none

struct String {
    ptr: *u8,
    len: u32,
    cap: u32,
}

function disposeString(input: String): none {
    free(input.ptr as *none);
}

function main(): i32 {
    return 0;
}
dtcxzyw commented 1 year ago

Could you please provide the LLVM IR file that reproduces this crash?

bluefish43 commented 1 year ago

I unfortunately am not able to get the ir from the bitcode module itself because llvm-dis crashes when I try to disassemble it, getting a segmentation fault:

$ llvm-dis ./tmp/main.bc -o main.ll
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace.
Stack dump:
0.      Program arguments: llvm-dis ./tmp/main.bc -o main.ll
 #0 0x00007f8f523fa446 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/usr/lib/llvm-18/bin/../lib/libLLVM-18.so.1+0xd2c446)
 #1 0x00007f8f523f8400 llvm::sys::RunSignalHandlers() (/usr/lib/llvm-18/bin/../lib/libLLVM-18.so.1+0xd2a400)
 #2 0x00007f8f523fab0b (/usr/lib/llvm-18/bin/../lib/libLLVM-18.so.1+0xd2cb0b)
 #3 0x00007f8f511a2520 (/lib/x86_64-linux-gnu/libc.so.6+0x42520)
 #4 0x00007f8f5258cc07 llvm::StructType::getTypeAtIndex(llvm::Value const*) const (/usr/lib/llvm-18/bin/../lib/libLLVM-18.so.1+0xebec07)
 #5 0x00007f8f52e2801e (/usr/lib/llvm-18/bin/../lib/libLLVM-18.so.1+0x175a01e)
 #6 0x00007f8f52e20e77 (/usr/lib/llvm-18/bin/../lib/libLLVM-18.so.1+0x1752e77)
 #7 0x00007f8f52e215f8 (/usr/lib/llvm-18/bin/../lib/libLLVM-18.so.1+0x17535f8)
 #8 0x00007f8f52563c87 llvm::Module::materializeAll() (/usr/lib/llvm-18/bin/../lib/libLLVM-18.so.1+0xe95c87)
 #9 0x0000558f6ed1d870 (/usr/lib/llvm-18/bin/llvm-dis+0x5870)
#10 0x00007f8f51189d90 __libc_start_call_main ./csu/../sysdeps/nptl/libc_start_call_main.h:58:16
#11 0x00007f8f51189e40 call_init ./csu/../csu/libc-start.c:128:20
#12 0x00007f8f51189e40 __libc_start_main ./csu/../csu/libc-start.c:379:5
#13 0x0000558f6ed1bcb5 (/usr/lib/llvm-18/bin/llvm-dis+0x3cb5)
Segmentation fault (core dumped)
dtcxzyw commented 1 year ago

You can just update the llvm bitcode :)

bluefish43 commented 1 year ago

Okay. I am uploading it with .jpg and one with .json extension because GitHub is not allowing me to publish files with the .bc extension and I don't know if any of these will work. main main.json

bluefish43 commented 1 year ago

Okay. I am uploading it with .jpg and one with .json extension because GitHub is not allowing me to publish files with the .bc extension and I don't know if any of these will work. main main.json

I just tried it and just the .json file works.

dtcxzyw commented 1 year ago

The low-level records dumped by llvm-bcanalyzer:

<IDENTIFICATION_BLOCK_ID NumWords=5 BlockCodeSize=5>
  <STRING abbrevid=4 op0=76 op1=76 op2=86 op3=77 op4=49 op5=56 op6=46 op7=48 op8=46 op9=48/> record string = 'LLVM18.0.0'
  <EPOCH abbrevid=5 op0=0/>
</IDENTIFICATION_BLOCK_ID>
<MODULE_BLOCK NumWords=407 BlockCodeSize=3>
  <VERSION op0=2/>
  <BLOCKINFO_BLOCK/>
  <TYPE_BLOCK_ID NumWords=19 BlockCodeSize=4>
    <NUMENTRY op0=16/>
    <UnknownCode25 abbrevid=4 op0=0/>
    <VOID/>
    <FUNCTION abbrevid=5 op0=0 op1=1 op2=0/>
    <UnknownCode25 abbrevid=4 op0=0/>
    <INTEGER op0=32/>
    <FUNCTION abbrevid=5 op0=1 op1=4/>
    <INTEGER op0=32/>
    <FUNCTION abbrevid=5 op0=0 op1=1 op2=6/>
    <FUNCTION abbrevid=5 op0=0 op1=0 op2=0/>
    <STRUCT_NAME abbrevid=7 op0=83 op1=116 op2=114 op3=105 op4=110 op5=103/> record string = 'String'
    <STRUCT_NAMED abbrevid=8 op0=0 op1=0 op2=6 op3=6/>
    <FUNCTION abbrevid=5 op0=0 op1=1 op2=9/>
    <FUNCTION abbrevid=5 op0=0 op1=6/>
    <FUNCTION abbrevid=5 op0=0 op1=6 op2=6 op3=0 op4=0/>
    <METADATA/>
    <LABEL/>
    <STRUCT_ANON abbrevid=6 op0=0 op1=0/>
  </TYPE_BLOCK_ID>
  <TRIPLE op0=120 op1=56 op2=54 op3=95 op4=54 op5=52 op6=45 op7=108 op8=105 op9=110 op10=117 op11=120 op12=45 op13=103 op14=110 op15=117/>
  <SOURCE_FILENAME abbrevid=4 op0=109 op1=97 op2=105 op3=110 op4=46 op5=97 op6=112/> record string = 'main.ap'
  <FUNCTION op0=0 op1=22 op2=2 op3=0 op4=1 op5=0 op6=0 op7=0 op8=0 op9=0 op10=0 op11=0 op12=0 op13=0 op14=0 op15=0 op16=0 op17=0 op18=0 op19=22 op20=0/>
  <FUNCTION op0=22 op1=20 op2=5 op3=0 op4=1 op5=0 op6=0 op7=0 op8=0 op9=0 op10=0 op11=0 op12=0 op13=0 op14=0 op15=0 op16=0 op17=0 op18=0 op19=22 op20=0/>
  <FUNCTION op0=42 op1=4 op2=7 op3=0 op4=1 op5=0 op6=0 op7=0 op8=0 op9=0 op10=0 op11=0 op12=0 op13=0 op14=0 op15=0 op16=0 op17=0 op18=0 op19=22 op20=0/>
  <FUNCTION op0=46 op1=4 op2=8 op3=0 op4=1 op5=0 op6=0 op7=0 op8=0 op9=0 op10=0 op11=0 op12=0 op13=0 op14=0 op15=0 op16=0 op17=0 op18=0 op19=22 op20=0/>
  <FUNCTION op0=50 op1=13 op2=10 op3=0 op4=0 op5=0 op6=0 op7=0 op8=0 op9=0 op10=0 op11=0 op12=0 op13=0 op14=0 op15=0 op16=0 op17=0 op18=0 op19=22 op20=0/>
  <FUNCTION op0=63 op1=4 op2=11 op3=0 op4=0 op5=0 op6=0 op7=0 op8=0 op9=0 op10=0 op11=0 op12=0 op13=0 op14=0 op15=0 op16=0 op17=0 op18=0 op19=22 op20=0/>
  <FUNCTION op0=67 op1=23 op2=12 op3=0 op4=0 op5=0 op6=0 op7=0 op8=0 op9=0 op10=0 op11=0 op12=0 op13=0 op14=0 op15=0 op16=2 op17=0 op18=0 op19=22 op20=0/>
  <VSTOFFSET abbrevid=5 op0=410/>
  <CONSTANTS_BLOCK NumWords=5 BlockCodeSize=4>
    <SETTYPE abbrevid=4 op0=0/>
    <NULL/>
  </CONSTANTS_BLOCK>
  <METADATA_KIND_BLOCK NumWords=193 BlockCodeSize=3>
    <KIND op0=0 op1=100 op2=98 op3=103/>
    <KIND op0=1 op1=116 op2=98 op3=97 op4=97/>
    <KIND op0=2 op1=112 op2=114 op3=111 op4=102/>
    <KIND op0=3 op1=102 op2=112 op3=109 op4=97 op5=116 op6=104/>
    <KIND op0=4 op1=114 op2=97 op3=110 op4=103 op5=101/>
    <KIND op0=5 op1=116 op2=98 op3=97 op4=97 op5=46 op6=115 op7=116 op8=114 op9=117 op10=99 op11=116/>
    <KIND op0=6 op1=105 op2=110 op3=118 op4=97 op5=114 op6=105 op7=97 op8=110 op9=116 op10=46 op11=108 op12=111 op13=97 op14=100/>
    <KIND op0=7 op1=97 op2=108 op3=105 op4=97 op5=115 op6=46 op7=115 op8=99 op9=111 op10=112 op11=101/>
    <KIND op0=8 op1=110 op2=111 op3=97 op4=108 op5=105 op6=97 op7=115/>
    <KIND op0=9 op1=110 op2=111 op3=110 op4=116 op5=101 op6=109 op7=112 op8=111 op9=114 op10=97 op11=108/>
    <KIND op0=10 op1=108 op2=108 op3=118 op4=109 op5=46 op6=109 op7=101 op8=109 op9=46 op10=112 op11=97 op12=114 op13=97 op14=108 op15=108 op16=101 op17=108 op18=95 op19=108 op20=111 op21=111 op22=112 op23=95 op24=97 op25=99 op26=99 op27=101 op28=115 op29=115/>
    <KIND op0=11 op1=110 op2=111 op3=110 op4=110 op5=117 op6=108 op7=108/>
    <KIND op0=12 op1=100 op2=101 op3=114 op4=101 op5=102 op6=101 op7=114 op8=101 op9=110 op10=99 op11=101 op12=97 op13=98 op14=108 op15=101/>
    <KIND op0=13 op1=100 op2=101 op3=114 op4=101 op5=102 op6=101 op7=114 op8=101 op9=110 op10=99 op11=101 op12=97 op13=98 op14=108 op15=101 op16=95 op17=111 op18=114 op19=95 op20=110 op21=117 op22=108 op23=108/>
    <KIND op0=14 op1=109 op2=97 op3=107 op4=101 op5=46 op6=105 op7=109 op8=112 op9=108 op10=105 op11=99 op12=105 op13=116/>
    <KIND op0=15 op1=117 op2=110 op3=112 op4=114 op5=101 op6=100 op7=105 op8=99 op9=116 op10=97 op11=98 op12=108 op13=101/>
    <KIND op0=16 op1=105 op2=110 op3=118 op4=97 op5=114 op6=105 op7=97 op8=110 op9=116 op10=46 op11=103 op12=114 op13=111 op14=117 op15=112/>
    <KIND op0=17 op1=97 op2=108 op3=105 op4=103 op5=110/>
    <KIND op0=18 op1=108 op2=108 op3=118 op4=109 op5=46 op6=108 op7=111 op8=111 op9=112/>
    <KIND op0=19 op1=116 op2=121 op3=112 op4=101/>
    <KIND op0=20 op1=115 op2=101 op3=99 op4=116 op5=105 op6=111 op7=110 op8=95 op9=112 op10=114 op11=101 op12=102 op13=105 op14=120/>
    <KIND op0=21 op1=97 op2=98 op3=115 op4=111 op5=108 op6=117 op7=116 op8=101 op9=95 op10=115 op11=121 op12=109 op13=98 op14=111 op15=108/>
    <KIND op0=22 op1=97 op2=115 op3=115 op4=111 op5=99 op6=105 op7=97 op8=116 op9=101 op10=100/>
    <KIND op0=23 op1=99 op2=97 op3=108 op4=108 op5=101 op6=101 op7=115/>
    <KIND op0=24 op1=105 op2=114 op3=114 op4=95 op5=108 op6=111 op7=111 op8=112/>
    <KIND op0=25 op1=108 op2=108 op3=118 op4=109 op5=46 op6=97 op7=99 op8=99 op9=101 op10=115 op11=115 op12=46 op13=103 op14=114 op15=111 op16=117 op17=112/>
    <KIND op0=26 op1=99 op2=97 op3=108 op4=108 op5=98 op6=97 op7=99 op8=107/>
    <KIND op0=27 op1=108 op2=108 op3=118 op4=109 op5=46 op6=112 op7=114 op8=101 op9=115 op10=101 op11=114 op12=118 op13=101 op14=46 op15=97 op16=99 op17=99 op18=101 op19=115 op20=115 op21=46 op22=105 op23=110 op24=100 op25=101 op26=120/>
    <KIND op0=28 op1=118 op2=99 op3=97 op4=108 op5=108 op6=95 op7=118 op8=105 op9=115 op10=105 op11=98 op12=105 op13=108 op14=105 op15=116 op16=121/>
    <KIND op0=29 op1=110 op2=111 op3=117 op4=110 op5=100 op6=101 op7=102/>
    <KIND op0=30 op1=97 op2=110 op3=110 op4=111 op5=116 op6=97 op7=116 op8=105 op9=111 op10=110/>
    <KIND op0=31 op1=110 op2=111 op3=115 op4=97 op5=110 op6=105 op7=116 op8=105 op9=122 op10=101/>
    <KIND op0=32 op1=102 op2=117 op3=110 op4=99 op5=95 op6=115 op7=97 op8=110 op9=105 op10=116 op11=105 op12=122 op13=101/>
    <KIND op0=33 op1=101 op2=120 op3=99 op4=108 op5=117 op6=100 op7=101/>
    <KIND op0=34 op1=109 op2=101 op3=109 op4=112 op5=114 op6=111 op7=102/>
    <KIND op0=35 op1=99 op2=97 op3=108 op4=108 op5=115 op6=105 op7=116 op8=101/>
    <KIND op0=36 op1=107 op2=99 op3=102 op4=105 op5=95 op6=116 op7=121 op8=112 op9=101/>
    <KIND op0=37 op1=112 op2=99 op3=115 op4=101 op5=99 op6=116 op7=105 op8=111 op9=110 op10=115/>
    <KIND op0=38 op1=68 op2=73 op3=65 op4=115 op5=115 op6=105 op7=103 op8=110 op9=73 op10=68/>
    <KIND op0=39 op1=99 op2=111 op3=114 op4=111 op5=46 op6=111 op7=117 op8=116 op9=115 op10=105 op11=100 op12=101 op13=46 op14=102 op15=114 op16=97 op17=109 op18=101/>
  </METADATA_KIND_BLOCK>
  <OPERAND_BUNDLE_TAGS_BLOCK NumWords=45 BlockCodeSize=3>
    <OPERAND_BUNDLE_TAG op0=100 op1=101 op2=111 op3=112 op4=116/>
    <OPERAND_BUNDLE_TAG op0=102 op1=117 op2=110 op3=99 op4=108 op5=101 op6=116/>
    <OPERAND_BUNDLE_TAG op0=103 op1=99 op2=45 op3=116 op4=114 op5=97 op6=110 op7=115 op8=105 op9=116 op10=105 op11=111 op12=110/>
    <OPERAND_BUNDLE_TAG op0=99 op1=102 op2=103 op3=117 op4=97 op5=114 op6=100 op7=116 op8=97 op9=114 op10=103 op11=101 op12=116/>
    <OPERAND_BUNDLE_TAG op0=112 op1=114 op2=101 op3=97 op4=108 op5=108 op6=111 op7=99 op8=97 op9=116 op10=101 op11=100/>
    <OPERAND_BUNDLE_TAG op0=103 op1=99 op2=45 op3=108 op4=105 op5=118 op6=101/>
    <OPERAND_BUNDLE_TAG op0=99 op1=108 op2=97 op3=110 op4=103 op5=46 op6=97 op7=114 op8=99 op9=46 op10=97 op11=116 op12=116 op13=97 op14=99 op15=104 op16=101 op17=100 op18=99 op19=97 op20=108 op21=108/>
    <OPERAND_BUNDLE_TAG op0=112 op1=116 op2=114 op3=97 op4=117 op5=116 op6=104/>
    <OPERAND_BUNDLE_TAG op0=107 op1=99 op2=102 op3=105/>
    <OPERAND_BUNDLE_TAG op0=99 op1=111 op2=110 op3=118 op4=101 op5=114 op6=103 op7=101 op8=110 op9=99 op10=101 op11=99 op12=116 op13=114 op14=108/>
  </OPERAND_BUNDLE_TAGS_BLOCK>
  <UnknownBlock26 NumWords=6 BlockCodeSize=2>
    <UnknownCode1 op0=115 op1=105 op2=110 op3=103 op4=108 op5=101 op6=116 op7=104 op8=114 op9=101 op10=97 op11=100/>
    <UnknownCode1/>
  </UnknownBlock26>
  <FUNCTION_BLOCK NumWords=12 BlockCodeSize=4>
    <DECLAREBLOCKS op0=1/>
    <CONSTANTS_BLOCK NumWords=2 BlockCodeSize=4>
      <SETTYPE abbrevid=4 op0=6/>
      <NULL/>
      <INTEGER abbrevid=5 op0=2/>
    </CONSTANTS_BLOCK>
    <INST_ALLOCA op0=0 op1=6 op2=10 op3=68/>
    <INST_GEP abbrevid=13 op0=1 op1=0 op2=4 op3=3 op4=3/>
    <INST_STORE op0=2 op1=1 op2=4 op3=0/>
    <INST_CALL op0=0 op1=32768 op2=8 op3=10 op4=2/>
  </FUNCTION_BLOCK>
  <FUNCTION_BLOCK NumWords=5 BlockCodeSize=4>
    <DECLAREBLOCKS op0=1/>
    <CONSTANTS_BLOCK NumWords=1 BlockCodeSize=4>
      <SETTYPE abbrevid=4 op0=6/>
      <NULL/>
    </CONSTANTS_BLOCK>
    <INST_RET abbrevid=11 op0=1/>
  </FUNCTION_BLOCK>
  <FUNCTION_BLOCK NumWords=31 BlockCodeSize=4>
    <DECLAREBLOCKS op0=5/>
    <CONSTANTS_BLOCK NumWords=2 BlockCodeSize=4>
      <SETTYPE abbrevid=4 op0=6/>
      <INTEGER abbrevid=5 op0=2/>
      <INTEGER abbrevid=5 op0=462/>
    </CONSTANTS_BLOCK>
    <INST_BR op0=1/>
    <INST_INVOKE op0=0 op1=8192 op2=2 op3=3 op4=11 op5=8/>
    <INST_ALLOCA op0=6 op1=6 op2=11 op3=67/>
    <INST_STORE op0=1 op1=2 op2=3 op3=0/>
    <INST_CALL op0=0 op1=32768 op2=7 op3=13 op4=2/>
    <INST_UNREACHABLE abbrevid=12/>
    <UnknownCode47 op0=15 op1=1 op2=0/>
    <INST_EXTRACTVAL op0=1 op1=0/>
    <INST_ALLOCA op0=0 op1=6 op2=11 op3=68/>
    <INST_STORE op0=1 op1=2 op2=4 op3=0/>
    <INST_CALL op0=0 op1=32768 op2=7 op3=16 op4=6/>
    <INST_UNREACHABLE abbrevid=12/>
    <INST_CALL op0=0 op1=32768 op2=7 op3=16 op4=6/>
    <INST_UNREACHABLE abbrevid=12/>
    <VALUE_SYMTAB NumWords=6 BlockCodeSize=4>
      <BBENTRY abbrevid=7 op0=0 op1=95 op2=95 op3=97 op4=112 op5=111 op6=108 op7=108 op8=111 op9=99 op10=95 op11=114 op12=116 op13=95 op14=101 op15=110 op16=116 op17=114 op18=121 op19=95 op20=98 op21=108 op22=111 op23=99 op24=107/> record string = '__apolloc_rt_entry_block'
    </VALUE_SYMTAB>
  </FUNCTION_BLOCK>
  <VALUE_SYMTAB NumWords=4 BlockCodeSize=4>
    <FNENTRY abbrevid=8 op0=4 op1=356/>
    <FNENTRY abbrevid=8 op0=5 op1=370/>
    <FNENTRY abbrevid=8 op0=6 op1=377/>
  </VALUE_SYMTAB>
</MODULE_BLOCK>
<STRTAB_BLOCK NumWords=33 BlockCodeSize=3>
  <BLOB abbrevid=4/> blob data = '_Unwind_RaiseException__gxx_personality_v0exitfreedisposeStringmain__apolloc_rt_entrypoint18.0.0x86_64-linux-gnumain.ap'
</STRTAB_BLOCK>

It seems that the type info of struct Person was not added to the module. Could you please provide the output of target/debug/apolloc build main.ap -o main.ll --output-type=llvm-ir -O0 -v?

bluefish43 commented 1 year ago

Surely:

$ target/debug/apolloc build main.ap -o main.ll --output-type=llvm-ir -O0 -v
VERBOSE Reading input file `main.ap`
VERBOSE Creating directory `./tmp`
VERBOSE Writing output bitcode to `./tmp/main.bc`
VERBOSE Executing command `opt ./tmp/main.bc -o ./tmp/opt.bc -O0`
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace.
Stack dump:
0.      Program arguments: opt ./tmp/main.bc -o ./tmp/opt.bc -O0
 #0 0x00007f50edc4d486 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/usr/lib/llvm-18/bin/../lib/libLLVM-18.so.1+0xd2c486)
 #1 0x00007f50edc4b440 llvm::sys::RunSignalHandlers() (/usr/lib/llvm-18/bin/../lib/libLLVM-18.so.1+0xd2a440)
 #2 0x00007f50edc4db4b (/usr/lib/llvm-18/bin/../lib/libLLVM-18.so.1+0xd2cb4b)
 #3 0x00007f50ec9f5520 (/lib/x86_64-linux-gnu/libc.so.6+0x42520)
 #4 0x00007f50edddfc47 llvm::StructType::getTypeAtIndex(llvm::Value const*) const (/usr/lib/llvm-18/bin/../lib/libLLVM-18.so.1+0xebec47)
 #5 0x00007f50ee67b23e (/usr/lib/llvm-18/bin/../lib/libLLVM-18.so.1+0x175a23e)
 #6 0x00007f50ee674097 (/usr/lib/llvm-18/bin/../lib/libLLVM-18.so.1+0x1753097)
 #7 0x00007f50ee674818 (/usr/lib/llvm-18/bin/../lib/libLLVM-18.so.1+0x1753818)
 #8 0x00007f50eddb6cc7 llvm::Module::materializeAll() (/usr/lib/llvm-18/bin/../lib/libLLVM-18.so.1+0xe95cc7)
 #9 0x00007f50ee66b945 llvm::BitcodeModule::getModuleImpl(llvm::LLVMContext&, bool, bool, bool, llvm::ParserCallbacks) (/usr/lib/llvm-18/bin/../lib/libLLVM-18.so.1+0x174a945)
#10 0x00007f50ee67226a llvm::BitcodeModule::parseModule(llvm::LLVMContext&, llvm::ParserCallbacks) (/usr/lib/llvm-18/bin/../lib/libLLVM-18.so.1+0x175126a)
#11 0x00007f50ee672461 llvm::parseBitcodeFile(llvm::MemoryBufferRef, llvm::LLVMContext&, llvm::ParserCallbacks) (/usr/lib/llvm-18/bin/../lib/libLLVM-18.so.1+0x1751461)
#12 0x00007f50ede55d19 llvm::parseIR(llvm::MemoryBufferRef, llvm::SMDiagnostic&, llvm::LLVMContext&, llvm::ParserCallbacks) (/usr/lib/llvm-18/bin/../lib/libLLVM-18.so.1+0xf34d19)
#13 0x00007f50ede562ad llvm::parseIRFile(llvm::StringRef, llvm::SMDiagnostic&, llvm::LLVMContext&, llvm::ParserCallbacks) (/usr/lib/llvm-18/bin/../lib/libLLVM-18.so.1+0xf352ad)
#14 0x0000564af4045e66 main (/usr/lib/llvm-18/bin/opt+0x29e66)
#15 0x00007f50ec9dcd90 __libc_start_call_main ./csu/../sysdeps/nptl/libc_start_call_main.h:58:16
#16 0x00007f50ec9dce40 call_init ./csu/../csu/libc-start.c:128:20
#17 0x00007f50ec9dce40 __libc_start_main ./csu/../csu/libc-start.c:379:5
#18 0x0000564af4033f65 _start (/usr/lib/llvm-18/bin/opt+0x17f65)
VERBOSE Removing directory `./tmp`
Error: opt was terminated by a signal
dtcxzyw commented 1 year ago

Well, I think it is likely a bug in apolloc. Please provide the repo URL of apolloc. If it is a closed-source software, I am sorry I couldn't be of more help.

bluefish43 commented 1 year ago

Here is the link of the repository.

dtcxzyw commented 1 year ago

Alright, I will take a look.

dtcxzyw commented 1 year ago

There are some problems in apolloc:

  1. You cannot do GEP on an aggregate value. It just accepts a pointer. To extract value from an aggregate value, please use extractvalue. I think it is definitely not what you want. I recommend you pass the pointer of String to disposeString and use gep/load/store insts. See also https://llvm.org/docs/GetElementPtr.html.
  2. The entry block of disposeString should end with a terminator.
  3. The verifier reported that @__gxx_personality_v0's context does not match Module context.

The following code snippet may be helpful (I added it into ApolloBuilder::get_bitcode):

    use llvm_sys::analysis::*;
    use std::ffi::CStr;

    self.dump();
    let mut out: *mut::libc::c_char = std::ptr::null_mut();
    let res = LLVMVerifyModule(
        self.module.as_llvm_ref(),
        LLVMVerifierFailureAction::LLVMReturnStatusAction,
        &mut out);
    if res != 0 {
        let err_msg = CStr::from_ptr(out).to_string_lossy().into_owned();
        return Err(anyhow!("LLVM module verification failed: {}", err_msg));
    }

Dumped IR:

; ModuleID = 'main.ap'
source_filename = "main.ap"
target triple = "x86_64-linux-gnu"

%String = type { ptr, i32, i32 }
%Person = type { ptr, i32 }

@0 = private unnamed_addr constant [6 x i8] c"Hello\00", align 1
@1 = private unnamed_addr constant [15 x i8] c"Hello, world!\0A\00", align 1

declare void @_Unwind_RaiseException(ptr %0)

declare i32 @__gxx_personality_v0(...)

declare void @exit(i32 %0)

declare i32 @printf(ptr %0, ...)

declare i64 @strlen(ptr %0)

declare ptr @malloc(i64 %0)

declare ptr @free(ptr %0)

define void @disposeString(%String %0) {
  %2 = alloca ptr, align 8
  %3 = getelementptr inbounds ptr, %String %0, i32 0, i32 0
  store %String %3, ptr %2, align 8
  %4 = call ptr @free(ptr %2)
}

define i32 @main() {
  %1 = alloca %Person, align 8
  store %Person { ptr @0, i32 -13 }, ptr %1, align 8
  %2 = call i32 @printf(ptr @1)
  ret i32 0
}

define i32 @__apolloc_rt_entrypoint(i32 %0, ptr %1, ptr %2) personality ptr @__gxx_personality_v0 {
__apolloc_rt_entry_block:
  br label %3

3:                                                ; preds = %__apolloc_rt_entry_block
  %4 = invoke i32 @main()
          to label %5 unwind label %7

5:                                                ; preds = %3
  %6 = alloca i32, align 4
  store i32 %4, ptr %6, align 4
  call void @exit(i32 %4)
  unreachable

7:                                                ; preds = %3
  %8 = landingpad { ptr }
          cleanup
  %9 = extractvalue { ptr } %8, 0
  %10 = alloca ptr, align 8
  store ptr %9, ptr %10, align 8
  call void @exit(i32 231)
  unreachable

11:                                               ; No predecessors!
  call void @exit(i32 231)
  unreachable
}
bluefish43 commented 1 year ago

Thank you very much for helping me, this solved the problem!