Skip to content

Commit

Permalink
Merge pull request #3532 from onflow/fxamacker/check-cbor-tag-range-f…
Browse files Browse the repository at this point in the history
…or-cadence-internal-value-encoding

Check if CBOR tag number is reserved by atree before using it to encode Cadence values
  • Loading branch information
fxamacker authored Aug 19, 2024
2 parents 0ff758d + 3c1ac94 commit 9e055a2
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 17 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ require (
github.com/kr/pretty v0.3.1
github.com/leanovate/gopter v0.2.9
github.com/logrusorgru/aurora/v4 v4.0.0
github.com/onflow/atree v0.8.0-rc.5
github.com/onflow/atree v0.8.0-rc.6
github.com/rivo/uniseg v0.4.4
github.com/schollz/progressbar/v3 v3.13.1
github.com/stretchr/testify v1.9.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db h1:62I3jR2Em
github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db/go.mod h1:l0dey0ia/Uv7NcFFVbCLtqEBQbrT4OCwCSKTEv6enCw=
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e h1:fD57ERR4JtEqsWbfPhv4DMiApHyliiK5xCTNVSPiaAs=
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
github.com/onflow/atree v0.8.0-rc.5 h1:1sU+c6UfDzq/EjM8nTw4EI8GvEMarcxkWkJKy6piFSY=
github.com/onflow/atree v0.8.0-rc.5/go.mod h1:yccR+LR7xc1Jdic0mrjocbHvUD7lnVvg8/Ct1AA5zBo=
github.com/onflow/atree v0.8.0-rc.6 h1:GWgaylK24b5ta2Hq+TvyOF7X5tZLiLzMMn7lEt59fsA=
github.com/onflow/atree v0.8.0-rc.6/go.mod h1:yccR+LR7xc1Jdic0mrjocbHvUD7lnVvg8/Ct1AA5zBo=
github.com/onflow/crypto v0.25.0 h1:BeWbLsh3ZD13Ej+Uky6kg1PL1ZIVBDVX+2MVBNwqddg=
github.com/onflow/crypto v0.25.0/go.mod h1:C8FbaX0x8y+FxWjbkHy0Q4EASCDR9bSPWZqlpCLYyVI=
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
Expand Down
18 changes: 4 additions & 14 deletions runtime/cmd/decode-state-values/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,23 +115,13 @@ func decodeSlab(id atree.SlabID, data []byte) (atree.Slab, error) {
}

func slabIDToStorageKey(id atree.SlabID) storageKey {
const (
addressSize = len(atree.Address{})
indexSize = len(atree.SlabIndex{})
slabIDSize = addressSize + indexSize
indexPos = addressSize
)

var b [slabIDSize]byte
_, err := id.ToRawBytes(b[:])
if err != nil {
panic(err)
}
address := id.Address()
index := id.Index()

return storageKey{
string(b[:addressSize]),
string(address[:]),
"",
"$" + string(b[indexPos:]),
"$" + string(index[:]),
}
}

Expand Down
35 changes: 35 additions & 0 deletions runtime/interpreter/encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package interpreter

import (
"bytes"
"fmt"
"math"
"math/big"

Expand Down Expand Up @@ -253,6 +254,40 @@ var CBOREncMode = func() cbor.EncMode {
return encMode
}()

func init() {
// Here, init is only used for sanity checks (coding errors) and does not perform any initialization.

// Check if the CBOR tag number range is not reserved for internal use by atree.
// Cadence must only use available (unreserved by atree) CBOR tag numbers to encode elements in atree managed containers.

// As of Aug 15, 2024:
// - Atree reserves CBOR tag numbers from 240 to 255 for atree internal use.
// - Cadence uses CBOR tag numbers from 128 to 230 to encode internal Cadence values.

// When a new tag number is needed, Atree will use higher tag number first from its reserved range.
// In contrast, Cadence will use lower tag numbers first from its own (different) reserved range.
// This allows Atree and Cadence more flexibility in case we need to revisit the
// allocation of adjacent unused ranges for Atree and Cadence.

minCBORTagNum := uint64(CBORTagBase)
maxCBORTagNum := uint64(CBORTag_Count) - 1

tagNumOK, err := atree.IsCBORTagNumberRangeAvailable(minCBORTagNum, maxCBORTagNum)
if err != nil {
panic(err)
}

if !tagNumOK {
atreeMinCBORTagNum, atreeMaxCBORTagNum := atree.ReservedCBORTagNumberRange()
panic(fmt.Errorf(
"cadence internal tag numbers [%d, %d] overlaps with atree internal tag numbers [%d, %d]",
minCBORTagNum,
maxCBORTagNum,
atreeMinCBORTagNum,
atreeMaxCBORTagNum))
}
}

// Encode encodes the value as a CBOR nil
func (v NilValue) Encode(e *atree.Encoder) error {
// NOTE: when updating, also update NilValue.ByteSize
Expand Down

0 comments on commit 9e055a2

Please sign in to comment.