@@ -27,6 +27,7 @@ class DataCollector {
27
27
}
28
28
29
29
this . lastHash = null ;
30
+ this . viaIR = viaIR ;
30
31
}
31
32
32
33
/**
@@ -39,9 +40,9 @@ class DataCollector {
39
40
if ( this . validOpcodes [ info . opcode . name ] && info . stack . length > 0 ) {
40
41
const idx = info . stack . length - 1 ;
41
42
let hash = '0x' + info . stack [ idx ] . toString ( 16 ) ;
43
+
42
44
this . _registerHash ( hash )
43
45
44
- // console.log('hash: ' + info.opcode.name + " " + hash)
45
46
}
46
47
} catch ( err ) { /*Ignore*/ } ;
47
48
}
@@ -65,15 +66,28 @@ class DataCollector {
65
66
_registerHash ( hash ) {
66
67
hash = this . _normalizeHash ( hash ) ;
67
68
68
- //console.log('normalized: ' + hash);
69
-
70
69
if ( this . instrumentationData [ hash ] ) {
71
-
72
70
// abi.encode (used to circumvent viaIR) puts the hash on the stack twice
73
71
if ( this . lastHash !== hash ) {
74
72
this . lastHash = hash ;
75
73
this . instrumentationData [ hash ] . hits ++
76
74
}
75
+ return ;
76
+ }
77
+
78
+ // Detect and recover from viaIR mangled hashes by left-padding single `0`
79
+ // for some cases
80
+ if ( this . viaIR && hash . length === 18 ) {
81
+ hash = hash . slice ( 2 ) ;
82
+ hash = '0' + hash ;
83
+ hash = hash . slice ( 0 , 16 ) ;
84
+ hash = '0x' + hash ;
85
+ if ( this . instrumentationData [ hash ] ) {
86
+ if ( this . lastHash !== hash ) {
87
+ this . lastHash = hash ;
88
+ this . instrumentationData [ hash ] . hits ++
89
+ }
90
+ }
77
91
}
78
92
}
79
93
@@ -83,10 +97,14 @@ class DataCollector {
83
97
* but prevents left-padding shorter irrelevant hashes
84
98
*
85
99
* @param {String } hash data hash from evm stack.
86
- * @return {String } 0x prefixed hash of length 66 .
100
+ * @return {String } 0x prefixed hash of length 18 .
87
101
*/
88
102
_normalizeHash ( hash ) {
89
- if ( hash . length < 18 && hash . length > 11 ) {
103
+ // viaIR sometimes zero right-pads the hashes out to 32bytes
104
+ // but it doesn't preserve leading zeroes when it does this
105
+ if ( this . viaIR && hash . length >= 18 ) {
106
+ hash = hash . slice ( 0 , 18 ) ;
107
+ } else if ( hash . length < 18 && hash . length > 11 ) {
90
108
hash = hash . slice ( 2 ) ;
91
109
while ( hash . length < 16 ) hash = '0' + hash ;
92
110
hash = '0x' + hash
0 commit comments