eth/tracers: fix up 4byte and evmdis tracer

This commit is contained in:
Péter Szilágyi 2017-12-20 13:30:47 +02:00
parent 42d03fc687
commit e7a1b2ea66
9 changed files with 257 additions and 144 deletions

View file

@ -1,69 +1,85 @@
/**
// Copyright 2017 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
4bytes searches for 4byte-identifiers, and collects them for post-processing.
It collects the methods identifiers along with the size of the supplied data, so that
a reversed signature can be matched against the size of the data.
Example:
> debug.traceTransaction( "0x214e597e35da083692f5386141e69f47e973b2c56e7a8073b1ea08fd7571e9de", {tracer: "4bytes"})
{
0x27dc297e-128: 1,
0x38cc4831-0: 2,
0x524f3889-96: 1,
0xadf59f99-288: 1,
0xc281d19e-0: 1
}
@author MH Swende
**/
// 4byteTracer searches for 4byte-identifiers, and collects them for post-processing.
// It collects the methods identifiers along with the size of the supplied data, so
// a reversed signature can be matched against the size of the data.
//
// Example:
// > debug.traceTransaction( "0x214e597e35da083692f5386141e69f47e973b2c56e7a8073b1ea08fd7571e9de", {tracer: "4byteTracer"})
// {
// 0x27dc297e-128: 1,
// 0x38cc4831-0: 2,
// 0x524f3889-96: 1,
// 0xadf59f99-288: 1,
// 0xc281d19e-0: 1
// }
{
// ids aggregates the 4byte ids found.
ids : {},
// callType returns 'false' for non-calls, or the
// peek-index for the first param after 'value', i.e. meminstart
callType : function(opstr){
switch(opstr){
case "CALL":
case "CALLCODE":
//g,a,v, memin, meminsz, memout, memoutsz
return 3 //stack ptr to memin
case "DELEGATECALL":
case "STATICCALL":
//g,a,memin, meminsz, memout, memoutsz
return 2;//stack ptr to memin
}
return false;
},
// callType returns 'false' for non-calls, or the peek-index for the first param
// after 'value', i.e. meminstart.
callType: function(opstr){
switch(opstr){
case "CALL": case "CALLCODE":
// gas, addr, val, memin, meminsz, memout, memoutsz
return 3; // stack ptr to memin
// store save the given indentifier and datasize
case "DELEGATECALL": case "STATICCALL":
// gas, addr, memin, meminsz, memout, memoutsz
return 2; // stack ptr to memin
}
return false;
},
// store save the given indentifier and datasize.
store: function(id, size){
var key = ""+id+"-"+size;
this.ids[key] = this.ids[key]+1 || 1;
var key = "" + toHex(id) + "-" + size;
this.ids[key] = this.ids[key] + 1 || 1;
},
// step is invoked for every opcode that the VM executes.
step: function(log, db) {
var ct = this.callType(log.op.toString());
if (!ct) return;
// Skip any pre-compile invocations, those are just fancy opcodes
if (isPrecompiled(toAddress(log.stack.peek(1).Bytes()))) {
return
}
// Gather internal call details
var inSz = log.stack.peek(ct + 1).Int64();
if (inSz >= 4) {
var inOff = log.stack.peek(ct).Int64();
this.store(toHex(log.memory.slice(inOff, inOff + 4)), inSz-4);
}
// Skip any opcodes that are not internal calls
var ct = this.callType(log.op.toString());
if (!ct) {
return;
}
// Skip any pre-compile invocations, those are just fancy opcodes
if (isPrecompiled(toAddress(log.stack.peek(1)))) {
return;
}
// Gather internal call details
var inSz = log.stack.peek(ct + 1).valueOf();
if (inSz >= 4) {
var inOff = log.stack.peek(ct).valueOf();
this.store(log.memory.slice(inOff, inOff + 4), inSz-4);
}
},
// fault is invoked when the actual execution of an opcode fails.
fault: function(log, db) { },
// result is invoked when all the opcodes have been iterated over and returns
// the final result of the tracing.
result: function(ctx) {
//Save the outer calldata also
if(ctx.input.length > 4){
this.store(toHex(ctx.input.slice(0,4)),ctx.input.length-4 )
// Save the outer calldata also
if (ctx.input.length > 4) {
this.store(ctx.input.slice(0, 4), ctx.input.length-4)
}
return this.ids;
},

File diff suppressed because one or more lines are too long

View file

@ -1,3 +1,19 @@
// Copyright 2017 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
// callTracer is a full blown transaction tracer that extracts and reports all
// the internal calls made by a transaction, along with any useful information.
{

View file

@ -1,74 +0,0 @@
/**
This tracer returns sufficent information from a trace to
perform evmdis-style disassembly
@author Nick Johnson, Martin Swende
**/
{
"stack": [{"ops": []}],
"npushes": {0: 0, 1: 1, 2: 1, 3: 1, 4: 1, 5: 1, 6: 1, 7: 1, 8: 1, 9: 1, 10: 1, 11: 1, 16: 1, 17: 1, 18: 1, 19: 1, 20: 1, 21: 1, 22: 1, 23: 1, 24: 1, 25: 1, 26: 1, 32: 1, 48: 1, 49: 1, 50: 1, 51: 1, 52: 1, 53: 1, 54: 1, 55: 0, 56: 1, 57: 0, 58: 1, 59: 1, 60: 0, 64: 1, 65: 1, 66: 1, 67: 1, 68: 1, 69: 1, 80: 0, 81: 1, 82: 0, 83: 0, 84: 1, 85: 0, 86: 0, 87: 0, 88: 1, 89: 1, 90: 1, 91: 0, 96: 1, 97: 1, 98: 1, 99: 1, 100: 1, 101: 1, 102: 1, 103: 1, 104: 1, 105: 1, 106: 1, 107: 1, 108: 1, 109: 1, 110: 1, 111: 1, 112: 1, 113: 1, 114: 1, 115: 1, 116: 1, 117: 1, 118: 1, 119: 1, 120: 1, 121: 1, 122: 1, 123: 1, 124: 1, 125: 1, 126: 1, 127: 1, 128: 2, 129: 3, 130: 4, 131: 5, 132: 6, 133: 7, 134: 8, 135: 9, 136: 10, 137: 11, 138: 12, 139: 13, 140: 14, 141: 15, 142: 16, 143: 17, 144: 2, 145: 3, 146: 4, 147: 5, 148: 6, 149: 7, 150: 8, 151: 9, 152: 10, 153: 11, 154: 12, 155: 13, 156: 14, 157: 15, 158: 16, 159: 17, 160: 0, 161: 0, 162: 0, 163: 0, 164: 0, 240: 1, 241: 1, 242: 1, 243: 0, 244: 0, 255: 0},
"result": function() { return this.stack[0].ops; },
"step": function(log, db) {
var frame = this.stack[this.stack.length - 1];
if(log.err) {
frame["error"] = log.err.toString();
} else if(log.depth == this.stack.length) {
opinfo = {
"op": log.op.toNumber(),
"depth" : log.depth,
"result": [],
};
if(frame.ops.length > 0) {
var prevop = frame.ops[frame.ops.length - 1];
for(var i = 0; i < this.npushes[prevop.op]; i++)
prevop.result.push(log.stack.peek(i).Text(16));
}
switch(log.op.toString()) {
case "CALL":
case "CALLCODE":
var instart = log.stack.peek(3).Int64();
var insize = log.stack.peek(4).Int64();
opinfo["gas"] = log.stack.peek(0).Int64();
opinfo["to"] = log.stack.peek(1).Text(16);
opinfo["value"] = log.stack.peek(2).String();
opinfo["input"] = log.memory.slice(instart, instart + insize);
opinfo["error"] = null;
opinfo["return"] = null;
opinfo["ops"] = [];
this.stack.push(opinfo);
break;
case "DELEGATECALL":
case "STATICCALL":
var instart = log.stack.peek(2).Int64();
var insize = log.stack.peek(3).Int64();
opinfo["op"] = log.op.toString();
opinfo["gas"] = log.stack.peek(0).Int64();
opinfo["to"] = log.stack.peek(1).Text(16);
opinfo["input"] = log.memory.slice(instart, instart + insize);
opinfo["error"] = null;
opinfo["return"] = null;
opinfo["ops"] = [];
this.stack.push(opinfo);
break;
case "RETURN":
var out = log.stack.peek(0).Int64();
var outsize = log.stack.peek(1).Int64();
frame.return = log.memory.slice(out, out + outsize);
break;
case "STOP":
case "SUICIDE":
frame.return = log.memory.slice(0, 0);
break;
case "JUMPDEST":
opinfo["pc"] = log.pc;
}
if(log.op.isPush()) {
opinfo["len"] = log.op.toNumber() - 0x5e;
}
frame.ops.push(opinfo);
} else {
this.stack = this.stack.slice(0, log.depth);
}
}
}

View file

@ -0,0 +1,93 @@
// Copyright 2017 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
// evmdisTracer returns sufficent information from a trace to perform evmdis-style
// disassembly.
{
stack: [{ops: []}],
npushes: {0: 0, 1: 1, 2: 1, 3: 1, 4: 1, 5: 1, 6: 1, 7: 1, 8: 1, 9: 1, 10: 1, 11: 1, 16: 1, 17: 1, 18: 1, 19: 1, 20: 1, 21: 1, 22: 1, 23: 1, 24: 1, 25: 1, 26: 1, 32: 1, 48: 1, 49: 1, 50: 1, 51: 1, 52: 1, 53: 1, 54: 1, 55: 0, 56: 1, 57: 0, 58: 1, 59: 1, 60: 0, 64: 1, 65: 1, 66: 1, 67: 1, 68: 1, 69: 1, 80: 0, 81: 1, 82: 0, 83: 0, 84: 1, 85: 0, 86: 0, 87: 0, 88: 1, 89: 1, 90: 1, 91: 0, 96: 1, 97: 1, 98: 1, 99: 1, 100: 1, 101: 1, 102: 1, 103: 1, 104: 1, 105: 1, 106: 1, 107: 1, 108: 1, 109: 1, 110: 1, 111: 1, 112: 1, 113: 1, 114: 1, 115: 1, 116: 1, 117: 1, 118: 1, 119: 1, 120: 1, 121: 1, 122: 1, 123: 1, 124: 1, 125: 1, 126: 1, 127: 1, 128: 2, 129: 3, 130: 4, 131: 5, 132: 6, 133: 7, 134: 8, 135: 9, 136: 10, 137: 11, 138: 12, 139: 13, 140: 14, 141: 15, 142: 16, 143: 17, 144: 2, 145: 3, 146: 4, 147: 5, 148: 6, 149: 7, 150: 8, 151: 9, 152: 10, 153: 11, 154: 12, 155: 13, 156: 14, 157: 15, 158: 16, 159: 17, 160: 0, 161: 0, 162: 0, 163: 0, 164: 0, 240: 1, 241: 1, 242: 1, 243: 0, 244: 0, 255: 0},
// result is invoked when all the opcodes have been iterated over and returns
// the final result of the tracing.
result: function() { return this.stack[0].ops; },
// fault is invoked when the actual execution of an opcode fails.
fault: function(log, db) { },
// step is invoked for every opcode that the VM executes.
step: function(log, db) {
var frame = this.stack[this.stack.length - 1];
var error = log.getError();
if (error) {
frame["error"] = error;
} else if (log.getDepth() == this.stack.length) {
opinfo = {
op: log.op.toNumber(),
depth : log.getDepth(),
result: [],
};
if (frame.ops.length > 0) {
var prevop = frame.ops[frame.ops.length - 1];
for(var i = 0; i < this.npushes[prevop.op]; i++)
prevop.result.push(log.stack.peek(i).toString(16));
}
switch(log.op.toString()) {
case "CALL": case "CALLCODE":
var instart = log.stack.peek(3).valueOf();
var insize = log.stack.peek(4).valueOf();
opinfo["gas"] = log.stack.peek(0).valueOf();
opinfo["to"] = log.stack.peek(1).toString(16);
opinfo["value"] = log.stack.peek(2).toString();
opinfo["input"] = log.memory.slice(instart, instart + insize);
opinfo["error"] = null;
opinfo["return"] = null;
opinfo["ops"] = [];
this.stack.push(opinfo);
break;
case "DELEGATECALL": case "STATICCALL":
var instart = log.stack.peek(2).valueOf();
var insize = log.stack.peek(3).valueOf();
opinfo["op"] = log.op.toString();
opinfo["gas"] = log.stack.peek(0).valueOf();
opinfo["to"] = log.stack.peek(1).toString(16);
opinfo["input"] = log.memory.slice(instart, instart + insize);
opinfo["error"] = null;
opinfo["return"] = null;
opinfo["ops"] = [];
this.stack.push(opinfo);
break;
case "RETURN":
var out = log.stack.peek(0).valueOf();
var outsize = log.stack.peek(1).valueOf();
frame.return = log.memory.slice(out, out + outsize);
break;
case "STOP": case "SUICIDE":
frame.return = log.memory.slice(0, 0);
break;
case "JUMPDEST":
opinfo["pc"] = log.getPC();
}
if(log.op.isPush()) {
opinfo["len"] = log.op.toNumber() - 0x5e;
}
frame.ops.push(opinfo);
} else {
this.stack = this.stack.slice(0, log.getDepth());
}
}
}

View file

@ -1,3 +1,19 @@
// Copyright 2017 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
// noopTracer is just the barebone boilerplate code required from a JavaScript
// object to be usable as a transaction tracer.
{

View file

@ -1,3 +1,19 @@
// Copyright 2017 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
// opcountTracer is a sample tracer that just counts the number of instructions
// executed by the EVM before the transaction terminated.
{

View file

@ -1,3 +1,19 @@
// Copyright 2017 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
// prestateTracer outputs sufficient information to create a local execution of
// the transaction from a custom assembled genesis block.
{

View file

@ -559,11 +559,25 @@ func (jst *Tracer) CaptureEnd(output []byte, gasUsed uint64, t time.Duration, er
// GetResult calls the Javascript 'result' function and returns its value, or any accumulated error
func (jst *Tracer) GetResult() (json.RawMessage, error) {
// Push all the data buffers into the stack first
keys := make([]string, 0, len(jst.ctx))
for key := range jst.ctx {
keys = append(keys, key)
}
buffers := 0
for _, key := range keys {
switch val := jst.ctx[key].(type) {
case []byte:
ptr := jst.vm.PushFixedBuffer(len(val))
copy(makeSlice(ptr, uint(len(val))), val[:])
buffers++
}
}
// Transform the context into a JavaScript object and inject into the state
obj := jst.vm.PushObject()
for key, val := range jst.ctx {
switch val := val.(type) {
for _, key := range keys {
switch val := jst.ctx[key].(type) {
case uint64:
jst.vm.PushUint(uint(val))
@ -571,8 +585,8 @@ func (jst *Tracer) GetResult() (json.RawMessage, error) {
jst.vm.PushString(val)
case []byte:
ptr := jst.vm.PushFixedBuffer(len(val))
copy(makeSlice(ptr, uint(len(val))), val[:])
jst.vm.PushBufferObject(-(buffers + 1), len(val), len(val), duktape.BufobjArraybuffer)
buffers--
case common.Address:
ptr := jst.vm.PushFixedBuffer(20)