[LLVMdev] clang modifying clobbered register in case of inline assembly resulting in data corruption

70 views
Skip to first unread message

Karthik Bhat

unread,
Nov 29, 2012, 2:49:40 AM11/29/12
to llv...@cs.uiuc.edu
Hi All,
I'm looking into this simple inline assembly code.
Were we copy contents on data to eax,ebx,ecx and edx and later copy
them back from the registers to data.

Test Case -

#include <stdio.h>

int data[] = {
0x14131211,
0x24232221,
0x34333231,
0x44434241,
};

int
main (int argc, char **argv)
{
asm ("mov 0(%0), %%eax\n\t"
"mov 4(%0), %%ebx\n\t"
"mov 8(%0), %%ecx\n\t"
"mov 12(%0), %%edx\n\t"
: /* no output operands */
: "r" (data)
: "eax", "ebx", "ecx", "edx");
asm ("nop");

asm ("mov %%eax, 0(%0)\n\t"
"mov %%ebx, 4(%0)\n\t"
"mov %%ecx, 8(%0)\n\t"
"mov %%edx, 12(%0)\n\t"
: /* no output operands */
: "r" (data)
: "eax", "ebx", "ecx", "edx");

printf("data[0] = %x \n",data[0]);
printf("data[1] = %x \n",data[1]);
printf("data[2] = %x \n",data[2]);
printf("data[3] = %x \n",data[3]);

return 0;
}


In this case the value of data[0] has got corrupted as clang is not
honoring clobbered register and stores a different value in $eax.


Similarly when compiled with -fPIE option, GCC emits an error as -

i386-pseudo.c: In function ‘main’:
i386-pseudo.c:13:3: error: PIC register clobbered by ‘ebx’ in ‘asm’
i386-pseudo.c:22:3: error: PIC register clobbered by ‘ebx’ in ‘asm’

Clang compiles successfully without reporting this error.

Could someone guide me which part of code to look into to fix this issue.

Thanks
Karthik

_______________________________________________
LLVM Developers mailing list
LLV...@cs.uiuc.edu http://llvm.cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev

Eli Friedman

unread,
Nov 29, 2012, 4:07:17 AM11/29/12
to Karthik Bhat, llv...@cs.uiuc.edu
On Wed, Nov 28, 2012 at 11:49 PM, Karthik Bhat <karthik...@gmail.com> wrote:
> In this case the value of data[0] has got corrupted as clang is not
> honoring clobbered register and stores a different value in $eax.

Not a bug; that isn't how clobbers work. We don't make any guarantees
about the values of registers between inline asm statements. If you
need an operand in a particular register, see
http://gcc.gnu.org/onlinedocs/gcc/Local-Reg-Vars.html etc.

-Eli
Reply all
Reply to author
Forward
0 new messages