<div dir="ltr">IIRC, we do already have a verifier rule for this. At least, I'm pretty sure I added one.<div><br></div><div>I was going to say, passes are supposed to handle this by checking ReturnInst::isPrecededByMustTailCall, but apparently that helper doesn't exist yet! I just imagined it. :) Let's add it. I think if we had such a predicate, one that scans backwards skipping debug intrinsics and casts looking for a musttail calls or any other non-cast, non-musttail call instruction, it would make it easier for passes to get this right.</div><div><br></div><div>You are right, though, this construct is designed in a way that makes it hard for instrumentation passes to be correct by default, and it's not their fault that they are all "broken" according to the LangRef. The only other implementation strategy I can think of for this feature is to have a special "tail call" instruction that is both a call and return, since that's really what we are expressing with two separate instructions. That has a different set of tradeoffs, not all beneficial.<br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Sun, Nov 1, 2020 at 12:53 PM Philip Reames via llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org">llvm-dev@lists.llvm.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">I just skimmed your message, so I might be missing something, but first <br>
impression would be that this sounds like a missing verifier rule. Any <br>
reason the verifier can't check for this legality restriction? If it <br>
can, then the existing verify after all functionality should be <br>
sufficient to catch the bugs you're referencing?<br>
<br>
Philip<br>
<br>
On 10/30/20 7:47 PM, Xun Li via llvm-dev wrote:<br>
> Hi,<br>
><br>
> A musttail call must be preceded by an optional bitcast instruction<br>
> and a return instruction. It seems that there isn't a mechanism to<br>
> make sure the musttail call contract isn't broken by any instruction<br>
> insertion in the IR.<br>
> As far as I can tell, all IR transformations don't really look at<br>
> what's before a ReturnInst when inserting instructions. For example,<br>
> all Instrumentation passes ignored them, and would insert<br>
> instrumentation instructions in between musttail call and ReturnInst,<br>
> which would break the contract.<br>
> I fixed it for ASAN in <a href="https://reviews.llvm.org/D87777" rel="noreferrer" target="_blank">https://reviews.llvm.org/D87777</a> and fixed it<br>
> for TSAN in <a href="https://reviews.llvm.org/D87620" rel="noreferrer" target="_blank">https://reviews.llvm.org/D87620</a>.<br>
> But lately I also found that SafeStack pass has the same problem. For<br>
> instance, running "opt --safe-stack" on the following IR would crash<br>
> Clang:<br>
><br>
> target triple = "x86_64-unknown-linux-gnu"<br>
><br>
> declare i32 @foo(i32* %p)<br>
> declare void @alloca_test_use([10 x i8]*)<br>
><br>
> define i32 @call_foo(i32* %a) safestack {<br>
> %x = alloca [10 x i8], align 1<br>
> call void @alloca_test_use([10 x i8]* %x)<br>
> %r = musttail call i32 @foo(i32* %a)<br>
> ret i32 %r<br>
> }<br>
><br>
> I am pretty sure that SafeStack isn't the last pass that has this<br>
> problem and there will be more like this in the future.<br>
> I am trying to see if there are good systematic ways to solve this<br>
> problem without having to require every pass author to be careful when<br>
> inserting instructions before ReturnInst.<br>
><br>
> One solution I can think of is to update<br>
> IRBuilderBase::SetInsertPoint(Instruction *I) to always check if "I"<br>
> is a ReturnInst followed by a musttail call contract, and if so, move<br>
> the insertion point to before the call. Since this approach adds<br>
> overhead to every insertion point creation, I am not sure if it's the<br>
> best solution.<br>
><br>
> Advice and feedback much appreciated!<br>
><br>
_______________________________________________<br>
LLVM Developers mailing list<br>
<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a><br>
<a href="https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" rel="noreferrer" target="_blank">https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev</a><br>
</blockquote></div>