I’ve been trying to figure out if there is a feasible way to prevent values from ever spilling from registers to the stack. I’ve looked for code or documentation on how to do this but haven’t found anything, apologies if this has already been done.
Recent security research has shown that protection schemes such as CFI (that might otherwise be secure) are undermined by sensitive values spilling to the stack. When security-critical values spill from registers to the stack, they can be read and overwritten by an attacker with arbitrary memory read or write capabilities. See “Losing Control” from CCS 2015 for more details on this sort of attack: https://www.ics.uci.edu/~perl/ccs15_stackdefiler.pdf
I think it would be great if we could allow values or at least virtual regs to be tagged as “security-sensitive” and disallow spilling of these values across their lifetime. I expect that the best way to do this would be to start at the virtual register level and push support up to IR values as well if and only if machine IR turns out to be insufficient.
Is this a good idea? Does something to support register pinning already exist? I’m unfortunately not familiar enough with the register allocators to know how to best support this, although I’m willing to give it a try if people can point me in the right direction.
I don't a real answer for you, but my 1st idea is that it would be an
attribute you could apply to a variable. How you *force* that to never
spill on the backend is an entirely different issue. You'd basically
force a split on a certain variable under certain conditions - would
it be possible - (why not) Unless I'm missing something it would be
hell to schedule though..
There's use cases in HPC and GPGPU codes which may benefit from
something similar, but for different reasons. (Some really hot
variable that you never want spilled for example)
That breaks the whole IR idea of using alloca to allocate/denote space for local variables, and then optimize those
into SSA values when optimization proves that is OK.
Also, for a lot of things, that attribute is simply impossible to implement. Any value that is live across a call needs to be spilled to memory.
You cannot put an unspillable value in a callee preserved register, because you cannot know whether the callee may save that or not.
And if it is in a caller-save register, then the caller has to spill it if it is live across a call.
Thanks, I hadn't thought about the HPC applications. I'm not sure that the
requirements for the HPC and security use-cases are compatible. Pinning for
performance can tolerate spills if it is still fast, while security uses
can tolerate slow rematerialization but not spills. Maybe a shared
infrastructure is possible but with variable constraints?
That breaks the whole IR idea of using alloca to allocate/denote space for
local variables, and then optimize those
into SSA values when optimization proves that is OK.
Agreed, but the values I care about (which is not really the HPC use-case)
would actually be values internal to the compiler, such as the location of
CFI metadata, which are never exposed to the front end. Thus I would be
happy with something at the machine IR level, after that abstraction has
been lost.
Also, for a lot of things, that attribute is simply impossible to
implement. Any value that is live across a call needs to be spilled to
memory.
You cannot put an unspillable value in a callee preserved register,
because you cannot know whether the callee may save that or not.
And if it is in a caller-save register, then the caller has to spill it if
it is live across a call.
Yes, but what about values that can be rematerialized? Of course values
that _need_ to be live across calls are out, but I'm more concerned about
values that were coalesced, hoisted, and which then get spilled. Maybe
prevention of coalescing for sensitive values and explicitly inserting
materialization at each use is an option here, although that sounds like
teaching a lot of passes about sensitive values, with no guarantee of
finding all of them. Wouldn't the register allocator still need to make
sure that sensitive values are not present in dead callee-saved registers,
so they don't get accidentally spilled by a callee?
Sounds like such a security-sensitive value would need to treat calls as barriers for any kind of reordering.
Also, at the end of the value’s live range, it would not have to be merely dead, but dead-and-buried (i.e. overwritten) to avoid scavenging by subsequent callees. Same goes for merely copying the value from one register to another, the source would have to be erased.
There are plenty of compilers which allow you to specify that a certain callee-save register is dedicated to holding a particular variable within a particular function or compilation unit. GCC, for example (register int *foo asm (“r12”);), or ARM Ltd’s compiler.
This doesn’t prevent some other library code that you call from temporarily saving and restoring that register.
Usually that is fine for correctness purposes (if you don’t have callbacks anyway), but it defeats your purpose.
GCC also has the -ffixed-reg command line option to prevent generated code from using that register at all.
I realize I am a few years late on this… Regarding the paper you provided, the authors demonstrate a patch that prevents the CFI-values fro spillling onto the stack. But I can’t find if their patch was ever implemented into clang. Does anyone know if this issue was ever addressed or why the authors patch never got implemented into Clang?