[cfe-dev] How do I prevent cloning of code?

Markus Gutschke (顧孟勤) markus at google.com
Thu Oct 4 18:22:32 PDT 2012


I am writing some low-level sandboxing code on Linux, and an unusual
problem came up. I need to write an assembly function that has a
"magic" instruction; and the kernel needs to know about the location
of this instruction.

This is relatively simple to do from an "asm()" instruction; I can
just make it return the address of this instruction. But all of that
fails, if the C/C++ compiler decides to duplicate my assembly code.
The kernel wouldn't know what to do, if there are suddenly multiple of
these "magic" instructions all over the source code.

In gcc, I believe I can put my "asm()" code into a function, and then
mark the function as "noinline" and "noclone". For good measure, I
also mark the assembly as "volatile", but I am not sure that is
strictly necessary.

clang doesn't appear to have a "noclone" attribute. This causes two
problems: a) unconditionally setting the "noclone" attribute results
in a warning, which fails because of -Werror in our build scripts, and
b) I am worried that clang might actually decide to clone my function.

I currently put the function into a separate compilation unit, so
things might turn out OK anyway. But I'd much rather tell the compiler
what I want it to do.

As a last resort, I could move the code into a *.S file (or more
likely, one *.S file per architecture). But since it deals with
variadic arguments, that's a little awkward and probably needs some C
glue anyway. Having everything in a single *.c or *.cc file would be
my preference.


Markus



More information about the cfe-dev mailing list