#Why does libc exit use returnCode % 1024 and not returnCode & 0xFF ?

79 messages · Page 1 of 1 (latest)

vapid canopy
#

"The exit() function causes normal process termination and the
least significant byte of status (i.e., status & 0xFF) is
returned to the parent (see wait(2))."

Why are the docs lying to me?

near bronzeBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

sharp plume
#

where exactly do you see status % 1024 @vapid canopy ?`

#

whats more interesting is how the status is communicated to the calling process
because that thing doesnt return at all, so i assume they rely on status being in %rax

sturdy brook
#

Interesting

#

You know what's even more interesting (imo)? _exit() calls abort() which can itself call _exit() monkaS

sharp plume
#

lmao

#

time to look what musl does

#

oh

#

the exit syscall specified by rax expects the status in rdi so how does it communiate that nootlikethis

#

  /* If we can't signal ourselves and the abort instruction failed, exit.  */
  if (stage == 5)
    {
      ++stage;
      _exit (127);
    }

"if it does not exit, make sure it exits by overflowing the stack"

sturdy brook
#

indeed

sturdy brook
#

Here we go

#

I think this is the actual _exit on sysv

sharp plume
#

oh

#

wtf was i looking at then

sturdy brook
#

Presumably that's the fallback implementation for unknown systems

sharp plume
#

i see

sharp plume
#

in the debugger ?

vapid canopy
#

in $? from the shell, the literal process exit code

#

literally just run it and see

sturdy brook
#

@vapid canopy this is an implementation-defined thing. What system are you on.

sharp plume
#

cant reproduce

sturdy brook
#

^

vapid canopy
vapid canopy
#

kay let me pop open the ubuntu VM

vapid canopy
sharp plume
#

i mean you can also try on ;compile

vapid canopy
sturdy brook
#

But the compiler and libc are the most important part here

vapid canopy
#

yes

sharp plume
#

;compile bash

echo -e "#include <stdlib.h>\nint main(int argc, char **argv) { return atoi(argv[1]); }" > main.c
gcc main.c
./a.out 127 ; echo $? ; uname -r
wet basaltBOT
#
Program Output
prog.sh: line 4:    28 Segmentation fault      (core dumped) ./a.out
139
5.4.0-113-generic

sturdy brook
#

The segfault message is misleading

#

It’s not actually

sharp plume
#

what is segfaulting lol

sturdy brook
#

It’s not

#

It’s an artifact of the sandbox

sharp plume
#

oh

sturdy brook
#

Iirc

vapid canopy
# sturdy brook That’s ancient lol

ok so I can't reproduce it here either,
let me ask my question in a different way

the return type is int, why can't libc just, put the same return code as the actaul int value, and NOT truncate it to the last byte? Or in different libc implementations, do other arithmetic with the int value?

sharp plume
#

can you give the output of ldd --version

#

on the system where it happens

#

@vapid canopy

sturdy brook
vapid canopy
sharp plume
#

on the system where you can return with more than 255

#

i just need the glibc version

#

2.31 was released in 2020

vapid canopy
sharp plume
#

yeah but thats on ubuntu

#

i thought u encountered the problem in centos ?

vapid canopy
sharp plume
#

i mean essentially you can look up the version and download the source and look if they & 0xff on status exit

#

if they dont you know why its 1024

vapid canopy
#

libc?

#

ldd?

sharp plume
#

glibc

vapid canopy
#

which piece of software's source code exactly

#

aha

sharp plume
#

2.31 is the glibc version

#

download the source of that release and look what exit does

sharp plume
#

.../stdlib/exit.c
is the path on the current release

#

and then follow any function calls

#

if you dont know where to follow it look at what is included in that file

vapid canopy
#

ohhhhhhhhhhhhhhhh

#

so yes, on the machine where I ran this, it is again modulo 256

#

!solved