Skip to content

Commit

Permalink
fix: host func casting
Browse files Browse the repository at this point in the history
  • Loading branch information
danielvladco committed Nov 27, 2024
1 parent 5b7a042 commit c3906f8
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 59 deletions.
28 changes: 14 additions & 14 deletions internal/polkavm/host_call/accumulate_functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ func Bless(gas Gas, regs Registers, mem Memory, ctxPair AccumulateContextPair) (
// let [m, a, v, o, n] = ω7...12
managerServiceId, assignServiceId, designateServiceId, addr, servicesNr := regs[A0], regs[A1], regs[A2], regs[A3], regs[A4]
// let g = {(s ↦ g) where E4(s) ⌢ E8(g) = μ_o+12i⋅⋅⋅+12 | i ∈ Nn} if Zo⋅⋅⋅+12n ⊂ Vμ otherwise ∇
for i := range servicesNr {
serviceId, err := readNumber[block.ServiceId](mem, addr+(12*i), 4)
for i := range uint32(servicesNr) {
serviceId, err := readNumber[block.ServiceId](mem, uint32(addr)+(12*i), 4)
if err != nil {
return gas, withCode(regs, OOB), mem, ctxPair, err
}
serviceGas, err := readNumber[uint64](mem, addr+(12*i)+4, 8)
serviceGas, err := readNumber[uint64](mem, uint32(addr)+(12*i)+4, 8)
if err != nil {
return gas, withCode(regs, OOB), mem, ctxPair, err
}
Expand All @@ -54,12 +54,12 @@ func Assign(gas Gas, regs Registers, mem Memory, ctxPair AccumulateContextPair)
// let o = ω8
addr := regs[A1]
core := regs[A0]
if core >= uint32(common.TotalNumberOfCores) {
if core >= uint64(common.TotalNumberOfCores) {
return gas, withCode(regs, CORE), mem, ctxPair, nil
}
for i := 0; i < state.PendingAuthorizersQueueSize; i++ {
bytes := make([]byte, 32)
if err := mem.Read(addr+uint32(32*i), bytes); err != nil {
if err := mem.Read(uint32(addr)+uint32(32*i), bytes); err != nil {
return gas, withCode(regs, OOB), mem, ctxPair, nil
}
ctxPair.RegularCtx.AccumulationState.WorkReportsQueue[core][i] = crypto.Hash(bytes)
Expand All @@ -84,7 +84,7 @@ func Designate(gas Gas, regs Registers, mem Memory, ctxPair AccumulateContextPai
addr := regs[A0]
for i := 0; i < common.NumberOfValidators; i++ {
bytes := make([]byte, 336)
if err := mem.Read(addr+uint32(336*i), bytes); err != nil {
if err := mem.Read(uint32(addr)+uint32(336*i), bytes); err != nil {
return gas, withCode(regs, OOB), mem, ctxPair, nil
}

Expand All @@ -109,7 +109,7 @@ func Checkpoint(gas Gas, regs Registers, mem Memory, ctxPair AccumulateContextPa
ctxPair.ExceptionalCtx = ctxPair.RegularCtx

// Set the new ϱ' value into ω′7
regs[A0] = uint32(gas & ((1 << 32) - 1))
regs[A0] = uint64(gas & ((1 << 32) - 1))

return gas, regs, mem, ctxPair, nil
}
Expand All @@ -126,7 +126,7 @@ func New(gas Gas, regs Registers, mem Memory, ctxPair AccumulateContextPair) (Ga

// c = μo⋅⋅⋅+32 if No⋅⋅⋅+32 ⊂ Vμ otherwise ∇
codeHashBytes := make([]byte, 32)
if err := mem.Read(addr, codeHashBytes); err != nil {
if err := mem.Read(uint32(addr), codeHashBytes); err != nil {
return gas, withCode(regs, OOB), mem, ctxPair, nil
}

Expand All @@ -149,7 +149,7 @@ func New(gas Gas, regs Registers, mem Memory, ctxPair AccumulateContextPair) (Ga

// if a ≠ ∇ ∧ b ≥ (xs)t
if b >= ctxPair.RegularCtx.ServiceAccount().ThresholdBalance() {
regs[A0] = uint32(ctxPair.RegularCtx.ServiceId)
regs[A0] = uint64(ctxPair.RegularCtx.ServiceId)
currentAccount := ctxPair.RegularCtx.ServiceAccount()
currentAccount.Balance = b
ctxPair.RegularCtx.ServiceState[ctxPair.RegularCtx.ServiceId] = currentAccount
Expand All @@ -176,7 +176,7 @@ func Upgrade(gas Gas, regs Registers, mem Memory, ctxPair AccumulateContextPair)

// c = μo⋅⋅⋅+32 if No⋅⋅⋅+32 ⊂ Vμ otherwise ∇
codeHash := make([]byte, 32)
if err := mem.Read(addr, codeHash); err != nil {
if err := mem.Read(uint32(addr), codeHash); err != nil {
return gas, withCode(regs, OOB), mem, ctxPair, nil
}

Expand All @@ -202,7 +202,7 @@ func Transfer(gas Gas, regs Registers, mem Memory, ctxPair AccumulateContextPair

// m = μo⋅⋅⋅+M if No⋅⋅⋅+M ⊂ Vμ otherwise ∇
m := make([]byte, service.TransferMemoSizeBytes)
if err := mem.Read(o, m); err != nil {
if err := mem.Read(uint32(o), m); err != nil {
return gas, withCode(regs, OK), mem, ctxPair, nil
}

Expand Down Expand Up @@ -263,7 +263,7 @@ func Quit(gas Gas, regs Registers, mem Memory, ctxPair AccumulateContextPair) (G

// m = E−1(μo⋅⋅⋅+M)
memo := make([]byte, service.TransferMemoSizeBytes)
if err := mem.Read(addr, memo); err != nil {
if err := mem.Read(uint32(addr), memo); err != nil {
return gas, withCode(regs, OOB), mem, ctxPair, nil
}

Expand Down Expand Up @@ -309,7 +309,7 @@ func Solicit(gas Gas, regs Registers, mem Memory, ctxPair AccumulateContextPair,
addr, preimageLength := regs[A0], regs[A1]
// let h = μo⋅⋅⋅+32 if Zo⋅⋅⋅+32 ⊂ Vμ otherwise ∇
preimageHashBytes := make([]byte, 32)
if err := mem.Read(addr, preimageHashBytes); err != nil {
if err := mem.Read(uint32(addr), preimageHashBytes); err != nil {
return gas, withCode(regs, OOB), mem, ctxPair, nil
}

Expand Down Expand Up @@ -350,7 +350,7 @@ func Forget(gas Gas, regs Registers, mem Memory, ctxPair AccumulateContextPair,

// let h = μo⋅⋅⋅+32 if Zo⋅⋅⋅+32 ⊂ Vμ otherwise ∇
preimageHashBytes := make([]byte, 32)
if err := mem.Read(addr, preimageHashBytes); err != nil {
if err := mem.Read(uint32(addr), preimageHashBytes); err != nil {
return gas, withCode(regs, OOB), mem, ctxPair, nil
}

Expand Down
24 changes: 12 additions & 12 deletions internal/polkavm/host_call/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,19 @@ const (
ForgetID = 14
)

type Code uint32
type Code uint64

const (
NONE Code = math.MaxUint32
WHAT Code = math.MaxUint32 - 1
OOB Code = math.MaxUint32 - 2
WHO Code = math.MaxUint32 - 3
FULL Code = math.MaxUint32 - 4
CORE Code = math.MaxUint32 - 5
CASH Code = math.MaxUint32 - 6
LOW Code = math.MaxUint32 - 7
HIGH Code = math.MaxUint32 - 8
HUH Code = math.MaxUint32 - 9
NONE Code = math.MaxUint64
WHAT Code = math.MaxUint64 - 1
OOB Code = math.MaxUint64 - 2
WHO Code = math.MaxUint64 - 3
FULL Code = math.MaxUint64 - 4
CORE Code = math.MaxUint64 - 5
CASH Code = math.MaxUint64 - 6
LOW Code = math.MaxUint64 - 7
HIGH Code = math.MaxUint64 - 8
HUH Code = math.MaxUint64 - 9
OK Code = 0
)

Expand Down Expand Up @@ -98,6 +98,6 @@ func readNumber[U interface{ ~uint32 | ~uint64 }](mem Memory, addr uint32, lengt
}

func withCode(regs Registers, s Code) Registers {
regs[A0] = uint32(s)
regs[A0] = uint64(s)
return regs
}
53 changes: 20 additions & 33 deletions internal/polkavm/host_call/general_functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func GasRemaining(gas polkavm.Gas, regs polkavm.Registers) (polkavm.Gas, polkavm
gas -= GasRemainingCost

// Set the new ϱ' value into ω′7
regs[polkavm.A0] = uint32(gas)
regs[polkavm.A0] = uint64(gas)

return gas, regs, nil
}
Expand All @@ -44,12 +44,12 @@ func Lookup(gas polkavm.Gas, regs polkavm.Registers, mem polkavm.Memory, s servi

// Determine the lookup key 'a'
a := s
if uint64(omega7) != math.MaxUint64 && omega7 != uint32(serviceId) {
if uint64(omega7) != math.MaxUint64 && omega7 != uint64(serviceId) {
var exists bool
// Lookup service account by serviceId in the serviceState
a, exists = serviceState[serviceId]
if !exists {
regs[polkavm.A0] = uint32(NONE)
regs[polkavm.A0] = uint64(NONE)
return gas, regs, mem, nil
}
}
Expand All @@ -60,8 +60,7 @@ func Lookup(gas polkavm.Gas, regs polkavm.Registers, mem polkavm.Memory, s servi
memorySlice := make([]byte, 32)
err := mem.Read(ho, memorySlice)

Check failure on line 61 in internal/polkavm/host_call/general_functions.go

View workflow job for this annotation

GitHub Actions / Lint

cannot use ho (variable of type uint64) as uint32 value in argument to mem.Read
if err != nil {
regs[polkavm.A0] = uint32(OOB)
return gas, regs, mem, err
return gas, withCode(regs, OOB), mem, err
}

// Compute the hash H(µho..ho+32)
Expand All @@ -70,8 +69,7 @@ func Lookup(gas polkavm.Gas, regs polkavm.Registers, mem polkavm.Memory, s servi
// Lookup value in storage (v) using the hash
v, exists := a.Storage[hash]
if !exists {
regs[polkavm.A0] = uint32(NONE)
return gas, regs, mem, nil
return gas, withCode(regs, NONE), mem, nil
}

bo := regs[polkavm.A2]
Expand All @@ -80,15 +78,13 @@ func Lookup(gas polkavm.Gas, regs polkavm.Registers, mem polkavm.Memory, s servi
// Write value to memory if within bounds
if len(v) > 0 && len(v) <= int(bz) {
if err = mem.Write(bo, v); err != nil {

Check failure on line 80 in internal/polkavm/host_call/general_functions.go

View workflow job for this annotation

GitHub Actions / Lint

cannot use bo (variable of type uint64) as uint32 value in argument to mem.Write
regs[polkavm.A0] = uint32(OOB)
return gas, regs, mem, err
return gas, withCode(regs, OOB), mem, err
}
} else {
regs[polkavm.A0] = uint32(OOB)
return gas, regs, mem, err
return gas, withCode(regs, OOB), mem, err
}

regs[polkavm.A0] = uint32(len(v))
regs[polkavm.A0] = uint64(len(v))
return gas, regs, mem, err
}

Expand All @@ -106,7 +102,7 @@ func Read(gas polkavm.Gas, regs polkavm.Registers, mem polkavm.Memory, s service
bz := regs[polkavm.A4]

a := s
if uint64(omega7) != math.MaxUint64 && omega7 != uint32(serviceId) {
if uint64(omega7) != math.MaxUint64 && omega7 != uint64(serviceId) {
var exists bool
a, exists = serviceState[block.ServiceId(omega7)]
if !exists {
Expand All @@ -118,8 +114,7 @@ func Read(gas polkavm.Gas, regs polkavm.Registers, mem polkavm.Memory, s service
keyData := make([]byte, kz)
err := mem.Read(ko, keyData)

Check failure on line 115 in internal/polkavm/host_call/general_functions.go

View workflow job for this annotation

GitHub Actions / Lint

cannot use ko (variable of type uint64) as uint32 value in argument to mem.Read
if err != nil {
regs[polkavm.A0] = uint32(OOB)
return gas, regs, mem, nil
return gas, withCode(regs, OOB), mem, nil
}

serviceIdBytes, err := jam.Marshal(omega7)
Expand All @@ -137,24 +132,21 @@ func Read(gas polkavm.Gas, regs polkavm.Registers, mem polkavm.Memory, s service

v, exists := a.Storage[k]
if !exists {
regs[polkavm.A0] = uint32(NONE)
return gas, regs, mem, nil
return gas, withCode(regs, NONE), mem, nil
}

writeLen := int(math.Min(float64(bz), float64(len(v))))

if writeLen > 0 {
if err = mem.Write(bo, v[:writeLen]); err != nil {

Check failure on line 141 in internal/polkavm/host_call/general_functions.go

View workflow job for this annotation

GitHub Actions / Lint

cannot use bo (variable of type uint64) as uint32 value in argument to mem.Write
regs[polkavm.A0] = uint32(OOB)
return gas, regs, mem, nil
return gas, withCode(regs, OOB), mem, nil
}

regs[polkavm.A0] = uint32(len(v))
regs[polkavm.A0] = uint64(len(v))
return gas, regs, mem, nil
}

regs[polkavm.A0] = uint32(NONE)
return gas, regs, mem, nil
return gas, withCode(regs, NONE), mem, nil
}

// Write ΩW(ϱ, ω, μ, s, s)
Expand Down Expand Up @@ -189,26 +181,23 @@ func Write(gas polkavm.Gas, regs polkavm.Registers, mem polkavm.Memory, s servic
valueData := make([]byte, vz)
err := mem.Read(vo, valueData)

Check failure on line 182 in internal/polkavm/host_call/general_functions.go

View workflow job for this annotation

GitHub Actions / Lint

cannot use vo (variable of type uint64) as uint32 value in argument to mem.Read
if err != nil {
regs[polkavm.A0] = uint32(OOB)
return gas, regs, mem, s, err
return gas, withCode(regs, OOB), mem, s, err
}

a.Storage[k] = valueData
}

storageItem, ok := s.Storage[k]
if !ok {
regs[polkavm.A0] = uint32(NONE)
return gas, regs, mem, s, err
return gas, withCode(regs, NONE), mem, s, err
}

if a.ThresholdBalance() > a.Balance {
regs[polkavm.A0] = uint32(FULL)
return gas, regs, mem, s, err
return gas, withCode(regs, FULL), mem, s, err
}

// otherwise a.ThresholdBalance() <= a.Balance
regs[polkavm.A0] = uint32(len(storageItem)) // l
regs[polkavm.A0] = uint64(len(storageItem)) // l
return gas, regs, mem, a, err // return service account 'a' as opposed to 's' for not successful paths
}

Expand Down Expand Up @@ -247,10 +236,8 @@ func Info(gas polkavm.Gas, regs polkavm.Registers, mem polkavm.Memory, serviceId
}

if err := mem.Write(omega8, m); err != nil {

Check failure on line 238 in internal/polkavm/host_call/general_functions.go

View workflow job for this annotation

GitHub Actions / Lint

cannot use omega8 (variable of type uint64) as uint32 value in argument to mem.Write
regs[polkavm.A0] = uint32(OOB)
return gas, regs, mem, nil
return gas, withCode(regs, OOB), mem, nil
}

regs[polkavm.A0] = uint32(OK)
return gas, regs, mem, nil
return gas, withCode(regs, OK), mem, nil
}

0 comments on commit c3906f8

Please sign in to comment.