Skip to content

Create type prediction plugins #128

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
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
157 changes: 80 additions & 77 deletions libraries/cfg-javascript/lib/ControlFlowGraphGenerator.ts

Large diffs are not rendered by default.

19 changes: 11 additions & 8 deletions libraries/cfg-javascript/lib/ControlFlowGraphVisitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,16 @@ import {
BranchNode,
ControlFlowGraph,
Node,
Edge,
NodeType,
Operation,
PlaceholderNode,
RootNode,
} from "@syntest/cfg-core";

export class ControlFlowGraphVisitor {
private _cfg: ControlFlowGraph;
private _nodes: Node[];
private _edges: Edge[];

// private _nodeStack: Node[][]
//
Expand All @@ -42,7 +44,8 @@ export class ControlFlowGraphVisitor {
private _nodeStack: Node[];

constructor() {
this._cfg = new ControlFlowGraph();
this._nodes = [];
this._edges = [];

this._trackingLeaves = [];
this._firstExit = true;
Expand Down Expand Up @@ -407,7 +410,7 @@ export class ControlFlowGraphVisitor {
description: description,
};

this._cfg.nodes.push(node);
this._nodes.push(node);

return node;
}
Expand All @@ -430,7 +433,7 @@ export class ControlFlowGraphVisitor {
statements: statements,
};

this._cfg.nodes.push(node);
this._nodes.push(node);

return node;
}
Expand All @@ -446,7 +449,7 @@ export class ControlFlowGraphVisitor {
statements: statements,
};

this._cfg.nodes.push(node);
this._nodes.push(node);

return node;
}
Expand All @@ -465,7 +468,7 @@ export class ControlFlowGraphVisitor {
probe: false,
};

this._cfg.nodes.push(node);
this._nodes.push(node);

return node;
}
Expand All @@ -476,7 +479,7 @@ export class ControlFlowGraphVisitor {
}
for (const parent of parents) {
for (const child of children) {
this._cfg.edges.push({
this._edges.push({
from: parent.id,
to: child.id,
});
Expand All @@ -485,6 +488,6 @@ export class ControlFlowGraphVisitor {
}

get cfg(): ControlFlowGraph {
return this._cfg;
return new ControlFlowGraph(this._nodes, this._edges);
}
}
4 changes: 2 additions & 2 deletions libraries/cfg-javascript/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@
"test:watch": "mocha --config ../../.mocharc.json --watch"
},
"dependencies": {
"@syntest/cfg-core": "*",
"@syntest/core": "*"
"@syntest/cfg-core": "^0.3.0-beta.7",
"@syntest/core": "^0.4.0-beta.7"
},
"engines": {
"node": ">=10.24.0"
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#!/usr/bin/env node
/*
* Copyright 2020-2023 Delft University of Technology and SynTest contributors
*
Expand All @@ -16,14 +15,12 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { EventManager, PluginManager } from "@syntest/core";
import { JavaScriptLauncher } from "./JavaScriptLauncher";
import { JavaScriptTestCase } from "./testcase/JavaScriptTestCase";

const name = "syntest-javascript";
const state = {};
const eventManager = new EventManager<JavaScriptTestCase>(state);
const pluginManager = new PluginManager<JavaScriptTestCase>();
import { JavaScriptTestCase } from "../../../testcase/JavaScriptTestCase";

const launcher = new JavaScriptLauncher(name, eventManager, pluginManager);
launcher.run(process.argv);
export abstract class DynamicTypeResolver {
abstract processTestResult(
testCase: JavaScriptTestCase,
testResult: Mocha.Test
): void;
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* Copyright 2020-2023 Delft University of Technology and SynTest contributors
*
* This file is part of SynTest Framework - SynTest Javascript.
* This file is part of SynTest Framework - SynTest JavaScript.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -15,44 +15,18 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { JavaScriptTestCase } from "../JavaScriptTestCase";
import { Statement } from "../statements/Statement";
import Mocha = require("mocha");

export default class ExecutionInformationIntegrator {
// eslint-disable-next-line
processSuccess(testCase: JavaScriptTestCase, testResult: Mocha.Test) {
// TODO
// const queue: Statement[] = [testCase.root]
//
// while (queue.length) {
// const root = queue.pop()
// const children = root.getChildren()
//
// for (const child of children) {
// child.identifierDescription.typeProbabilityMap.addExecutionScore(child.type, 1)
// queue.push(child)
// }
// }
}

processError(testCase: JavaScriptTestCase, testResult: Mocha.Test) {
// console.log(testResult.err.name)
// console.log(testResult.err.message)
// console.log()

// if (!testResult.err.stack.split('\n')[2].includes('tempTest.spec.js')) {
// // console.log(testResult.err)
// // console.log()
// return
// }
//
// if (testResult.err.name !== 'TypeError') {
// return
// }

// console.log(testResult.err)
import { JavaScriptTestCase } from "../../../testcase/JavaScriptTestCase";
import { Statement } from "../../../testcase/statements/Statement";
import { DynamicTypeResolver } from "./DynamicTypeResolver";

export class SimpleDynamicTypeResolver extends DynamicTypeResolver {
processTestResult(
testCase: JavaScriptTestCase,
testResult: Mocha.Test
): void {
if (!testResult.err || !testResult.err.message) {
return;
}

const queue: Statement[] = [testCase.root];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,27 +20,22 @@ import * as path from "path";
import { ControlFlowGraph } from "@syntest/cfg-core";
import { getAllFiles, readFile } from "../../utils/fileSystem";
import { AbstractSyntaxTreeGenerator } from "@syntest/ast-javascript";
import {
CONFIG,
EventManager,
TargetMetaData,
TargetPool,
} from "@syntest/core";
import { TargetMetaData, TargetPool } from "@syntest/core";
import { TargetMapGenerator } from "./map/TargetMapGenerator";
import { ControlFlowGraphGenerator } from "@syntest/cfg-javascript";
import { ImportGenerator } from "./dependency/ImportGenerator";
import { ExportGenerator } from "./dependency/ExportGenerator";
import { existsSync, lstatSync } from "fs";
import { Export } from "./dependency/ExportVisitor";
import { SubjectType } from "../../search/JavaScriptSubject";
import { TypeResolver } from "./types/resolving/TypeResolver";
import { StaticTypeResolver } from "./types/resolving/StaticTypeResolver";
import { VariableGenerator } from "./types/discovery/VariableGenerator";
import { ObjectGenerator } from "./types/discovery/object/ObjectGenerator";
import { ComplexObject } from "./types/discovery/object/ComplexObject";
import { Relation } from "./types/discovery/Relation";
import { Element } from "@syntest/ast-javascript";
import { TypeEnum } from "./types/resolving/TypeEnum";
import { TypeProbability } from "./types/resolving/TypeProbability";
import { TypeEnum } from "./types/TypeEnum";
import { TypeProbability } from "./types/TypeProbability";
import { Instrumenter } from "@syntest/instrumentation-javascript";
import { ExportType } from "./dependency/IdentifierVisitor";
import * as t from "@babel/types";
Expand All @@ -62,7 +57,7 @@ export class JavaScriptTargetPool extends TargetPool<JavaScriptTestCase> {
protected controlFlowGraphGenerator: ControlFlowGraphGenerator;
protected importGenerator: ImportGenerator;
protected exportGenerator: ExportGenerator;
private _typeResolver: TypeResolver;
private _typeResolver: StaticTypeResolver;

// Mapping: filepath -> source code
protected _sources: Map<string, string>;
Expand All @@ -89,15 +84,14 @@ export class JavaScriptTargetPool extends TargetPool<JavaScriptTestCase> {
protected _exportMap: Map<string, Export[]>;

constructor(
eventManager: EventManager<JavaScriptTestCase>,
abstractSyntaxTreeGenerator: AbstractSyntaxTreeGenerator,
targetMapGenerator: TargetMapGenerator,
controlFlowGraphGenerator: ControlFlowGraphGenerator,
importGenerator: ImportGenerator,
exportGenerator: ExportGenerator,
typeResolver: TypeResolver
typeResolver: StaticTypeResolver
) {
super(eventManager);
super();
this.abstractSyntaxTreeGenerator = abstractSyntaxTreeGenerator;
this.targetMapGenerator = targetMapGenerator;
this.controlFlowGraphGenerator = controlFlowGraphGenerator;
Expand Down Expand Up @@ -374,12 +368,15 @@ export class JavaScriptTargetPool extends TargetPool<JavaScriptTestCase> {
return this._dependencyMaps.get(absoluteTargetPath);
}

async prepareAndInstrument(): Promise<void> {
const absoluteRootPath = path.resolve(CONFIG.targetRootDirectory);
async prepareAndInstrument(
targetRootDirectory: string,
tempInstrumentedDirectory: string
): Promise<void> {
const absoluteRootPath = path.resolve(targetRootDirectory);

const destinationPath = path.resolve(
CONFIG.tempInstrumentedDirectory,
path.basename(CONFIG.targetRootDirectory)
tempInstrumentedDirectory,
path.basename(targetRootDirectory)
);

// copy everything
Expand All @@ -405,8 +402,8 @@ export class JavaScriptTargetPool extends TargetPool<JavaScriptTestCase> {
}
}

scanTargetRootDirectory(): void {
const absoluteRootPath = path.resolve(CONFIG.targetRootDirectory);
scanTargetRootDirectory(targetRootDirectory: string): void {
const absoluteRootPath = path.resolve(targetRootDirectory);

// TODO remove the filters
const files = getAllFiles(absoluteRootPath, ".js").filter(
Expand Down Expand Up @@ -583,7 +580,7 @@ export class JavaScriptTargetPool extends TargetPool<JavaScriptTestCase> {
);
}

get typeResolver(): TypeResolver {
get typeResolver(): StaticTypeResolver {
return this._typeResolver;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { TargetMetaData } from "@syntest/core";
import { ActionType } from "../parsing/ActionType";
import { ActionVisibility } from "../parsing/ActionVisibility";
import { ActionDescription } from "../parsing/ActionDescription";
import { TypeProbability } from "../types/resolving/TypeProbability";
import { TypeProbability } from "../types/TypeProbability";
import { AbstractSyntaxTreeVisitor } from "@syntest/ast-javascript";
import { IdentifierDescription } from "../parsing/IdentifierDescription";
import { ComplexObject } from "../types/discovery/object/ComplexObject";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* limitations under the License.
*/

import { TypeProbability } from "../types/resolving/TypeProbability";
import { TypeProbability } from "../types/TypeProbability";

/**
* Interface for a IdentifierDescription Description.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@
* limitations under the License.
*/

import { ComplexObject } from "../../discovery/object/ComplexObject";
import { TypeProbability } from "../TypeProbability";
import { ComplexObject } from "./discovery/object/ComplexObject";
import { TypeProbability } from "./TypeProbability";
import { Element, ElementType } from "@syntest/ast-javascript";
import { Relation, RelationType } from "../../discovery/Relation";
import { TypeEnum } from "../TypeEnum";
import { Relation, RelationType } from "./discovery/Relation";
import { TypeEnum } from "./TypeEnum";

export function createAnonObject(
properties: Element[],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@
*/

import { TypeEnum } from "./TypeEnum";
import { CONFIG, prng } from "@syntest/core";
import { ComplexObject } from "../discovery/object/ComplexObject";
import { JavaScriptArguments } from "../../../../JavaScriptLauncher";
import { prng } from "@syntest/core";
import { ComplexObject } from "./discovery/object/ComplexObject";

/**
* Type Probability Map
Expand Down Expand Up @@ -190,10 +189,7 @@ export class TypeProbability {
minValue = Math.min(minValue, this.executionScores.get(key));
}

if (
(<JavaScriptArguments>CONFIG).incorporateExecutionInformation &&
this.executionScores.size
) {
if (this.executionScores.size) {
// calculate total
let totalScore = 0;
for (const key of this.probabilities.keys()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
*/

import { Element } from "@syntest/ast-javascript";
import { TypeProbability } from "../resolving/TypeProbability";
import { TypeProbability } from "../TypeProbability";

export class ElementTypeMap {
private elementMap: Map<string, Element>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* limitations under the License.
*/

import { TypeProbability } from "../../resolving/TypeProbability";
import { TypeProbability } from "../../TypeProbability";
import { Export } from "../../../dependency/ExportVisitor";

export interface ComplexObject {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@
* limitations under the License.
*/

import { TypeResolver } from "../TypeResolver";
import { StaticTypeResolver } from "./StaticTypeResolver";
import { elementTypeToTypingType, TypeEnum } from "../TypeEnum";
import { Relation, RelationType } from "../../discovery/Relation";
import { Relation, RelationType } from "../discovery/Relation";
import { Element, Scope } from "@syntest/ast-javascript";
import { ComplexObject } from "../../discovery/object/ComplexObject";
import { ComplexObject } from "../discovery/object/ComplexObject";
import { TypeProbability } from "../TypeProbability";
import { createAnonObject } from "./ObjectMatcher";
import { createAnonObject } from "../ObjectMatcher";

export class TypeResolverInference extends TypeResolver {
export class ProbabilisticStaticTypeResolver extends StaticTypeResolver {
/**
* This function resolves constant elements such as numerical constants or other primitives
* @param elements the elements to resolve
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@
* limitations under the License.
*/

import { TypeResolver } from "./TypeResolver";
import { TypeProbability } from "./TypeProbability";
import { StaticTypeResolver } from "./StaticTypeResolver";
import { TypeProbability } from "../TypeProbability";

export class TypeResolverUnknown extends TypeResolver {
export class RandomTypeStaticResolver extends StaticTypeResolver {
getTyping(): TypeProbability {
return new TypeProbability();
}
Expand Down
Loading