Skip to content

Commit

Permalink
fix(runtime): add child prefix check (#4143)
Browse files Browse the repository at this point in the history
  • Loading branch information
jimjbrettj authored Sep 3, 2024
1 parent 26b7961 commit e7dc9f7
Showing 1 changed file with 21 additions and 4 deletions.
25 changes: 21 additions & 4 deletions lib/runtime/wazero/imports.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ var (
emptyByteVectorEncoded []byte = scale.MustMarshal([]byte{})
noneEncoded []byte = []byte{0x00}
allZeroesBytes = [32]byte{}

childStorageKeyPrefix = []byte(":child_storage:")
)

const (
Expand Down Expand Up @@ -2065,10 +2067,15 @@ func ext_storage_clear_prefix_version_1(ctx context.Context, m api.Module, prefi
prefix := read(m, prefixSpan)
logger.Debugf("prefix: 0x%x", prefix)

err := storage.ClearPrefix(prefix)
if err != nil {
panic(err)
if !bytes.HasPrefix(prefix, childStorageKeyPrefix) {
err := storage.ClearPrefix(prefix)
if err != nil {
panic(err)
}
} else {
logger.Warnf("cannot clear prefix that is part of or contains a child storage key")
}

}

// toKillStorageResultEnum encodes the `allRemoved` flag and
Expand Down Expand Up @@ -2117,6 +2124,17 @@ func ext_storage_clear_prefix_version_2(ctx context.Context, m api.Module, prefi
limitPtr = &maxLimit
}

if bytes.HasPrefix(prefix, childStorageKeyPrefix) {
logger.Warnf("cannot clear child prefix: 0x%x", prefix)
encBytes, err := toKillStorageResultEnum(false, 0)
if err != nil {
panic(err)
}

valueSpan := mustWrite(m, rtCtx.Allocator, encBytes)
return valueSpan
}

numRemoved, all, err := storage.ClearPrefixLimit(prefix, *limitPtr)
if err != nil {
logger.Errorf("failed to clear prefix limit: %s", err)
Expand All @@ -2134,7 +2152,6 @@ func ext_storage_clear_prefix_version_2(ctx context.Context, m api.Module, prefi
logger.Errorf("failed to allocate: %s", err)
panic(err)
}

return valueSpan
}

Expand Down

0 comments on commit e7dc9f7

Please sign in to comment.