MrSmith33 / vox

Vox language compiler. AOT / JIT / Linker. Zero dependencies
Boost Software License 1.0
327 stars 18 forks source link

Incorrect codegen for syscall instruction #33

Closed MrSmith33 closed 2 years ago

MrSmith33 commented 2 years ago

Discussed in https://github.com/MrSmith33/vox/discussions/29

Originally posted by **rempas** December 3, 2021 I'm trying to implement the "mmap" system call on Linux 64bit. I have the following code: ``` /* mmap system call */ /* Protections */ enum PROT_READ = 0x1; /* Page can be read. */ enum PROT_WRITE = 0x2; /* Page can be written. */ enum PROT_EXEC = 0x4; /* Page can be executed. */ enum PROT_NONE = 0x0; /* Page can not be accessed. */ /* Flags */ enum MAP_SHARED = 0x01; /* Share changes. */ enum MAP_PRIVATE = 0x02; /* Changes are private. */ enum MAP_FIXED = 0x10; /* Interpret addr exactly. */ enum MAP_ANONYMOUS = 0x20; /* Don't use a file. */ @extern(syscall, 9) void* sys_mmap(void* addr, u64 len, i32 prot, i32 flags, i32 fd, i64 off); void* malloc(u64 len) { return sys_mmap(null, len, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); } void main() { u8* val = cast(u8*)malloc(4096); // PAGESIZE *val = 10; exit(0); } ``` I got the signature for "mmap" from the [man](https://www.man7.org/linux/man-pages/man2/mmap.2.html) pages. This code will work under C but it will throw a "segmentation fault" on Vox when I try to de-reference the pointer. Any ideas?
MrSmith33 commented 2 years ago

The actual issue was that eax wasn't added to the argument list of the syscall instruction causing incorrect liveness analysis and as a result incorrect register allocation, leading to overwrite of the value.

Fixed by 28c386a0c89afe9f0fa476d2ef9b27fb6cadd3a5

MrSmith33 commented 2 years ago

@rempas, this issue should be fixed now, I verified it on my machine.

rempas commented 2 years ago

Unfortunately it seems that this problem has not been fixed yet. The wrapper in the "exit" system call results in the same error. The code:

@extern(syscall, 60)
void sys_exit(u64 code);

void exit(u64 code) {
  exit(code);
}
void main() {
  exit(0);
}
MrSmith33 commented 2 years ago

You have infinite recursion here with exit function

rempas commented 2 years ago

Oh, I'm so sorry! Well you see I was coding continuously for about an hour and it seems I'm not used to it. Don't hesitate to call me blind the next time tho, maybe this will help me to double check first...