@@ -23,6 +23,19 @@ use self::vm::{
23
23
24
24
use ewasm_api:: types:: { Bytes20 , Bytes32 , Uint128 } ;
25
25
26
+ trait AsCheckedU64 {
27
+ fn as_checked_u64 ( & self ) -> u64 ;
28
+ }
29
+
30
+ impl AsCheckedU64 for U256 {
31
+ fn as_checked_u64 ( & self ) -> u64 {
32
+ if self > & U256 :: from ( "ffffffffffffffff" ) {
33
+ ewasm_api:: abort ( ) ;
34
+ }
35
+ self . as_u64 ( )
36
+ }
37
+ }
38
+
26
39
// For some explanation see ethcore/vm/src/tests.rs::FakeExt
27
40
28
41
#[ derive( Default ) ]
@@ -119,9 +132,7 @@ impl vm::Ext for EwasmExt {
119
132
call_type : CallType ,
120
133
trap : bool ,
121
134
) -> :: std:: result:: Result < MessageCallResult , TrapKind > {
122
- // FIXME: set this properly
123
- //let gas_limit = u64::from(gas);
124
- let gas_limit = gas. as_u64 ( ) ;
135
+ let gas_limit = gas. as_checked_u64 ( ) ;
125
136
126
137
// FIXME: might not be good enough
127
138
let gas_start = ewasm_api:: gas_left ( ) ;
@@ -332,7 +343,7 @@ pub extern "C" fn main() {
332
343
// Could run `result.finalize(ext)` here, but processing manually seemed simpler.
333
344
match result {
334
345
Ok ( Ok ( GasLeft :: Known ( gas_left) ) ) => {
335
- ewasm_api:: consume_gas ( startgas - gas_left. as_u64 ( ) ) ;
346
+ ewasm_api:: consume_gas ( startgas - gas_left. as_checked_u64 ( ) ) ;
336
347
if ext. selfdestruct_address . is_some ( ) {
337
348
let beneficiary: [ u8 ; 20 ] = ext. selfdestruct_address . unwrap ( ) . into ( ) ;
338
349
ewasm_api:: selfdestruct ( & ewasm_api:: types:: Bytes20 { bytes : beneficiary } )
@@ -345,7 +356,7 @@ pub extern "C" fn main() {
345
356
data,
346
357
apply_state,
347
358
} ) ) => {
348
- ewasm_api:: consume_gas ( startgas - gas_left. as_u64 ( ) ) ;
359
+ ewasm_api:: consume_gas ( startgas - gas_left. as_checked_u64 ( ) ) ;
349
360
if apply_state {
350
361
ewasm_api:: finish_data ( & data. deref ( ) )
351
362
} else {
0 commit comments