Skip to content

Commit

Permalink
Merge pull request #2114 from CortexFoundation/dev
Browse files Browse the repository at this point in the history
core/vm: use uint64 in memory for indices everywhere
  • Loading branch information
ucwong authored Aug 12, 2024
2 parents 260dd8e + ba0f7bb commit bdb1b68
Show file tree
Hide file tree
Showing 83 changed files with 161,011 additions and 8,991 deletions.
14 changes: 7 additions & 7 deletions core/vm/eips.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,9 @@ func enable1884(jt *JumpTable) {
}
}

func opSelfBalance(pc *uint64, interpreter *CVMInterpreter, callContext *ScopeContext) ([]byte, error) {
balance, _ := uint256.FromBig(interpreter.cvm.StateDB.GetBalance(callContext.Contract.Address()))
callContext.Stack.push(balance)
func opSelfBalance(pc *uint64, interpreter *CVMInterpreter, scope *ScopeContext) ([]byte, error) {
balance, _ := uint256.FromBig(interpreter.cvm.StateDB.GetBalance(scope.Contract.Address()))
scope.Stack.push(balance)
return nil, nil
}

Expand All @@ -112,9 +112,9 @@ func enable1344(jt *JumpTable) {
}

// opChainID implements CHAINID opcode
func opChainID(pc *uint64, interpreter *CVMInterpreter, callContext *ScopeContext) ([]byte, error) {
func opChainID(pc *uint64, interpreter *CVMInterpreter, scope *ScopeContext) ([]byte, error) {
chainId, _ := uint256.FromBig(interpreter.cvm.chainConfig.ChainID)
callContext.Stack.push(chainId)
scope.Stack.push(chainId)
return nil, nil
}

Expand All @@ -135,7 +135,7 @@ func enable3855(jt *JumpTable) {
}

// opPush0 implements the PUSH0 opcode
func opPush0(pc *uint64, interpreter *CVMInterpreter, callContext *ScopeContext) ([]byte, error) {
callContext.Stack.push(new(uint256.Int))
func opPush0(pc *uint64, interpreter *CVMInterpreter, scope *ScopeContext) ([]byte, error) {
scope.Stack.push(new(uint256.Int))
return nil, nil
}
474 changes: 237 additions & 237 deletions core/vm/instructions.go

Large diffs are not rendered by default.

44 changes: 22 additions & 22 deletions core/vm/instructions_infer.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,30 +28,30 @@ import (
"github.com/CortexFoundation/CortexTheseus/params"
)

func opInfer(pc *uint64, interpreter *CVMInterpreter, callContext *ScopeContext) ([]byte, error) {
_modelAddr, _inputAddr, _outputOffset := callContext.Stack.pop(), callContext.Stack.pop(), callContext.Stack.pop()
func opInfer(pc *uint64, interpreter *CVMInterpreter, scope *ScopeContext) ([]byte, error) {
_modelAddr, _inputAddr, _outputOffset := scope.Stack.pop(), scope.Stack.pop(), scope.Stack.pop()
modelAddr := common.Address(_modelAddr.Bytes20())
inputAddr := common.Address(_inputAddr.Bytes20())
var (
modelMeta *torrentfs.ModelMeta
inputMeta *torrentfs.InputMeta
)
modelMeta, modelErr := checkModel(interpreter.cvm, callContext.Stack, modelAddr)
modelMeta, modelErr := checkModel(interpreter.cvm, scope.Stack, modelAddr)
if modelErr != nil {
callContext.Stack.push(new(uint256.Int).Clear())
scope.Stack.push(new(uint256.Int).Clear())
return nil, modelErr
}

inputMeta, inputErr := checkInputMeta(interpreter.cvm, callContext.Stack, inputAddr)
inputMeta, inputErr := checkInputMeta(interpreter.cvm, scope.Stack, inputAddr)
if inputErr != nil {
callContext.Stack.push(new(uint256.Int).Clear())
scope.Stack.push(new(uint256.Int).Clear())
return nil, inputErr
}

log.Debug("interpreter check shape 1", "modelMeta", modelMeta, "inputMeta", inputMeta)
// Model&Input shape should match
if len(modelMeta.InputShape) != len(inputMeta.Shape) {
callContext.Stack.push(new(uint256.Int).Clear())
scope.Stack.push(new(uint256.Int).Clear())
if interpreter.cvm.vmConfig.DebugInferVM {
fmt.Println("modelmeta: ", modelMeta.InputShape, " inputmeta: ", inputMeta.Shape)
}
Expand All @@ -60,7 +60,7 @@ func opInfer(pc *uint64, interpreter *CVMInterpreter, callContext *ScopeContext)
log.Debug("interpreter check shape 2", "modelMeta", modelMeta, "inputMeta", inputMeta)
for idx, modelShape := range modelMeta.InputShape {
if modelShape != inputMeta.Shape[idx] || modelShape == 0 || inputMeta.Shape[idx] == 0 {
callContext.Stack.push(new(uint256.Int).Clear())
scope.Stack.push(new(uint256.Int).Clear())
if interpreter.cvm.vmConfig.DebugInferVM {
fmt.Println("modelmeta: ", modelMeta.InputShape, " inputmeta: ", inputMeta.Shape)
}
Expand All @@ -74,14 +74,14 @@ func opInfer(pc *uint64, interpreter *CVMInterpreter, callContext *ScopeContext)
fmt.Println("DebugInferVM ", "output: ", output, " err: ", err, "model = ", modelMeta.Hash.Hex(), "input = ", inputMeta.Hash.Hex())
}
if err != nil {
callContext.Stack.push(new(uint256.Int).Clear())
scope.Stack.push(new(uint256.Int).Clear())
return nil, err
}
if err := callContext.Memory.WriteSolidityUint256Array(int64(_outputOffset.Uint64()), output); err != nil {
callContext.Stack.push(new(uint256.Int).Clear())
if err := scope.Memory.WriteSolidityUint256Array(int64(_outputOffset.Uint64()), output); err != nil {
scope.Stack.push(new(uint256.Int).Clear())
return nil, err
}
callContext.Stack.push(new(uint256.Int).SetOne())
scope.Stack.push(new(uint256.Int).SetOne())

return nil, nil
}
Expand Down Expand Up @@ -151,20 +151,20 @@ func checkInputMeta(cvm *CVM, stack *Stack, inputAddr common.Address) (*torrentf
return inputMeta, nil
}

func opInferArray(pc *uint64, interpreter *CVMInterpreter, callContext *ScopeContext) ([]byte, error) {
_modelAddr, _inputHeaderOffset, _outputOffset := callContext.Stack.pop(), callContext.Stack.pop(), callContext.Stack.pop()
func opInferArray(pc *uint64, interpreter *CVMInterpreter, scope *ScopeContext) ([]byte, error) {
_modelAddr, _inputHeaderOffset, _outputOffset := scope.Stack.pop(), scope.Stack.pop(), scope.Stack.pop()
// fmt.Println(fmt.Sprintf("%d, %d, %d", _modelAddr, _inputHeaderOffset, _outputOffset))
inputBuff, inputError := interpreter.cvm.StateDB.GetSolidityBytes(callContext.Contract.Address(), common.Hash(_inputHeaderOffset.Bytes32()))
inputBuff, inputError := interpreter.cvm.StateDB.GetSolidityBytes(scope.Contract.Address(), common.Hash(_inputHeaderOffset.Bytes32()))
if inputError != nil {
return nil, inputError
}
inputSize := uint256.NewInt(uint64(len(inputBuff)))
modelAddr := common.Address(_modelAddr.Bytes20())
// log.Debug(fmt.Sprintf("_input = %v, payload = %v ", inputSize, inputBuff))

modelMeta, modelErr := checkModel(interpreter.cvm, callContext.Stack, modelAddr)
modelMeta, modelErr := checkModel(interpreter.cvm, scope.Stack, modelAddr)
if modelErr != nil {
callContext.Stack.push(new(uint256.Int).Clear())
scope.Stack.push(new(uint256.Int).Clear())
return nil, modelErr
}

Expand All @@ -175,7 +175,7 @@ func opInferArray(pc *uint64, interpreter *CVMInterpreter, callContext *ScopeCon
dataSize *= modelShape
}
if dataSize != inputSize.Uint64() {
callContext.Stack.push(new(uint256.Int).Clear())
scope.Stack.push(new(uint256.Int).Clear())
if interpreter.cvm.vmConfig.DebugInferVM {
fmt.Println("modelmeta: ", modelMeta.InputShape, "datasize: ", dataSize, "inputSize: ", inputSize)
}
Expand All @@ -189,18 +189,18 @@ func opInferArray(pc *uint64, interpreter *CVMInterpreter, callContext *ScopeCon
output, err = interpreter.cvm.InferArray(modelMeta.Hash.Hex(),
inputBuff, modelMeta.RawSize)
if err != nil {
callContext.Stack.push(new(uint256.Int).Clear())
scope.Stack.push(new(uint256.Int).Clear())
return nil, err
}
if interpreter.cvm.vmConfig.DebugInferVM {
fmt.Println("output", output)
}
if err := callContext.Memory.WriteSolidityUint256Array(int64(_outputOffset.Uint64()), output); err != nil {
callContext.Stack.push(new(uint256.Int).Clear())
if err := scope.Memory.WriteSolidityUint256Array(int64(_outputOffset.Uint64()), output); err != nil {
scope.Stack.push(new(uint256.Int).Clear())
return nil, err
}
// interpreter.intPool.get().SetUint64
callContext.Stack.push(new(uint256.Int).SetOne())
scope.Stack.push(new(uint256.Int).SetOne())

return nil, nil
}
16 changes: 8 additions & 8 deletions core/vm/interpreter.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,10 +200,10 @@ func (in *CVMInterpreter) Run(contract *Contract, input []byte, readOnly bool) (
}

var (
op OpCode // current opcode
mem = NewMemory() // bound memory
stack = newstack() // local stack
callContext = &ScopeContext{
op OpCode // current opcode
mem = NewMemory() // bound memory
stack = newstack() // local stack
scope = &ScopeContext{
Memory: mem,
Stack: stack,
Contract: contract,
Expand Down Expand Up @@ -233,9 +233,9 @@ func (in *CVMInterpreter) Run(contract *Contract, input []byte, readOnly bool) (
defer func() {
if err != nil {
if !logged {
in.cvm.Config().Tracer.CaptureState(pcCopy, op, gasCopy, cost, callContext, in.returnData, in.cvm.depth, err)
in.cvm.Config().Tracer.CaptureState(pcCopy, op, gasCopy, cost, scope, in.returnData, in.cvm.depth, err)
} else {
in.cvm.Config().Tracer.CaptureFault(pcCopy, op, gasCopy, cost, callContext, in.cvm.depth, err)
in.cvm.Config().Tracer.CaptureFault(pcCopy, op, gasCopy, cost, scope, in.cvm.depth, err)
}
}
}()
Expand Down Expand Up @@ -325,12 +325,12 @@ func (in *CVMInterpreter) Run(contract *Contract, input []byte, readOnly bool) (
}

if debug {
in.cvm.Config().Tracer.CaptureState(pc, op, gasCopy, cost, callContext, in.returnData, in.cvm.depth, err)
in.cvm.Config().Tracer.CaptureState(pc, op, gasCopy, cost, scope, in.returnData, in.cvm.depth, err)
logged = true
}

// execute the operation
ret, err = operation.execute(&pc, in, callContext)
ret, err = operation.execute(&pc, in, scope)
if in.cvm.Config().RPC_GetInternalTransaction {
if op == CALL {
res = append(res, ret...)
Expand Down
2 changes: 1 addition & 1 deletion core/vm/jump_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
)

type (
executionFunc func(pc *uint64, interpreter *CVMInterpreter, callContext *ScopeContext) ([]byte, error)
executionFunc func(pc *uint64, interpreter *CVMInterpreter, scope *ScopeContext) ([]byte, error)
gasFunc func(params.GasTable, *CVM, *Contract, *Stack, *Memory, uint64) (uint64, error) // last parameter is the requested memory size as a uint64
stackValidationFunc func(*Stack) error
memorySizeFunc func(*Stack) (size uint64, overflow bool)
Expand Down
31 changes: 12 additions & 19 deletions core/vm/memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,32 +69,25 @@ func (m *Memory) Resize(size uint64) {
}

// GetCopy returns offset + size as a new slice
func (m *Memory) GetCopy(offset, size int64) (cpy []byte) {
func (m *Memory) GetCopy(offset, size uint64) (cpy []byte) {
if size == 0 {
return nil
}

if len(m.store) > int(offset) {
cpy = make([]byte, size)
copy(cpy, m.store[offset:offset+size])

return
}

// memory is always resized before being accessed, no need to check bounds
cpy = make([]byte, size)
copy(cpy, m.store[offset:offset+size])
return
}

// GetPtr returns the offset + size
func (m *Memory) GetPtr(offset, size int64) []byte {
func (m *Memory) GetPtr(offset, size uint64) []byte {
if size == 0 {
return nil
}

if len(m.store) > int(offset) {
return m.store[offset : offset+size]
}

return nil
// memory is always resized before being accessed, no need to check bounds
return m.store[offset : offset+size]
}

// Len returns the length of the backing slice
Expand Down Expand Up @@ -124,9 +117,9 @@ func (m *Memory) Print() {

func (m *Memory) GetSolidityBytes(slot int64) ([]byte, error) {
bigLen := new(uint256.Int)
length_buff := m.GetPtr(slot, 32)
length_buff := m.GetPtr(uint64(slot), 32)
bigLen.SetBytes(length_buff)
buff := m.GetPtr(slot+32, int64(bigLen.Uint64()))
buff := m.GetPtr(uint64(slot+32), bigLen.Uint64())
return buff, nil
}

Expand All @@ -146,7 +139,7 @@ func (m* Memory) GetLengthOfSolidityUint256Array(slot int64) (uint64, error) {

func (m *Memory) WriteSolidityUint256Array(slot int64, data []byte) error {
bigLen := new(uint256.Int)
length_buff := m.GetPtr(slot, 32)
length_buff := m.GetPtr(uint64(slot), 32)
bigLen.SetBytes(length_buff)
// uint256 has 32 bytes
bigLen.Mul(bigLen, uint256.NewInt(32))
Expand All @@ -159,10 +152,10 @@ func (m *Memory) WriteSolidityUint256Array(slot int64, data []byte) error {

func (m *Memory) GetSolidityUint256(slot int64) ([]byte, error) {
bigLen := new(uint256.Int)
length_buff := m.GetPtr(slot, 32)
length_buff := m.GetPtr(uint64(slot), 32)
bigLen.SetBytes(length_buff)
bigLen.Mul(bigLen, uint256.NewInt(32))
buff := m.GetPtr(slot+32, int64(bigLen.Uint64()))
buff := m.GetPtr(uint64(slot+32), bigLen.Uint64())
return buff, nil
}

Expand Down
2 changes: 1 addition & 1 deletion ctxc/tracers/js/goja.go
Original file line number Diff line number Diff line change
Expand Up @@ -620,7 +620,7 @@ func (mo *memoryObj) getUint(addr int64) (*big.Int, error) {
if mo.memory.Len() < int(addr)+32 || addr < 0 {
return nil, fmt.Errorf("tracer accessed out of bound memory: available %d, offset %d, size %d", mo.memory.Len(), addr, 32)
}
return new(big.Int).SetBytes(mo.memory.GetPtr(addr, 32)), nil
return new(big.Int).SetBytes(mo.memory.GetPtr(uint64(addr), 32)), nil
}

func (mo *memoryObj) Length() int {
Expand Down
2 changes: 1 addition & 1 deletion ctxc/tracers/native/prestate.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ func (t *prestateTracer) CaptureState(pc uint64, op vm.OpCode, gas, cost uint64,
case stackLen >= 4 && op == vm.CREATE2:
offset := stackData[stackLen-2]
size := stackData[stackLen-3]
init := scope.Memory.GetCopy(int64(offset.Uint64()), int64(size.Uint64()))
init := scope.Memory.GetCopy(offset.Uint64(), size.Uint64())
inithash := crypto.Keccak256(init)
salt := stackData[stackLen-4]
addr := crypto.CreateAddress2(caller, salt.Bytes32(), inithash)
Expand Down
4 changes: 2 additions & 2 deletions ctxc/tracers/tracers.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,15 +83,15 @@ func GetMemoryCopyPadded(m *vm.Memory, offset, size int64) ([]byte, error) {
return nil, fmt.Errorf("offset or size must not be negative")
}
if int(offset+size) < m.Len() { // slice fully inside memory
return m.GetCopy(offset, size), nil
return m.GetCopy(uint64(offset), uint64(size)), nil
}
paddingNeeded := int(offset+size) - m.Len()
if paddingNeeded > memoryPadLimit {
return nil, fmt.Errorf("reached limit for padding memory slice: %d", paddingNeeded)
}
cpy := make([]byte, size)
if overlap := int64(m.Len()) - offset; overlap > 0 {
copy(cpy, m.GetPtr(offset, overlap))
copy(cpy, m.GetPtr(uint64(offset), uint64(overlap)))
}
return cpy, nil
}
Loading

0 comments on commit bdb1b68

Please sign in to comment.