TheDan64 / inkwell

It's a New Kind of Wrapper for Exposing LLVM (Safely)
https://thedan64.github.io/inkwell/
Apache License 2.0
2.38k stars 229 forks source link

Malloc get seg fault #540

Open noahfraiture opened 1 month ago

noahfraiture commented 1 month ago

Describe the Bug Hello, I'm trying to emit a simple malloc call but I get a segfault when the call is made, before even returning from the malloc. It compile correctly, but crash on run in a JIT

With a code as simple as that

        let malloc = self
            .builder_main
            .build_malloc(self.context.custom_width_int_type(8), "malloc")
            .expect("call malloc");

with nothing else being compiled, I get the following opcode

call:                                             ; No predecessors!
  %malloc = tail call ptr @malloc(i32 ptrtoint (ptr getelementptr (i8, ptr null, i32 1) to i32))
  ret void
}

As we can see there's a getelementptr (i8, ptr null, i32 1) which I think might cause the issue.

I tried i8_type or different name but the result is always the same. I tried to add a free instruction after that but it doesn't reach the free on run.

LLVM Version: inkwell = { version = "0.5.0", features = ["llvm18-0"] } llvm-sys-180 = { package = "llvm-sys", version = "180.0.0", features = [ "prefer-static", ] }

Desktop (please complete the following information):