Samsung / jalangi2

Dynamic analysis framework for JavaScript
Apache License 2.0
435 stars 119 forks source link

Destructuring assignment syntax support #161

Open hyllee opened 4 years ago

hyllee commented 4 years ago

Unhandled exception from array destructuring assignment

Destructuring assignment syntax is not supported and results in an exception in version v0.2.6. This results in the instrumentor to crash and not produce any outputs.

let a;
let b;
let c = [1,2];

[a, b] = c;

Stack trace:

Failed to instrument let a;
let b;
let c = [1,2];

[a, b] = c;

/home/hyllee/gelato/core/node_modules/jalangi2/src/js/instrument/esnstrument.js:1952
                throw ex;
                ^

TypeError: Cannot read property 'name' of undefined
    at getPropertyAsAst (/home/hyllee/gelato/core/node_modules/jalangi2/src/js/instrument/esnstrument.js:1103:76)
    at instrumentStore (/home/hyllee/gelato/core/node_modules/jalangi2/src/js/instrument/esnstrument.js:1134:56)
    at Object.AssignmentExpression (/home/hyllee/gelato/core/node_modules/jalangi2/src/js/instrument/esnstrument.js:1448:24)
    at transformAst (/home/hyllee/gelato/core/node_modules/jalangi2/src/js/instrument/astUtil.js:152:36)
    at transformAst (/home/hyllee/gelato/core/node_modules/jalangi2/src/js/instrument/astUtil.js:145:39)
    at transformAst (/home/hyllee/gelato/core/node_modules/jalangi2/src/js/instrument/astUtil.js:145:39)
    at Object.transformAst (/home/hyllee/gelato/core/node_modules/jalangi2/src/js/instrument/astUtil.js:145:39)
    at transformString (/home/hyllee/gelato/core/node_modules/jalangi2/src/js/instrument/esnstrument.js:1871:30)
    at instrumentCode (/home/hyllee/gelato/core/node_modules/jalangi2/src/js/instrument/esnstrument.js:1943:30)
    at instrumentFile (/home/hyllee/gelato/core/node_modules/jalangi2/src/js/commands/esnstrument_cli.js:191:31)

Invalid code generated from object destructuring assignment

Another issue with destructuring assignment where it does not throw an exception but outputs syntactically invalid code:

const f = ({ var: e }) => console.log(e);
f({ var: 5 });

which is transformed into something in the likes of the following:

            const f = J$.X1(57, J$.W(49, 'f', ({
                J$.R(9, 'var', var, 2): J$.R(17, 'e', e, 2)
            }) => J$.M(41, J$.R(25, 'console', console, 2), 'log', 0)(J$.R(33, 'e', e, 2)), f, 3));
            J$.X1(97, J$.F(89, J$.R(65, 'f', f, 1), 0)(J$.T(81, {
                var: J$.T(73, 5, 22, false)
            }, 11, false)));

This, when run, results in a syntax error at line 2: Uncaught SyntaxError: Unexpected token '.'

msridhar commented 4 years ago

@hyllee most features introduced in ECMAScript 2015 and after are not support by Jalangi. Can you run your code through Babel first and then run the result through Jalangi?

behnazh-w commented 4 years ago

@msridhar thanks for the suggestion. Indeed we are also trying to use Babel for ECMAScript 2015 features. But given the complexity of our applications, that might not be the solution at the end. Apart from that, we didn't expect unhandled exceptions in Jalangi2 due to lack of support for a feature. We have noticed that the 0.2.5 release does not throw the exception. So it could possibly be due to a change in 0.2.6?

In general, I think it would make sense to make sure the unsupported features don't get instrumented and the code runs correctly.

msridhar commented 4 years ago

In 0.2.6 I updated some dependencies (like the acorn parser) from extremely old versions. It's possible that's what caused the problem. I agree it would be great if Jalangi at least didn't break code with unsupported features. I would welcome a PR here to fix this issue. All the instrumentation code is in this file. It would take a bit of time to understand but shouldn't be too bad. I am happy to explain parts that are tricky.