programming languages - Segmentation fault encountered at `ret void` in llvm-ir instructions - Stack Overflow

I'm currently making a compiler that outputs bare LLVM-IR instructions and implementing variadic f

I'm currently making a compiler that outputs bare LLVM-IR instructions and implementing variadic function calls. I have defined a println function that accepts a (format) string and variable amount of arguments for the printf call. I have included printf calls to see where my program faults and it is as the return of the function, which would make me think that there is something wrong with cleaning up the @llvm.va_end calls, since it does what i wanted it to do before the fault.

Here is the llvm instrucitons:

declare void @llvm.va_start(i8*)
declare void @llvm.va_end(i8*)
declare void @vprintf(i8*, i8*)
@.str_3 = private unnamed_addr constant [2 x i8] c"\0A\00"
declare void @printf(i8*, ...)
@.str_5 = private unnamed_addr constant [4 x i8] c"%i\0A\00"
@.str_6 = private unnamed_addr constant [16 x i8] c"number is %i %i\00"

define void @println(i8* %a, ...) {
entry:
    call void @printf(i8* @.str_5, i32 1) ; debug, added prior
    %.va_list = alloca i8*
    call void @printf(i8* @.str_5, i32 2) ; debug, added prior
    call void @llvm.va_start(i8* %.va_list)
    call void @printf(i8* @.str_5, i32 3) ; debug, added prior
    call void @vprintf(i8* %a, i8* %.va_list)
    call void @printf(i8* @.str_3)
    call void @printf(i8* @.str_5, i32 4) ; debug, added prior
    call void @llvm.va_end(i8* %.va_list)
    call void @printf(i8* @.str_5, i32 5) ; debug, added prior
    ret void
}

define void @main() {
entry:
    call void @printf(i8* @.str_5, i32 0) ; debug, added prior
    call void @println(i8* @.str_6, i32 5, i32 2)
    call void @printf(i8* @.str_5, i32 6) ; debug, added prior
    ret void
}

Output of running the built program:

0
1
2
3
number is 5 2
4
5
Segmentation fault (core dumped)

As you can see here i get the segmentation fault between printf(5) and printf(6) which would entail that there is something going on at the return/deallocating of memory or something in the println function.

I'm currently making a compiler that outputs bare LLVM-IR instructions and implementing variadic function calls. I have defined a println function that accepts a (format) string and variable amount of arguments for the printf call. I have included printf calls to see where my program faults and it is as the return of the function, which would make me think that there is something wrong with cleaning up the @llvm.va_end calls, since it does what i wanted it to do before the fault.

Here is the llvm instrucitons:

declare void @llvm.va_start(i8*)
declare void @llvm.va_end(i8*)
declare void @vprintf(i8*, i8*)
@.str_3 = private unnamed_addr constant [2 x i8] c"\0A\00"
declare void @printf(i8*, ...)
@.str_5 = private unnamed_addr constant [4 x i8] c"%i\0A\00"
@.str_6 = private unnamed_addr constant [16 x i8] c"number is %i %i\00"

define void @println(i8* %a, ...) {
entry:
    call void @printf(i8* @.str_5, i32 1) ; debug, added prior
    %.va_list = alloca i8*
    call void @printf(i8* @.str_5, i32 2) ; debug, added prior
    call void @llvm.va_start(i8* %.va_list)
    call void @printf(i8* @.str_5, i32 3) ; debug, added prior
    call void @vprintf(i8* %a, i8* %.va_list)
    call void @printf(i8* @.str_3)
    call void @printf(i8* @.str_5, i32 4) ; debug, added prior
    call void @llvm.va_end(i8* %.va_list)
    call void @printf(i8* @.str_5, i32 5) ; debug, added prior
    ret void
}

define void @main() {
entry:
    call void @printf(i8* @.str_5, i32 0) ; debug, added prior
    call void @println(i8* @.str_6, i32 5, i32 2)
    call void @printf(i8* @.str_5, i32 6) ; debug, added prior
    ret void
}

Output of running the built program:

0
1
2
3
number is 5 2
4
5
Segmentation fault (core dumped)

As you can see here i get the segmentation fault between printf(5) and printf(6) which would entail that there is something going on at the return/deallocating of memory or something in the println function.

Share Improve this question asked Nov 16, 2024 at 15:45 johronjohron 131 silver badge3 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

I ended up trying lots of stuff, checking llvm ir from different c programs with variadic functions, clever chat gpt prompts and managed to fix the issue by appenind , i32 128 at the %.va_list:

The working println function code is:

define void @println(i8* %a, ...) {
entry:
    %.va_list = alloca i8, i32 128 ; here is the appended alignment
    call void @llvm.va_start(i8* %.va_list)
    call void @vprintf(i8* %a, i8* %.va_list)
    call void @printf(i8* @.str_3)
    call void @llvm.va_end(i8* %.va_list)
    ret void
}

ChatGPT thread that helped me: https://chatgpt/share/6738f7c5-3994-800e-90ab-5af879464fa8

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745654060a4638438.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信