@@ -29,8 +29,9 @@ import (
2929)
3030
3131var (
32- bigZero = new (big.Int )
33- errWriteProtection = errors .New ("evm: write protection" )
32+ bigZero = new (big.Int )
33+ errWriteProtection = errors .New ("evm: write protection" )
34+ errReturnDataOutOfBounds = errors .New ("evm: return data out of bounds" )
3435)
3536
3637func opAdd (pc * uint64 , evm * EVM , contract * Contract , memory * Memory , stack * Stack ) ([]byte , error ) {
@@ -242,13 +243,15 @@ func opAnd(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stac
242243 evm .interpreter .intPool .put (y )
243244 return nil , nil
244245}
246+
245247func opOr (pc * uint64 , evm * EVM , contract * Contract , memory * Memory , stack * Stack ) ([]byte , error ) {
246248 x , y := stack .pop (), stack .pop ()
247249 stack .push (x .Or (x , y ))
248250
249251 evm .interpreter .intPool .put (y )
250252 return nil , nil
251253}
254+
252255func opXor (pc * uint64 , evm * EVM , contract * Contract , memory * Memory , stack * Stack ) ([]byte , error ) {
253256 x , y := stack .pop (), stack .pop ()
254257 stack .push (x .Xor (x , y ))
@@ -268,6 +271,7 @@ func opByte(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Sta
268271 evm .interpreter .intPool .put (th )
269272 return nil , nil
270273}
274+
271275func opAddmod (pc * uint64 , evm * EVM , contract * Contract , memory * Memory , stack * Stack ) ([]byte , error ) {
272276 x , y , z := stack .pop (), stack .pop (), stack .pop ()
273277 if z .Cmp (bigZero ) > 0 {
@@ -281,6 +285,7 @@ func opAddmod(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *S
281285 evm .interpreter .intPool .put (y , z )
282286 return nil , nil
283287}
288+
284289func opMulmod (pc * uint64 , evm * EVM , contract * Contract , memory * Memory , stack * Stack ) ([]byte , error ) {
285290 x , y , z := stack .pop (), stack .pop (), stack .pop ()
286291 if z .Cmp (bigZero ) > 0 {
@@ -338,25 +343,47 @@ func opCallValue(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack
338343 return nil , nil
339344}
340345
341- func opCalldataLoad (pc * uint64 , evm * EVM , contract * Contract , memory * Memory , stack * Stack ) ([]byte , error ) {
346+ func opCallDataLoad (pc * uint64 , evm * EVM , contract * Contract , memory * Memory , stack * Stack ) ([]byte , error ) {
342347 stack .push (new (big.Int ).SetBytes (getDataBig (contract .Input , stack .pop (), big32 )))
343348 return nil , nil
344349}
345350
346- func opCalldataSize (pc * uint64 , evm * EVM , contract * Contract , memory * Memory , stack * Stack ) ([]byte , error ) {
351+ func opCallDataSize (pc * uint64 , evm * EVM , contract * Contract , memory * Memory , stack * Stack ) ([]byte , error ) {
347352 stack .push (evm .interpreter .intPool .get ().SetInt64 (int64 (len (contract .Input ))))
348353 return nil , nil
349354}
350355
351- func opCalldataCopy (pc * uint64 , evm * EVM , contract * Contract , memory * Memory , stack * Stack ) ([]byte , error ) {
356+ func opCallDataCopy (pc * uint64 , evm * EVM , contract * Contract , memory * Memory , stack * Stack ) ([]byte , error ) {
357+ var (
358+ memOffset = stack .pop ()
359+ dataOffset = stack .pop ()
360+ length = stack .pop ()
361+ )
362+ memory .Set (memOffset .Uint64 (), length .Uint64 (), getDataBig (contract .Input , dataOffset , length ))
363+
364+ evm .interpreter .intPool .put (memOffset , dataOffset , length )
365+ return nil , nil
366+ }
367+
368+ func opReturnDataSize (pc * uint64 , evm * EVM , contract * Contract , memory * Memory , stack * Stack ) ([]byte , error ) {
369+ stack .push (evm .interpreter .intPool .get ().SetUint64 (uint64 (len (evm .interpreter .returnData ))))
370+ return nil , nil
371+ }
372+
373+ func opReturnDataCopy (pc * uint64 , evm * EVM , contract * Contract , memory * Memory , stack * Stack ) ([]byte , error ) {
352374 var (
353- mOff = stack .pop ()
354- cOff = stack .pop ()
355- l = stack .pop ()
375+ memOffset = stack .pop ()
376+ dataOffset = stack .pop ()
377+ length = stack .pop ()
356378 )
357- memory .Set (mOff .Uint64 (), l .Uint64 (), getDataBig (contract .Input , cOff , l ))
379+ defer evm .interpreter .intPool .put (memOffset , dataOffset , length )
380+
381+ end := new (big.Int ).Add (dataOffset , length )
382+ if end .BitLen () > 64 || uint64 (len (evm .interpreter .returnData )) < end .Uint64 () {
383+ return nil , errReturnDataOutOfBounds
384+ }
385+ memory .Set (memOffset .Uint64 (), length .Uint64 (), evm .interpreter .returnData [dataOffset .Uint64 ():end .Uint64 ()])
358386
359- evm .interpreter .intPool .put (mOff , cOff , l )
360387 return nil , nil
361388}
362389
@@ -378,31 +405,28 @@ func opCodeSize(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack
378405
379406func opCodeCopy (pc * uint64 , evm * EVM , contract * Contract , memory * Memory , stack * Stack ) ([]byte , error ) {
380407 var (
381- mOff = stack .pop ()
382- cOff = stack .pop ()
383- l = stack .pop ()
408+ memOffset = stack .pop ()
409+ codeOffset = stack .pop ()
410+ length = stack .pop ()
384411 )
385- codeCopy := getDataBig (contract .Code , cOff , l )
412+ codeCopy := getDataBig (contract .Code , codeOffset , length )
413+ memory .Set (memOffset .Uint64 (), length .Uint64 (), codeCopy )
386414
387- memory .Set (mOff .Uint64 (), l .Uint64 (), codeCopy )
388-
389- evm .interpreter .intPool .put (mOff , cOff , l )
415+ evm .interpreter .intPool .put (memOffset , codeOffset , length )
390416 return nil , nil
391417}
392418
393419func opExtCodeCopy (pc * uint64 , evm * EVM , contract * Contract , memory * Memory , stack * Stack ) ([]byte , error ) {
394420 var (
395- addr = common .BigToAddress (stack .pop ())
396- mOff = stack .pop ()
397- cOff = stack .pop ()
398- l = stack .pop ()
421+ addr = common .BigToAddress (stack .pop ())
422+ memOffset = stack .pop ()
423+ codeOffset = stack .pop ()
424+ length = stack .pop ()
399425 )
400- codeCopy := getDataBig (evm .StateDB .GetCode (addr ), cOff , l )
401-
402- memory .Set (mOff .Uint64 (), l .Uint64 (), codeCopy )
403-
404- evm .interpreter .intPool .put (mOff , cOff , l )
426+ codeCopy := getDataBig (evm .StateDB .GetCode (addr ), codeOffset , length )
427+ memory .Set (memOffset .Uint64 (), length .Uint64 (), codeCopy )
405428
429+ evm .interpreter .intPool .put (memOffset , codeOffset , length )
406430 return nil , nil
407431}
408432
@@ -507,6 +531,7 @@ func opJump(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Sta
507531 evm .interpreter .intPool .put (pos )
508532 return nil , nil
509533}
534+
510535func opJumpi (pc * uint64 , evm * EVM , contract * Contract , memory * Memory , stack * Stack ) ([]byte , error ) {
511536 pos , cond := stack .pop (), stack .pop ()
512537 if cond .Sign () != 0 {
@@ -522,6 +547,7 @@ func opJumpi(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *St
522547 evm .interpreter .intPool .put (pos , cond )
523548 return nil , nil
524549}
550+
525551func opJumpdest (pc * uint64 , evm * EVM , contract * Contract , memory * Memory , stack * Stack ) ([]byte , error ) {
526552 return nil , nil
527553}
0 commit comments