r/programming • • Feb 01 '20

Emulator bug? No, LLVM bug

https://cookieplmonster.github.io/2020/02/01/emulator-bug-llvm-bug/
282 Upvotes

87 comments sorted by

35

u/bbolli Feb 01 '20

Did you find out what caused the differences on the two CPUs?

33

u/CookiePLMonster Feb 01 '20

I didn't. However, given that the crash happens once freed memory is reused/decommited, I assume it is somewhat related to whatever heuristics control that behaviour - e.g. how soon this memory is needed again etc.

11

u/lurking_bishop Feb 02 '20

Yeah, just because std::vector isn't guaranteed to grow contiguously, there's nothing that forbids it from doing so either.

6

u/CookiePLMonster Feb 02 '20

Good point! One thing I haven't done (and should have) was running with Page Heap on the 7800X PC. I am sure it'd force the bug to occur the same way.

25

u/[deleted] Feb 02 '20

I still think that SmallVector is getting called out harder than it deserves. You’d get the same thing with vector.reserve(64), for instance.

Otherwise, good bug hunting, and good solutions.

3

u/CookiePLMonster Feb 02 '20

You're right, but the difference is that with vectors an explicit reserve may force you to think twice - SmallVector makes this behaviour implicit so IMO you're more likely to forget.

1

u/flatfinger Feb 02 '20

I wonder if it would be useful to have a type which would could be used interchangeably with vector in cases where it would never need to grow beyond a pre-specified size, but which would trigger a fatal error if relocation would become necessary? Being able to assume that the backing store for such a type will have a non-changing address can sometimes be useful, and there should be nothing "wrong" with having code do so when it offers some real benefit, provided that it uses a vector type where such relocation is guaranteed not to occur. If changing requirements for a program cause the required size to increase slightly, having a fatal error indicate that a fixed-address object was allocated with 130 entries but asked to grow beyond that would be far more useful than silently breaking all references to items within the object.

1

u/CookiePLMonster Feb 02 '20

That would require an implication there is a hard limit to how many items this vector can store - and I have no idea if such assumption can be made here.

1

u/flatfinger Feb 02 '20

A "guaranteed not to relocate" vector would only be usable in situations where the maximum required size is known in advance of its creation. If that would be workable here, and if code gained some advantage from not having to allow for relocation, then such a type would be useful here. If no bound could be safely predicted, then code will have to be reworked to avoid any reliance upon objects' address stability.

In any case, a function whose job is to e.g. add an item to a vector shouldn't need to know or care whether or not the vector is expandable. If the vector can accommodate the new item, great. If for whatever reason it can't, the operation will fail. No need for the function to be concerned about why.

1

u/CookiePLMonster Feb 02 '20

I'm inclined to believe reworking not to assume stability of references is easier, given how one of the two proposed fixes did that and basing on my tests it's stable. Sure,other cases may still be broken but it does not result in any new issues.

→ More replies (1)

5

u/matthieum Feb 02 '20

You’d get the same thing with vector.reserve(64), for instance.

Not quite.

vector.reserve(...) still performs a memory allocation, while a SmallVector initially doesn't and uses the stack instead.

If most of the time you only need a small number of elements, eschewing a memory allocation -- and thus deallocation -- is an easy performance win. Not necessarily major, but easy, so why not?

4

u/[deleted] Feb 02 '20

I was referring to whether you’d hide UAFs or not. I know that SmallVector doesn’t allocate at first.

At the point where you get a heap use-after-free from SmallVector, std::vector will also necessarily trigger a use-after-free. It’s true that that on the first reallocation, SmallVector only puts “garbage” in the first handful of its storage bytes.

2

u/matthieum Feb 02 '20

I... completely misunderstood you :) And you're right.

2

u/smbear Feb 03 '20

Do I understand correctly, that SPU cache is responsible for compiling PS3 native instructions (is it Cell CPU?) to amd64 instructions? Does QEMU have anything similar?

1

u/CookiePLMonster Feb 03 '20

Kind of. From what I know, RPCS3 gathers SPU programs as the game executes (they are kind of like compute shaders so you cannot gather them ahead of time), translates them to IR and then uses LLVM to compile that to amd64 code. For subsequent runs, those SPU programs are being cached so instead of compiling on runtime, they are prepared as the emulation starts. This essentially makes games run better over time, because less on-the-fly compilation is taking place as you gather more shaders and SPU programs.

As for QEMU, no idea.

33

u/flatfinger Feb 01 '20

I wonder if the apparent-use-after-free could tie in with LLVM's seemingly fundamental (and fundamentally unsound) assumption that two pointers which hold the same address may be freely substituted? Consider, for example, clang's behavior with this gem (which I would expect is a consequence of LLVM's optimizations):

    #include <stdint.h>
    int test(int * restrict p, int *q, int i)
    {
        uintptr_t pp = (uintptr_t)(p+i);
        uintptr_t qq = (uintptr_t)q;
        if (pp != qq) return 0;
        p[0] = 1;
        p[i] = 2;
        return p[0]; // Generated code returns a constant 1
    }

The restrict qualifier does not forbid the mere existence of pointers that have the same address as a restrict pointer, but aren't actually used to access any objects in conflicting fashion. The above code doesn't do anything with pointer q except convert it to a value of type uintptr_t which is never used to synthesize any other pointer. Nonetheless, the compiler assumes that because p+i and q have the same representation, it may freely replace any accesses to p[i] with accesses to *q. Because the compiler would not be required to recognize that an access made via pointer based upon q might affect p[0], it ignores the possibility that an access to p[i] might affect q.

The Standard's definition of "based upon" becomes ambiguous in the last three statements of the above function, but under any reasonable reading I can fathom, either nothing is based upon p within that context (in which case p[i] would be allowed to access the same storage as p[0]) or p[i] and p[0] would both be based upon p (allowing the access in that case too).

If there are any comparisons between pointers in the vicinity of the problematic code, I would suggest investigating the possibility that clang is using them to infer that an object can't change despite the fact that it actually can.

23

u/[deleted] Feb 02 '20

It’s a cool bug, but I wonder how you’ve come to associate it with this article.

19

u/flatfinger Feb 02 '20

Memory-management code frequently needs to compare pointers that have the same addresses but are not usable to access the same objects. If an object gets relocated but old references are updated using pointers which the compiler regards as incapable of accessing their targets, that could easily leave dangling pointers to the old object.

While I don't know of any particular chain of events via which LLVM's unsound inferences (which it also tends to share with gcc, btw) would yield the described behavior, I can easily imagine a chain of events via which it could.

14

u/[deleted] Feb 02 '20

I found that it was pretty well-explained that the UAF is caused by a vector being resized.

2

u/flatfinger Feb 02 '20

Yes, but I thought the problem was that when the vector got resized, not all references to its address got adjusted.

14

u/[deleted] Feb 02 '20

It's about & references, not abstract memory references, like this:

vector<int> foo = {1, 2, 3};
int& bar = foo[1];
foo.resize(...large value...);
bar = 4;

but with LLVM SmallVectors instead of std::vector.

7

u/CookiePLMonster Feb 02 '20

On top of that, I have a feeling that /u/flatfinger is talking about code generated by LLVM, while this is the inverse - code in this case is generated by Visual Studio compiler, and relates to LLVM's code per se. So yeah, unrelated.

3

u/flatfinger Feb 02 '20

Sorry--I mistakenly thought that LLVM was being used to bootstrap itself. Didn't Visual Studio move to using LLVM for its back end?

→ More replies (1)

3

u/Enamex Feb 01 '20

I don't even get how it can make that assumption. Nothing in the code flow and what I know of C precludes p[i] from pointing to p[0] in any reasonable fashion.

7

u/flatfinger Feb 01 '20

I'm not sure what you mean by "how it can make that assumption". It makes that assumption because it was programmed to do so.

The Standard would allow an implementation make either of two assumptions if it knew that p+i and q identify the same address:

  1. Accesses to p[i] may be replaced by accesses to *q, which might improve performance by virtue of the simpler address computation.

  2. Accesses to *q may be assumed not to affect p[0] (a compiler could assume this whether or not it knew about the equality of p+i and q).

So far as I can tell, LLVM seems unable to reliably recognize cases where two or more assumptions are individually valid and would justify optimizations, but optimizations based upon one assumption would invalidate the other.

4

u/Enamex Feb 01 '20

Aha. Now I see it. Thanks.

"how it can make that assumption" = "I don't seem to know the rules it's building off of to that to decide what it does".

6

u/flatfinger Feb 02 '20

"I don't seem to know the rules it's building off of to that to decide what it does".

For years before the Standard was ratified, and continuing ever since, there have been many situations which different implementations would process constructs in the same useful fashion, but do so for different reasons. Rather than trying to document detailed rules which would require some implementation to expend lots of effort handling useless corner changes, the Standard sought instead to describe a few cases where implementations should be expected to behave consistently, and figured that implementations meeting those would naturally as a consequence also handle other cases their customers would require.

Unfortunately, gcc and clang both evolved abstraction models which are grossly incompatible with a lot of legacy code and can't really handle all of the corner cases in the Standard. There are some cases in the Standard that would needlessly handle optimization, but there are others such as the Common Initial Sequence guarantees which were included to facilitate constructs beyond those mandated by the Standard. From what I can tell, the maintainers of these compilers view places where the Standard doesn't fit their abstraction model as defects in the Standard; I'm not sure whether one can really describe as a "bug" a situation where clang and gcc both behave in the same nonsensical fashion.

→ More replies (1)

2

u/[deleted] Feb 02 '20

I believe Rust has had to frequently turn off optimizations based on aliasing because LLVM doesn't actually do the optimizations properly. Since Rust can guarantee that there is no overlap and memory is uniquely accessed. Part of the reason is that it probably only sees limited use in C.

3

u/flatfinger Feb 02 '20

What's maddening is that the behavior of `restrict` could have been defined much more easily and usefully if the authors of the Standard had recognized the concept of a pointer that is "at least potentially" based on another. Most operators that yield a pointer or lvalue have a source operand upon which the result is based. The few cases that end up being ambiguous could easily be resolved by saying that if the representation of a restrict pointer is examined, and another pointer is subsequently synthesized in a way that could have a control or data dependency upon the result of that examination, the latter pointer is "at least potentially" based upon the first.

Under such an analysis, all expressions of the form `p+intval` would be pointers based upon `p`. While `p+(q-p)` might always hold an address that is coincidentally equal to `q`, it would still be an expression of the form `p+intval`, and thus based upon `p`.

If the abstraction model used by LLVM is unable to handle the notion of one pointer being "at least potentially" based upon another, I would regard that as a defect in the abstraction model even though the authors of LLVM might view as defective any language standards that would require such a notion.

2

u/[deleted] Feb 02 '20

I think this is one of the major issues with design by committee. Well, other than design by committee.

But that is to say, that you can't ever really be certain of the standard unless one of the guys who was there, is right there with you when you're implementing it.

4

u/flatfinger Feb 02 '20

If one looks at CompCert C, it is deliberately designed so that the only optimizations it allows are those which can be performed in arbitrary combinations, which means that code after each stage of optimization will be semantically equivalent to code before. I don't know what kind of committee designed LLVM, but if multiple people were involved in different stages of optimization, it's not hard to imagine that they had inconsistent ideas about what constructs should be considered equivalent at different stages of optimization.

As for the Committee that developed the C Standards, even they could have no idea of what various things "mean" if there's never been a consensus understanding. The portion of the C99 Standard that talks about the Common Initial Sequence guarantees, for example, gives an examples of code which is obviously strictly conforming given the rules as described, and an example that obviously isn't, but fails to give an example of code where the rule as stated is unclear--most likely because some committee members would veto the code as an example of a strictly-conforming program, but others would veto its inclusion as an example of a non-strictly-conforming program.

1

u/rhetoricalanarchist Feb 02 '20

How are q and p+i related? I'm not sure I understand the bug here. It looks like they point to different things?

1

u/flatfinger Feb 02 '20

They do, and a compiler would be entitled to assume that the lvalue `*q` won't be used to access `p[0]`, but the fact that `p[i]` is coincidentally equal to some unrelated pointer `q` shouldn't cause a compiler to assume that `p[i]` can't access `p[0]`.

3

u/ilammy Feb 02 '20 edited Feb 02 '20

shouldn't cause a compiler to assume that p[i] can't access p[0]

But it can. restrict promises the compiler that p and q never refer to the same object. Therefore &p[i] is never equal to &q for all values of i. Therefore pp != qq is always true. Therefore i != 0 (because it would have to be zero for pp == qq to be true). Therefore p[i] is never referring to the same object as p[0]. For want of a restrict, the correctness was lost.

The compiler assumes that you know all of this and never pass this function arguments that violate these assumptions, because otherwise your program’s behavior is undefined. That is, you are to pass only pointers to unrelated objects which cause zero to be returned. Everything else is undefined behavior – hence always one.

7

u/SirClueless Feb 02 '20

restrict promises the compiler that p and q never refer to the same object.

This is not true. restrict only promises that if an object accessed through p is modified, then all accesses (reads and writes) to that object will be done through p. This is true if *q is never accessed, whether or not it aliases.

→ More replies (9)

56

u/chochokavo Feb 01 '20

Looks like it's time to rewrite LLVM in Rust :)))

41

u/birdbrainswagtrain Feb 01 '20 edited Feb 02 '20

Cranelift is a thing. It's obviously a lot less mature than LLVM, and seems to have slightly different goals. My understanding is that LLVM is too slow to be used in a real JIT most JIT applications, whereas that seems to be one of Cranelift's goals.

Cranelift is designed to be a code generator for WebAssembly, but it is general enough to be useful elsewhere too.

5

u/SpacemanCraig3 Feb 01 '20

Isn't the llvm jit what Julia uses?

16

u/birdbrainswagtrain Feb 02 '20

I don't know. My guess is that the Julia people are willing to sacrifice the speed of code generation, since having optimized code is considered really important for scientific computing applications. It's not like there's a user who's going to get angry because your program is busy getting compiled.

16

u/CQQL Feb 02 '20

That user was me a few days ago. I programmed a small thing in Julia and all the wait times were so frustrating. Julia is JIT-compiled only but not interpreted (so 100% of the code gets compiled before execution) and does not support partial compilation or reusing compiled artifacts. So everytime I ran my program, I had to wait 24 seconds for my imports to compile. That added up to a lot of staring at screens because 24 seconds is long enough to be annoying and too short to do anything else.

8

u/birdbrainswagtrain Feb 02 '20

Wow. That is significantly worse than what I had imagined.

→ More replies (4)

8

u/SpacemanCraig3 Feb 02 '20

Fair point. Julia would be really well suited to general purpose use though if there wasn't such a long startup time for even simple stuff.

2

u/seamsay Feb 02 '20

Funnily enough, slow compilation is one of the massive pain points in Julia that people keep complaining about.

2

u/VirginiaMcCaskey Feb 02 '20

It's also slow af

1

u/bumblebritches57 Feb 03 '20

LLVM is used in jit mode for all kinds of things, like recompiling shaders on the fly for computers that aren't powerful enough.

Apple does that.

11

u/millstone Feb 02 '20

If every instance of this comment were a line of code then it'd already be done.

3

u/falconfetus8 Feb 02 '20

Talk about bootstrapping

2

u/AngularBeginner Feb 02 '20

It's time to rewrite Rust in Rust!

6

u/immibis Feb 02 '20

Is it not already?

16

u/AngularBeginner Feb 02 '20

It is, so? You can still rewrite it.

1

u/chochokavo Feb 02 '20

But it will lead to the Singularity!

0

u/[deleted] Feb 01 '20

[deleted]

-38

u/shevy-ruby Feb 01 '20

Wait until the Rust people do so!

They want to rewrite ALL THE THINGS (well at the least C and C++).

47

u/Uristqwerty Feb 01 '20

Rewriting everything in Rust seems like a meme perpetuated at least as much by non-rust-users. If there's a constant influx of new community members, and the first thing half of them post is a joke about RiiR-for-the-sake-of-RiiR, it would quickly become very tiresome to the established members.

No, I'd think (without bothering to research actual statistics for my assumptions, as is reddit tradition) that meme is largely perpetuated by /r/programming users who only really hear about the language once every few days. And pcj, but I'd hope they're at least self-aware enough to keep it within their own subreddit.

6

u/VirginiaMcCaskey Feb 02 '20

don't feed the trolls

1

u/allhaillordreddit Feb 02 '20

Never change, shevy

1

u/danny54670 Feb 02 '20

Issue is yet to be reported to upstream LLVM.

I would love to know what the problem is and how it is fixed in LLVM. It may be that using a random access, reference stable container like std::deque is the proper solution, or maybe there is a simpler solution. Examining the code, this looks suspicious to me:

    SectionEntry &Section = Sections[SectionID];
    // ...
    if (i != Stubs.end()) {
      // ...
    } else {
      // Create a new stub function (equivalent to a PLT entry).
      LLVM_DEBUG(dbgs() << " Create a new stub function\n");

      uintptr_t BaseAddress = uintptr_t(Section.getAddress());
      uintptr_t StubAlignment = getStubAlignment();
      StubAddress =
          (BaseAddress + Section.getStubOffset() + StubAlignment - 1) &
          -StubAlignment;
      unsigned StubOffset = StubAddress - BaseAddress;
      Stubs[Value] = StubOffset;
      createStubFunction((uint8_t *)StubAddress);

      // Bump our stub offset counter
      Section.advanceStubOffset(getMaxStubSize());

      // Allocate a GOT Entry
      uint64_t GOTOffset = allocateGOTEntries(1);

      // ...
    }

    // Make the target call a call into the stub table.
    resolveRelocation(Section, Offset, StubAddress, ELF::R_X86_64_PC32,
                      Addend);

Because allocateGOTEntries can mutate Sections, it might be that the SectionEntry &Section reference created earlier is invalid by the time resolveRelocation is called. It may be that passing Sections[SectionID] as the first argument to resolveRelocation will fix the problem, or at least one of the problems.

2

u/CookiePLMonster Feb 02 '20

In the article, I proposed that solution too - I even included a diff removing `Section` temporaries.

3

u/danny54670 Feb 02 '20

Oh, sorry. When I first read your article, I sort of glossed over the "Proposed fix #2" part because I was confused by "goodbye to a Sections local variable"; Sections is a member variable, whereas the local reference variables are named Section.

I think that your diff for implementing solution #2 can be simplified. For example, RuntimeDyldELF::resolveRelocation doesn't need to be changed.

2

u/CookiePLMonster Feb 02 '20

It's totally possible. For that I mostly mass replaced it, but when I get to submitting a patch to LLVM (still need to figure out the procedure there) I'll need to take a second look at it, yeah.

Good call about the variable name too, fixed.

2

u/[deleted] Feb 02 '20

The address sanitizer and guard malloc are also good ways of catching this sort of thing (at least identifying the nature of the issue)

2

u/[deleted] Feb 03 '20

This is pretty enlightening

2

u/michalf6 Feb 03 '20

0xdddddddddddddddd

xDDDDDDDDDDDDDDDD

1

u/Rindhallow Feb 01 '20

I would love to see a video where you actually use those debug tools.

-25

u/shevy-ruby Feb 01 '20

LLVM should increase their internal code quality - not because it would be of low quality, but simply because so many other projects depend a LOT on LLVM these days.

I also can't help but feel that C++ became WAY too complex, beyond people WANTING to use it.

We need simpler, but fast, compiled and efficient language, with an elegant syntax. Anyone trying that route? (Go is the only language that tried to make some changes syntax-wise, but I can not stand Google controlling languages; and the fat lazy walrus operator should be flat-out forbidden after it killed guido.)

13

u/lookmeat Feb 01 '20

Yeez with the walrus operator.

It was a controversial decision in python, because it solved a problem that already had another way to be shown, instead of adding new things. The question of whether it was needed or not is separate. The reason Guido left is because he's done with python, it happens to everyone, you build a project and then you're done, then you let everyone else take over and move on. That the walrus operator discussion was what triggered Guido's realization doesn't mean it caused it.

Now on go the walrus operator is defined from the get go. You can always recognize the difference between assignment (of an existing variable) and definition (of a new variable), because unlike python they are always written differently. The walrus operator is simply a way to say the type should be guessed by the compiler, and you need it because the type is what separates definition and assignment otherwise.

5

u/bakery2k Feb 02 '20

Yes. The problem with the walrus operator in Python is its semantics, not the fact that it is a token composed of a colon followed by an equals sign.

Go uses the same token but its semantics is completely different.

3

u/flatfinger Feb 02 '20

What's more important than elegant syntax is a semantic model that can distinguish useless and worse-than-useless operations. While some programs are used in contexts where it would be impossible for them to do anything particularly bad, many more are used in contexts where there are some things they absolutely positively MUST NOT DO.

Even if there is no situation where a program that was processing useful input would need to enter an endless loop with no side-effects, there are many circumstances where a program that gets blocked by an endless loop while uselessly trying to process invalid input might as a consequence be prevented from behaving in worse-than-useless fashion. Blindlessly eliminating endless loops without making any effort to recognize situations where they might be important may be fine if there are no worse-than-useless behaviors, but is dangerous in cases where programs that are exposed to the public internet will be processing data from untrusted sources. Requiring that programmers add dummy side effects that would block useful optimizations in order to avoid having compilers impose dangerous ones hardly seems a recipe for performance.

-10

u/immibis Feb 02 '20

Note: bugs in your program which are caused by the libraries you use are still bugs in your program.

5

u/CookiePLMonster Feb 02 '20

You're right of course, but in here it was a matter of where to look for a bug - LLVM code or RPCS3 code.

-11

u/peterfirefly Feb 02 '20

So, not actually an LLVM bug after all.

A similar hidden problem occurs in C with realloc().

14

u/CookiePLMonster Feb 02 '20

How is it not a LLVM bug? Reallocation is not a problem, the fact LLVM assumes it will not happen is.