8015892: canBeUndefined too conservative for some use before declaration cases

Mon, 10 Jun 2013 13:21:29 +0200

author
lagergren
date
Mon, 10 Jun 2013 13:21:29 +0200
changeset 335
8f890b6bf6de
parent 334
918a986b0478
child 336
a6f8ea57f048

8015892: canBeUndefined too conservative for some use before declaration cases
Reviewed-by: attila, hannesw

src/jdk/nashorn/internal/codegen/Attr.java file | annotate | diff | comparison | revisions
src/jdk/nashorn/internal/ir/Symbol.java file | annotate | diff | comparison | revisions
test/script/basic/JDK-8015892.js file | annotate | diff | comparison | revisions
test/script/basic/fib_wtf.js file | annotate | diff | comparison | revisions
test/script/basic/fib_wtf.js.EXPECTED file | annotate | diff | comparison | revisions
     1.1 --- a/src/jdk/nashorn/internal/codegen/Attr.java	Fri Jun 07 17:44:25 2013 +0200
     1.2 +++ b/src/jdk/nashorn/internal/codegen/Attr.java	Mon Jun 10 13:21:29 2013 +0200
     1.3 @@ -202,18 +202,52 @@
     1.4      private void acceptDeclarations(final FunctionNode functionNode, final Block body) {
     1.5          // This visitor will assign symbol to all declared variables, except function declarations (which are taken care
     1.6          // in a separate step above) and "var" declarations in for loop initializers.
     1.7 +        //
     1.8 +        // It also handles the case that a variable can be undefined, e.g
     1.9 +        // if (cond) {
    1.10 +        //    x = x.y;
    1.11 +        // }
    1.12 +        // var x = 17;
    1.13 +        //
    1.14 +        // by making sure that no identifier has been found earlier in the body than the
    1.15 +        // declaration - if such is the case the identifier is flagged as caBeUndefined to
    1.16 +        // be safe if it turns into a local var. Otherwise corrupt bytecode results
    1.17 +
    1.18          body.accept(new NodeVisitor<LexicalContext>(new LexicalContext()) {
    1.19 +            private final Set<String> uses = new HashSet<>();
    1.20 +            private final Set<String> canBeUndefined = new HashSet<>();
    1.21 +
    1.22              @Override
    1.23              public boolean enterFunctionNode(final FunctionNode nestedFn) {
    1.24                  return false;
    1.25              }
    1.26  
    1.27              @Override
    1.28 +            public Node leaveIdentNode(final IdentNode identNode) {
    1.29 +                uses.add(identNode.getName());
    1.30 +                return identNode;
    1.31 +            }
    1.32 +
    1.33 +            @Override
    1.34 +            public boolean enterVarNode(final VarNode varNode) {
    1.35 +                final String name = varNode.getName().getName();
    1.36 +                //if this is used the var node symbol needs to be tagged as can be undefined
    1.37 +                if (uses.contains(name)) {
    1.38 +                    canBeUndefined.add(name);
    1.39 +                }
    1.40 +                return true;
    1.41 +            }
    1.42 +
    1.43 +            @Override
    1.44              public Node leaveVarNode(final VarNode varNode) {
    1.45                  // any declared symbols that aren't visited need to be typed as well, hence the list
    1.46                  if (varNode.isStatement()) {
    1.47                      final IdentNode ident  = varNode.getName();
    1.48                      final Symbol    symbol = defineSymbol(body, ident.getName(), IS_VAR);
    1.49 +                    if (canBeUndefined.contains(ident.getName())) {
    1.50 +                        symbol.setType(Type.OBJECT);
    1.51 +                        symbol.setCanBeUndefined();
    1.52 +                    }
    1.53                      functionNode.addDeclaredSymbol(symbol);
    1.54                      if (varNode.isFunctionDeclaration()) {
    1.55                          newType(symbol, FunctionNode.FUNCTION_TYPE);
     2.1 --- a/src/jdk/nashorn/internal/ir/Symbol.java	Fri Jun 07 17:44:25 2013 +0200
     2.2 +++ b/src/jdk/nashorn/internal/ir/Symbol.java	Mon Jun 10 13:21:29 2013 +0200
     2.3 @@ -462,7 +462,7 @@
     2.4       */
     2.5      public void setCanBeUndefined() {
     2.6          assert type.isObject() : type;
     2.7 -        if(!canBeUndefined()) {
     2.8 +        if (!isParam() && !canBeUndefined()) {//parameters are never undefined
     2.9              assert !isShared();
    2.10              flags |= CAN_BE_UNDEFINED;
    2.11          }
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/test/script/basic/JDK-8015892.js	Mon Jun 10 13:21:29 2013 +0200
     3.3 @@ -0,0 +1,38 @@
     3.4 +/*
     3.5 + * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
     3.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3.7 + * 
     3.8 + * This code is free software; you can redistribute it and/or modify it
     3.9 + * under the terms of the GNU General Public License version 2 only, as
    3.10 + * published by the Free Software Foundation.
    3.11 + * 
    3.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    3.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    3.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    3.15 + * version 2 for more details (a copy is included in the LICENSE file that
    3.16 + * accompanied this code).
    3.17 + * 
    3.18 + * You should have received a copy of the GNU General Public License version
    3.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    3.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    3.21 + * 
    3.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    3.23 + * or visit www.oracle.com if you need additional information or have any
    3.24 + * questions.
    3.25 + */
    3.26 +
    3.27 +/**
    3.28 + * JDK-8015892.js : use before definition with valid declaration that turns into
    3.29 + * local var must be "canBeUndefined"
    3.30 + *
    3.31 + * @test
    3.32 + * @run
    3.33 + */
    3.34 +
    3.35 +function doIt() { 
    3.36 +    if (something) { 
    3.37 +	x = x.obj; 
    3.38 +    } else { 
    3.39 +	var x = "x"; 
    3.40 +    } 
    3.41 +} 
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/test/script/basic/fib_wtf.js	Mon Jun 10 13:21:29 2013 +0200
     4.3 @@ -0,0 +1,38 @@
     4.4 +/*
     4.5 + * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
     4.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4.7 + * 
     4.8 + * This code is free software; you can redistribute it and/or modify it
     4.9 + * under the terms of the GNU General Public License version 2 only, as
    4.10 + * published by the Free Software Foundation.
    4.11 + * 
    4.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    4.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    4.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    4.15 + * version 2 for more details (a copy is included in the LICENSE file that
    4.16 + * accompanied this code).
    4.17 + * 
    4.18 + * You should have received a copy of the GNU General Public License version
    4.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    4.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    4.21 + * 
    4.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    4.23 + * or visit www.oracle.com if you need additional information or have any
    4.24 + * questions.
    4.25 + */
    4.26 +
    4.27 +/**
    4.28 + * fib_wtf - obfuscated fibonacci
    4.29 + *
    4.30 + * @test
    4.31 + * @run
    4.32 + */
    4.33 +
    4.34 +function fib(_) {
    4.35 +    for(_=[+[],++[[]][+[]],+[],_],_[++[++[++[[]][+[]]][+[]]][+[]]]=(((_[++[++[++[[]][+[]]][+[]]][+[]]]-(++[[]][+[]]))&(((--[[]][+[]])>>>(++[[]][+[]]))))===(_[++[++[++[[]][+[]]][+[]]][+[]]]-(++[[]][+[]])))?(_[++[++[[]][+[]]][+[]]]=++[[]][+[]],_[++[++[++[[]][+[]]][+[]]][+[]]]-(++[[]][+[]])):+[];_[++[++[++[[]][+[]]][+[]]][+[]]]--;_[+[]]=(_[++[[]][+[]]]=_[++[++[[]][+[]]][+[]]]=_[+[]]+_[++[[]][+[]]])-_[+[]]);
    4.36 +    return _[++[++[[]][+[]]][+[]]];
    4.37 +}
    4.38 +
    4.39 +for (var x = -1; x <= 63; x++) {
    4.40 +    print(fib(x));
    4.41 +}
     5.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.2 +++ b/test/script/basic/fib_wtf.js.EXPECTED	Mon Jun 10 13:21:29 2013 +0200
     5.3 @@ -0,0 +1,65 @@
     5.4 +0
     5.5 +0
     5.6 +1
     5.7 +1
     5.8 +2
     5.9 +3
    5.10 +5
    5.11 +8
    5.12 +13
    5.13 +21
    5.14 +34
    5.15 +55
    5.16 +89
    5.17 +144
    5.18 +233
    5.19 +377
    5.20 +610
    5.21 +987
    5.22 +1597
    5.23 +2584
    5.24 +4181
    5.25 +6765
    5.26 +10946
    5.27 +17711
    5.28 +28657
    5.29 +46368
    5.30 +75025
    5.31 +121393
    5.32 +196418
    5.33 +317811
    5.34 +514229
    5.35 +832040
    5.36 +1346269
    5.37 +2178309
    5.38 +3524578
    5.39 +5702887
    5.40 +9227465
    5.41 +14930352
    5.42 +24157817
    5.43 +39088169
    5.44 +63245986
    5.45 +102334155
    5.46 +165580141
    5.47 +267914296
    5.48 +433494437
    5.49 +701408733
    5.50 +1134903170
    5.51 +1836311903
    5.52 +2971215073
    5.53 +4807526976
    5.54 +7778742049
    5.55 +12586269025
    5.56 +20365011074
    5.57 +32951280099
    5.58 +53316291173
    5.59 +86267571272
    5.60 +139583862445
    5.61 +225851433717
    5.62 +365435296162
    5.63 +591286729879
    5.64 +956722026041
    5.65 +1548008755920
    5.66 +2504730781961
    5.67 +4052739537881
    5.68 +6557470319842

mercurial