github.com/phillinzzz/newBsc@v1.1.6/core/vm/instructions.go (about) 1 // Copyright 2015 The go-ethereum Authors 2 // This file is part of the go-ethereum library. 3 // 4 // The go-ethereum library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // The go-ethereum library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 16 17 package vm 18 19 import ( 20 "github.com/phillinzzz/newBsc/common" 21 "github.com/phillinzzz/newBsc/core/types" 22 "github.com/phillinzzz/newBsc/params" 23 "github.com/holiman/uint256" 24 "golang.org/x/crypto/sha3" 25 ) 26 27 func opAdd(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 28 x, y := scope.Stack.pop(), scope.Stack.peek() 29 y.Add(&x, y) 30 return nil, nil 31 } 32 33 func opSub(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 34 x, y := scope.Stack.pop(), scope.Stack.peek() 35 y.Sub(&x, y) 36 return nil, nil 37 } 38 39 func opMul(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 40 x, y := scope.Stack.pop(), scope.Stack.peek() 41 y.Mul(&x, y) 42 return nil, nil 43 } 44 45 func opDiv(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 46 x, y := scope.Stack.pop(), scope.Stack.peek() 47 y.Div(&x, y) 48 return nil, nil 49 } 50 51 func opSdiv(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 52 x, y := scope.Stack.pop(), scope.Stack.peek() 53 y.SDiv(&x, y) 54 return nil, nil 55 } 56 57 func opMod(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 58 x, y := scope.Stack.pop(), scope.Stack.peek() 59 y.Mod(&x, y) 60 return nil, nil 61 } 62 63 func opSmod(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 64 x, y := scope.Stack.pop(), scope.Stack.peek() 65 y.SMod(&x, y) 66 return nil, nil 67 } 68 69 func opExp(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 70 base, exponent := scope.Stack.pop(), scope.Stack.peek() 71 exponent.Exp(&base, exponent) 72 return nil, nil 73 } 74 75 func opSignExtend(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 76 back, num := scope.Stack.pop(), scope.Stack.peek() 77 num.ExtendSign(num, &back) 78 return nil, nil 79 } 80 81 func opNot(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 82 x := scope.Stack.peek() 83 x.Not(x) 84 return nil, nil 85 } 86 87 func opLt(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 88 x, y := scope.Stack.pop(), scope.Stack.peek() 89 if x.Lt(y) { 90 y.SetOne() 91 } else { 92 y.Clear() 93 } 94 return nil, nil 95 } 96 97 func opGt(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 98 x, y := scope.Stack.pop(), scope.Stack.peek() 99 if x.Gt(y) { 100 y.SetOne() 101 } else { 102 y.Clear() 103 } 104 return nil, nil 105 } 106 107 func opSlt(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 108 x, y := scope.Stack.pop(), scope.Stack.peek() 109 if x.Slt(y) { 110 y.SetOne() 111 } else { 112 y.Clear() 113 } 114 return nil, nil 115 } 116 117 func opSgt(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 118 x, y := scope.Stack.pop(), scope.Stack.peek() 119 if x.Sgt(y) { 120 y.SetOne() 121 } else { 122 y.Clear() 123 } 124 return nil, nil 125 } 126 127 func opEq(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 128 x, y := scope.Stack.pop(), scope.Stack.peek() 129 if x.Eq(y) { 130 y.SetOne() 131 } else { 132 y.Clear() 133 } 134 return nil, nil 135 } 136 137 func opIszero(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 138 x := scope.Stack.peek() 139 if x.IsZero() { 140 x.SetOne() 141 } else { 142 x.Clear() 143 } 144 return nil, nil 145 } 146 147 func opAnd(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 148 x, y := scope.Stack.pop(), scope.Stack.peek() 149 y.And(&x, y) 150 return nil, nil 151 } 152 153 func opOr(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 154 x, y := scope.Stack.pop(), scope.Stack.peek() 155 y.Or(&x, y) 156 return nil, nil 157 } 158 159 func opXor(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 160 x, y := scope.Stack.pop(), scope.Stack.peek() 161 y.Xor(&x, y) 162 return nil, nil 163 } 164 165 func opByte(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 166 th, val := scope.Stack.pop(), scope.Stack.peek() 167 val.Byte(&th) 168 return nil, nil 169 } 170 171 func opAddmod(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 172 x, y, z := scope.Stack.pop(), scope.Stack.pop(), scope.Stack.peek() 173 if z.IsZero() { 174 z.Clear() 175 } else { 176 z.AddMod(&x, &y, z) 177 } 178 return nil, nil 179 } 180 181 func opMulmod(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 182 x, y, z := scope.Stack.pop(), scope.Stack.pop(), scope.Stack.peek() 183 z.MulMod(&x, &y, z) 184 return nil, nil 185 } 186 187 // opSHL implements Shift Left 188 // The SHL instruction (shift left) pops 2 values from the stack, first arg1 and then arg2, 189 // and pushes on the stack arg2 shifted to the left by arg1 number of bits. 190 func opSHL(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 191 // Note, second operand is left in the stack; accumulate result into it, and no need to push it afterwards 192 shift, value := scope.Stack.pop(), scope.Stack.peek() 193 if shift.LtUint64(256) { 194 value.Lsh(value, uint(shift.Uint64())) 195 } else { 196 value.Clear() 197 } 198 return nil, nil 199 } 200 201 // opSHR implements Logical Shift Right 202 // The SHR instruction (logical shift right) pops 2 values from the stack, first arg1 and then arg2, 203 // and pushes on the stack arg2 shifted to the right by arg1 number of bits with zero fill. 204 func opSHR(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 205 // Note, second operand is left in the stack; accumulate result into it, and no need to push it afterwards 206 shift, value := scope.Stack.pop(), scope.Stack.peek() 207 if shift.LtUint64(256) { 208 value.Rsh(value, uint(shift.Uint64())) 209 } else { 210 value.Clear() 211 } 212 return nil, nil 213 } 214 215 // opSAR implements Arithmetic Shift Right 216 // The SAR instruction (arithmetic shift right) pops 2 values from the stack, first arg1 and then arg2, 217 // and pushes on the stack arg2 shifted to the right by arg1 number of bits with sign extension. 218 func opSAR(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 219 shift, value := scope.Stack.pop(), scope.Stack.peek() 220 if shift.GtUint64(256) { 221 if value.Sign() >= 0 { 222 value.Clear() 223 } else { 224 // Max negative shift: all bits set 225 value.SetAllOne() 226 } 227 return nil, nil 228 } 229 n := uint(shift.Uint64()) 230 value.SRsh(value, n) 231 return nil, nil 232 } 233 234 func opSha3(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 235 offset, size := scope.Stack.pop(), scope.Stack.peek() 236 data := scope.Memory.GetPtr(int64(offset.Uint64()), int64(size.Uint64())) 237 238 if interpreter.hasher == nil { 239 interpreter.hasher = sha3.NewLegacyKeccak256().(keccakState) 240 } else { 241 interpreter.hasher.Reset() 242 } 243 interpreter.hasher.Write(data) 244 interpreter.hasher.Read(interpreter.hasherBuf[:]) 245 246 evm := interpreter.evm 247 if evm.vmConfig.EnablePreimageRecording { 248 evm.StateDB.AddPreimage(interpreter.hasherBuf, data) 249 } 250 251 size.SetBytes(interpreter.hasherBuf[:]) 252 return nil, nil 253 } 254 func opAddress(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 255 scope.Stack.push(new(uint256.Int).SetBytes(scope.Contract.Address().Bytes())) 256 return nil, nil 257 } 258 259 func opBalance(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 260 slot := scope.Stack.peek() 261 address := common.Address(slot.Bytes20()) 262 slot.SetFromBig(interpreter.evm.StateDB.GetBalance(address)) 263 return nil, nil 264 } 265 266 func opOrigin(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 267 scope.Stack.push(new(uint256.Int).SetBytes(interpreter.evm.Origin.Bytes())) 268 return nil, nil 269 } 270 func opCaller(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 271 scope.Stack.push(new(uint256.Int).SetBytes(scope.Contract.Caller().Bytes())) 272 return nil, nil 273 } 274 275 func opCallValue(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 276 v, _ := uint256.FromBig(scope.Contract.value) 277 scope.Stack.push(v) 278 return nil, nil 279 } 280 281 func opCallDataLoad(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 282 x := scope.Stack.peek() 283 if offset, overflow := x.Uint64WithOverflow(); !overflow { 284 data := getData(scope.Contract.Input, offset, 32) 285 x.SetBytes(data) 286 } else { 287 x.Clear() 288 } 289 return nil, nil 290 } 291 292 func opCallDataSize(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 293 scope.Stack.push(new(uint256.Int).SetUint64(uint64(len(scope.Contract.Input)))) 294 return nil, nil 295 } 296 297 func opCallDataCopy(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 298 var ( 299 memOffset = scope.Stack.pop() 300 dataOffset = scope.Stack.pop() 301 length = scope.Stack.pop() 302 ) 303 dataOffset64, overflow := dataOffset.Uint64WithOverflow() 304 if overflow { 305 dataOffset64 = 0xffffffffffffffff 306 } 307 // These values are checked for overflow during gas cost calculation 308 memOffset64 := memOffset.Uint64() 309 length64 := length.Uint64() 310 scope.Memory.Set(memOffset64, length64, getData(scope.Contract.Input, dataOffset64, length64)) 311 312 return nil, nil 313 } 314 315 func opReturnDataSize(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 316 scope.Stack.push(new(uint256.Int).SetUint64(uint64(len(interpreter.returnData)))) 317 return nil, nil 318 } 319 320 func opReturnDataCopy(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 321 var ( 322 memOffset = scope.Stack.pop() 323 dataOffset = scope.Stack.pop() 324 length = scope.Stack.pop() 325 ) 326 327 offset64, overflow := dataOffset.Uint64WithOverflow() 328 if overflow { 329 return nil, ErrReturnDataOutOfBounds 330 } 331 // we can reuse dataOffset now (aliasing it for clarity) 332 var end = dataOffset 333 end.Add(&dataOffset, &length) 334 end64, overflow := end.Uint64WithOverflow() 335 if overflow || uint64(len(interpreter.returnData)) < end64 { 336 return nil, ErrReturnDataOutOfBounds 337 } 338 scope.Memory.Set(memOffset.Uint64(), length.Uint64(), interpreter.returnData[offset64:end64]) 339 return nil, nil 340 } 341 342 func opExtCodeSize(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 343 slot := scope.Stack.peek() 344 slot.SetUint64(uint64(interpreter.evm.StateDB.GetCodeSize(slot.Bytes20()))) 345 return nil, nil 346 } 347 348 func opCodeSize(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 349 l := new(uint256.Int) 350 l.SetUint64(uint64(len(scope.Contract.Code))) 351 scope.Stack.push(l) 352 return nil, nil 353 } 354 355 func opCodeCopy(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 356 var ( 357 memOffset = scope.Stack.pop() 358 codeOffset = scope.Stack.pop() 359 length = scope.Stack.pop() 360 ) 361 uint64CodeOffset, overflow := codeOffset.Uint64WithOverflow() 362 if overflow { 363 uint64CodeOffset = 0xffffffffffffffff 364 } 365 codeCopy := getData(scope.Contract.Code, uint64CodeOffset, length.Uint64()) 366 scope.Memory.Set(memOffset.Uint64(), length.Uint64(), codeCopy) 367 368 return nil, nil 369 } 370 371 func opExtCodeCopy(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 372 var ( 373 stack = scope.Stack 374 a = stack.pop() 375 memOffset = stack.pop() 376 codeOffset = stack.pop() 377 length = stack.pop() 378 ) 379 uint64CodeOffset, overflow := codeOffset.Uint64WithOverflow() 380 if overflow { 381 uint64CodeOffset = 0xffffffffffffffff 382 } 383 addr := common.Address(a.Bytes20()) 384 codeCopy := getData(interpreter.evm.StateDB.GetCode(addr), uint64CodeOffset, length.Uint64()) 385 scope.Memory.Set(memOffset.Uint64(), length.Uint64(), codeCopy) 386 387 return nil, nil 388 } 389 390 // opExtCodeHash returns the code hash of a specified account. 391 // There are several cases when the function is called, while we can relay everything 392 // to `state.GetCodeHash` function to ensure the correctness. 393 // (1) Caller tries to get the code hash of a normal contract account, state 394 // should return the relative code hash and set it as the result. 395 // 396 // (2) Caller tries to get the code hash of a non-existent account, state should 397 // return common.Hash{} and zero will be set as the result. 398 // 399 // (3) Caller tries to get the code hash for an account without contract code, 400 // state should return emptyCodeHash(0xc5d246...) as the result. 401 // 402 // (4) Caller tries to get the code hash of a precompiled account, the result 403 // should be zero or emptyCodeHash. 404 // 405 // It is worth noting that in order to avoid unnecessary create and clean, 406 // all precompile accounts on mainnet have been transferred 1 wei, so the return 407 // here should be emptyCodeHash. 408 // If the precompile account is not transferred any amount on a private or 409 // customized chain, the return value will be zero. 410 // 411 // (5) Caller tries to get the code hash for an account which is marked as suicided 412 // in the current transaction, the code hash of this account should be returned. 413 // 414 // (6) Caller tries to get the code hash for an account which is marked as deleted, 415 // this account should be regarded as a non-existent account and zero should be returned. 416 func opExtCodeHash(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 417 slot := scope.Stack.peek() 418 address := common.Address(slot.Bytes20()) 419 if interpreter.evm.StateDB.Empty(address) { 420 slot.Clear() 421 } else { 422 slot.SetBytes(interpreter.evm.StateDB.GetCodeHash(address).Bytes()) 423 } 424 return nil, nil 425 } 426 427 func opGasprice(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 428 v, _ := uint256.FromBig(interpreter.evm.GasPrice) 429 scope.Stack.push(v) 430 return nil, nil 431 } 432 433 func opBlockhash(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 434 num := scope.Stack.peek() 435 num64, overflow := num.Uint64WithOverflow() 436 if overflow { 437 num.Clear() 438 return nil, nil 439 } 440 var upper, lower uint64 441 upper = interpreter.evm.Context.BlockNumber.Uint64() 442 if upper < 257 { 443 lower = 0 444 } else { 445 lower = upper - 256 446 } 447 if num64 >= lower && num64 < upper { 448 num.SetBytes(interpreter.evm.Context.GetHash(num64).Bytes()) 449 } else { 450 num.Clear() 451 } 452 return nil, nil 453 } 454 455 func opCoinbase(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 456 scope.Stack.push(new(uint256.Int).SetBytes(interpreter.evm.Context.Coinbase.Bytes())) 457 return nil, nil 458 } 459 460 func opTimestamp(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 461 v, _ := uint256.FromBig(interpreter.evm.Context.Time) 462 scope.Stack.push(v) 463 return nil, nil 464 } 465 466 func opNumber(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 467 v, _ := uint256.FromBig(interpreter.evm.Context.BlockNumber) 468 scope.Stack.push(v) 469 return nil, nil 470 } 471 472 func opDifficulty(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 473 v, _ := uint256.FromBig(interpreter.evm.Context.Difficulty) 474 scope.Stack.push(v) 475 return nil, nil 476 } 477 478 func opGasLimit(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 479 scope.Stack.push(new(uint256.Int).SetUint64(interpreter.evm.Context.GasLimit)) 480 return nil, nil 481 } 482 483 func opPop(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 484 scope.Stack.pop() 485 return nil, nil 486 } 487 488 func opMload(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 489 v := scope.Stack.peek() 490 offset := int64(v.Uint64()) 491 v.SetBytes(scope.Memory.GetPtr(offset, 32)) 492 return nil, nil 493 } 494 495 func opMstore(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 496 // pop value of the stack 497 mStart, val := scope.Stack.pop(), scope.Stack.pop() 498 scope.Memory.Set32(mStart.Uint64(), &val) 499 return nil, nil 500 } 501 502 func opMstore8(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 503 off, val := scope.Stack.pop(), scope.Stack.pop() 504 scope.Memory.store[off.Uint64()] = byte(val.Uint64()) 505 return nil, nil 506 } 507 508 func opSload(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 509 loc := scope.Stack.peek() 510 hash := common.Hash(loc.Bytes32()) 511 val := interpreter.evm.StateDB.GetState(scope.Contract.Address(), hash) 512 loc.SetBytes(val.Bytes()) 513 return nil, nil 514 } 515 516 func opSstore(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 517 loc := scope.Stack.pop() 518 val := scope.Stack.pop() 519 interpreter.evm.StateDB.SetState(scope.Contract.Address(), 520 loc.Bytes32(), val.Bytes32()) 521 return nil, nil 522 } 523 524 func opJump(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 525 pos := scope.Stack.pop() 526 if !scope.Contract.validJumpdest(&pos) { 527 return nil, ErrInvalidJump 528 } 529 *pc = pos.Uint64() 530 return nil, nil 531 } 532 533 func opJumpi(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 534 pos, cond := scope.Stack.pop(), scope.Stack.pop() 535 if !cond.IsZero() { 536 if !scope.Contract.validJumpdest(&pos) { 537 return nil, ErrInvalidJump 538 } 539 *pc = pos.Uint64() 540 } else { 541 *pc++ 542 } 543 return nil, nil 544 } 545 546 func opJumpdest(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 547 return nil, nil 548 } 549 550 func opPc(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 551 scope.Stack.push(new(uint256.Int).SetUint64(*pc)) 552 return nil, nil 553 } 554 555 func opMsize(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 556 scope.Stack.push(new(uint256.Int).SetUint64(uint64(scope.Memory.Len()))) 557 return nil, nil 558 } 559 560 func opGas(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 561 scope.Stack.push(new(uint256.Int).SetUint64(scope.Contract.Gas)) 562 return nil, nil 563 } 564 565 func opCreate(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 566 var ( 567 value = scope.Stack.pop() 568 offset, size = scope.Stack.pop(), scope.Stack.pop() 569 input = scope.Memory.GetCopy(int64(offset.Uint64()), int64(size.Uint64())) 570 gas = scope.Contract.Gas 571 ) 572 if interpreter.evm.chainRules.IsEIP150 { 573 gas -= gas / 64 574 } 575 // reuse size int for stackvalue 576 stackvalue := size 577 578 scope.Contract.UseGas(gas) 579 //TODO: use uint256.Int instead of converting with toBig() 580 var bigVal = big0 581 if !value.IsZero() { 582 bigVal = value.ToBig() 583 } 584 585 res, addr, returnGas, suberr := interpreter.evm.Create(scope.Contract, input, gas, bigVal) 586 // Push item on the stack based on the returned error. If the ruleset is 587 // homestead we must check for CodeStoreOutOfGasError (homestead only 588 // rule) and treat as an error, if the ruleset is frontier we must 589 // ignore this error and pretend the operation was successful. 590 if interpreter.evm.chainRules.IsHomestead && suberr == ErrCodeStoreOutOfGas { 591 stackvalue.Clear() 592 } else if suberr != nil && suberr != ErrCodeStoreOutOfGas { 593 stackvalue.Clear() 594 } else { 595 stackvalue.SetBytes(addr.Bytes()) 596 } 597 scope.Stack.push(&stackvalue) 598 scope.Contract.Gas += returnGas 599 600 if suberr == ErrExecutionReverted { 601 return res, nil 602 } 603 return nil, nil 604 } 605 606 func opCreate2(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 607 var ( 608 endowment = scope.Stack.pop() 609 offset, size = scope.Stack.pop(), scope.Stack.pop() 610 salt = scope.Stack.pop() 611 input = scope.Memory.GetCopy(int64(offset.Uint64()), int64(size.Uint64())) 612 gas = scope.Contract.Gas 613 ) 614 615 // Apply EIP150 616 gas -= gas / 64 617 scope.Contract.UseGas(gas) 618 // reuse size int for stackvalue 619 stackvalue := size 620 //TODO: use uint256.Int instead of converting with toBig() 621 bigEndowment := big0 622 if !endowment.IsZero() { 623 bigEndowment = endowment.ToBig() 624 } 625 res, addr, returnGas, suberr := interpreter.evm.Create2(scope.Contract, input, gas, 626 bigEndowment, &salt) 627 // Push item on the stack based on the returned error. 628 if suberr != nil { 629 stackvalue.Clear() 630 } else { 631 stackvalue.SetBytes(addr.Bytes()) 632 } 633 scope.Stack.push(&stackvalue) 634 scope.Contract.Gas += returnGas 635 636 if suberr == ErrExecutionReverted { 637 return res, nil 638 } 639 return nil, nil 640 } 641 642 func opCall(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 643 stack := scope.Stack 644 // Pop gas. The actual gas in interpreter.evm.callGasTemp. 645 // We can use this as a temporary value 646 temp := stack.pop() 647 gas := interpreter.evm.callGasTemp 648 // Pop other call parameters. 649 addr, value, inOffset, inSize, retOffset, retSize := stack.pop(), stack.pop(), stack.pop(), stack.pop(), stack.pop(), stack.pop() 650 toAddr := common.Address(addr.Bytes20()) 651 // Get the arguments from the memory. 652 args := scope.Memory.GetPtr(int64(inOffset.Uint64()), int64(inSize.Uint64())) 653 654 var bigVal = big0 655 //TODO: use uint256.Int instead of converting with toBig() 656 // By using big0 here, we save an alloc for the most common case (non-ether-transferring contract calls), 657 // but it would make more sense to extend the usage of uint256.Int 658 if !value.IsZero() { 659 gas += params.CallStipend 660 bigVal = value.ToBig() 661 } 662 663 ret, returnGas, err := interpreter.evm.Call(scope.Contract, toAddr, args, gas, bigVal) 664 665 if err != nil { 666 temp.Clear() 667 } else { 668 temp.SetOne() 669 } 670 stack.push(&temp) 671 if err == nil || err == ErrExecutionReverted { 672 ret = common.CopyBytes(ret) 673 scope.Memory.Set(retOffset.Uint64(), retSize.Uint64(), ret) 674 } 675 scope.Contract.Gas += returnGas 676 677 return ret, nil 678 } 679 680 func opCallCode(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 681 // Pop gas. The actual gas is in interpreter.evm.callGasTemp. 682 stack := scope.Stack 683 // We use it as a temporary value 684 temp := stack.pop() 685 gas := interpreter.evm.callGasTemp 686 // Pop other call parameters. 687 addr, value, inOffset, inSize, retOffset, retSize := stack.pop(), stack.pop(), stack.pop(), stack.pop(), stack.pop(), stack.pop() 688 toAddr := common.Address(addr.Bytes20()) 689 // Get arguments from the memory. 690 args := scope.Memory.GetPtr(int64(inOffset.Uint64()), int64(inSize.Uint64())) 691 692 //TODO: use uint256.Int instead of converting with toBig() 693 var bigVal = big0 694 if !value.IsZero() { 695 gas += params.CallStipend 696 bigVal = value.ToBig() 697 } 698 699 ret, returnGas, err := interpreter.evm.CallCode(scope.Contract, toAddr, args, gas, bigVal) 700 if err != nil { 701 temp.Clear() 702 } else { 703 temp.SetOne() 704 } 705 stack.push(&temp) 706 if err == nil || err == ErrExecutionReverted { 707 ret = common.CopyBytes(ret) 708 scope.Memory.Set(retOffset.Uint64(), retSize.Uint64(), ret) 709 } 710 scope.Contract.Gas += returnGas 711 712 return ret, nil 713 } 714 715 func opDelegateCall(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 716 stack := scope.Stack 717 // Pop gas. The actual gas is in interpreter.evm.callGasTemp. 718 // We use it as a temporary value 719 temp := stack.pop() 720 gas := interpreter.evm.callGasTemp 721 // Pop other call parameters. 722 addr, inOffset, inSize, retOffset, retSize := stack.pop(), stack.pop(), stack.pop(), stack.pop(), stack.pop() 723 toAddr := common.Address(addr.Bytes20()) 724 // Get arguments from the memory. 725 args := scope.Memory.GetPtr(int64(inOffset.Uint64()), int64(inSize.Uint64())) 726 727 ret, returnGas, err := interpreter.evm.DelegateCall(scope.Contract, toAddr, args, gas) 728 if err != nil { 729 temp.Clear() 730 } else { 731 temp.SetOne() 732 } 733 stack.push(&temp) 734 if err == nil || err == ErrExecutionReverted { 735 ret = common.CopyBytes(ret) 736 scope.Memory.Set(retOffset.Uint64(), retSize.Uint64(), ret) 737 } 738 scope.Contract.Gas += returnGas 739 740 return ret, nil 741 } 742 743 func opStaticCall(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 744 // Pop gas. The actual gas is in interpreter.evm.callGasTemp. 745 stack := scope.Stack 746 // We use it as a temporary value 747 temp := stack.pop() 748 gas := interpreter.evm.callGasTemp 749 // Pop other call parameters. 750 addr, inOffset, inSize, retOffset, retSize := stack.pop(), stack.pop(), stack.pop(), stack.pop(), stack.pop() 751 toAddr := common.Address(addr.Bytes20()) 752 // Get arguments from the memory. 753 args := scope.Memory.GetPtr(int64(inOffset.Uint64()), int64(inSize.Uint64())) 754 755 ret, returnGas, err := interpreter.evm.StaticCall(scope.Contract, toAddr, args, gas) 756 if err != nil { 757 temp.Clear() 758 } else { 759 temp.SetOne() 760 } 761 stack.push(&temp) 762 if err == nil || err == ErrExecutionReverted { 763 ret = common.CopyBytes(ret) 764 scope.Memory.Set(retOffset.Uint64(), retSize.Uint64(), ret) 765 } 766 scope.Contract.Gas += returnGas 767 768 return ret, nil 769 } 770 771 func opReturn(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 772 offset, size := scope.Stack.pop(), scope.Stack.pop() 773 ret := scope.Memory.GetPtr(int64(offset.Uint64()), int64(size.Uint64())) 774 775 return ret, nil 776 } 777 778 func opRevert(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 779 offset, size := scope.Stack.pop(), scope.Stack.pop() 780 ret := scope.Memory.GetPtr(int64(offset.Uint64()), int64(size.Uint64())) 781 782 return ret, nil 783 } 784 785 func opStop(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 786 return nil, nil 787 } 788 789 func opSuicide(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 790 beneficiary := scope.Stack.pop() 791 balance := interpreter.evm.StateDB.GetBalance(scope.Contract.Address()) 792 interpreter.evm.StateDB.AddBalance(beneficiary.Bytes20(), balance) 793 interpreter.evm.StateDB.Suicide(scope.Contract.Address()) 794 return nil, nil 795 } 796 797 // following functions are used by the instruction jump table 798 799 // make log instruction function 800 func makeLog(size int) executionFunc { 801 return func(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 802 topics := make([]common.Hash, size) 803 stack := scope.Stack 804 mStart, mSize := stack.pop(), stack.pop() 805 for i := 0; i < size; i++ { 806 addr := stack.pop() 807 topics[i] = addr.Bytes32() 808 } 809 810 d := scope.Memory.GetCopy(int64(mStart.Uint64()), int64(mSize.Uint64())) 811 interpreter.evm.StateDB.AddLog(&types.Log{ 812 Address: scope.Contract.Address(), 813 Topics: topics, 814 Data: d, 815 // This is a non-consensus field, but assigned here because 816 // core/state doesn't know the current block number. 817 BlockNumber: interpreter.evm.Context.BlockNumber.Uint64(), 818 }) 819 820 return nil, nil 821 } 822 } 823 824 // opPush1 is a specialized version of pushN 825 func opPush1(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 826 var ( 827 codeLen = uint64(len(scope.Contract.Code)) 828 integer = new(uint256.Int) 829 ) 830 *pc += 1 831 if *pc < codeLen { 832 scope.Stack.push(integer.SetUint64(uint64(scope.Contract.Code[*pc]))) 833 } else { 834 scope.Stack.push(integer.Clear()) 835 } 836 return nil, nil 837 } 838 839 // make push instruction function 840 func makePush(size uint64, pushByteSize int) executionFunc { 841 return func(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 842 codeLen := len(scope.Contract.Code) 843 844 startMin := codeLen 845 if int(*pc+1) < startMin { 846 startMin = int(*pc + 1) 847 } 848 849 endMin := codeLen 850 if startMin+pushByteSize < endMin { 851 endMin = startMin + pushByteSize 852 } 853 854 integer := new(uint256.Int) 855 scope.Stack.push(integer.SetBytes(common.RightPadBytes( 856 scope.Contract.Code[startMin:endMin], pushByteSize))) 857 858 *pc += size 859 return nil, nil 860 } 861 } 862 863 // make dup instruction function 864 func makeDup(size int64) executionFunc { 865 return func(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 866 scope.Stack.dup(int(size)) 867 return nil, nil 868 } 869 } 870 871 // make swap instruction function 872 func makeSwap(size int64) executionFunc { 873 // switch n + 1 otherwise n would be swapped with n 874 size++ 875 return func(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 876 scope.Stack.swap(int(size)) 877 return nil, nil 878 } 879 }