1

Every time I try to make a futex-related system call in C, I get a return value of -1 and errno = 14 (EFAULT - Bad Address).

First I adapted code from the futex manpage. Futex Manpage

My changes are small and are as follows:

  • Print futexes
       static void
       fwait(int *futexp)
       {
           printf("%p\n",  futexp);
           printf("%d\n", *futexp);
           int s;
  • Deadlock the processes by commenting out unlock logic.
// fpost(futex2);
...
// fpost(futex1);

The altered program printed the following:

0x7f9e696b3004
0x7f9e696b3000
1
0
Parent (88542) 0
0x7f9e696b3004
0

While the code is running, I ran strace -p 88542 to verify the address of the futex.

strace: Process 88542 attached
futex(0x7f9e696b3004, FUTEX_WAIT, 0, NULL

Notice that 0x7f9e696b3004 appears in both outputs.

Finally, I attempted to call FUTEX_WAKE on the futex. I used this code:

#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <sys/syscall.h>
#include <stdint.h>

#define futex(a, b, c, d, e, f) syscall(SYS_futex, a, b, c, d, e, f)

int main(void)
{
int rc;

  uint32_t *uaddr =  (uint32_t *)0x7f9e696b3004;
  printf("%p", uaddr);
  printf("\n");

  rc = futex(uaddr, 1, 0, (const struct timespec *)0, (int *)0, 0);
  printf("rc=%d\n", rc);
  printf("Value of errno: %d",errno);
  printf("\n");

  return 0;
} // main

Adapted from Mngt_futex_kernel

The result is

sudo ./fwake.out
0x7f9e696b3004
rc=-1
Value of errno: 14

I have tried identifying the physical address associated with the logical address. In this case, it is 0x1ae8101004. The result is still errno = 14.

How can I overcome this error?

Jacob Quisenberry
  • 1,131
  • 3
  • 20
  • 48
  • 4
    Why do you think you can use the futex address from one program in a completely unrelated program? Addresses are per-process. – Barmar May 04 '23 at 22:53
  • I expected that making the syscall with the virtual address would not work. I hoped that translating the virtual address to the physical address would. I forgot that making the syscall on that address means I made the call on that address in the virtual address space of the second process. – Jacob Quisenberry May 05 '23 at 15:00
  • User-mode processes never deal with physical addresses. Except for memory-mapped hardware ports, physical addresses are assigned dynamically by the MMU and change all the time. – Barmar May 05 '23 at 15:02
  • I even considered translating the physical address to the virtual address space of the second process. I then realized that if there is not shared memory, then there may not be a virtual address to point to the physical address. – Jacob Quisenberry May 05 '23 at 15:04
  • Exactly. If you want to share a futex between processes, you need to use shared memory. – Barmar May 05 '23 at 15:06

0 Answers0