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