<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Thu, Apr 3, 2014 at 7:44 PM, Peter Collingbourne <span dir="ltr"><<a href="mailto:peter@pcc.me.uk" target="_blank">peter@pcc.me.uk</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div>On Thu, Apr 03, 2014 at 06:54:55PM -0700, Reid Kleckner wrote:<br>
> I think it's a little scary to assume things about LLVM's x86 code<br>
> generation. I haven't really finished the codegen side of the change, but<br>
> I'm pretty sure in it's current state it will emit extra loads and stores,<br>
> even if they are unnecessary.<br></div></blockquote><div><br></div><div>I have an implementation of my proposal that generates the right instructions and only those instructions. This is because of the other annotations on the jumptable function: optnone, noreturn, naked, and unwind. I've just posted my current version of the patch as <a href="http://llvm-reviews.chandlerc.com/D3292">http://llvm-reviews.chandlerc.com/D3292</a></div>
<div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div>
<br>
</div>Right, I had similar concerns. Now that I've thought about it a little more,<br>
I think that representing the jump table entries as IR functions in any<br>
form presents a risk that a change in code generation could endanger the<br>
correctness of the CFI mechanism.<br></blockquote><div><br></div><div>I guess this boils down to the question of whether or not it's possible to insist on naked functions with no optimization. My jumptable intrinsic is guaranteed by the IR verifier constraints to be the only reachable IR instruction in the function at code-generation time.</div>
<div><br></div><div>Because this instruction is an intrinsic and only undergoes lowering to a jump to a symbol, and because the function is annotated with naked, optnone, noreturn, and nounwind, I believe that I have complete control over the instructions in that function, and that there will be no preamble/postamble. Are there mechanisms that can insert other code in these circumstances? </div>
<div><br></div><div>From a perusal of CodeGen, it looks like the attribute optnone translates to CodeGenOpt::None, which in turn seems to turn off lots of work that would otherwise be performed on the function. </div><div>
<br></div><div>I can certainly add test cases that make sure that no extra code is added now and that will break if anyone ever adds code that does this.</div>
<div><br></div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
<br>
The other alternative we were thinking about was to put the responsibility for<br>
generating jump tables in the hands of the code generator using attributes<br>
[1]. From a security perspective, I think that might be preferable since<br>
the generation of the jump table would be tightly controlled.<br></blockquote><div><br></div><div>I see the value in terms of the security guarantees, though isn't it the case that other code could in principle be added to the function unless the jump-table generation was literally the last transform to run?</div>
<div><br></div><div>My concern about it is more to do with code duplication and maintainability for multiple backends. Consider the set of changes that the jump-table transformation does:</div><div><br></div><div>1. find all address-taken functions</div>
<div>2. create a jump-table function for each one</div><div>3. replace all address-taking sites with the appropriate jump-table function address</div><div>3. ensure that the jump-table functions are compiled into a form that admits useful runtime checks</div>
<div><br></div><div>Most of this is not target-specific, and these are all very natural IR operations. And I want them to work at least for X86 and ARM (I currently support both with my inline asm version), and probably for other backends, too. The only target-specific step is the lowering of the jump-table intrinsic to its equivalent branch statement, and the only reason that this is target-specific is that there's no way to represent an unconditional jump to a function in IR. So, I'd really like to confine the target-specific code to exactly that lowering.</div>
<div><br></div><div>Note that there are CFI transformations that don't depend on the tightness of the jump table: e.g., if the check code just compares a given pointer against the start and the end functions in the table, then function section annotations would suffice, since that would guarantee that all and only jumptable functions would be in a given section. I would view the tightness of the jump table more as an optimization than as a requirement.</div>
<div><br></div><div>It seems to me to be unnecessarily painful to have to fiddle with the lowering, e.g., of function symbols to their associated jump-table functions in the target backend because we don't want to represent the jump-table entries as functions at IR time.</div>
<div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
<br>
Peter<br>
<br>
[1] <a href="http://lists.cs.uiuc.edu/pipermail/llvmdev/2014-March/071340.html" target="_blank">http://lists.cs.uiuc.edu/pipermail/llvmdev/2014-March/071340.html</a><br>
<div><div><br>
><br>
><br>
> On Thu, Apr 3, 2014 at 6:28 PM, Peter Collingbourne <<a href="mailto:peter@pcc.me.uk" target="_blank">peter@pcc.me.uk</a>> wrote:<br>
><br>
> > On Wed, Apr 02, 2014 at 05:28:04PM -0700, Tom Roeder wrote:<br>
> > > On Fri, Mar 21, 2014 at 1:46 PM, Peter Collingbourne <<a href="mailto:peter@pcc.me.uk" target="_blank">peter@pcc.me.uk</a><br>
> > >wrote:<br>
> > ><br>
> > > > On Fri, Mar 21, 2014 at 12:54:07PM -0700, Tom Roeder wrote:<br>
> > > > > On Fri, Mar 21, 2014 at 12:15 PM, Peter Collingbourne <<br>
> > <a href="mailto:peter@pcc.me.uk" target="_blank">peter@pcc.me.uk</a>><br>
> > > > wrote:<br>
> > > > > >> The way I've implemented it (see the patch I sent to llvm-commits<br>
> > > > > >> yesterday), it's not just metadata: the intrinsic lowers to the<br>
> > > > > >> jumptable entry code given above. The CFI pass then generates a<br>
> > > > > >> function for each jump table; the function consists solely of<br>
> > these<br>
> > > > > >> intrinsic calls.<br>
> > > > > ><br>
> > > > > > Well, the intrinsic you proposed has no effect on the caller and<br>
> > has<br>
> > > > > > non-local effects on other specified functions. I'm not aware of<br>
> > any<br>
> > > > other<br>
> > > > > > intrinsic with similar behavior.<br>
> > > > ><br>
> > > > > I agree that it's not very similar to other intrinsics. But I don't<br>
> > > > > exactly follow these statements. There are definitely intrinsics that<br>
> > > > > have no effect on the caller, like llvm.var.annotation.<br>
> > > ><br>
> > > > Yes but the purpose of such intrinsics is to communicate information<br>
> > about<br>
> > > > a specific value that may have an effect on analysis, optimization or<br>
> > code<br>
> > > > generation for that caller. On the other hand, the intrinsic you are<br>
> > > > proposing<br>
> > > > has nothing to do with the caller.<br>
> > > ><br>
> > > > > And AFAIK,<br>
> > > > > there is no non-local behavior: all the intrinsic does is lower to<br>
> > the<br>
> > > > > labeled jump instruction; the changes to address-taken functions are<br>
> > > > > done separately by the CFI pass. Note that in the patch I sent, the<br>
> > > > > intrinsic only takes one argument: the function to jump to. Are there<br>
> > > > > other effects in this case?<br>
> > > ><br>
> > > > The non-local effect is that the intrinsic describes the definition of<br>
> > a<br>
> > > > function in the global scope. Normally such definitions come from<br>
> > top-level<br>
> > > > entities.<br>
> > > ><br>
> > > > > So, maybe it would be better to call it something like<br>
> > > > > @llvm.unconditional.jump(i8*)? I could then make it only lower to the<br>
> > > > > jump and add an intrinsic that lowered to a function label as well.<br>
> > > ><br>
> > > > I'd imagine that might present more problems. For example, if you used<br>
> > > > either intrinsic in the middle of a regular function the behavior<br>
> > would not<br>
> > > > necessarily be well defined. It would be necessary to carefully<br>
> > document<br>
> > > > where and how these intrinsics may be used.<br>
> > > ><br>
> > ><br>
> > > Taking these considerations into account, I propose this:<br>
> > ><br>
> > > Jump functions created for the jump table are actual functions, marked<br>
> > with<br>
> > > a new jumptable attribute, as well as naked and noreturn and optnone.<br>
> > Each<br>
> > > table is put in a special section using the section attribute, and<br>
> > > alignment of these functions is done using the align attribute. The<br>
> > > combination of these attributes means that the function has no preamble<br>
> > or<br>
> > > postamble, so it consists of exactly a global function label and its<br>
> > > instructions.<br>
> > ><br>
> > > Then I think it would make sense to create an intrinsic like<br>
> > > llvm.jumptable.instr(i8*) that would be a placeholder for an<br>
> > unconditional<br>
> > > jump. I can add code to the verifier that insists on two conditions:<br>
> > ><br>
> > > 1. functions marked as jumptable must have exactly two instructions: an<br>
> > > llvm.jumptable.instr followed by a unreachable.<br>
> > > 2. llvm.jumptable.instr can only occur in a jumptable function.<br>
> > ><br>
> > > I think this handles the problem of using llvm.jumptable.instr in normal<br>
> > > functions: that isn't allowed. And I think it deals with the problems of<br>
> > > non-local behavior by making functions out of the things that really are<br>
> > > functions. Each entry in the jump table really is a function, albeit a<br>
> > > rather strange one, so it should look like a function at the IR level.<br>
> > ><br>
> > > In other words, this change would add a new attribute that marks a very<br>
> > > specialized kind of function and an intrinsic that can only occur in this<br>
> > > kind of function.<br>
> > ><br>
> > > What do you think?<br>
> ><br>
> > I think you might be close.<br>
> ><br>
> > The llvm.jumptable.instr intrinsic you have proposed is similar to the<br>
> > 'musttail' call marker that Reid (cc'd) is working on in [1]. That marker<br>
> > will<br>
> > also have verifier support. I think it might be reasonable to require the<br>
> > code generator to emit the body of a function containing only a 'musttail'<br>
> > function call as a single branch instruction. Then you could emit such<br>
> > functions in the jump table section.<br>
> ><br>
> > Reid, what do you think?<br>
> ><br>
> > Thanks,<br>
> > --<br>
> > Peter<br>
> ><br>
> > [1] <a href="http://llvm-reviews.chandlerc.com/D3240" target="_blank">http://llvm-reviews.chandlerc.com/D3240</a><br>
> ><br>
<br>
</div></div><span><font color="#888888">--<br>
Peter<br>
</font></span></blockquote></div><br></div></div>