I'm looking to run inline asm in a thread but I get a seg fault on certain instructions, such as the below:
#include <thread>
void Foo(int &x)
{
int temp;
asm volatile ("movl $5, %%edx;"
"movl $3, %%eax;"
"addl %%edx, %%eax;"
"movl %%eax, -24(%%rbp);" // seg faults here
"movl -24(%%rbp), %0;"
: "=r" (temp) : : );
x=temp;
}
int main()
{
int x;
std::thread t1(Foo, std::ref(x));
t1.join();
return 0;
}
(I'm using std::ref
to be able to pass a reference to an std::thread
, but have to use the temp
variable because the extended asm syntax doesn't work with references.)
I've tried clobbering all involved registers and it doesn't help. If I don't pass any arguments to the thread, it seems to work: and it strangely also works if I clobber the %ebx
register (which isn't involved). I'm using gcc 4.8.4 on 64-bit Ubuntu 14.04.
What is the best/safest way I can execute inline asm in a thread?