r/javascript • • Jun 20 '16

Javascript developers, be warned about this crazy JIT bug in V8!

Last week I've spent a full day to fix very strange behavior in a single page application for my customer that only happens on Chrome 51 (maybe 52, but not in Canary 53).

After a while our SPA started behaving incorrectly...

It turned out to be a nasty bug in the V8 JIT engine.

Try the following in Chrome's developer console:

function foo() {
  return typeof null === "undefined";
}

for(var i=0; i<10000; ++i) console.log(foo())

This should of course always print false, but in Chrome 51, it prints true after about about 100 iterations (it varies...)

I want to warn other web-developers for this bug, to avoid many $$$ being wasted (or becoming insane ;-))

 

EDIT: The following - more realistic case that we had in third-party library code - will also fail, but after about 10 thousand iterations:

function isUndefined(thing){
    return typeof thing === "undefined";
}

for(var i=0; i<100000; i++) console.log(isUndefined(null));

 

EDIT: The bug also manifests itself with the more common pattern

typeof x !== "undefined"

UPDATE: I just tried this with the latest stable Chrome (52.0.2743.82), and it now works correctly :)

482 Upvotes

117 comments sorted by

View all comments

14

u/Pyrise Jun 20 '16

Anyone have any insight into why this happens?(Newbie here)

28

u/RainHappens Jun 20 '16

I found the bugfix.

Suffice to say (and I am probably wrong here, having next-to-no knowledge of the codebase) that the optimizer keeps track of "special" strings (booleans, "undefined", some integers, etc). It also does some type propagation (as in what types a variable etc can be). There's a special "undetectable" type for when a variable is undefined or null. In this case the optimizer goes "null is undetectable, so it can be replaced with the "undefined" string for the comparison. Oh look, both strings are the same!"

If you notice, this:

for(let i=0; i<10000; ++i) console.log(typeof null === 'object')

exhibits the same incorrect behavior, albeit reversed. Same thing. The initial interpreter thinks 'typeof null' is 'object' for comparison purposes, but the optimizer thinks 'typeof null' is 'undefined' for comparison purposes.

4

u/gsnedders Jun 20 '16

The initial interpreter

FWIW, V8 currently doesn't have an interpreter, they always run JIT'd code. (Ignition, which will finally add an interpreter, is still under development.)

1

u/RedditWithBoners Jun 20 '16

Given the last paragraph here, why can't a second JIT exist to fulfill the purpose of Ignition?

4

u/gsnedders Jun 20 '16

I presume the paragraph you mean is:

Note that the role that the interpreter plays in the case of a mixed-mode execution engine, namely providing fast startup, and also potentially collecting information and providing fallback capability may alternatively also be played by a second JIT compiler. This is how V8 works, for example. V8 never interprets, it always compiles. The first compiler is a very fast, very slim compiler that starts up very quick. The code it produces isn't very fast, though. This compiler also injects profiling code into the code it generates. The other compiler is slower and uses more memory, but produces much faster code, and it can use the profiling information collected by running the code compiled by the first compiler.

Calling it a second JIT is actually slightly confusing to me, I'm used to tiers being ordered by hotness (so the baseline JIT is the first tier, not the second). Regardless…

Essentially, while it is a very fast compiler, for code that runs only once or twice the overhead of compilation will almost certainly exceed any performance you gain by eliminating the dispatch overhead of an interpreter (which is the biggest gain of the baseline JIT). You also save a lot of memory by not having to allocate memory for the native code you're not generating, which on embedded devices can be significant.

These slides from BlinkOn 6 are a decent overview about motivating factors behind Ignition.

1

u/RedditWithBoners Jun 21 '16

Thanks, will have to look into this later.