The following C program calls select() to get a list of client sockets that have data in the socket buffer (using UNIX domain datagram sockets). It takes four parameters on call from a NASM program. The parameter fds_array[] is a 3-element integer array created in NASM and passed into the program.
int select_clientsockets(int nfds, fd_set rfds, int fds_array[], int fd_count){ int fds_ready; int abc; fd_count = 3; FD_ZERO(&rfds); FD_SET(0,&rfds); fds_ready = select(3, &rfds, NULL, NULL, 0); for (int i = 0; i < fd_count; i++){ fds_array[i] = 0; if (FD_ISSET(i, &rfds)) { fds_array[i] = 1; } } for (int i = 0; i < fd_count; i++){ abc = fds_array[i]; printf("Value of array at %d, is %dn", i, abc ); } return 0; }
The data are passed in from NASM in rdi, rsi, rdx and rcx. :
mov rdi,7 ; range of fd #s lea rsi,[socket_client_fds] lea rdx,[fds_ready] mov rcx,3 ; number of open client sockets call [rel select_clientsockets wrt ..got]
As written, the program hangs. When I step through it with gdb, it does its calculations correctly and the values in fds_array are as expected. But at the return line (return 0), gdb reports this:
0x00007ffff720cfed in select_clientsockets ( nfds=<error reading variable: Cannot access memory at address 0xfffffffffffffffc>, rfds=<error reading variable: Cannot access memory at address 0x10>, fds_array=<error reading variable: Cannot access memory at address 0xfffffffffffffff0>, fd_count=<error reading variable: Cannot access memory at address 0xffffffffffffffec>) at POSIX_Datagram_Socket.c:139
So it says the variables nfds, rfds and fds_array cannot be accessed. But I just finished stepping through it and everything was correct.
My research turned up a number of Stack Overflow questions on that gdb error message, but they’re all fact-specific and I didn’t find any that fit these facts.
Thanks for any ideas on how to fix this error on exit.
Advertisement
Answer
Let’s look at this call…
mov rdi,7 ; range of fd #s lea rsi,[socket_client_fds] lea rdx,[fds_ready] mov rcx,3 ; number of open client sockets call [rel select_clientsockets wrt ..got]
In the System V AMD64 ABI, the arguments would be passed in:
- rdi – ndfs
- rsi – fds_array
- rsp..rsp+127 – rfds
- rdx – fd_count
It looks like you’re passing rfds
as if it were a pointer. It’s not. It goes on the stack.
If this were Windows, it would be passed as a pointer. However, it sounds like you’re not using Windows, since you mention Unix domain sockets (although AF_UNIX
is actually available on Windows).
If you ever need help calling a C function from assembly, just use Godbolt.