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.
1 Answer
Reset to default 0I 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.
1 Answer
Reset to default 0I 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
版权声明:本文标题:programming languages - Segmentation fault encountered at `ret void` in llvm-ir instructions - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745654059a2161492.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论