St.Antario
St.Antario

Reputation: 27435

Why did not I get segmentation fault?

I'm new to assembly programming and experementing with simple examples and gdb. Here is the program I wrote:

1.asm

section .text
    global _start
    extern _print_func

_start:
    push str
    movzx rdx, byte [str_len]
    push dx ;  <---  typo here, should be rdx
    call _print_func

    mov rax, 60
    syscall

section .data
    str: db 'Some data',0x0A,0x0D
    str_len: db $ - str

2.asm

section .text
    global _print_func

_print_func:
    pop rbx
    pop rdx
    pop rsi
    mov rax, 0x01
    mov rdi, 0x01
    syscall
    push rbx
    ret

section .data
    str: db 'Some string',0x0A,0x0D
    str_len: db $ - str

After compiling, linking (with ld) and running the program it just printed nothing. So I examined the content of registers before the syscall made.

(gdb) info registers 
rax            0x1      1
rbx            0x4000c5 4194501
rcx            0x0      0
rdx            0x6000e4000b     412331802635  ; <-- obviously wrong
rsi            0x10000  65536
rdi            0x1      1
rbp            0x0      0x0
rsp            0x7fffffffdcc6   0x7fffffffdcc6

So the syscall should try read 412331802635 bytes starting at 0x10000 which I thought should have caused Segmentation Fault since the program is not allowed to access all the bytes.

But it silently printed nothing. Why? Why didn't Segmantation Fault raised? Was that some sort of undefined behavior? I'n using Ubuntu 16.04 LTS under intel core i5.

Upvotes: 1

Views: 114

Answers (1)

Jester
Jester

Reputation: 58812

sys_write does not raise a segfault, it just returns an -EFAULT error code. You should see that in rax after the syscall finishes. See also man 2 write

Upvotes: 4

Related Questions