This commit is contained in:
2025-07-26 13:44:32 +01:00
parent 5b584a90a5
commit 2d02acacc7
24 changed files with 4519 additions and 77 deletions

View File

@@ -56,9 +56,9 @@ export class ExpressionArrayVisitor extends Visitor {
this.arguments.push(node.value);
}
ObjectExpression(node) {
console.warning("Not finished");
throw Error("TBD");
console.warn("Not finished");
this.arguments.push(new ObjectSimplifierVisitor().visit(node));
throw Error("TBD");
}
/**
@@ -88,17 +88,100 @@ class ObjectSimplifierVisitor extends Visitor {
*
* @param {import("estree").Property} node
*/
Property(node){
this.#topOfStack()?.[ node.key.value];
Property(node) {
this.#topOfStack()?.[node.key.value];
}
/**
*
* @param {import('estree').ObjectExpression} node
*/
ObjectExpression(node) {
const x = {};
for (const property of node.properties) {
if (property.type === 'Property') {
if( property.key.type==='PrivateIdentifier'){
}
const propertyKey =
x[property.key]
}
}
}
/**
*
* @param {import('estree').Literal} node
*/
Literal(node) {
return node.value;
}
/**
*
* @param {SpreadExpression} node
*/
SpreadExpression(node) {
throw new Error("unsupported");
}
visit(node) {
console.log("Objvisit",node);
console.log("Objvisit", node);
return super.visit(node);
}
#topOfStack(){
return this.exprStack[this.exprStack.length-1];
#topOfStack() {
return this.exprStack[this.exprStack.length - 1];
}
}
class GetExpressionStaticValue extends Visitor {
/**
*
* @param {import('estree').Identifier} node
*/
Identifier(node) {
throw new Error('not compat');
}
/**
*
* @param {import('estree').Literal} node
*/
Literal(node) {
return node.value;
}
/**
*
* @param {import('estree').BinaryExpression} node
* @returns
*/
BinaryExpression(node) {
return this.applyOperation(super.left(node.left), super.right(node.right), node.operator);
}
/**
*
* @param {*} left
* @param {*} right
* @param {import('estree').BinaryOperator} operator
*/
applyOperation(left, right, operator) {
switch (operator) {
case '+': return left + right;
case '-': return left - right;
case '*': return left * right;
case '/': return left / right;
case '%': return left % right;
case '==': return left == right;
case '===': return left === right;
case '>': return left > right;
case '<': return left < right;
case '>=': return left >= right;
case '<=': return left <= right;
case '<<': return left << right;
case '>>': return left >> right;
case '^': return left ^ right;
case '&': return left & right;
}
}
}

View File

@@ -5,7 +5,6 @@ import { LibraryCallsRecorder } from './libcalls.mjs';
import { tagASTNode, getTagKey, untagASTNode } from './ast/tag.mjs';
import { ExpressionArrayVisitor } from './ast/visitors.mjs';
import assert from 'assert';
/**
*
* @param {import('eslint').Scope.ScopeManager} scopeManager

View File

@@ -3,12 +3,12 @@ import assert from 'node:assert';
import { getASTAndScope } from './ast/analysis.mjs';
import { getRequireCallsAndConstantArgs } from './calls.mjs';
import { analyze, instrumentString, instrumentDir } from 'jalangi2';
import { readFileSync ,realpathSync ,mkdirSync} from 'node:fs';
import { writeFile } from 'node:fs/promises';
import tsc, { Project, SyntaxKind } from 'ts-morph';
import {getSliceAndInfoSync} from 'slice-js/src/slice-code/test/helpers/utils.js';
import path, { dirname,join } from 'node:path';
// import tsc from 'typescript'
/**
* Call parameter generation
*/
@@ -49,7 +49,7 @@ function main() {
console.log(`Writing to`,writePath);
writePromises.push(writeFile(writePath,slicedCode));
}
Promise.all(writePromises).then(p=>{
@@ -57,9 +57,71 @@ function main() {
}).catch(console.log);
}
class ImportCall{
/**
* @type {'import'|'importExpr'|'require'}
*/
importType;
/**
* @type {string}
*/
importSyntax;
/**
*
* @param {'import'|'importExpr'|'require'} importType
* @param {string} importSyntax
*/
constructor(importType, importSyntax){
this.importSyntax = importSyntax;
this.importType = importType;
}
}
function main2() {
const FILE_PATH = './test_src/index.cjs';
const project = new Project({compilerOptions:{allowJs: true, checkJs: false,}});
project.addSourceFileAtPathIfExists(FILE_PATH);
// const project = tsc.createProgram([FILE_PATH],);
const checker = project.getTypeChecker();
const sourceFile = project.getSourceFile(FILE_PATH)
const importDecls = sourceFile.getImportStringLiterals()
for(const importStringDecl of importDecls){
console.log(importStringDecl);
const importDecl = importStringDecl.getFirstAncestor();
if(importDecl.isKind(SyntaxKind.CallExpression)){
// the declaration is callExpression. Verify its based an identifier aliasing import or require
const importExpr = importDecl.getExpression();
const type = checker.getTypeAtLocation(importExpr);
console.log("Type of import expression",checker.compilerObject.resolveName());
console.log(importExpr);
if(importExpr.isKind(SyntaxKind.Identifier)){
// import is a require or import
const importName = importExpr.getText();
if(importName==='require' || importName==='import'){
console.log("Found require/import call",importExpr);
}
}
}else if(importDecl.isKind(SyntaxKind.ImportDeclaration)){
// TODO pending extract the calls.
}else{
console.error("Unexpected import specifier",SyntaxKind[importDecl]);
}
const importThing = importStringDecl.getParent()
}
console.log(importDecls);
}
if (process.argv[1] === import.meta.filename) {
console.log("[SafeImport] started")
main();
console.log("[SafeImport] started");
main2();
console.log("done");
}
@@ -77,4 +139,3 @@ function logCallList(calls) {
function isRelativeModule(moduleName) {
return moduleName.startsWith('.');
}