github.com/unicornultrafoundation/go-u2u@v1.0.0-rc1.0.20240205080301-e74a83d3fadc/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/holiman/uint256" 21 "github.com/unicornultrafoundation/go-u2u/common" 22 "github.com/unicornultrafoundation/go-u2u/core/types" 23 "github.com/unicornultrafoundation/go-u2u/params" 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.Config.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 // 394 // (1) Caller tries to get the code hash of a normal contract account, state 395 // 396 // should return the relative code hash and set it as the result. 397 // 398 // (2) Caller tries to get the code hash of a non-existent account, state should 399 // 400 // return common.Hash{} and zero will be set as the result. 401 // 402 // (3) Caller tries to get the code hash for an account without contract code, 403 // 404 // state should return emptyCodeHash(0xc5d246...) as the result. 405 // 406 // (4) Caller tries to get the code hash of a precompiled account, the result 407 // 408 // should be zero or emptyCodeHash. 409 // 410 // It is worth noting that in order to avoid unnecessary create and clean, 411 // all precompile accounts on mainnet have been transferred 1 wei, so the return 412 // here should be emptyCodeHash. 413 // If the precompile account is not transferred any amount on a private or 414 // customized chain, the return value will be zero. 415 // 416 // (5) Caller tries to get the code hash for an account which is marked as suicided 417 // 418 // in the current transaction, the code hash of this account should be returned. 419 // 420 // (6) Caller tries to get the code hash for an account which is marked as deleted, 421 // 422 // this account should be regarded as a non-existent account and zero should be returned. 423 func opExtCodeHash(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 424 slot := scope.Stack.peek() 425 address := common.Address(slot.Bytes20()) 426 if interpreter.evm.StateDB.Empty(address) { 427 slot.Clear() 428 } else { 429 slot.SetBytes(interpreter.evm.StateDB.GetCodeHash(address).Bytes()) 430 } 431 return nil, nil 432 } 433 434 func opGasprice(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 435 v, _ := uint256.FromBig(interpreter.evm.GasPrice) 436 scope.Stack.push(v) 437 return nil, nil 438 } 439 440 func opBlockhash(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 441 num := scope.Stack.peek() 442 num64, overflow := num.Uint64WithOverflow() 443 if overflow { 444 num.Clear() 445 return nil, nil 446 } 447 var upper, lower uint64 448 upper = interpreter.evm.Context.BlockNumber.Uint64() 449 if upper < 257 { 450 lower = 0 451 } else { 452 lower = upper - 256 453 } 454 if num64 >= lower && num64 < upper { 455 num.SetBytes(interpreter.evm.Context.GetHash(num64).Bytes()) 456 } else { 457 num.Clear() 458 } 459 return nil, nil 460 } 461 462 func opCoinbase(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 463 scope.Stack.push(new(uint256.Int).SetBytes(interpreter.evm.Context.Coinbase.Bytes())) 464 return nil, nil 465 } 466 467 func opTimestamp(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 468 v, _ := uint256.FromBig(interpreter.evm.Context.Time) 469 scope.Stack.push(v) 470 return nil, nil 471 } 472 473 func opNumber(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 474 v, _ := uint256.FromBig(interpreter.evm.Context.BlockNumber) 475 scope.Stack.push(v) 476 return nil, nil 477 } 478 479 func opDifficulty(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 480 v, _ := uint256.FromBig(interpreter.evm.Context.Difficulty) 481 scope.Stack.push(v) 482 return nil, nil 483 } 484 485 func opGasLimit(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 486 scope.Stack.push(new(uint256.Int).SetUint64(interpreter.evm.Context.GasLimit)) 487 return nil, nil 488 } 489 490 func opPop(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 491 scope.Stack.pop() 492 return nil, nil 493 } 494 495 func opMload(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 496 v := scope.Stack.peek() 497 offset := int64(v.Uint64()) 498 v.SetBytes(scope.Memory.GetPtr(offset, 32)) 499 return nil, nil 500 } 501 502 func opMstore(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 503 // pop value of the stack 504 mStart, val := scope.Stack.pop(), scope.Stack.pop() 505 scope.Memory.Set32(mStart.Uint64(), &val) 506 return nil, nil 507 } 508 509 func opMstore8(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 510 off, val := scope.Stack.pop(), scope.Stack.pop() 511 scope.Memory.store[off.Uint64()] = byte(val.Uint64()) 512 return nil, nil 513 } 514 515 func opSload(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 516 loc := scope.Stack.peek() 517 hash := common.Hash(loc.Bytes32()) 518 val := interpreter.evm.StateDB.GetState(scope.Contract.Address(), hash) 519 loc.SetBytes(val.Bytes()) 520 return nil, nil 521 } 522 523 func opSstore(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 524 loc := scope.Stack.pop() 525 val := scope.Stack.pop() 526 interpreter.evm.StateDB.SetState(scope.Contract.Address(), 527 loc.Bytes32(), val.Bytes32()) 528 return nil, nil 529 } 530 531 func opJump(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 532 pos := scope.Stack.pop() 533 if !scope.Contract.validJumpdest(&pos) { 534 return nil, ErrInvalidJump 535 } 536 *pc = pos.Uint64() 537 return nil, nil 538 } 539 540 func opJumpi(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 541 pos, cond := scope.Stack.pop(), scope.Stack.pop() 542 if !cond.IsZero() { 543 if !scope.Contract.validJumpdest(&pos) { 544 return nil, ErrInvalidJump 545 } 546 *pc = pos.Uint64() 547 } else { 548 *pc++ 549 } 550 return nil, nil 551 } 552 553 func opJumpdest(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 554 return nil, nil 555 } 556 557 func opPc(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 558 scope.Stack.push(new(uint256.Int).SetUint64(*pc)) 559 return nil, nil 560 } 561 562 func opMsize(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 563 scope.Stack.push(new(uint256.Int).SetUint64(uint64(scope.Memory.Len()))) 564 return nil, nil 565 } 566 567 func opGas(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 568 scope.Stack.push(new(uint256.Int).SetUint64(scope.Contract.Gas)) 569 return nil, nil 570 } 571 572 func opCreate(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 573 var ( 574 value = scope.Stack.pop() 575 offset, size = scope.Stack.pop(), scope.Stack.pop() 576 input = scope.Memory.GetCopy(int64(offset.Uint64()), int64(size.Uint64())) 577 gas = scope.Contract.Gas 578 ) 579 if interpreter.evm.chainRules.IsEIP150 { 580 gas -= gas / 64 581 } 582 // reuse size int for stackvalue 583 stackvalue := size 584 585 scope.Contract.UseGas(gas) 586 //TODO: use uint256.Int instead of converting with toBig() 587 var bigVal = big0 588 if !value.IsZero() { 589 bigVal = value.ToBig() 590 } 591 592 res, addr, returnGas, suberr := interpreter.evm.Create(scope.Contract, input, gas, bigVal) 593 // Push item on the stack based on the returned error. If the ruleset is 594 // homestead we must check for CodeStoreOutOfGasError (homestead only 595 // rule) and treat as an error, if the ruleset is frontier we must 596 // ignore this error and pretend the operation was successful. 597 if interpreter.evm.chainRules.IsHomestead && suberr == ErrCodeStoreOutOfGas { 598 stackvalue.Clear() 599 } else if suberr != nil && suberr != ErrCodeStoreOutOfGas { 600 stackvalue.Clear() 601 } else { 602 stackvalue.SetBytes(addr.Bytes()) 603 } 604 scope.Stack.push(&stackvalue) 605 scope.Contract.Gas += returnGas 606 607 if suberr == ErrExecutionReverted { 608 return res, nil 609 } 610 return nil, nil 611 } 612 613 func opCreate2(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 614 var ( 615 endowment = scope.Stack.pop() 616 offset, size = scope.Stack.pop(), scope.Stack.pop() 617 salt = scope.Stack.pop() 618 input = scope.Memory.GetCopy(int64(offset.Uint64()), int64(size.Uint64())) 619 gas = scope.Contract.Gas 620 ) 621 622 // Apply EIP150 623 gas -= gas / 64 624 scope.Contract.UseGas(gas) 625 // reuse size int for stackvalue 626 stackvalue := size 627 //TODO: use uint256.Int instead of converting with toBig() 628 bigEndowment := big0 629 if !endowment.IsZero() { 630 bigEndowment = endowment.ToBig() 631 } 632 res, addr, returnGas, suberr := interpreter.evm.Create2(scope.Contract, input, gas, 633 bigEndowment, &salt) 634 // Push item on the stack based on the returned error. 635 if suberr != nil { 636 stackvalue.Clear() 637 } else { 638 stackvalue.SetBytes(addr.Bytes()) 639 } 640 scope.Stack.push(&stackvalue) 641 scope.Contract.Gas += returnGas 642 643 if suberr == ErrExecutionReverted { 644 return res, nil 645 } 646 return nil, nil 647 } 648 649 func opCall(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 650 stack := scope.Stack 651 // Pop gas. The actual gas in interpreter.evm.callGasTemp. 652 // We can use this as a temporary value 653 temp := stack.pop() 654 gas := interpreter.evm.callGasTemp 655 // Pop other call parameters. 656 addr, value, inOffset, inSize, retOffset, retSize := stack.pop(), stack.pop(), stack.pop(), stack.pop(), stack.pop(), stack.pop() 657 toAddr := common.Address(addr.Bytes20()) 658 // Get the arguments from the memory. 659 args := scope.Memory.GetPtr(int64(inOffset.Uint64()), int64(inSize.Uint64())) 660 661 var bigVal = big0 662 //TODO: use uint256.Int instead of converting with toBig() 663 // By using big0 here, we save an alloc for the most common case (non-ether-transferring contract calls), 664 // but it would make more sense to extend the usage of uint256.Int 665 if !value.IsZero() { 666 gas += params.CallStipend 667 bigVal = value.ToBig() 668 } 669 670 ret, returnGas, err := interpreter.evm.Call(scope.Contract, toAddr, args, gas, bigVal) 671 672 if err != nil { 673 temp.Clear() 674 } else { 675 temp.SetOne() 676 } 677 stack.push(&temp) 678 if err == nil || err == ErrExecutionReverted { 679 ret = common.CopyBytes(ret) 680 scope.Memory.Set(retOffset.Uint64(), retSize.Uint64(), ret) 681 } 682 scope.Contract.Gas += returnGas 683 684 return ret, nil 685 } 686 687 func opCallCode(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 688 // Pop gas. The actual gas is in interpreter.evm.callGasTemp. 689 stack := scope.Stack 690 // We use it as a temporary value 691 temp := stack.pop() 692 gas := interpreter.evm.callGasTemp 693 // Pop other call parameters. 694 addr, value, inOffset, inSize, retOffset, retSize := stack.pop(), stack.pop(), stack.pop(), stack.pop(), stack.pop(), stack.pop() 695 toAddr := common.Address(addr.Bytes20()) 696 // Get arguments from the memory. 697 args := scope.Memory.GetPtr(int64(inOffset.Uint64()), int64(inSize.Uint64())) 698 699 //TODO: use uint256.Int instead of converting with toBig() 700 var bigVal = big0 701 if !value.IsZero() { 702 gas += params.CallStipend 703 bigVal = value.ToBig() 704 } 705 706 ret, returnGas, err := interpreter.evm.CallCode(scope.Contract, toAddr, args, gas, bigVal) 707 if err != nil { 708 temp.Clear() 709 } else { 710 temp.SetOne() 711 } 712 stack.push(&temp) 713 if err == nil || err == ErrExecutionReverted { 714 ret = common.CopyBytes(ret) 715 scope.Memory.Set(retOffset.Uint64(), retSize.Uint64(), ret) 716 } 717 scope.Contract.Gas += returnGas 718 719 return ret, nil 720 } 721 722 func opDelegateCall(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 723 stack := scope.Stack 724 // Pop gas. The actual gas is in interpreter.evm.callGasTemp. 725 // We use it as a temporary value 726 temp := stack.pop() 727 gas := interpreter.evm.callGasTemp 728 // Pop other call parameters. 729 addr, inOffset, inSize, retOffset, retSize := stack.pop(), stack.pop(), stack.pop(), stack.pop(), stack.pop() 730 toAddr := common.Address(addr.Bytes20()) 731 // Get arguments from the memory. 732 args := scope.Memory.GetPtr(int64(inOffset.Uint64()), int64(inSize.Uint64())) 733 734 ret, returnGas, err := interpreter.evm.DelegateCall(scope.Contract, toAddr, args, gas) 735 if err != nil { 736 temp.Clear() 737 } else { 738 temp.SetOne() 739 } 740 stack.push(&temp) 741 if err == nil || err == ErrExecutionReverted { 742 ret = common.CopyBytes(ret) 743 scope.Memory.Set(retOffset.Uint64(), retSize.Uint64(), ret) 744 } 745 scope.Contract.Gas += returnGas 746 747 return ret, nil 748 } 749 750 func opStaticCall(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 751 // Pop gas. The actual gas is in interpreter.evm.callGasTemp. 752 stack := scope.Stack 753 // We use it as a temporary value 754 temp := stack.pop() 755 gas := interpreter.evm.callGasTemp 756 // Pop other call parameters. 757 addr, inOffset, inSize, retOffset, retSize := stack.pop(), stack.pop(), stack.pop(), stack.pop(), stack.pop() 758 toAddr := common.Address(addr.Bytes20()) 759 // Get arguments from the memory. 760 args := scope.Memory.GetPtr(int64(inOffset.Uint64()), int64(inSize.Uint64())) 761 762 ret, returnGas, err := interpreter.evm.StaticCall(scope.Contract, toAddr, args, gas) 763 if err != nil { 764 temp.Clear() 765 } else { 766 temp.SetOne() 767 } 768 stack.push(&temp) 769 if err == nil || err == ErrExecutionReverted { 770 ret = common.CopyBytes(ret) 771 scope.Memory.Set(retOffset.Uint64(), retSize.Uint64(), ret) 772 } 773 scope.Contract.Gas += returnGas 774 775 return ret, nil 776 } 777 778 func opReturn(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 opRevert(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 786 offset, size := scope.Stack.pop(), scope.Stack.pop() 787 ret := scope.Memory.GetPtr(int64(offset.Uint64()), int64(size.Uint64())) 788 789 return ret, nil 790 } 791 792 func opStop(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 793 return nil, nil 794 } 795 796 func opSuicide(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 797 beneficiary := scope.Stack.pop() 798 balance := interpreter.evm.StateDB.GetBalance(scope.Contract.Address()) 799 interpreter.evm.StateDB.AddBalance(beneficiary.Bytes20(), balance) 800 interpreter.evm.StateDB.Suicide(scope.Contract.Address()) 801 if interpreter.cfg.Debug { 802 interpreter.cfg.Tracer.CaptureEnter(SELFDESTRUCT, scope.Contract.Address(), beneficiary.Bytes20(), []byte{}, 0, balance) 803 interpreter.cfg.Tracer.CaptureExit([]byte{}, 0, nil) 804 } 805 return nil, nil 806 } 807 808 // following functions are used by the instruction jump table 809 810 // make log instruction function 811 func makeLog(size int) executionFunc { 812 return func(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 813 topics := make([]common.Hash, size) 814 stack := scope.Stack 815 mStart, mSize := stack.pop(), stack.pop() 816 for i := 0; i < size; i++ { 817 addr := stack.pop() 818 topics[i] = addr.Bytes32() 819 } 820 821 d := scope.Memory.GetCopy(int64(mStart.Uint64()), int64(mSize.Uint64())) 822 interpreter.evm.StateDB.AddLog(&types.Log{ 823 Address: scope.Contract.Address(), 824 Topics: topics, 825 Data: d, 826 // This is a non-consensus field, but assigned here because 827 // core/state doesn't know the current block number. 828 BlockNumber: interpreter.evm.Context.BlockNumber.Uint64(), 829 }) 830 831 return nil, nil 832 } 833 } 834 835 // opPush1 is a specialized version of pushN 836 func opPush1(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 837 var ( 838 codeLen = uint64(len(scope.Contract.Code)) 839 integer = new(uint256.Int) 840 ) 841 *pc += 1 842 if *pc < codeLen { 843 scope.Stack.push(integer.SetUint64(uint64(scope.Contract.Code[*pc]))) 844 } else { 845 scope.Stack.push(integer.Clear()) 846 } 847 return nil, nil 848 } 849 850 // make push instruction function 851 func makePush(size uint64, pushByteSize int) executionFunc { 852 return func(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 853 codeLen := len(scope.Contract.Code) 854 855 startMin := codeLen 856 if int(*pc+1) < startMin { 857 startMin = int(*pc + 1) 858 } 859 860 endMin := codeLen 861 if startMin+pushByteSize < endMin { 862 endMin = startMin + pushByteSize 863 } 864 865 integer := new(uint256.Int) 866 scope.Stack.push(integer.SetBytes(common.RightPadBytes( 867 scope.Contract.Code[startMin:endMin], pushByteSize))) 868 869 *pc += size 870 return nil, nil 871 } 872 } 873 874 // make dup instruction function 875 func makeDup(size int64) executionFunc { 876 return func(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 877 scope.Stack.dup(int(size)) 878 return nil, nil 879 } 880 } 881 882 // make swap instruction function 883 func makeSwap(size int64) executionFunc { 884 // switch n + 1 otherwise n would be swapped with n 885 size++ 886 return func(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 887 scope.Stack.swap(int(size)) 888 return nil, nil 889 } 890 }