I need the futex syscall in a 32-bit Linux process, but cannot use the syscall
function (header is unavailable). That could still be done by using inline asm, as follows:
#include <time.h>
#define SYS_futex 0xf0
// We need -fomit-frame-pointer in order to set EBP
__attribute__((optimize("-fomit-frame-pointer")))
int futex(int* uaddr, int futex_op, int val, const struct timespec* timeout, int* uaddr2, int val3)
{
register int ebp asm ("ebp") = val3;
int result;
asm volatile("int $0x80"
: "=a"(result)
: "a"(SYS_futex), "b"(uaddr), "c"(futex_op), "d"(val), "S"(timeout), "D"(uaddr2), "r"(ebp)
// : "memory" // would make this safe, but could cause some unnecessary spills. THIS VERSION IS UNSAFE ON PURPOSE, DO NOT USE.
);
if (result < 0)
{
// Error handling
return -1;
}
return result;
}
That compiles, as expected.
However, since we haven't specified the memory locations that may be read and/or written to, it could cause some sneaky bugs. So instead, we can use dummy memory inputs and outputs (How can I indicate that the memory *pointed* to by an inline ASM argument may be used?)
asm volatile("int $0x80"
: "=a"(result), "+m"(uaddr2)
: "a"(SYS_futex), "b"(uaddr), "c"(futex_op), "d"(val), "S"(timeout), "D"(uaddr2), "r"(ebp), "m"(*uaddr), "m"(*timeout));
When compiled with gcc -m32
, it fails with 'asm' operand has impossible constraints
. When compiled with clang -fomit-frame-pointer -m32
, it fails with inline assembly requires more registers than available
. I don't see why, though.
But, when compiled with -O1 -m32
(or any level other than -O0
), it compiles fine.
I see two obvious solutions:
- Use the
"memory"
clobber instead, which may be too restrictive, stopping the compiler from keeping unrelated variables in registers - Use
__attribute__((optimize("-O3")))
, which I'd like to avoid
Is there any other solution?