bdwgc icon indicating copy to clipboard operation
bdwgc copied to clipboard

GC_init segfaults if called from asm code on Linux/x86_64

Open gyps opened this issue 6 months ago • 2 comments

Dear all, Since I'm currently writing a compiler for a functional language I thought about using bdwgc as a garbage collector. Since the compiler generates code for the x86_64 plattform (using nasm as assembler) I wanted to use the GC in assembler code directly, avoiding parts written in C/C++. So far it worked well but today I dicovered a strange phenomenon: the following code works fine:

extern  GC_malloc, GC_init, exit

section .text
	global main

main:
	push rbp
	call GC_init
	pop rbp
	mov edi, 0 
	call exit  

while

extern  GC_malloc, GC_init, exit

section .text
	global main

main:
	push rbp
	push rbp
	call GC_init
	pop rbp
	pop rbp
	mov edi, 0 
	call exit  

segfaults. How can pushing and popping rbp to/from the stack twice can influence the behavior of GC_init? Or am I calling it completely wrong?

The commands used to assemble the program are:

$ nasm -o test_gc.o -f elf64 test_gc.asm
$ gcc -o test_gc test_gc.o -lgc
$ ./test_gc

gyps avatar Aug 13 '24 15:08 gyps