Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What happens if there is no exit system call in an assembly program?

In an assembly program, the .text section is loaded at 0x08048000; the .data and the .bss section comes after that.

What would happen if I don't put an exit syscall in the .text section? Would it lead to the .data and the .bss section being interpreted as code causing "unpredictable" behavior? When will the program terminate -- probably after every "instruction" is executed?

I can easily write a program without the exit syscall, but testing if .data and .bss gets executed is something I don't know because I guess I would have to know the real machine code that is generated under-the-hoods to understand that.

I think this question is more about "How would OS and CPU handle such a scenario?" than assembly language, but it is still interesting to know for assembly programmers etc.

like image 959
Nishant Avatar asked Apr 05 '18 13:04

Nishant


People also ask

What does exit () system call do?

The exit operation typically performs clean-up operations within the process space before returning control back to the operating system. Some systems and programming languages allow user subroutines to be registered so that they are invoked at program termination before the process actually terminates for good.

Is exit a system call?

exit() The exit() is such a function or one of the system calls that is used to terminate the process.

What is a system call in assembly?

System calls enable users to request a service from the operating system (OS). To execute a system call , the execution of the process is halted, and the execution of the system call starts in kernel mode. This switch from user mode to kernel mode may incur a short delay.

What registers does syscall use?

Syscall parameters are passed in registers rdi, rsi, rdx, r10, r8, r9, which you'll notice is *somewhat* like a function call but with slightly different registers! The return value, normally an integer error code, is returned in rax.


1 Answers

The processor does not know where your code ends. It faithfully executes one instruction after another until execution is redirected elsewhere (e.g. by a jump, call, interrupt, system call, or similar). If your code ends without jumping elsewhere, the processor continues executing whatever is in memory after your code. It is fairly unpredictable what exactly happens, but eventually, your code typically crashes because it tries to execute an invalid instruction or tries to access memory that it is not allowed to access. If neither happens and no jump occurs, eventually the processor tries to execute unmapped memory or memory that is marked as “not executable” as code, causing a segmentation violation. On Linux, this raises a SIGSEGV or SIGBUS. When unhandled, these terminate your process and optionally produce core dumps.

like image 83
fuz Avatar answered Oct 11 '22 20:10

fuz