titzer / virgil

A fast and lightweight native programming language
1.24k stars 48 forks source link

x86-64-linux executables exit with random non-zero exit codes #78

Closed srackham closed 2 years ago

srackham commented 2 years ago

This is an odd one: x86-64-linux executables sometimes return a (seemingly random) non-zero exit code from the main() function:

$ cat hello.v3 && v3c-x86-64-linux hello.v3 && ./hello; echo $?
def main() {
  System.puts("Hello World\n");
}
Hello World
216

If a second print statement is added the exit code is zero:

$ cat hello.v3 && v3c-x86-64-linux hello.v3 && ./hello; echo $?
def main() {
  System.puts("Hello World\n");
  System.puts("Hello World\n");
}
Hello World
Hello World
0

Explicitly returning an exit code from main() seems to resolve the problem:

$ cat hello.v3 && v3c-x86-64-linux hello.v3 && ./hello; echo $?
def main() -> int {
  System.puts("Hello World\n");
  return 0;
}
Hello World
0

The x86-linux target does not seem to exhibit this behaviour.

$ cat hello.v3 && v3c-x86-linux hello.v3 && ./hello; echo $? 
def main() {
  System.puts("Hello World\n");
}
Hello World
0
titzer commented 2 years ago

Ah, nice catch. There was a check for this but the logic was broken, so it was exiting with whatever happened to be in rax.

Fixed in https://github.com/titzer/virgil/commit/15c489e792016a624660641452344ba9b0b0dd62.

srackham commented 2 years ago

Verified! Thanks for the super-fast bugfix.