2018-08-01 15:48:17 +00:00
|
|
|
const SourceMap = require('./source_map');
|
|
|
|
|
|
|
|
class ContractSource {
|
2018-08-21 20:23:00 +00:00
|
|
|
constructor(file, path, body) {
|
2018-08-10 14:40:58 +00:00
|
|
|
let self = this;
|
|
|
|
|
2018-08-01 15:48:17 +00:00
|
|
|
this.file = file;
|
2018-08-21 20:23:00 +00:00
|
|
|
this.path = path;
|
2018-08-01 15:48:17 +00:00
|
|
|
this.body = body;
|
|
|
|
|
|
|
|
this.lineLengths = body.split("\n").map((line) => { return line.length; });
|
|
|
|
this.lineCount = this.lineLengths.length;
|
|
|
|
|
2018-08-10 14:40:58 +00:00
|
|
|
this.lineOffsets = this.lineLengths.reduce((sum, _elt, i) => {
|
2018-10-10 20:34:04 +00:00
|
|
|
sum[i] = (i === 0) ? 0 : self.lineLengths[i-1] + sum[i-1] + 1;
|
2018-08-10 14:40:58 +00:00
|
|
|
return sum;
|
|
|
|
}, []);
|
2018-08-01 15:48:17 +00:00
|
|
|
|
|
|
|
this.contracts = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
sourceMapToLocations(sourceMap) {
|
2018-08-09 20:46:20 +00:00
|
|
|
var [offset, length, ..._] = sourceMap.split(":").map((val) => {
|
2018-08-01 15:48:17 +00:00
|
|
|
return parseInt(val, 10);
|
|
|
|
});
|
|
|
|
|
|
|
|
var locations = {};
|
|
|
|
|
2018-08-09 20:46:20 +00:00
|
|
|
for(let i = 0; i < this.lineCount; i++) {
|
|
|
|
if(this.lineOffsets[i+1] <= offset) continue;
|
2018-08-01 15:48:17 +00:00
|
|
|
|
|
|
|
locations.start = {line: i, column: offset - this.lineOffsets[i]};
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
for(var i = locations.start.line; i < this.lineCount; i++) {
|
2018-08-09 20:46:20 +00:00
|
|
|
if(this.lineOffsets[i+1] <= offset + length) continue;
|
2018-08-01 15:48:17 +00:00
|
|
|
|
|
|
|
locations.end = {line: i, column: ((offset + length) - this.lineOffsets[i])};
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure we return an "end" as a safeguard if the marker ends up to be
|
|
|
|
// or surpass the offset for last character.
|
|
|
|
if(!locations.end) {
|
|
|
|
var lastLine = this.lineCount - 1;
|
|
|
|
locations.end = {line: lastLine, column: this.lineLengths[lastLine]};
|
|
|
|
}
|
|
|
|
|
|
|
|
// Istanbul likes lines to be 1-indexed, so we'll increment here before returning.
|
|
|
|
locations.start.line++;
|
|
|
|
locations.end.line++;
|
|
|
|
return locations;
|
|
|
|
}
|
|
|
|
|
|
|
|
parseSolcOutput(source, contracts) {
|
|
|
|
this.id = source.id;
|
|
|
|
this.ast = source.ast;
|
|
|
|
this.contractBytecode = {};
|
|
|
|
|
|
|
|
for(var contractName in contracts) {
|
|
|
|
this.contractBytecode[contractName] = {};
|
|
|
|
|
|
|
|
var contract = contracts[contractName];
|
|
|
|
var bytecodeMapping = this.contractBytecode[contractName];
|
|
|
|
var opcodes = contract.evm.deployedBytecode.opcodes.trim().split(' ');
|
|
|
|
var sourceMaps = contract.evm.deployedBytecode.sourceMap.split(';');
|
|
|
|
|
|
|
|
var bytecodeIdx = 0;
|
|
|
|
var pc = 0;
|
|
|
|
var instructions = 0;
|
2018-08-20 17:54:44 +00:00
|
|
|
var previousSourceMap = null;
|
2018-08-21 20:23:00 +00:00
|
|
|
|
2018-08-01 15:48:17 +00:00
|
|
|
do {
|
2018-08-20 17:54:44 +00:00
|
|
|
let sourceMap;
|
|
|
|
|
|
|
|
if(previousSourceMap === null) {
|
|
|
|
sourceMap = new SourceMap(sourceMaps[instructions]);
|
|
|
|
} else {
|
|
|
|
sourceMap = previousSourceMap.createRelativeTo(sourceMaps[instructions]);
|
|
|
|
}
|
|
|
|
|
2018-08-01 15:48:17 +00:00
|
|
|
var instruction = opcodes[bytecodeIdx];
|
|
|
|
var length = this._instructionLength(instruction);
|
|
|
|
bytecodeMapping[pc] = {
|
|
|
|
instruction: instruction,
|
2018-08-20 17:54:44 +00:00
|
|
|
sourceMap: sourceMap,
|
|
|
|
jump: sourceMap.jump,
|
2018-08-01 15:48:17 +00:00
|
|
|
seen: false
|
2018-08-09 20:46:20 +00:00
|
|
|
};
|
2018-08-01 15:48:17 +00:00
|
|
|
|
|
|
|
pc += length;
|
|
|
|
instructions++;
|
|
|
|
bytecodeIdx += (length > 1) ? 2 : 1;
|
2018-08-20 17:54:44 +00:00
|
|
|
previousSourceMap = sourceMap;
|
2018-08-01 15:48:17 +00:00
|
|
|
} while(bytecodeIdx < opcodes.length);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-23 23:07:57 +00:00
|
|
|
isInterface() {
|
|
|
|
return this.contractBytecode !== undefined &&
|
|
|
|
Object.values(this.contractBytecode).every((contractBytecode) => { return (Object.values(contractBytecode).length <= 1); });
|
|
|
|
}
|
|
|
|
|
2018-10-29 14:33:12 +00:00
|
|
|
/*eslint complexity: ["error", 44]*/
|
2018-08-01 15:48:17 +00:00
|
|
|
generateCodeCoverage(trace) {
|
2018-08-09 20:46:20 +00:00
|
|
|
if(!this.ast || !this.contractBytecode) throw new Error('Error generating coverage: solc output was not assigned');
|
2018-08-01 15:48:17 +00:00
|
|
|
|
2018-08-21 19:25:18 +00:00
|
|
|
let coverage = {
|
2018-08-20 17:54:44 +00:00
|
|
|
code: this.body.trim().split("\n"),
|
2018-08-01 15:48:17 +00:00
|
|
|
l: {},
|
2018-08-21 20:23:00 +00:00
|
|
|
path: this.path,
|
2018-08-01 15:48:17 +00:00
|
|
|
s: {},
|
|
|
|
b: {},
|
|
|
|
f: {},
|
|
|
|
fnMap: {},
|
|
|
|
statementMap: {},
|
2018-08-09 20:46:20 +00:00
|
|
|
branchMap: {}
|
2018-08-01 15:48:17 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
var nodesRequiringVisiting = [this.ast];
|
|
|
|
var sourceMapToNodeType = {};
|
|
|
|
|
|
|
|
do {
|
2018-08-21 19:25:18 +00:00
|
|
|
let node = nodesRequiringVisiting.pop();
|
2018-08-09 20:46:20 +00:00
|
|
|
if(!node) continue;
|
2018-08-01 15:48:17 +00:00
|
|
|
|
2018-08-21 19:25:18 +00:00
|
|
|
let children = [];
|
|
|
|
let markLocations = [];
|
|
|
|
let location;
|
2018-08-01 15:48:17 +00:00
|
|
|
|
|
|
|
switch(node.nodeType) {
|
2018-08-20 17:54:44 +00:00
|
|
|
case 'Assignment':
|
2018-08-21 20:23:00 +00:00
|
|
|
case 'EventDefinition':
|
2018-08-30 17:46:26 +00:00
|
|
|
case 'ImportDirective':
|
2018-08-01 15:48:17 +00:00
|
|
|
case 'Literal':
|
2018-08-30 16:49:19 +00:00
|
|
|
case 'PlaceholderStatement':
|
2018-08-10 14:58:18 +00:00
|
|
|
case 'PragmaDirective':
|
2018-08-30 17:46:26 +00:00
|
|
|
case 'StructDefinition':
|
2018-08-20 17:54:44 +00:00
|
|
|
case 'VariableDeclaration':
|
2018-08-01 15:48:17 +00:00
|
|
|
// We don't need to do anything with these. Just carry on.
|
|
|
|
break;
|
|
|
|
|
2018-08-28 17:51:55 +00:00
|
|
|
case 'IfStatement': {
|
2018-08-21 19:25:18 +00:00
|
|
|
location = this.sourceMapToLocations(node.src);
|
|
|
|
let trueBranchLocation = this.sourceMapToLocations(node.trueBody.src);
|
2018-08-01 15:48:17 +00:00
|
|
|
|
2018-08-21 19:25:18 +00:00
|
|
|
let declarationSourceMap = new SourceMap(node.src).subtract(new SourceMap(node.trueBody.src));
|
|
|
|
let declarationLocation = this.sourceMapToLocations(declarationSourceMap.toString());
|
2018-08-08 18:26:40 +00:00
|
|
|
|
|
|
|
var falseBranchLocation;
|
|
|
|
if(node.falseBody) {
|
|
|
|
falseBranchLocation = this.sourceMapToLocations(node.falseBody.src);
|
|
|
|
} else {
|
|
|
|
falseBranchLocation = trueBranchLocation;
|
|
|
|
}
|
2018-08-01 15:48:17 +00:00
|
|
|
|
2018-08-21 19:25:18 +00:00
|
|
|
coverage.b[node.id] = [0,0];
|
2018-08-01 15:48:17 +00:00
|
|
|
coverage.branchMap[node.id] = {
|
|
|
|
type: 'if',
|
|
|
|
locations: [trueBranchLocation, falseBranchLocation],
|
2018-08-08 18:26:40 +00:00
|
|
|
line: location.start.line
|
|
|
|
};
|
2018-08-01 15:48:17 +00:00
|
|
|
|
2018-08-21 19:25:18 +00:00
|
|
|
markLocations = [declarationLocation];
|
2018-09-26 15:13:14 +00:00
|
|
|
children = [node.condition];
|
2018-08-01 15:48:17 +00:00
|
|
|
|
2018-09-26 15:13:14 +00:00
|
|
|
let trueExpression = (node.trueBody && node.trueBody.statements && node.trueBody.statements[0]) || node.trueBody;
|
|
|
|
if(trueExpression) {
|
2018-09-26 15:47:02 +00:00
|
|
|
children = children.concat(trueExpression);
|
2018-09-26 15:13:14 +00:00
|
|
|
trueExpression._parent = {type: 'b', id: node.id, idx: 0};
|
2018-08-09 20:46:20 +00:00
|
|
|
}
|
2018-08-01 15:48:17 +00:00
|
|
|
|
2018-09-26 15:13:14 +00:00
|
|
|
let falseExpression = (node.falseBody && node.falseBody.statements && node.falseBody.statements[0]) || node.falseBody;
|
|
|
|
if(falseExpression) {
|
2018-09-26 15:47:02 +00:00
|
|
|
children = children.concat(falseExpression);
|
2018-09-26 15:13:14 +00:00
|
|
|
falseExpression._parent = {type: 'b', id: node.id, idx: 1};
|
2018-08-09 20:46:20 +00:00
|
|
|
}
|
2018-08-01 15:48:17 +00:00
|
|
|
|
2018-08-09 20:46:20 +00:00
|
|
|
sourceMapToNodeType[node.src] = [{type: 'b', id: node.id, body: {loc: location}}];
|
2018-08-01 15:48:17 +00:00
|
|
|
break;
|
2018-08-28 17:51:55 +00:00
|
|
|
}
|
2018-08-01 15:48:17 +00:00
|
|
|
|
2018-08-28 17:51:55 +00:00
|
|
|
case 'EmitStatement': {
|
2018-08-21 20:23:00 +00:00
|
|
|
children = [node.eventCall];
|
|
|
|
break;
|
2018-08-28 17:51:55 +00:00
|
|
|
}
|
2018-08-21 20:23:00 +00:00
|
|
|
|
2018-08-01 15:48:17 +00:00
|
|
|
case 'BinaryOperation':
|
|
|
|
case 'ExpressionStatement':
|
2018-08-21 20:23:00 +00:00
|
|
|
case 'FunctionCall':
|
2018-08-20 17:54:44 +00:00
|
|
|
case 'Identifier':
|
2018-08-21 19:25:18 +00:00
|
|
|
case 'Return':
|
2018-08-20 17:54:44 +00:00
|
|
|
case 'UnaryOperation':
|
2018-08-01 15:48:17 +00:00
|
|
|
coverage.s[node.id] = 0;
|
|
|
|
|
2018-08-09 20:46:20 +00:00
|
|
|
location = this.sourceMapToLocations(node.src);
|
2018-08-01 15:48:17 +00:00
|
|
|
coverage.statementMap[node.id] = location;
|
|
|
|
|
|
|
|
if(!sourceMapToNodeType[node.src]) sourceMapToNodeType[node.src] = [];
|
|
|
|
sourceMapToNodeType[node.src].push({
|
|
|
|
type: 's',
|
|
|
|
id: node.id,
|
|
|
|
body: {loc: coverage.statementMap[node.id]},
|
2018-08-09 20:46:20 +00:00
|
|
|
parent: node._parent
|
2018-08-01 15:48:17 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
markLocations = [location];
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'ContractDefinition':
|
|
|
|
case 'SourceUnit':
|
|
|
|
children = node.nodes;
|
|
|
|
break;
|
|
|
|
|
2018-08-30 16:49:19 +00:00
|
|
|
case 'ModifierDefinition':
|
2018-08-01 15:48:17 +00:00
|
|
|
case 'FunctionDefinition':
|
|
|
|
// Istanbul only wants the function definition, not the body, so we're
|
|
|
|
// going to do some fun math here.
|
|
|
|
var functionSourceMap = new SourceMap(node.src);
|
|
|
|
var functionParametersSourceMap = new SourceMap(node.parameters.src);
|
|
|
|
|
|
|
|
var functionDefinitionSourceMap = new SourceMap(
|
|
|
|
functionSourceMap.offset,
|
|
|
|
(functionParametersSourceMap.offset + functionParametersSourceMap.length) - functionSourceMap.offset
|
|
|
|
).toString();
|
|
|
|
|
|
|
|
var fnName = node.isConstructor ? "(constructor)" : node.name;
|
2018-08-09 20:46:20 +00:00
|
|
|
location = this.sourceMapToLocations(functionDefinitionSourceMap);
|
2018-08-01 15:48:17 +00:00
|
|
|
|
|
|
|
coverage.f[node.id] = 0;
|
|
|
|
coverage.fnMap[node.id] = {
|
|
|
|
name: fnName,
|
|
|
|
line: location.start.line,
|
2018-08-09 20:46:20 +00:00
|
|
|
loc: location
|
2018-08-01 15:48:17 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Record function positions.
|
2018-08-09 20:46:20 +00:00
|
|
|
sourceMapToNodeType[node.src] = [{type: 'f', id: node.id, body: coverage.fnMap[node.id]}];
|
2018-08-01 15:48:17 +00:00
|
|
|
|
2018-08-08 18:26:40 +00:00
|
|
|
if(node.body) children = node.body.statements;
|
2018-08-01 15:48:17 +00:00
|
|
|
markLocations = [location];
|
|
|
|
break;
|
|
|
|
|
2018-08-28 17:51:55 +00:00
|
|
|
case 'ForStatement': {
|
2018-08-20 17:54:44 +00:00
|
|
|
// For statements will be a bit of a special case. We want to count the body
|
|
|
|
// iterations but we only want to count the for loop being hit once. Because
|
|
|
|
// of this, we cover the initialization on the node.
|
|
|
|
let sourceMap = new SourceMap(node.src);
|
|
|
|
let bodySourceMap = new SourceMap(node.body.src);
|
|
|
|
let forLoopDeclaration = sourceMap.subtract(bodySourceMap).toString();
|
|
|
|
|
|
|
|
location = this.sourceMapToLocations(forLoopDeclaration);
|
|
|
|
|
2018-10-02 16:50:07 +00:00
|
|
|
let markExpression = node.initializationExpression || node.loopExpression;
|
|
|
|
let expressionLocation = this.sourceMapToLocations(markExpression.src);
|
|
|
|
|
|
|
|
if(!sourceMapToNodeType[markExpression.src]) sourceMapToNodeType[markExpression.src] = [];
|
|
|
|
sourceMapToNodeType[markExpression.src].push({type: 's', id: node.id, body: {loc: location}});
|
|
|
|
markLocations = [expressionLocation];
|
|
|
|
|
2018-08-20 17:54:44 +00:00
|
|
|
coverage.s[node.id] = 0;
|
|
|
|
coverage.statementMap[node.id] = location;
|
|
|
|
|
|
|
|
children = node.body.statements;
|
|
|
|
break;
|
2018-08-28 17:51:55 +00:00
|
|
|
}
|
2018-08-20 17:54:44 +00:00
|
|
|
|
2018-08-28 17:51:55 +00:00
|
|
|
case 'VariableDeclarationStatement': {
|
2018-08-20 17:54:44 +00:00
|
|
|
location = this.sourceMapToLocations(node.src);
|
|
|
|
|
|
|
|
coverage.s[node.id] = 0;
|
|
|
|
coverage.statementMap[node.id] = location;
|
|
|
|
markLocations = [location];
|
|
|
|
|
|
|
|
if(!sourceMapToNodeType[node.src]) sourceMapToNodeType[node.src] = [];
|
|
|
|
sourceMapToNodeType[node.src].push({type: 's', id: node.id, body: {loc: location}, foo: 'bar'});
|
|
|
|
|
|
|
|
break;
|
2018-08-28 17:51:55 +00:00
|
|
|
}
|
2018-08-20 17:54:44 +00:00
|
|
|
|
2018-08-01 15:48:17 +00:00
|
|
|
default:
|
2018-09-26 22:36:56 +00:00
|
|
|
//console.log(`Don't know how to handle node type ${node.nodeType}`);
|
2018-08-01 15:48:17 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
nodesRequiringVisiting = nodesRequiringVisiting.concat(children);
|
|
|
|
|
|
|
|
markLocations.forEach((location) => {
|
|
|
|
for(var i = location.start.line; i <= location.end.line; i++) {
|
|
|
|
coverage.l[i] = 0;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
} while(nodesRequiringVisiting.length > 0);
|
|
|
|
|
2018-08-08 18:26:40 +00:00
|
|
|
var contractMatches = true;
|
2018-08-01 15:48:17 +00:00
|
|
|
for(var contractName in this.contractBytecode) {
|
|
|
|
var bytecode = this.contractBytecode[contractName];
|
2018-08-08 18:26:40 +00:00
|
|
|
|
|
|
|
// Try to match the contract to the bytecode. If it doesn't,
|
|
|
|
// then we bail.
|
2018-08-10 14:58:18 +00:00
|
|
|
contractMatches = trace.structLogs.every((step) => { return bytecode[step.pc]; });
|
2018-09-23 22:40:51 +00:00
|
|
|
if(!contractMatches) continue;
|
2018-08-08 18:26:40 +00:00
|
|
|
|
2018-08-01 15:48:17 +00:00
|
|
|
trace.structLogs.forEach((step) => {
|
2018-08-09 20:46:20 +00:00
|
|
|
step = bytecode[step.pc];
|
2018-08-30 17:01:13 +00:00
|
|
|
if(!step.sourceMap || step.sourceMap === '' || step.sourceMap === SourceMap.empty()) return;
|
2018-08-20 17:54:44 +00:00
|
|
|
let sourceMapString = step.sourceMap.toString(this.id);
|
|
|
|
var nodes = sourceMapToNodeType[sourceMapString];
|
2018-08-01 15:48:17 +00:00
|
|
|
|
|
|
|
if(!nodes) return;
|
|
|
|
|
|
|
|
nodes.forEach((node) => {
|
2018-08-20 17:54:44 +00:00
|
|
|
// Skip duplicate function reports by only reporting when there is a jump.
|
2018-10-29 14:33:12 +00:00
|
|
|
if(node.type === 'f' && step.jump) return;
|
2018-08-20 17:54:44 +00:00
|
|
|
|
2018-10-29 14:33:12 +00:00
|
|
|
if(node.type !== 'b' && node.body && node.body.loc) {
|
2018-08-01 15:48:17 +00:00
|
|
|
for(var line = node.body.loc.start.line; line <= node.body.loc.end.line; line++) {
|
|
|
|
coverage.l[line]++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-06 13:57:16 +00:00
|
|
|
if(node.type !== 'b') coverage[node.type][node.id]++;
|
2018-08-01 15:48:17 +00:00
|
|
|
|
2018-08-09 20:46:20 +00:00
|
|
|
if(!node.parent) return;
|
2018-08-01 15:48:17 +00:00
|
|
|
|
|
|
|
switch(node.parent.type) {
|
|
|
|
case 'b':
|
|
|
|
coverage.b[node.parent.id][node.parent.idx]++;
|
|
|
|
break;
|
2018-08-09 20:46:20 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
// do nothing
|
2018-08-01 15:48:17 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return coverage;
|
|
|
|
}
|
|
|
|
|
|
|
|
_instructionLength(instruction) {
|
2018-09-06 13:57:16 +00:00
|
|
|
if(instruction.indexOf('PUSH') === -1) return 1;
|
2018-08-01 15:48:17 +00:00
|
|
|
return parseInt(instruction.match(/PUSH(\d+)/m)[1], 10) + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = ContractSource;
|