最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

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

matteradmin9PV0评论

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

Post a comment

comment list (0)

  1. No comments so far