Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,7 @@ syntest/
.*
*.json
*.md


# Since the javascript files have a lot of eslint errors that will be fixed by refactoring to typescript we ignore them for now.
src/**/*.js
3 changes: 0 additions & 3 deletions api.js

This file was deleted.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@
"detect-port": "1.3.0",
"figlet": "1.5.2",
"fs-extra": "11.1.0",
"global-modules": "^2.0.0",
"globby": "^11.0.4",
"lodash.clonedeep": "4.5.0",
"mocha": "10.2.0",
Expand Down
22 changes: 11 additions & 11 deletions src/SolidityLauncher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { SoliditySuiteBuilder } from "./testbuilding/SoliditySuiteBuilder";
import { SolidityRunner } from "./testcase/execution/SolidityRunner";
import { SolidityRandomSampler } from "./testcase/sampling/SolidityRandomSampler";
import { SolidityCFGFactory } from "./graph/SolidityCFGFactory";
const SolidityParser = require("@solidity-parser/parser");
import SolidityParser = require("@solidity-parser/parser");

import {
Archive,
Expand Down Expand Up @@ -65,7 +65,6 @@ import { setNetwork, setNetworkFrom } from "./util/network";
import {
createTruffleConfig,
getTestFilePaths,
loadLibrary,
setupTempFolders,
} from "./util/fileSystem";

Expand All @@ -88,8 +87,9 @@ import {
collectInitialVariables,
collectStatistics,
} from "./util/collection";

// eslint-disable-next-line
const pkg = require("../package.json");
// eslint-disable-next-line
const Web3 = require("web3");

export class SolidityLauncher {
Expand Down Expand Up @@ -190,7 +190,7 @@ export class SolidityLauncher {

getUserInterface().report("clear", []);
getUserInterface().report("asciiArt", ["Syntest"]);
getUserInterface().report("version", [require("../package.json").version]);
getUserInterface().report("version", [pkg.version]);

this.config.compilers = {
solc: {
Expand All @@ -204,7 +204,7 @@ export class SolidityLauncher {
},
},
};
this.truffle = loadLibrary(this.config);
this.truffle = require("truffle");
this.api = new API(myConfig);

if (args.includes("--help") || args.includes("-h")) {
Expand Down Expand Up @@ -327,7 +327,7 @@ export class SolidityLauncher {
for (const target of targetPool.targets) {
targetPaths.add(target.canonicalPath);

const [importsMap, dependencyMap] = targetPool.getImportDependencies(
const { dependencyMap } = targetPool.getImportDependencies(
target.canonicalPath,
target.targetName
);
Expand Down Expand Up @@ -366,7 +366,7 @@ export class SolidityLauncher {
target.canonicalPath,
target.targetName
);
const [importsMap, dependencyMap] = targetPool.getImportDependencies(
const { importMap, dependencyMap } = targetPool.getImportDependencies(
target.canonicalPath,
target.targetName
);
Expand All @@ -375,7 +375,7 @@ export class SolidityLauncher {

finalImportsMap = new Map([
...Array.from(finalImportsMap.entries()),
...Array.from(importsMap.entries()),
...Array.from(importMap.entries()),
]);
finalDependencies = new Map([
...Array.from(finalDependencies.entries()),
Expand Down Expand Up @@ -454,7 +454,7 @@ export class SolidityLauncher {

const ast = targetPool.getAST(targetPath);

const functionMap = targetPool.getFunctionMap(targetPath, target);
const functionMap = targetPool.getFunctionMapSpecific(targetPath, target);

const currentSubject = new SoliditySubject(
path.basename(targetPath),
Expand All @@ -468,12 +468,12 @@ export class SolidityLauncher {
return new Archive();
}

const [importsMap, dependencyMap] = targetPool.getImportDependencies(
const { importMap, dependencyMap } = targetPool.getImportDependencies(
targetPath,
target
);

const stringifier = new SolidityDecoder(importsMap, dependencyMap);
const stringifier = new SolidityDecoder(importMap, dependencyMap);
const suiteBuilder = new SoliditySuiteBuilder(
stringifier,
this.api,
Expand Down
24 changes: 13 additions & 11 deletions src/analysis/static/SolidityTarget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import { DependencyAnalyzer } from "./dependency/DependencyAnalyzer";
import { TargetContext } from "./dependency/TargetContext";
import { ContractMetadata } from "./map/ContractMetadata";
import { Graph } from "./Graph";
import { SourceUnit } from "@solidity-parser/parser/dist/src/ast-types";
import { ContractFunction } from "./map/ContractFunction";

/**
* Target system under test.
Expand All @@ -41,12 +43,12 @@ export class SolidityTarget extends Target {
protected _sources: Map<string, string>;

// Mapping: filepath -> AST
protected _abstractSyntaxTrees: Map<string, any>;
protected _abstractSyntaxTrees: Map<string, SourceUnit>;

protected _context: TargetContext<ContractMetadata>;

// Mapping: target name -> function name -> function
protected _functions: Map<string, Map<string, any>>;
protected _functions: Map<string, Map<string, ContractFunction>>;

// Mapping: target name -> (function name -> CFG)
protected _controlFlowGraphs: Map<string, CFG>;
Expand All @@ -59,10 +61,10 @@ export class SolidityTarget extends Target {
targetPath: string,
targetName: string,
sources: Map<string, string>,
ASTs: Map<string, any>,
ASTs: Map<string, SourceUnit>,
context: TargetContext<ContractMetadata>,
functions: Map<string, Map<string, any>>,
CFGs: Map<string, any>,
functions: Map<string, Map<string, ContractFunction>>,
CFGs: Map<string, CFG>,
linkingGraph: Graph<string>
) {
super();
Expand Down Expand Up @@ -92,8 +94,8 @@ export class SolidityTarget extends Target {

// Get source, AST, FunctionMap, and CFG for target under test
const sources = new Map<string, string>();
const abstractSyntaxTrees = new Map<string, any>();
const functionMaps = new Map<string, Map<string, any>>();
const abstractSyntaxTrees = new Map<string, SourceUnit>();
const functionMaps = new Map<string, Map<string, ContractFunction>>();
const controlFlowGraphs = new Map<string, CFG>();

sources.set(absoluteTargetPath, targetPool.getSource(absoluteTargetPath));
Expand All @@ -103,7 +105,7 @@ export class SolidityTarget extends Target {
);
functionMaps.set(
targetName,
targetPool.getFunctionMap(absoluteTargetPath, targetName)
targetPool.getFunctionMapSpecific(absoluteTargetPath, targetName)
);
controlFlowGraphs.set(
targetName,
Expand All @@ -125,7 +127,7 @@ export class SolidityTarget extends Target {
context.getTargets(filePath).forEach((contractMetadata) => {
functionMaps.set(
contractMetadata.name,
targetPool.getFunctionMap(filePath, contractMetadata.name)
targetPool.getFunctionMapSpecific(filePath, contractMetadata.name)
);
});
});
Expand Down Expand Up @@ -160,15 +162,15 @@ export class SolidityTarget extends Target {
return this._sources.get(targetPath);
}

getAST(targetPath: string): any {
getAST(targetPath: string): SourceUnit {
return this._abstractSyntaxTrees.get(targetPath);
}

getContext(): TargetContext<ContractMetadata> {
return this._context;
}

getFunctions(targetName: string): Map<string, any> {
getFunctions(targetName: string): Map<string, ContractFunction> {
return this._functions.get(targetName);
}

Expand Down
54 changes: 39 additions & 15 deletions src/analysis/static/SolidityTargetPool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@ import { CFG, Properties, TargetPool, Target } from "@syntest/core";
import { ImportVisitor } from "./dependency/ImportVisitor";
import * as fs from "fs";
import { LibraryVisitor } from "./dependency/LibraryVisitor";
const SolidityParser = require("@solidity-parser/parser");
import SolidityParser = require("@solidity-parser/parser");
import { SourceUnit } from "@solidity-parser/parser/dist/src/ast-types";
// eslint-disable-next-line
const { outputFileSync, copySync } = require("fs-extra");
const Instrumenter = require("../../../src/instrumentation/instrumenter"); // Local version

/**
* Pool for retrieving and caching expensive processing calls.
Expand All @@ -48,7 +49,7 @@ export class SolidityTargetPool extends TargetPool {
protected _sources: Map<string, string>;

// Mapping: filepath -> AST
protected _abstractSyntaxTrees: Map<string, any>;
protected _abstractSyntaxTrees: Map<string, SourceUnit>;

// Mapping: filepath -> target name -> target
protected _targetMap: Map<string, Map<string, ContractMetadata>>;
Expand All @@ -62,10 +63,13 @@ export class SolidityTargetPool extends TargetPool {
// Mapping: filepath -> target name -> (function name -> CFG)
protected _controlFlowGraphs: Map<string, Map<string, CFG>>;

// Mapping: filepath -> target name -> [importsMap, dependencyMap]
// Mapping: filepath -> target name -> {importsMap, dependencyMap}
protected _dependencyMaps: Map<
string,
Map<string, [Map<string, string>, Map<string, Target[]>]>
Map<
string,
{ importMap: Map<string, string>; dependencyMap: Map<string, Target[]> }
>
>;

constructor(
Expand All @@ -81,7 +85,7 @@ export class SolidityTargetPool extends TargetPool {
this._controlFlowGraphGenerator = controlFlowGraphGenerator;

this._sources = new Map<string, string>();
this._abstractSyntaxTrees = new Map<string, any>();
this._abstractSyntaxTrees = new Map<string, SourceUnit>();
this._targetMap = new Map<string, Map<string, ContractMetadata>>();
this._functionMaps = new Map<
string,
Expand All @@ -104,7 +108,7 @@ export class SolidityTargetPool extends TargetPool {
}
}

getAST(targetPath: string): any {
getAST(targetPath: string): SourceUnit {
const absoluteTargetPath = path.resolve(targetPath);

if (this._abstractSyntaxTrees.has(absoluteTargetPath)) {
Expand All @@ -118,7 +122,7 @@ export class SolidityTargetPool extends TargetPool {
}
}

getTargetMap(targetPath: string): Map<string, any> {
getTargetMap(targetPath: string): Map<string, ContractMetadata> {
const absoluteTargetPath = path.resolve(targetPath);

if (this._targetMap.has(absoluteTargetPath)) {
Expand All @@ -133,7 +137,26 @@ export class SolidityTargetPool extends TargetPool {
}
}

getFunctionMap(targetPath: string, targetName: string): Map<string, any> {
getFunctionMap(
targetPath: string
): Map<string, Map<string, ContractFunction>> {
const absoluteTargetPath = path.resolve(targetPath);

if (!this._functionMaps.has(absoluteTargetPath)) {
const targetAST = this.getAST(absoluteTargetPath);
const { targetMap, functionMap } =
this._targetMapGenerator.generate(targetAST);
this._targetMap.set(absoluteTargetPath, targetMap);
this._functionMaps.set(absoluteTargetPath, functionMap);
}

return this._functionMaps.get(absoluteTargetPath);
}

getFunctionMapSpecific(
targetPath: string,
targetName: string
): Map<string, ContractFunction> {
const absoluteTargetPath = path.resolve(targetPath);

if (!this._functionMaps.has(absoluteTargetPath)) {
Expand Down Expand Up @@ -176,7 +199,7 @@ export class SolidityTargetPool extends TargetPool {
getImportDependencies(
targetPath: string,
targetName: string
): [Map<string, string>, Map<string, Target[]>] {
): { importMap: Map<string, string>; dependencyMap: Map<string, Target[]> } {
const absoluteTargetPath = path.resolve(targetPath);

if (!this._dependencyMaps.has(absoluteTargetPath))
Expand All @@ -186,8 +209,8 @@ export class SolidityTargetPool extends TargetPool {
return this._dependencyMaps.get(absoluteTargetPath).get(targetName);
} else {
// Import the contract under test
const importsMap = new Map<string, string>();
importsMap.set(targetName, targetName);
const importMap = new Map<string, string>();
importMap.set(targetName, targetName);

// Find all external imports in the contract under test
const importVisitor = new ImportVisitor();
Expand All @@ -214,7 +237,7 @@ export class SolidityTargetPool extends TargetPool {
SolidityParser.visit(astLib, libraryVisitor);

// Import the external file in the test
importsMap.set(
importMap.set(
path.basename(importPath).split(".")[0],
path.basename(importPath).split(".")[0]
);
Expand All @@ -237,11 +260,12 @@ export class SolidityTargetPool extends TargetPool {

this._dependencyMaps
.get(targetPath)
.set(targetName, [importsMap, dependencyMap]);
return [importsMap, dependencyMap];
.set(targetName, { importMap, dependencyMap });
return { importMap, dependencyMap };
}
}

// eslint-disable-next-line
async prepareAndInstrument(api: any): Promise<void> {
const absoluteRootPath = path.resolve(Properties.target_root_directory);

Expand Down
7 changes: 4 additions & 3 deletions src/analysis/static/ast/ASTGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
* limitations under the License.
*/

const SolidityParser = require("@solidity-parser/parser");
import SolidityParser = require("@solidity-parser/parser");
import { SourceUnit } from "@solidity-parser/parser/dist/src/ast-types";

/**
* Abstract Syntax Trees (AST) generator for targets.
Expand All @@ -29,8 +30,8 @@ export class ASTGenerator {
*
* @param targetSource The source of the target
*/
generate(targetSource: string): string {
return SolidityParser.parse(targetSource, {
generate(targetSource: string): SourceUnit {
return <SourceUnit>SolidityParser.parse(targetSource, {
loc: true,
range: true,
});
Expand Down
4 changes: 2 additions & 2 deletions src/analysis/static/dependency/DependencyAnalyzer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import { ImportVisitor } from "./ImportVisitor";
import { Graph } from "../Graph";
import { PublicVisibility } from "../parsing/Visibility";

const SolidityParser = require("@solidity-parser/parser");
import SolidityParser = require("@solidity-parser/parser");

/**
* Analyzer that discovers all dependencies in the target.
Expand Down Expand Up @@ -183,7 +183,7 @@ export class DependencyAnalyzer {
const contracts = this._targetPool.getTargetMap(importedFilePath);
contracts.forEach((contractMetadata: ContractMetadata) => {
if (contractMetadata.kind === ContractKind.Library) {
const functions = this._targetPool.getFunctionMap(
const functions = this._targetPool.getFunctionMapSpecific(
importedFilePath,
contractMetadata.name
);
Expand Down
Loading