Defect #683
closedLinux syscalls return negative value on error
100%
Description
I believe the general convention on Linux systems is that all
syscall return a negative value on error and that the error code
it the absolute of this value.
When I look at the code in amd64linuxrun.asm:
; standard fopen function
.code fopen
mov eax, 2
mov rdi, [rsp + 8]
mov rsi, [rsp + 16]
cmp byte [rsi], 'w'
mov rsi, 0
jne skip
mov rsi, 01 + 0100
skip: mov rdx, 04 + 040 + 0200 + 0400
syscall ; sys_open
cmp eax, -1
je fail
ret
fail: mov eax, 0
ret
The return code is compared to -1, but this is only valid if using a syscall wrapper
which typical captures the error return code, stores this in errno and return -1 in case
of a error.
This is what all online documentation of syscalls shows as it does not reflect
using these directly from assembler code.
I checked this and when opening a non-existing file for reading the return value
if the direct syscall is -2 which is error ENOENT (=2).
Perhaps the runtime code should be update to follow c syscall wrappers and store
the return code in a global errno integer if it is negative and return -1 on failure?
This will create problems with multiple threads, but that is probably ok.