admin管理员组

文章数量:1026912

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

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

本文标签: programming languagesSegmentation fault encountered at ret void in llvmir instructionsStack Overflow