Project

General

Profile

Actions

Defect #683

closed

Linux syscalls return negative value on error

Added by Runar Tenfjord 7 months ago. Updated 7 months ago.

Status:
Closed
Priority:
Normal
Category:
Linux
Target version:
-
% Done:

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.

Actions #1

Updated by Florian Negele 7 months ago

  • Status changed from New to Closed
  • % Done changed from 0 to 100

The next release fixes the error checking, thanks for reporting. The return code will be stored in a thread-local variable called errno sometime in the future.

Actions

Also available in: Atom PDF