[cfe-commits] [PATCH] Fix support for naked C/C++ functions writing to unexpected memory regions in debug builds

Jeffrey Lim jeff at lim.com.au
Tue Nov 6 09:14:55 PST 2012


First time post to this list -- I think I posted to the wrong place before (cfe-dev). Hopefully I'm right this time.

The problem I was noticing is that a naked function would cause very unexpected behaviour. I simplified it to this test case:

test.c:

__attribute__((naked)) void NakedTest(int value, int value2)
{
	asm("");
}


clang -S test.cpp 
test.s:

	.section	__TEXT,__text,regular,pure_instructions
	.globl	__Z9NakedTestii
	.align	4, 0x90
__Z9NakedTestii:                        ## @_Z9NakedTestii
	.cfi_startproc
## BB#0:
	movl	%edi, -4(%rbp)
	movl	%esi, -8(%rbp)
	## InlineAsm Start
	## InlineAsm End
	ret
	.cfi_endproc


clang -flto -S test.cpp
test.s:

define void @_Z9NakedTestii(i32 %value, i32 %value2) nounwind uwtable noinline ssp naked {
entry:
  %value.addr = alloca i32, align 4
  %value2.addr = alloca i32, align 4
  store i32 %value, i32* %value.addr, align 4
  store i32 %value2, i32* %value2.addr, align 4
  call void asm sideeffect "", "~{dirflag},~{fpsr},~{flags}"() nounwind, !srcloc !0
  ret void
}

The bolded instructions above are generated in debug builds, causing problems. This happens in all architectures that I've tested on (ARM, x86, x64)


I've attached a patch file which I fixes the problem. I don't know if I've done it the right way (I only downloaded and looked at the source for clang for the first time 2 hours ago), but here's the newer result:

clang -S test.cpp
test.s:
	.section	__TEXT,__text,regular,pure_instructions
	.globl	__Z9NakedTestii
	.align	4, 0x90
__Z9NakedTestii:                        ## @_Z9NakedTestii
	.cfi_startproc
## BB#0:                                ## %entry
	## InlineAsm Start
	## InlineAsm End
	ret
	.cfi_endproc


clang -flto -S test.cpp
test.s:

define void @_Z9NakedTestii(i32, i32) nounwind uwtable noinline ssp naked {
entry:
  call void asm sideeffect "", "~{dirflag},~{fpsr},~{flags}"() nounwind, !srcloc !0
  ret void
}

Any comments, or alternative fixes to this problem would be greatly appreciated!

Thanks,
Jeffrey Lim

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20121107/b758ef61/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: CodeGenFunction.cpp.patch
Type: application/octet-stream
Size: 582 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20121107/b758ef61/attachment.obj>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20121107/b758ef61/attachment-0001.html>


More information about the cfe-commits mailing list