oracle / graaljs

GraalJS – A high-performance, ECMAScript compliant, and embeddable JavaScript runtime for Java
https://www.graalvm.org/javascript/
Universal Permissive License v1.0
1.81k stars 190 forks source link

unexpected decrement of variable higher in the call stack #177

Closed simbo1905 closed 5 years ago

simbo1905 commented 5 years ago

I am running:

$ sw_vers
ProductName:    Mac OS X
ProductVersion: 10.14.3
BuildVersion:   18D109

$ java -version
java version "1.8.0_212"
Java(TM) SE Runtime Environment (build 1.8.0_212-b31)
Java HotSpot(TM) GraalVM EE 19.0.1 (build 25.212-b31-jvmci-19-b01, mixed mode)

$ which node
/Library/Java/JavaVirtualMachines/graalvm-ee-19.0.1/Contents/Home/bin/node

I have a unit test calling a module exported method here where the method has a pretty simple loop:

  for ( i = 0; i < secret.length; i++ ){
    console.log("before i:"+ i + " "+  secret.length);
    const p = GF256.generate(randomBytes, k - 1, secret[0]);
    console.log("after i:"+ i + " "+ secret.length);
  }

The logic should be looping over the array secret but I removed passing i into the method call as it appears to get corrupted. The method call to GF256.generate resets i to 0. So the unit test loops forever until you ctrl+c it. You can see this behaviour by checking out the branch then at the top level run:

node src/test/js/SchemeTests.js

The first 15 lines are something like:

TAP version 13
# SchemeTests roundtrip
n:3, k:2 secret.length:3
before i:0 3
after  i:1 3
before i:2 3
after  i:1 3
before i:2 3
after  i:1 3
before i:2 3
after  i:1 3
before i:2 3
after  i:1 3
before i:2 3
after  i:1 3

I cannot see how i is getting assigned back to 1 in this code. I am thinking it might be a bug in graaljs

iamstolis commented 5 years ago

Note that your for loop (for ( i = 0; i < secret.length; i++ ){) is using a global variable i. This is not a good idea. Some other loops in your code are using the same global variable as well (namely degree function in GF256.js). Loops of this kind do not work when one is invoked from another. In other words, i is decreased by the mentioned degree function. So, the infinite loop is not a bug of GraalVM JavaScript engine. I suggest you to use function or block scope variables in your loops, i.e., use for (var i=0; ... or for (let i=0; ... instead of for (i=0; ....

simbo1905 commented 5 years ago

thanks! i have added "use strict" to the files which to stop the code from compiling while accidentally using undeclared globals.