taintable/dynamic_taint/jalangi/src/js/instrument/esnstrument.js
/**
     * if not yet open, open the IID map file and write the header.
     * @param {string} outputDir an optional output directory for the sourcemap file
     */
if not yet open, open the IID map file and write the header.
@param {string} outputDir an optional output directory for the sourcemap file
if not yet open open the IID map file and write the header . @param { string } outputDir an optional output directory for the sourcemap file
function writeIIDMapFile(outputDir, initIIDs, isAppend) {
        var traceWfh, fs = require('fs'), path = require('path');
        var smapFile = path.join(outputDir, SMAP_FILE_NAME);
        if (initIIDs) {
            traceWfh = fs.openSync(smapFile, 'w');
        } else {
            traceWfh = fs.openSync(smapFile, 'a');
        }
        var fh = null;
        if (isAppend) {
            fh = fs.openSync(instCodeFileName, 'w');
        }
        writeLineToIIDMap(fs, traceWfh, fh, "(function (sandbox) {\n if (!sandbox.iids) {sandbox.iids = []; sandbox.orig2Inst = {}; }\n");
        writeLineToIIDMap(fs, traceWfh, fh, "var iids = sandbox.iids; var orig2Inst = sandbox.orig2Inst;\n");
        writeLineToIIDMap(fs, traceWfh, fh, "var fn = \"" + curFileName + "\";\n");
        Object.keys(iidSourceInfo).forEach(function (iid) {
            var sourceInfo = iidSourceInfo[iid];
            writeLineToIIDMap(fs, traceWfh, fh, "iids[" + iid + "] = [fn," + sourceInfo[1] + "," + sourceInfo[2] + "," + sourceInfo[3] + "," + sourceInfo[4] + "];\n");
        });
        Object.keys(orig2Inst).forEach(function (filename) {
            writeLineToIIDMap(fs, traceWfh, fh, "orig2Inst[\"" + filename + "\"] = \"" + orig2Inst[filename] + "\";\n");
        });
        writeLineToIIDMap(fs, traceWfh, fh, "}(typeof " + astUtil.JALANGI_VAR + " === 'undefined'? " + astUtil.JALANGI_VAR + " = {}:" + astUtil.JALANGI_VAR + "));\n");
        fs.closeSync(traceWfh);
        if (isAppend) {
            fs.closeSync(fh);
        }
        var jsonFile = smapFile.replace(/.js$/, '.json');
        var outputObj = [iidSourceInfo, orig2Inst];
        if (!initIIDs && fs.existsSync(jsonFile)) {
            var oldInfo = JSON.parse(fs.readFileSync(jsonFile));
            var oldIIDInfo = oldInfo[0];
            var oldOrig2Inst = oldInfo[1];
            Object.keys(iidSourceInfo).forEach(function (iid) {
                oldIIDInfo[iid] = iidSourceInfo[iid];
            })
            Object.keys(orig2Inst).forEach(function (filename) {
                oldOrig2Inst[filename] = orig2Inst[filename];
            });
            outputObj = [oldIIDInfo, oldOrig2Inst];
        }
        fs.writeFileSync(jsonFile, JSON.stringify(outputObj));
        fs.writeFileSync(path.join(outputDir, COVERAGE_FILE_NAME), JSON.stringify({"covered":0, "branches":condCount / IID_INC_STEP * 2, "coverage":[]}), "utf8");
    }
function writeIIDMapFile ( outputDir , initIIDs , isAppend ) { var traceWfh , fs = require ( 'fs' ) , path = require ( 'path' ) ; var smapFile = path . join ( outputDir , SMAP_FILE_NAME ) ; if ( initIIDs ) { traceWfh = fs . openSync ( smapFile , 'w' ) ; } else { traceWfh = fs . openSync ( smapFile , 'a' ) ; } var fh = null ; if ( isAppend ) { fh = fs . openSync ( instCodeFileName , 'w' ) ; } writeLineToIIDMap ( fs , traceWfh , fh , "(function (sandbox) {\n if (!sandbox.iids) {sandbox.iids = []; sandbox.orig2Inst = {}; }\n" ) ; writeLineToIIDMap ( fs , traceWfh , fh , "var iids = sandbox.iids; var orig2Inst = sandbox.orig2Inst;\n" ) ; writeLineToIIDMap ( fs , traceWfh , fh , "var fn = \"" + curFileName + "\";\n" ) ; Object . keys ( iidSourceInfo ) . forEach ( function ( iid ) { var sourceInfo = iidSourceInfo [ iid ] ; writeLineToIIDMap ( fs , traceWfh , fh , "iids[" + iid + "] = [fn," + sourceInfo [ 1 ] + "," + sourceInfo [ 2 ] + "," + sourceInfo [ 3 ] + "," + sourceInfo [ 4 ] + "];\n" ) ; } ) ; Object . keys ( orig2Inst ) . forEach ( function ( filename ) { writeLineToIIDMap ( fs , traceWfh , fh , "orig2Inst[\"" + filename + "\"] = \"" + orig2Inst [ filename ] + "\";\n" ) ; } ) ; writeLineToIIDMap ( fs , traceWfh , fh , "}(typeof " + astUtil . JALANGI_VAR + " === 'undefined'? " + astUtil . JALANGI_VAR + " = {}:" + astUtil . JALANGI_VAR + "));\n" ) ; fs . closeSync ( traceWfh ) ; if ( isAppend ) { fs . closeSync ( fh ) ; } var jsonFile = smapFile . replace ( / .js$ / , '.json' ) ; var outputObj = [ iidSourceInfo , orig2Inst ] ; if ( ! initIIDs && fs . existsSync ( jsonFile ) ) { var oldInfo = JSON . parse ( fs . readFileSync ( jsonFile ) ) ; var oldIIDInfo = oldInfo [ 0 ] ; var oldOrig2Inst = oldInfo [ 1 ] ; Object . keys ( iidSourceInfo ) . forEach ( function ( iid ) { oldIIDInfo [ iid ] = iidSourceInfo [ iid ] ; } ) Object . keys ( orig2Inst ) . forEach ( function ( filename ) { oldOrig2Inst [ filename ] = orig2Inst [ filename ] ; } ) ; outputObj = [ oldIIDInfo , oldOrig2Inst ] ; } fs . writeFileSync ( jsonFile , JSON . stringify ( outputObj ) ) ; fs . writeFileSync ( path . join ( outputDir , COVERAGE_FILE_NAME ) , JSON . stringify ( { "covered" : 0 , "branches" : condCount / IID_INC_STEP * 2 , "coverage" : [ ] } ) , "utf8" ) ; }
if not yet open, open the IID map file and write the header.
if not yet open open the IID map file and write the header .
// write all the data // also write output as JSON, to make consumption easier