<div dir="ltr"><div>Hi Ralf,<br></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Sun, Jun 13, 2021 at 5:09 PM Ralf Jung 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">
to add to what Nuno said, I came up with a series of optimizations which <br>
demonstrate that dead store elimination, integer "substitution" (GVN replacing <br>
'a' by 'b' after an 'a == b'), and provenance-based alias analysis, are in <br>
conflict with each other: the current LLVM semantics are inconsistent and can <br>
lead to miscompilations.<br>
<br>
I posted them here as Rust code: <br>
<<a href="https://github.com/rust-lang/unsafe-code-guidelines/issues/286#issuecomment-860189806" rel="noreferrer" target="_blank">https://github.com/rust-lang/unsafe-code-guidelines/issues/286#issuecomment-860189806</a>>. <br>
A direct translation to C wouldn't be correct due to strict aliasing, but if you <br>
imagine this being LLVM IR (or C code compiled with -fno-strict-aliasing), then <br>
I hope this example demonstrates that *something* needs to give. This is not <br>
just a small bug somewhere, it is a case of the implicit assumptions made by <br>
different optimization passes / instructions being mutually incompatible.<br></blockquote><div><br></div><div>Agreed. Reading your example as LLVM IR, I think we have a choice to make: either memory is typed or it isn't. By "typed" I mean that an integer load following a pointer store or vice versa returns poison or is immediate UB.</div><div><br></div><div>If memory is typed, then your original example program has UB in at least two places:</div><div><br></div><div>1. A pointer stored via storage_ptr is loaded via storage_usize, used in a comparison, and the comparison result is branched on. This would be UB.</div><div>2. An integer is stored via storage_usize and then loaded via storage_ptr (pointer load) and then dereferenced. This would also be UB.<br></div><div> </div><div>If memory is untyped and you want the original program to have defined behavior, my first thought is that the resolution that makes the most sense is to say that removing the store to *storage_usize is incorrect because that store is not truly redundant: it changes the pointer provenance that is associated with the underlying memory. The other optimizations in that chain of optimizations seem of higher value (keeping in mind that *most* dead store elimination is still correct, it's just this particular narrow case which isn't).<br></div><div><br></div><div>Cheers,</div><div>Nicolai</div><div><br></div><div><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>
Kind regards,<br>
Ralf<br>
<br>
On 06.06.21 20:26, Nuno Lopes wrote:<br>
> Let me try to explain the issue around typed memory. It’s orthogonal to TBAA. <br>
> Plus I agree the allocation type is irrelevant; it’s the type of the load/store <br>
> operations that matters.<br>
> <br>
> Fact 1: LLVM needs an “universal holder” type in its IR.<br>
> <br>
> Why?<br>
> <br>
> * Frontends load data from memory and don’t necessarily know the type they are<br>
> loading. When you load a char in C, it can be part of a pointer or an<br>
> integer, for example. Clang has no way to know.<br>
> * That’s necessary to express implementations of memcpy in IR, as it copies<br>
> raw data without knowing what the type is.<br>
> * LLVM lowers small memcpys into load/store.<br>
> <br>
> I hope you agree LLVM needs a type that can contain any other type.<br>
> <br>
> Fact 2: optimizations for this “universal holder” type need to be correct for <br>
> any LLVM IR type.<br>
> <br>
> Since this type can hold anything, optimizations that apply to this type must be <br>
> correct for integers, pointers, etc. Otherwise, the results would be incorrect <br>
> if the data was cast to the “wrong” type.<br>
> <br>
> The corollary of this fact is that GVN is not correct for the “universal holder” <br>
> type in general. That’s because GVN is not correct for pointers in general <br>
> (though correct in some cases). GVN doesn’t work for pointers since just because <br>
> 2 pointers compare equal, it doesn’t mean they have the same provenance. As <br>
> LLVM’s AA uses provenance information, this information must be preserved.<br>
> <br>
> Hopefully you agree with everything I wrote so far. If not please stop me and ask.<br>
> <br>
> Ok, so what’s the solution?<br>
> <br>
> We have two candidates for this “universal holder” type:<br>
> <br>
> * integer (i/<x>/)<br>
> * byte (b/<x>/), a new dedicated type<br>
> <br>
> The advantage of going with integers is that they already exist. The <br>
> disadvantage? To make LLVM correct, we would need to disable GVN in most cases <br>
> or stop using provenance information in AA (make pointers == integers).<br>
> <br>
> This sounds pretty bad to me due to the expect performance hit.<br>
> <br>
> Introducing a new type requires work, yes. But it allows us to keep all integer <br>
> optimizations as aggressive as we want, and only throttle down when encountering <br>
> the byte type. Another benefit is that loading a byte from memory doesn’t escape <br>
> any pointer, while with the first solution you need to consider all stored <br>
> pointers as escaped when loading an integer.<br>
> <br>
> Introducing the byte type allow us to fix all the integer/pointer casts bugs in <br>
> LLVM IR. And it enables more optimizations as we won’t need to be careful <br>
> because of the LLVM IR bug.<br>
> <br>
> A 3^rd option is to keep everything as-is. That is, keep wrong optimizations in <br>
> LLVM and keep adding hacks to limit the miscompilations in real-world programs.<br>
> <br>
> That has bitten us multiple times. Last time someone told me my miscompilation <br>
> example was academic, LLVM ended up miscompiling itself, and then miscompiled <br>
> Google’s code. Only this year we are planning to fully fix that bug (we have a <br>
> hack right now).<br>
> <br>
> Last week Alive2 caught a miscompilation in the Linux kernel, in the network <br>
> stack. The optimization got the pointer arithmetic wrong. Pretty scary, and the <br>
> bug may have security implications.<br>
> <br>
> Anyway, just to argue that leaving things as-is is not an option. We have <br>
> momentum and 3 GSoC students ready to help to fix the last few design bugs in <br>
> LLVM IR.<br>
> <br>
> I know this stuff is complex; please ask questions if something is not clear or <br>
> if you disagree with anything I wrote above.<br>
> <br>
> Thanks,<br>
> <br>
> Nuno<br>
> <br>
> *From:* Chris Lattner via llvm-dev<br>
> *Sent:* 06 June 2021 05:27<br>
> *To:* John McCall <<a href="mailto:rjmccall@apple.com" target="_blank">rjmccall@apple.com</a>><br>
> *Cc:* <a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a>; Ralf Jung <<a href="mailto:jung@mpi-sws.org" target="_blank">jung@mpi-sws.org</a>>; <a href="mailto:cfe-dev@lists.llvm.org" target="_blank">cfe-dev@lists.llvm.org</a><br>
> *Subject:* Re: [llvm-dev] [cfe-dev] [RFC] Introducing a byte type to LLVM<br>
> <br>
> On Jun 4, 2021, at 11:25 AM, John McCall via cfe-dev <<a href="mailto:cfe-dev@lists.llvm.org" target="_blank">cfe-dev@lists.llvm.org</a> <br>
> <mailto:<a href="mailto:cfe-dev@lists.llvm.org" target="_blank">cfe-dev@lists.llvm.org</a>>> wrote:On 4 Jun 2021, at 11:24, George Mitenkov <br>
> wrote:<br>
> <br>
> Hi all,<br>
> <br>
> Together with Nuno Lopes and Juneyoung Lee we propose to add a new byte<br>
> type to LLVM to fix miscompilations due to load type punning. Please see<br>
> the proposal below. It would be great to hear the<br>
> feedback/comments/suggestions!<br>
> <br>
> <br>
> Motivation<br>
> ==========<br>
> <br>
> char and unsigned char are considered to be universal holders in C. They<br>
> can access raw memory and are used to implement memcpy. i8 is the LLVM’s<br>
> counterpart but it does not have such semantics, which is also not<br>
> desirable as it would disable many optimizations.<br>
> <br>
> I don’t believe this is correct. LLVM does not have an innate<br>
> concept of typed memory. The type of a global or local allocation<br>
> is just a roundabout way of giving it a size and default alignment,<br>
> and similarly the type of a load or store just determines the width<br>
> and default alignment of the access. There are no restrictions on<br>
> what types can be used to load or store from certain objects.<br>
> <br>
> C-style type aliasing restrictions are imposed using |tbaa|<br>
> metadata, which are unrelated to the IR type of the access.<br>
> <br>
> I completely agree with John. “i8” in LLVM doesn’t carry any implications about <br>
> aliasing (in fact, LLVM pointers are going towards being typeless). Any such <br>
> thing occurs at the accesses, and are part of TBAA.<br>
> <br>
> I’m opposed to adding a byte type to LLVM, as such semantic carrying types are <br>
> entirely unprecedented, and would add tremendous complexity to the entire system.<br>
> <br>
> -Chris<br>
> <br>
<br>
-- <br>
Website: <a href="https://people.mpi-sws.org/~jung/" rel="noreferrer" target="_blank">https://people.mpi-sws.org/~jung/</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><br clear="all"><br>-- <br><div dir="ltr" class="gmail_signature">Lerne, wie die Welt wirklich ist,<br>aber vergiss niemals, wie sie sein sollte.</div></div>