Skip to content
This repository has been archived by the owner on Apr 2, 2024. It is now read-only.

Commit

Permalink
get this temp-executation eval working
Browse files Browse the repository at this point in the history
  • Loading branch information
mikeseese committed May 9, 2018
1 parent 1d0db57 commit 3a56eff
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 53 deletions.
29 changes: 17 additions & 12 deletions src/evaluator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,11 +144,6 @@ function ` + functionName + `(` + argsString + `) returns (bool) {
return;
}

if (this._runtime._ongoingEvaluation !== null) {
// TODO: improve this
return;
}

expression = expression + (expression.endsWith(';') ? '' : ';');
let contract = this._runtime._contractsByAddress.get(this._runtime._stepData.contractAddress)!;
let file = this._runtime._files.get(contract.sourcePath)!;
Expand Down Expand Up @@ -290,20 +285,30 @@ function ` + functionName + `(` + argsString + `) returns (bool) {

// find last jumpdest thats within the source location

this._runtime._ongoingEvaluation = new LibSdbTypes.Evaluation();
this._runtime._ongoingEvaluation.functionName = functionInsert.name;
this._runtime._ongoingEvaluation.callback = callback;
this._runtime._ongoingEvaluation.returnVariable.originalType = returnTypeString;
let ongoingEvaluation = new LibSdbTypes.Evaluation();
ongoingEvaluation.functionName = functionInsert.name;
ongoingEvaluation.returnVariable.originalType = returnTypeString;
// TODO: handle referenceVars in result?
const compilationProcessor = new LibSdbCompilationProcessor();
const contractProcessor = new ContractProcessor(compilationProcessor, contract);
this._runtime._ongoingEvaluation.returnVariable.applyType("default", "ParameterList", contractProcessor);
this._runtime._ongoingEvaluation.contractAddress = this._runtime._stepData.contractAddress;
ongoingEvaluation.returnVariable.applyType("default", "ParameterList", contractProcessor);
ongoingEvaluation.contractAddress = this._runtime._stepData.contractAddress;

//this._runtime.continue(false, "stopOnEvalBreakpoint");
if (newStartPc !== null && newEndPc !== null) {
const evalRequest = new LibSdbTypes.EvaluationRequest(evaluationBytecode.object, newStartPc, newEndPc, contract.runtimeBytecode, this._runtime._stepData.vmData.pc);
await this._runtime._interface.requestEvaluation(evalRequest);
const vmData = await this._runtime._interface.requestEvaluation(evalRequest);

ongoingEvaluation.returnVariable.position = vmData.stack.length - 1;

let returnValue;
if (ongoingEvaluation.returnVariable.detail === null) {
returnValue = null;
}
else {
returnValue = await ongoingEvaluation.returnVariable.detail.decode(vmData.stack, vmData.memory, this._runtime._interface, ongoingEvaluation.contractAddress);
}
callback(returnValue);
}
}
else {
Expand Down
32 changes: 24 additions & 8 deletions src/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export class LibSdbInterface {
}
}

public requestInjectCode(bytecode: string, pc: number): Promise<void> {
public requestInjectCode(bytecode: string, pc: number, vmData: any = undefined): Promise<void> {
return new Promise<void>((resolve, reject) => {
const msgId = uuidv4();

Expand All @@ -76,6 +76,14 @@ export class LibSdbInterface {
}
};

if (vmData !== undefined) {
request.content.state = {
"stack": vmData.stack,
"memory": vmData.memory,
"gasLeft": vmData.gasLeft
}
}

this._debuggerMessages.set(msgId, resolve);

if (this.evm !== undefined) {
Expand All @@ -84,7 +92,7 @@ export class LibSdbInterface {
});
}

public requestRuntUntilPc(pc: number): Promise<void> {
public requestRunUntilPc(pc: number): Promise<any> {
return new Promise<void>((resolve, reject) => {
const msgId = uuidv4();

Expand All @@ -93,10 +101,12 @@ export class LibSdbInterface {
"messageType": "request",
"content": {
"type": "runUntilPc",
"stepId": this._runtime._stepData!.debuggerMessageId,
"pc": pc
}
};

this._debuggerMessages.delete(this._runtime._stepData!.debuggerMessageId);
this._debuggerMessages.set(msgId, resolve);

if (this.evm !== undefined) {
Expand All @@ -105,16 +115,14 @@ export class LibSdbInterface {
});
}

public async requestEvaluation(evalRequest: LibSdbTypes.EvaluationRequest): Promise<LibSdbTypes.Variable> {
public async requestEvaluation(evalRequest: LibSdbTypes.EvaluationRequest): Promise<any> {
await this.requestInjectCode(evalRequest.evaluationBytecode, evalRequest.evaluationStartPc);

await this.requestRuntUntilPc(evalRequest.evaluationEndPc);
const vmData = await this.requestRunUntilPc(evalRequest.evaluationEndPc);

// TODO: get current state and get return variable
await this.requestInjectCode(evalRequest.runtimeBytecode, evalRequest.runtimePc, this._runtime._stepData!.vmData);

await this.requestInjectCode(evalRequest.runtimeBytecode, evalRequest.runtimePc);

return new LibSdbTypes.Variable();
return vmData;
}

public async requestStorage(address: any, position: any): Promise<any> {
Expand Down Expand Up @@ -384,6 +392,14 @@ export class LibSdbInterface {
debuggerMessage(data.content);
}
this._debuggerMessages.delete(data.id);

if (triggerType === "runUntilPc") {
// the step data id gets modified due to changes in sdbhook
this._runtime._stepData!.debuggerMessageId = data.id;
this._debuggerMessages.set(data.id, (message) => {
this.evm.handleMessage(message);
});
}
}
}

Expand Down
36 changes: 7 additions & 29 deletions src/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { LibSdbBreakpoints } from "./breakpoints";
import { LibSdbEvaluator } from "./evaluator";
import { ValueDetail } from "./types/barrel";
import { LibSdbConstants } from "./utils/constants";
import { BN } from "bn.js";

const CircularJSON = require("circular-json");

Expand All @@ -23,8 +24,6 @@ export class LibSdbRuntime extends EventEmitter {
public _callStack: LibSdbTypes.StackFrame[];
public _priorUiCallStack: LibSdbTypes.StackFrame[] | null;

public _ongoingEvaluation: LibSdbTypes.Evaluation | null;

public _files: LibSdbTypes.FileMap;
public _filesById: LibSdbTypes.FileByIdMap;
public _contractsByName: LibSdbTypes.ContractMap;
Expand Down Expand Up @@ -58,8 +57,6 @@ export class LibSdbRuntime extends EventEmitter {

this._callStack = [];
this._priorUiCallStack = [];

this._ongoingEvaluation = null;
}

public static instance(): LibSdbRuntime {
Expand Down Expand Up @@ -114,30 +111,6 @@ export class LibSdbRuntime extends EventEmitter {
private async processJumpOut(contract: LibSdbTypes.Contract, stack: any, memory: any): Promise<void> {
// jump out, we should be at a JUMPDEST currently

if (this._priorStepData) {
const node = LibSdbUtils.SourceMappingDecoder.findNodeAtSourceLocation("FunctionDefinition", this._priorStepData.source, { AST: contract.ast });
if (node !== null) {
const functionName = node.attributes.name;
if (this._ongoingEvaluation !== null && this._ongoingEvaluation.functionName === functionName) {
// get variable at top of stack
// TODO: add support for multiple variable evaluations

this._ongoingEvaluation.returnVariable.position = stack.length - 1;

let returnValue;
if (this._ongoingEvaluation.returnVariable.detail === null) {
returnValue = null;
}
else {
returnValue = await this._ongoingEvaluation.returnVariable.detail.decode(stack, memory, this._interface, this._ongoingEvaluation.contractAddress);
}
this._ongoingEvaluation.callback(returnValue);

this._ongoingEvaluation = null;
}
}
}

this._callStack.shift();
}

Expand Down Expand Up @@ -257,7 +230,12 @@ export class LibSdbRuntime extends EventEmitter {
this._stepData.source = sourceLocation;
this._stepData.location = currentLocation;
this._stepData.contractAddress = address;
this._stepData.vmData = data.content;
this._stepData.vmData = CircularJSON.parse(CircularJSON.stringify(data.content)); // make a deep copy TODO: make this better
this._stepData.vmData.gasLeft = new BN(data.content.gasLeft);
this._stepData.vmData.stack = [];
for (let i = 0; i < data.content.stack.length; i++) {
this._stepData.vmData.stack.push(new BN(data.content.stack[i]));
}
this._stepData.scope = currentScope;
this._stepData.events = data.content.specialEvents;
if (data.exceptionError !== undefined) {
Expand Down
3 changes: 0 additions & 3 deletions src/types/evaluation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ export class Evaluation {
functionName: string;
returnVariable: Variable;
contractAddress: string;
callback: Function;

constructor() {
this.returnVariable = new Variable();
Expand All @@ -17,8 +16,6 @@ export class Evaluation {

clone.contractAddress = this.contractAddress;

clone.callback = this.callback;

return clone;
}
}
Expand Down
8 changes: 7 additions & 1 deletion src/types/stepData.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { AstScope } from "./astScope";
import { BN } from "bn.js";

const CircularJSON = require("circular-json");

Expand Down Expand Up @@ -28,7 +29,12 @@ export class StepData {

clone.contractAddress = this.contractAddress;

clone.vmData = CircularJSON.parse(CircularJSON.stringify(this.vmData));
clone.vmData = CircularJSON.parse(CircularJSON.stringify(this.vmData)); // TODO: make this better
clone.vmData.gasLeft = new BN(this.vmData.gasLeft);
clone.vmData.stack = [];
for (let i = 0; i < this.vmData.stack.length; i++) {
clone.vmData.stack.push(new BN(this.vmData.stack[i]));
}

for (let i = 0; i < this.scope.length; i++) {
clone.scope.push(this.scope[i].clone());
Expand Down

0 comments on commit 3a56eff

Please sign in to comment.