<div dir="ltr">Seems we also have a tangentially related issue with zero length variables too: <a href="https://bugs.llvm.org/show_bug.cgi?id=51848">https://bugs.llvm.org/show_bug.cgi?id=51848</a> fwiw. (will likely require a distinct solution, so I don't think there's much overlap here in terms of design/implementation - just conceptually related with some similar bugs in user code that this can create)</div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Wed, Sep 8, 2021 at 2:21 PM David Blaikie <<a href="mailto:dblaikie@gmail.com">dblaikie@gmail.com</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"><div dir="ltr"><div dir="ltr">On Wed, Sep 8, 2021 at 1:20 PM Reid Kleckner <<a href="mailto:rnk@google.com" target="_blank">rnk@google.com</a>> wrote:<br></div><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr">IMO TrapUnreachable goes farther than most users want: for every noreturn function call (abort, assert_fail, cxa_throw), it would add a trap instruction.</div></blockquote><div><br>Yep, there's a separate target option to turn that off: NoTrapAfterNoreturn. TrapUnreachable == true && NoTrapAfterNoreturn == ture is the mode I was considering in that direction.<br> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"> Those can be common, so that seems like a fairly high cost. And, it doesn't prevent the optimizer from exploiting UB to fold away a conditional branch to unreachable. If users want a mode with less UB, I think it would be more principled for the frontend to emit traps before every unreachable instruction, and to suppress the use of the LLVM noreturn function attribute.</div></blockquote><div><br>I think that's a separate option/mode - at least that's what clang does at -O0. Not sure if you can ask for it with other flags too.<br> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div>I think it would be cheap and non-controversial to emit traps instead of zero-length functions, which are really just labels pointing at unrelated code. Various targets and object file formats already require this behavior (MachO for atomization, others for other reasons). I think it would be reasonable to make this behavior standard to all platforms.</div></div></blockquote><div><br>Yeah - I'm OK with that too - though my attempt to implement it stalled out a bit (others with more knowledge/grit here might have more luck). Specifically I think that was the AMDGPU bit where it has to have functions attributed it a certain way to emit a trap instruction - and then I ran into a case where even inspecting (maybe the IR) instruction to check if it was zero length (to then add the attribute it needed) was insufficient since there were some cases where it became zero length... oh, yeah, a call to a null pointer, I think - just got elided entirely by the AMDGPU backend or something, I think.</div><div><br></div><div>- Dave<br> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Tue, Sep 7, 2021 at 5:24 AM Hans Wennborg via llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">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"><div dir="ltr"><div>Did anything come of this?</div><div><br></div><div>The Linux folks (cc Nick) keep running into issues where a function which ends with unreachable can fall through to an unrelated function, which confuses some machine code analyser they run (e.g. <a href="https://github.com/ClangBuiltLinux/linux/issues/1440" target="_blank">https://github.com/ClangBuiltLinux/linux/issues/1440</a>).</div><div><br></div><div>It seems TrapUnreachable would avoid such issues.</div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Fri, Mar 19, 2021 at 10:11 PM David Blaikie <<a href="mailto:dblaikie@gmail.com" target="_blank">dblaikie@gmail.com</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"><div dir="ltr">Just writing it down in this thread - this issue's been discussed a bit in this bug: <a href="https://bugs.llvm.org/show_bug.cgi?id=49599" target="_blank">https://bugs.llvm.org/show_bug.cgi?id=49599</a><br><br>And yeah, I'm considering adopting MachO's default (TrapUnreachable + NoTrapOnNoreturn) as the default for LLVM (will require some design discussion, no doubt) since it seems to capture most of the functionality desired. Maybe there are some cases where we have extra unreachables that could've otherwise been optimized away/elided, but hopefully nothing drastic.<br><br>(some platforms still need the trap-on-noreturn - Windows+AArch64 and maybe Sony, etc - happy for some folks to opt into that). I wonder whether TrapUnreachable shouldn't even be an option anymore though, if it becomes load bearing for correctness - or should it become a fallback option - "no trap unreachable" maybe means nop instead of trap, in case your target can't handle a trap sometimes (I came across an issue with AMDGPU not being able to add traps to functions that it isn't expecting - the function needs some special attribute to have a trap in it - but I guess it can be updated to add that attribute if the function has an unreachable in it (though then it has to recreate the no-trap-on-noreturn handling too when deciding whether to add the attribute... ))</div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Mon, Jul 27, 2020 at 9:20 AM Robinson, Paul <<a href="mailto:paul.robinson@sony.com" target="_blank">paul.robinson@sony.com</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"><br>
<br>
> -----Original Message-----<br>
> From: llvm-dev <<a href="mailto:llvm-dev-bounces@lists.llvm.org" target="_blank">llvm-dev-bounces@lists.llvm.org</a>> On Behalf Of Hans<br>
> Wennborg via llvm-dev<br>
> Sent: Monday, July 27, 2020 9:11 AM<br>
> To: David Blaikie <<a href="mailto:dblaikie@gmail.com" target="_blank">dblaikie@gmail.com</a>><br>
> Cc: llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a>>; Clang Dev <<a href="mailto:cfe-dev@lists.llvm.org" target="_blank">cfe-dev@lists.llvm.org</a>><br>
> Subject: Re: [llvm-dev] [cfe-dev] Zero length function pointer equality<br>
> <br>
> On Sat, Jul 25, 2020 at 3:40 AM David Blaikie <<a href="mailto:dblaikie@gmail.com" target="_blank">dblaikie@gmail.com</a>> wrote:<br>
> ><br>
> > Looks perfect to me!<br>
> ><br>
> > well, a couple of questions: Why a noop, rather than int3/ud2/etc?<br>
> <br>
> Probably no reason.<br>
<br>
FTR there is TargetOptions.TrapUnreachable, which some targets turn on<br>
(for X86 it's on for MachO and PS4), this turns 'unreachable' into ud2.<br>
Clearly it covers more than "empty" functions but is probably the kind<br>
of thing you're looking for.<br>
--paulr<br>
<br>
> <br>
> > Might be worth using the existing code that places such an instruction<br>
> > when building at -O0?<br>
> <br>
> I wasn't aware of that. Does it happen for all functions (e.g. I think<br>
> I got pulled into this due to functions with the naked attribute)?<br>
> <br>
> > & you mention that this causes problems on Windows - but ICF done by<br>
> > the Windows linker does not cause such problems? (I'd have thought<br>
> > they'd result in the same situation - two functions described as being<br>
> > at the same address?) is there a quick summary of why those two cases<br>
> > turn out differently?<br>
> <br>
> The case that we hit was that the Control Flow Guard table of<br>
> addresses in the binary ended up listing the same address twice, which<br>
> the loader didn't expect. It may be that the linker took care to avoid<br>
> that for ICF (if two ICF'd functions got the same address, only list<br>
> it once in the CFG table) but still didn't handle the "empty function"<br>
> problem.<br>
> <br>
> > On Fri, Jul 24, 2020 at 6:17 AM Hans Wennborg <<a href="mailto:hans@chromium.org" target="_blank">hans@chromium.org</a>> wrote:<br>
> > ><br>
> > > Maybe we can just expand this to always apply:<br>
> <a href="https://reviews.llvm.org/D32330" rel="noreferrer" target="_blank">https://reviews.llvm.org/D32330</a><br>
> > ><br>
> > > On Fri, Jul 24, 2020 at 2:46 AM David Blaikie via cfe-dev<br>
> > > <<a href="mailto:cfe-dev@lists.llvm.org" target="_blank">cfe-dev@lists.llvm.org</a>> wrote:<br>
> > > ><br>
> > > > LLVM can produce zero length functions from cases like this (when<br>
> > > > optimizations are enabled):<br>
> > > ><br>
> > > > void f1() { __builtin_unreachable(); }<br>
> > > > int f2() { /* missing return statement */ }<br>
> > > ><br>
> > > > This code is valid, so long as the functions are never called.<br>
> > > ><br>
> > > > I believe C++ requires that all functions have a distinct address<br>
> (ie:<br>
> > > > &f1 != &f2) and LLVM optimizes code on this basis (assert(f1 == f2)<br>
> > > > gets optimized into an unconditional assertion failure)<br>
> > > ><br>
> > > > But these zero length functions can end up with identical addresses.<br>
> > > ><br>
> > > > I'm unaware of anything in the C++ spec (or the LLVM langref) that<br>
> > > > would indicate that would allow distinct functions to have identical<br>
> > > > addresses - so should we do something about this in the LLVM<br>
> backend?<br>
> > > > add a little padding? a nop instruction? (if we're adding an<br>
> > > > instruction anyway, perhaps we might as well make it an int3?)<br>
> > > ><br>
> > > > (I came across this due to DWARF issues with zero length functions &<br>
> > > > thinking about if/how this should be supported)<br>
> > > > _______________________________________________<br>
> > > > cfe-dev mailing list<br>
> > > > <a href="mailto:cfe-dev@lists.llvm.org" target="_blank">cfe-dev@lists.llvm.org</a><br>
> > > > <a href="https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev" rel="noreferrer" target="_blank">https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev</a><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>
</blockquote></div></div>
_______________________________________________<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>
</blockquote></div></div>
</blockquote></div>