Skip to content

Add coverage for ternary conditionals #587

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

Merged
merged 1 commit into from
Dec 8, 2020
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
3 changes: 2 additions & 1 deletion lib/coverage.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ class Coverage {
case 'function': this.data[contractPath].f[id] = hits; break;
case 'statement': this.data[contractPath].s[id] = hits; break;
case 'branch': this.data[contractPath].b[id][data.locationIdx] = hits; break;
case 'logicalOR': this.data[contractPath].b[id][data.locationIdx] = hits / 2; break;
case 'and-true': this.data[contractPath].b[id][data.locationIdx] = hits / 2; break;
case 'or-false': this.data[contractPath].b[id][data.locationIdx] = hits / 2; break;
case 'requirePre': this.requireData[contractPath][id].preEvents = hits; break;
case 'requirePost': this.requireData[contractPath][id].postEvents = hits; break;
}
Expand Down
52 changes: 47 additions & 5 deletions lib/injector.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ class Injector {

_getInjectable(id, hash, type){
switch(type){
case 'logicalOR':
case 'and-true':
return ` && ${this._getTrueMethodIdentifier(id)}(${hash}))`;
case 'or-false':
return ` || ${this._getFalseMethodIdentifier(id)}(${hash}))`;
default:
return `${this._getDefaultMethodIdentifier(id)}(${hash}); /* ${type} */ \n`;
}
Expand All @@ -31,11 +33,16 @@ class Injector {
return `c_${web3Utils.keccak256(id).slice(0,10)}`
}

// Method returns boolean true
// Method returns boolean: true
_getTrueMethodIdentifier(id){
return `c_true${web3Utils.keccak256(id).slice(0,10)}`
}

// Method returns boolean: false
_getFalseMethodIdentifier(id){
return `c_false${web3Utils.keccak256(id).slice(0,10)}`
}

_getInjectionComponents(contract, injectionPoint, id, type){
const { start, end } = this._split(contract, injectionPoint);
const hash = this._getHash(id)
Expand Down Expand Up @@ -70,7 +77,19 @@ class Injector {
_getTrueMethodDefinition(id){
const hash = web3Utils.keccak256(id).slice(0,10);
const method = this._getTrueMethodIdentifier(id);
return `\nfunction ${method}(bytes32 c__${hash}) public pure returns (bool){ return true; }\n`;
return `function ${method}(bytes32 c__${hash}) public pure returns (bool){ return true; }\n`;
}

/**
* Generates a solidity statement injection defining a method
* *which returns boolean false* to pass instrumentation hash to.
* @param {String} fileName
* @return {String} ex: bytes32[1] memory _sc_82e0891
*/
_getFalseMethodDefinition(id){
const hash = web3Utils.keccak256(id).slice(0,10);
const method = this._getFalseMethodIdentifier(id);
return `function ${method}(bytes32 c__${hash}) public pure returns (bool){ return false; }\n`;
}

injectLine(contract, fileName, injectionPoint, injection, instrumentation){
Expand Down Expand Up @@ -230,6 +249,7 @@ class Injector {
contract.instrumented = `${start}` +
`${this._getDefaultMethodDefinition(id)}` +
`${this._getTrueMethodDefinition(id)}` +
`${this._getFalseMethodDefinition(id)}` +
`${end}`;
}

Expand All @@ -239,8 +259,30 @@ class Injector {
contract.instrumented = `${start}(${end}`;
}

injectLogicalOR(contract, fileName, injectionPoint, injection, instrumentation){
const type = 'logicalOR';
injectAndTrue(contract, fileName, injectionPoint, injection, instrumentation){
const type = 'and-true';
const id = `${fileName}:${injection.contractName}`;

const {
start,
end,
hash,
injectable
} = this._getInjectionComponents(contract, injectionPoint, id, type);

instrumentation[hash] = {
id: injection.branchId,
locationIdx: injection.locationIdx,
type: type,
contractPath: fileName,
hits: 0
}

contract.instrumented = `${start}${injectable}${end}`;
}

injectOrFalse(contract, fileName, injectionPoint, injection, instrumentation){
const type = 'or-false';
const id = `${fileName}:${injection.contractName}`;

const {
Expand Down
23 changes: 17 additions & 6 deletions lib/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,21 @@ parse.Block = function(contract, expression) {
};

parse.BinaryOperation = function(contract, expression, skipStatementRegistry) {
// Free-floating ternary conditional
if (expression.left && expression.left.type === 'Conditional'){
parse[expression.left.type](contract, expression.left, true);
register.statement(contract, expression);

// Ternary conditional assignment
} else if (expression.right && expression.right.type === 'Conditional'){
parse[expression.right.type](contract, expression.right, true);
register.statement(contract, expression);

// Regular binary operation
if (!skipStatementRegistry){
} else if(!skipStatementRegistry){
register.statement(contract, expression);

// LogicalOR conditional search...
// LogicalOR condition search...
} else {
parse[expression.left.type] &&
parse[expression.left.type](contract, expression.left, true);
Expand Down Expand Up @@ -81,12 +91,10 @@ parse.FunctionCall = function(contract, expression, skipStatementRegistry) {
};

parse.Conditional = function(contract, expression, skipStatementRegistry) {
if (!skipStatementRegistry){
register.statement(contract, expression);
}

parse[expression.condition.type] &&
parse[expression.condition.type](contract, expression.condition, skipStatementRegistry);

register.conditional(contract, expression);
};

parse.ContractDefinition = function(contract, expression) {
Expand Down Expand Up @@ -239,6 +247,9 @@ parse.UsingStatement = function (contract, expression) {
};

parse.VariableDeclarationStatement = function (contract, expression) {
if (expression.initialValue && expression.initialValue.type === 'Conditional'){
parse[expression.initialValue.type](contract, expression.initialValue, true)
}
register.statement(contract, expression);
};

Expand Down
92 changes: 90 additions & 2 deletions lib/registrar.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,54 @@ class Registrar {
};
};

addNewConditionalBranch(contract, expression){
let start;
// Instabul HTML highlighting location data...
const trueZero = expression.trueExpression.range[0];
const trueOne = expression.trueExpression.range[1];
const falseZero = expression.falseExpression.range[0];
const falseOne = expression.falseExpression.range[1];

start = contract.instrumented.slice(0, trueZero);
const trueStartLine = ( start.match(/\n/g) || [] ).length + 1;
const trueStartCol = trueZero - start.lastIndexOf('\n') - 1;

start = contract.instrumented.slice(0, trueOne);
const trueEndLine = ( start.match(/\n/g) || [] ).length + 1;
const trueEndCol = trueOne - start.lastIndexOf('\n') - 1;

start = contract.instrumented.slice(0, falseZero);
const falseStartLine = ( start.match(/\n/g) || [] ).length + 1;
const falseStartCol = falseZero - start.lastIndexOf('\n') - 1;

start = contract.instrumented.slice(0, falseOne);
const falseEndLine = ( start.match(/\n/g) || [] ).length + 1;
const falseEndCol = falseOne - start.lastIndexOf('\n') - 1;

contract.branchId += 1;

contract.branchMap[contract.branchId] = {
line: trueStartLine,
type: 'if',
locations: [{
start: {
line: trueStartLine, column: trueStartCol,
},
end: {
line: trueEndLine, column: trueEndCol,
},
}, {
start: {
line: falseStartLine, column: falseStartCol,
},
end: {
line: falseEndLine, column: falseEndCol,
},
}],
};

}

/**
* Registers injections for branch measurements. Used by logicalOR registration method.
* @param {Object} contract instrumentation target
Expand Down Expand Up @@ -241,6 +289,46 @@ class Registrar {
};
};

conditional(contract, expression){
this.addNewConditionalBranch(contract, expression);

// Double open parens
this._createInjectionPoint(
contract,
expression.condition.range[0],
{
type: 'injectOpenParen',
}
);
this._createInjectionPoint(
contract,
expression.condition.range[0],
{
type: 'injectOpenParen',
}
);
// False condition: (these get sorted in reverse order, so create in reversed order)
this._createInjectionPoint(
contract,
expression.condition.range[1] + 1,
{
type: 'injectOrFalse',
branchId: contract.branchId,
locationIdx: 1
}
);
// True condition
this._createInjectionPoint(
contract,
expression.condition.range[1] + 1,
{
type: 'injectAndTrue',
branchId: contract.branchId,
locationIdx: 0
}
);
}

/**
* Registers injections for logicalOR clause measurements (branches)
* @param {Object} contract instrumentation target
Expand All @@ -262,7 +350,7 @@ class Registrar {
contract,
expression.left.range[1] + 1,
{
type: 'injectLogicalOR',
type: 'injectAndTrue',
branchId: contract.branchId,
locationIdx: 0
}
Expand All @@ -280,7 +368,7 @@ class Registrar {
contract,
expression.right.range[1] + 1,
{
type: 'injectLogicalOR',
type: 'injectAndTrue',
branchId: contract.branchId,
locationIdx: 1
}
Expand Down
Loading