Futex Syscall in C Always Returns errno 14

135 Views Asked by At

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?

0

There are 0 best solutions below