flesh it out further and add a console command and fix annoying bugs

Signed-off-by: VoR0220 <catalanor0220@gmail.com>
This commit is contained in:
VoR0220 2018-06-04 13:25:42 -05:00
parent 7812951164
commit a0d0d9692b
2 changed files with 98 additions and 44 deletions

View File

@ -5,53 +5,107 @@ const _ = require('underscore');
// generates random inputs based on the inputs of an ABI // generates random inputs based on the inputs of an ABI
class ContractFuzzer { class ContractFuzzer {
constructor(abi) { constructor(embark) {
this.abi = abi; //this.abi = abi;
} this.embark = embark;
this.logger = embark.logger;
this.events = embark.events;
generateFuzz() { this.registerConsoleCommand();
this.abi.forEach((abiMethod) => { }
let inputTypes = abiMethod.inputs.map(input => input.type);
let fuzzedInputType = _.reduce(inputTypes, decipherType, 0);
})
}
getTypeFuzz(type) { generateFuzz(iterations, contract) {
switch() { let fuzz = [];
case 'uintN' || 'intN': for (let i = 0; i < iterations; i++)
contract.abiDefinition.forEach((abiMethod) => {
case 'bytesN': let inputTypes = abiMethod.inputs.map(input => input.type);
return generateRandomStaticBytes(size); console.log("INPUT TYPES:", inputTypes);
case 'string' || 'bytes': let fuzzedInputs = _.map(inputTypes, this.getTypeFuzz.bind(this));
return generateRandomDynamicType() console.log("FUZZED INPUTS:", fuzzedInputs);
case 'address': //fuzz.push(ethAbi.encodeFunctionCall(abiMethod, fuzzedInputs));
return generateRandomAddress(); console.log("FUZZ SO FAR:", fuzz);
default: });
} console.log("FUZZ:", fuzz);
} return fuzz;
}
generateArrayOfType(length, type) { getTypeFuzz(typeString) {
var arr = []; const self = this;
for (var i = 0; i < length; i++) { console.log("TYPE:", typeString);
arr.push(getTypeFuzz(type)); // Group 0: uint256[3]
} // Group 1: uint256
return arr; // Group 2: uint
} // Group 3: 256
// Group 4: [3]
// Group 5: 3
let regexObj = typeString.match(/((bool|int|uint|bytes|string|address)([0-9]*)?)(\[([0-9]*)\])*$/);
console.log("ARRAY OBJ:", regexObj);
let type = regexObj[1];
let size = regexObj[3];
switch(true) {
case (regexObj[2] !== undefined):
let length = regexObj[5] === undefined ? self.generateRandomInt(256) : regexObj[5];
return self.generateArrayOfType(length, type)
case (/bool/).test(type):
return self.generateRandomBool();
case (/(int|uint)([0-9]*)?/).test(type):
return self.generateRandomInt(size);
case (/^bytes([0-9]{1,})/).test(type):
return self.generateRandomStaticBytes(size);
case (/(string|bytes)/).test(type):
return self.generateRandomDynamicType()
case (/address/).test(type):
return self.generateRandomAddress();
default:
throw new Error("Couldn't find proper ethereum abi type");
}
}
generateRandomDynamicType() { generateRandomBool() {
return Math.random().toString(36).slice(2); return _.sample([true, false]);
} }
generateRandomStaticBytes(size) { generateArrayOfType(length, type) {
return utils.randomHex(size); var arr = [];
} for (var i = 0; i < length; i++) {
arr.push(this.getTypeFuzz(type));
}
return arr;
}
generateRandomInt(size) { generateRandomDynamicType() {
return utils.hexToNumber(utils.randomHex(size)); return Math.random().toString(36).slice(2);
} }
generateRandomAddress() { generateRandomStaticBytes(size) {
return utils.randomHex(20); return utils.randomHex(size);
} }
} generateRandomInt(size) {
return utils.hexToNumber(utils.randomHex(size / 8));
}
generateRandomAddress() {
return utils.randomHex(20);
}
registerConsoleCommand() {
const self = this;
self.embark.registerConsoleCommand((cmd, _options) => {
let splitCmd = cmd.split(' ');
let cmdName = splitCmd[0];
let contractName = splitCmd[1];
let iterations = splitCmd[2] === undefined ? 1 : splitCmd[2];
if (cmdName === 'fuzz') {
self.events.request('contracts:contract', contractName, (contract) => {
self.logger.info("-- fuzzed vals for " + contractName);
this.generateFuzz(1, contract);
});
return "";
}
return false;
});
}
}
module.exports = ContractFuzzer;

View File

@ -37,7 +37,7 @@ class Profiler {
return paramString; return paramString;
} }
generateGasEstimation(method) { /*generateGasEstimation(method) {
} }
@ -46,7 +46,7 @@ class Profiler {
case "uint256": case "uint256":
case "uint" case "uint"
} }
} }*/
registerConsoleCommand() { registerConsoleCommand() {
const self = this; const self = this;