Haiyang-Sun / nodeprof.js

Instrumentation framework for Node.js compliant to ECMAScript 2020 based on GraalVM.
Apache License 2.0
52 stars 22 forks source link

getField() doesn't work when using some library functions or iterator related functions/syntax/statements #92

Closed Soulike closed 3 years ago

Soulike commented 3 years ago

Hi, I'm developing a NodeProf analysis for logging all accesses to object/array fields. But I found getField() is not called when analyzing codes using some library functions (like Array.prototype.map()) or iterator related functions/syntax/statements (like Array.from(), spread expression, for...of statement). For example:

Analyzed Code

const array = [1, 2, 3, 4, 5];

function func()
{
    const arrayCopy = array.map(value => value);  // or [...array] or Array.from(array) or using for...of
}

func();

My Analysis Code

// DO NOT INSTRUMENT
(function (sandbox)
{
    function MyAnalysis()
    {
        this.getField = function (iid, base, offset, val, isComputed, isOpAssign, isMethodCall)
        {
            console.log(base);
            console.log(offset);
            console.log(val);
        };
    }

    sandbox.analysis = new MyAnalysis();
})(J$);

What I expect is that getField() should be called and telling me that offset: "0" "1" "2" "3" "4" of base: array are read with value 1 2 3 4 5 , respectively. Is it possible to achieve this?

alexjordan commented 3 years ago

Hi @Soulike,

NodeProf callbacks like getField work inside JavaScript code only. In your example the element access happens inside a builtin function, which cannot be instrumented and thus not analyzed. Working around this, you may want to try adding analysis models for certain builtin functions to your analysis, or as far as possible replace builtins with JS polyfills that can be analyzed.

Soulike commented 3 years ago

Hi @Soulike,

NodeProf callbacks like getField work inside JavaScript code only. In your example the element access happens inside a builtin function, which cannot be instrumented and thus not analyzed. Working around this, you may want to try adding analysis models for certain builtin functions to your analysis, or as far as possible replace builtins with JS polyfills that can be analyzed.

OK. Thank you for your reply. :)