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

2

u/AlGoreBestGore Jun 20 '16

While this is trippy, when would you need to check if null's type is undefined?

0

u/[deleted] Jun 20 '16

[deleted]

-4

u/hkrdrm Jun 20 '16

still wouldn't it be more proper to check

if(some_variable === null)

13

u/StoneCypher Jun 20 '16

no. null is a placeholder here.

what he's saying is "if you compare things to typeof undefined, every hundred or so iterations it'll fail. look, it even happens with null."

3

u/bits_and_bytes Jun 20 '16 edited Jun 20 '16

I've tried a few different permutations and I can only get it to reliably happen with null (or a var set to null). So I think /u/hkrdrm and /u/AlGoreBestGore are right to be asking their questions. typeof null is "object" which is why you get the false returns for the first 100+ tries when comparing it to "undefined". But I've tried comparing other "object"s with "undefined" and I don't have the same problem. For example:

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

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

This works just fine. But if thing = null, then it all blows up.

EDIT: I've found that the bug doesn't show up if null is in a var either:

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

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

This works correctly as well. It only seems to happen if you directly reference 'null' in the typeof comparison, and only if it's in a declared function. After the function starts returning true, it will always return true until it's re-initialized. A very strange bug indeed.

2

u/iamallamaa Jun 20 '16

I found that if the variable is defined within the function as null that it does still has the error.

function foo(){
    var thing=null;
    return typeof thing === "undefined";
}
for(var i=0; i<10000; i++) console.log(foo());

265 false 9735 true

And when null is passed as an argument, it doesn't.

function foo(thing){
    return typeof thing === "undefined";
}
for(var i=0; i<10000; i++) console.log(foo(null));

10000 false

1

u/ziriax Jun 21 '16

Try 100 thousand times... It will still fail at some point

function foo(thing){
    return typeof thing === "undefined";
}
for(var i=0; i<100000; i++) console.log(foo(null));

12291 false 87709 true

Also, once this function is JIT optimized, it can keep failing if doesn't get de-optimized

2

u/saintPirelli Jun 20 '16

Thanks for this comment. My noob ass finally gets it.

-8

u/hkrdrm Jun 20 '16

I get that but what I'm saying if you have some function and i want to know if the argument is null wouldn't it make more sense to check it like this

function foo(my_arg) {
  if(my_arg === null){
    console.log("arg is null here insert logic for null case here.");
  }
}

I'm not getting why anyone would need to check type equal to undefined.

14

u/StoneCypher Jun 20 '16

Because he's not checking if it's null, and you can't actually strict-equals compare to undefined in this language in a bunch of common circumstances.

It is very common to have to check if something is undefined. By example, if you retain a JSON object from an API, and you want to check whether the server actually set some or another value, before you attempt to use it.

He's talking about the undefined comparison.

I'd like you to stop focussing on null, please. It's beside the point. Thank you

1

u/RReverser Jun 21 '16

and you can't actually strict-equals compare to undefined in this language in a bunch of common circumstances.

Of course you can. The only case where you can't is if you're checking existance of a global variable for feature detection, in all the other cases === undefined works like a charm.

3

u/agmcleod @agmcleod Jun 20 '16

Properties on objects are not initialized to null by default. No doubt having your objects well defined with sensible defaults goes a long way, but this is still a nasty bug that shouldn't happen.

0

u/[deleted] Jun 20 '16

[deleted]

11

u/compteNumero9 Jun 20 '16

Look at the bug report, it's not only for this case. But you might have an unaffected version of Chrome/node

0

u/[deleted] Jun 20 '16

[deleted]

2

u/compteNumero9 Jun 20 '16

Hard to tell how the code is optimized in your case. It's possible for example that the foo(baz) function is just optimized away and replaced with the result.

3

u/[deleted] Jun 20 '16

but when you move variable to that function scope...

function foo() {
    var bar = null;
    return typeof bar === "undefined";
}  
for(var i=0; i<10000; ++i) console.log(foo())

1

u/Buckwheat469 Jun 20 '16 edited Jun 20 '16

I've created 4 different examples, after moving some code around I can no longer get it to reproduce the problem on the last 3 examples. The first example still returns true on Chrome 51 Linux x64. I want to point out that it did reproduce the error more often at first but I noticed some bad code and moved some functions around. This seemed to fix the problem.

https://jsfiddle.net/sssn9fvo/2/

Edit: I just noticed that the link above has an extra foo() function. As soon as I deleted this function it started working. Perhaps the interpreter is jumping between 2 different hoisted functions?

https://jsfiddle.net/sssn9fvo/3/

1

u/gsnedders Jun 20 '16

Once it gets inlined it'll start failing.

1

u/55704841711534631249 Jun 20 '16

That fix stops working if you set the loop to 100,000 cycles (at around the 12k mark)

function foo(bar) {
    return typeof bar === "undefined";
}  
var baz = null;
for(var i=0; i<100000; ++i) console.log(foo(baz))
12291 false VM1493:5
87709 true  VM1493:5 
undefined

https://postimg.org/image/sdwp5ttcn/ https://s31.postimg.org/t3fhi6tw9/failcase1.png

A better fix would be to add true to the expression inside the function:

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

for(var i=0; i<100000; i++) console.log(foo())
100000 false VM1494:5
undefined

https://postimg.org/image/gctc5qm9z/ https://s31.postimg.org/c3om3kj0p/wincase1.png

This is on OS X 10.11.5, Chrome 51. I haven't tested it on Windows, so YMMV.

6

u/leesusmusic Jun 20 '16 edited Jun 21 '16

Am I missing something? "undefined" + true will evaluate to "undefinedtrue", no?