Skip to content

Commit

Permalink
fix: sampling and statements
Browse files Browse the repository at this point in the history
  • Loading branch information
dstallenberg committed Sep 14, 2023
1 parent ab2d1d1 commit b32ba57
Show file tree
Hide file tree
Showing 14 changed files with 87 additions and 111 deletions.
10 changes: 6 additions & 4 deletions libraries/analysis-solidity/lib/instrumentation/Instrumenter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@ import { InstrumentationData } from "./datastructures/InstrumentationData";
import * as SolidityParser from "@solidity-parser/parser";
import { ASTNode } from "@solidity-parser/parser/dist/src/ast-types";

import Injector from "./injector"; // Local copy
import preprocess from "solidity-coverage/lib/preprocessor";

import parse from "./parse"; // Local copy
// eslint-disable-next-line @typescript-eslint/no-var-requires, unicorn/prefer-module
const Injector = require("./injector"); // Local copy
// eslint-disable-next-line @typescript-eslint/no-var-requires, unicorn/prefer-module
const preprocess = require("solidity-coverage/lib/preprocessor");
// eslint-disable-next-line @typescript-eslint/no-var-requires, unicorn/prefer-module
const parse = require("./parse"); // Local copy

export class Instrumenter {
protected instrumentationData;
Expand Down
14 changes: 7 additions & 7 deletions libraries/analysis-solidity/lib/types/Type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,42 +48,42 @@ export type BaseType = {

export type Address = {
type: TypeEnum.ADDRESS;
stateMutability: StateMutability | null;
stateMutability?: StateMutability | undefined;
};

export type Bool = {
type: TypeEnum.BOOL;
stateMutability: StateMutability | null;
stateMutability?: StateMutability | undefined;
};

export type Int = {
type: TypeEnum.INT;
bits: number;
signed: true;
stateMutability: StateMutability | null;
stateMutability?: StateMutability | undefined;
};

export type Uint = {
type: TypeEnum.INT;
bits: number;
signed: false;
stateMutability: StateMutability | null;
stateMutability?: StateMutability | undefined;
};

export type Fixed = {
type: TypeEnum.FIXED;
bits: number;
signed: true;
decimals: number;
stateMutability: StateMutability | null;
stateMutability?: StateMutability | undefined;
};

export type Ufixed = {
type: TypeEnum.FIXED;
bits: number;
signed: false;
decimals: number;
stateMutability: StateMutability | null;
stateMutability?: StateMutability | undefined;
};

export type FixedSizeByteArray = {
Expand Down Expand Up @@ -113,7 +113,7 @@ export type FunctionType = {
parameters: Parameter[];
returns: Parameter[];
visibility: Visibility;
stateMutability: StateMutability | null;
stateMutability?: StateMutability | undefined;
};

export type Mapping = {
Expand Down
2 changes: 1 addition & 1 deletion libraries/analysis-solidity/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@
"rootDir": ".",
"composite": true
},
"include": ["**/*.ts"]
"include": ["**/*.ts", "**/*.js"]
}
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ export class SolidityExecutionResult implements ExecutionResult {
/**
* @inheritDoc
*/
public getTraces(): Datapoint[] {
public getTraces(): Trace[] {
return this._traces;
}

Expand Down
12 changes: 6 additions & 6 deletions libraries/search-solidity/lib/testbuilding/SolidityDecoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export class SolidityDecoder implements Decoder<SolidityTestCase, string> {
}
}
const formattedArguments = arguments_
.map((a: PrimitiveStatement<any>) => a.varName)
.map((a: PrimitiveStatement<any, any>) => a.varName)
.join(", ");

const sender = (statement as ConstructorCall).getSender().getValue();
Expand Down Expand Up @@ -101,8 +101,8 @@ export class SolidityDecoder implements Decoder<SolidityTestCase, string> {
throw new TypeError(`${statement} is not a primitive statement`);
}

const primitive: PrimitiveStatement<any> =
statement as PrimitiveStatement<any>;
const primitive: PrimitiveStatement<any, any> =
statement as PrimitiveStatement<any, any>;
// TODO what happened to float support?
if (
statement.type.type.startsWith("int") ||
Expand Down Expand Up @@ -369,7 +369,7 @@ export class SolidityDecoder implements Decoder<SolidityTestCase, string> {
for (const variableName of gene.varNames) {
functionCalls.push(
`\t\tawait fs.writeFileSync('${path.join(
CONFIG.tempLogDirectory,
this.tempLogDirectory,
ind.id,
variableName
)}', '' + ${variableName})`
Expand All @@ -379,7 +379,7 @@ export class SolidityDecoder implements Decoder<SolidityTestCase, string> {
for (const variableName of gene.varNames) {
testString.push(
`\t\tawait fs.writeFileSync('${path.join(
CONFIG.tempLogDirectory,
this.tempLogDirectory,
ind.id,
variableName
)}', '' + ${variableName})`
Expand All @@ -403,7 +403,7 @@ export class SolidityDecoder implements Decoder<SolidityTestCase, string> {
if (addLogs) {
testString.push(`} catch (e) {`,
`await fs.writeFileSync('${path.join(
CONFIG.tempLogDirectory,
this.tempLogDirectory,
ind.id,
"error"
)}', '' + e.stack)`,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,12 @@ import {
TypeEnum,
Ufixed,
Uint,
Visibility,
} from "@syntest/analysis-solidity";
import { TargetType } from "@syntest/analysis";
import { FunctionTarget } from "@syntest/analysis-solidity";
import { IntegerStatement } from "../statements/primitive/IntegerStatement";
import { DynamicSizeByteArrayStatement } from "../statements/primitive/DynamicSizeByteArrayStatement";
import { FixedSizeByteArrayStatement } from "../statements/complex/FixedSizeByteArrayStatement";
import { FixedSizeByteArrayStatement } from "../statements/primitive/FixedSizeByteArrayStatement";

/**
* SolidityRandomSampler class
Expand Down Expand Up @@ -129,7 +128,13 @@ export class SolidityRandomSampler extends SoliditySampler {
root,
action.name,
arguments_,
AddressStatement.getRandom()
this.sampleAddressStatement(depth + 1, {
name: 'address',
type: {
type: TypeEnum.ADDRESS,
stateMutability: undefined
}
})
);
}

Expand All @@ -152,7 +157,13 @@ export class SolidityRandomSampler extends SoliditySampler {
`${action.name}`,
arguments_,
[],
AddressStatement.getRandom()
this.sampleAddressStatement(depth + 1, {
name: 'address',
type: {
type: TypeEnum.ADDRESS,
stateMutability: undefined
}
})
);

const nCalls = prng.nextInt(1, 5);
Expand All @@ -171,7 +182,13 @@ export class SolidityRandomSampler extends SoliditySampler {
`${this._subject.name}`,
[],
[],
AddressStatement.getRandom()
this.sampleAddressStatement(depth + 1, {
name: 'address',
type: {
type: TypeEnum.ADDRESS,
stateMutability: undefined
}
})
);

const nCalls = prng.nextInt(1, 5);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,11 @@ import { Statement } from "../statements/Statement";
import { SoliditySubject } from "../../search/SoliditySubject";
import { ConstantPool, RootContext } from "@syntest/analysis-solidity";
import { NumericStatement } from "../statements/primitive/NumericStatement";
import { ArrayStatement } from "../statements/complex/ArrayStatement";
import { AddressStatement } from "../statements/primitive/AddressStatement";
import { BoolStatement } from "../statements/primitive/BoolStatement";
import { IntegerStatement } from "../statements/primitive/IntegerStatement";
import { StringStatement } from "../statements/primitive/StringStatement";
import { FixedSizeByteArrayStatement } from "../statements/complex/FixedSizeByteArrayStatement";
import { FixedSizeByteArrayStatement } from "../statements/primitive/FixedSizeByteArrayStatement";
import { DynamicSizeByteArrayStatement } from "../statements/primitive/DynamicSizeByteArrayStatement";

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
*/

import { Statement } from "../Statement";
import { EncodingSampler } from "@syntest/search";
import { Encoding } from "@syntest/search";
import { Parameter } from "@syntest/analysis-solidity";

/**
Expand All @@ -36,13 +34,6 @@ export abstract class ActionStatement extends Statement {
this._arguments_ = arguments_;
}

abstract mutate(
sampler: EncodingSampler<Encoding>,
depth: number
): ActionStatement;

abstract copy(): ActionStatement;

hasChildren(): boolean {
return this._arguments_.length > 0;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,12 @@ export class ObjectFunctionCall extends ActionStatement {
);
}

hasChildren(): boolean {
override hasChildren(): boolean {
// since every object function call has an instance there must be atleast one child
return true;
}

getChildren(): Statement[] {
override getChildren(): Statement[] {
return [...this.arguments_];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { prng } from "@syntest/prng";
import { PrimitiveStatement } from "../primitive/PrimitiveStatement";
import { FixedSizeByteArray, Parameter } from "@syntest/analysis-solidity";
import { SoliditySampler } from "../../sampling/SoliditySampler";
import { Statement } from "../Statement";

/**
* Special statement specific to solidity contracts
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,8 @@
import { prng } from "@syntest/prng";

import BigNumber from "bignumber.js";
import { ConstantPool } from "../../../../../analysis-solidity/lib/constant/ConstantPool";
import { PrimitiveStatement } from "./PrimitiveStatement";
import { Fixed, Parameter, Ufixed } from "@syntest/analysis-solidity";
import { SolidityArguments } from "../../../SolidityLauncher";
import { SoliditySampler } from "../../sampling/SoliditySampler";
import { Statement } from "../Statement";

Expand Down
57 changes: 28 additions & 29 deletions libraries/search-solidity/lib/util/fileSystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,12 @@
* limitations under the License.
*/

import { existsSync, mkdirSync, rmdirSync, writeFileSync } from "node:fs";
import { existsSync, writeFileSync } from "node:fs";

import { CONFIG, getUserInterface } from "@syntest/search";
import globby = require("globby");
import recursive = require("recursive-readdir");
// import globby = require("globby");
// import recursive = require("recursive-readdir");

export async function createTruffleConfig() {
export async function createTruffleConfig(temporaryTestDirectory: string) {
const filepath = "./truffle-config.js";

if (existsSync(filepath)) {
Expand All @@ -32,32 +31,32 @@ export async function createTruffleConfig() {
await writeFileSync(
filepath,
`module.exports = {
test_directory: "${CONFIG.tempTestDirectory}",
test_directory: "${temporaryTestDirectory}",
plugins: ["@syntest/solidity"]
};`
);
}

/**
* Returns a list of test files to pass to mocha.
* @param {Object} config truffleConfig
* @return {String[]} list of files to pass to mocha
*/
export async function getTestFilePaths(config) {
let target;

// Handle --file <path|glob> cli option (subset of tests)
typeof config.file === "string"
? (target = globby.sync([config.file]))
: (target = await recursive(config.testDir));

// Filter native solidity tests and warn that they're skipped
const solregex = /.*\.(sol)$/;
const hasSols = target.filter((f) => f.match(solregex) != undefined);

if (hasSols.length > 0) LOGGER.info("sol-tests " + [hasSols.length]);

// Return list of test files
const testregex = /.*\.(js|ts|es|es6|jsx)$/;
return target.filter((f) => f.match(testregex) != undefined);
}
// /**
// * Returns a list of test files to pass to mocha.
// * @param {Object} config truffleConfig
// * @return {String[]} list of files to pass to mocha
// */
// export async function getTestFilePaths(config) {
// let target;

// // Handle --file <path|glob> cli option (subset of tests)
// typeof config.file === "string"
// ? (target = globby.sync([config.file]))
// : (target = await recursive(config.testDir));

// // Filter native solidity tests and warn that they're skipped
// const solregex = /.*\.(sol)$/;
// const hasSols = target.filter((f) => f.match(solregex) != undefined);

// if (hasSols.length > 0) console.info("sol-tests " + [hasSols.length]);

// // Return list of test files
// const testregex = /.*\.(js|ts|es|es6|jsx)$/;
// return target.filter((f) => f.match(testregex) != undefined);
// }
30 changes: 0 additions & 30 deletions libraries/search-solidity/test/testcase/AddressStatement.test.ts

This file was deleted.

Loading

0 comments on commit b32ba57

Please sign in to comment.