onflow / cadence

Cadence, the resource-oriented smart contract programming language 🏃‍♂️
https://developers.flow.com/cadence
Apache License 2.0
526 stars 136 forks source link

Prevent storage reference to another reference #3404

Closed turbolent closed 2 weeks ago

turbolent commented 3 weeks ago

Closes https://github.com/dapperlabs/cadence-internal/issues/235

Description

Based on #3398. Thanks for @dsainati1 for the initial investigation.

Nested references are not allowed.

The creation of an ephemeral reference, e.g. through a reference expression, already prevents this (NewUnmeteredEphemeralReferenceValue aborts with NestedReferenceError).

Also prevent storage references to references. Given that the target of a storage reference is a path and the value stored in the path may change, check the referenced value is not a reference value on each dereference, not just when the storage reference is created.


github-actions[bot] commented 3 weeks ago

Cadence Benchstat comparison

This branch with compared with the base branch onflow:master commit 58c36c5c6701cd9414615adbbd2402aacdb7b8d2 The command for i in {1..N}; do go test ./... -run=XXX -bench=. -benchmem -shuffle=on; done was used. Bench tests were run a total of 7 times on each branch.

Collapsed results for better readability

old.txtnew.txt
time/opdelta
DecodeBatchEventsCCF-4123ms ± 0%124ms ± 0%~(p=1.000 n=1+1)
DecodeBatchEventsJSON-4363ms ± 0%360ms ± 0%~(p=1.000 n=1+1)
DecodeCCF/FlowFees.FeesDeducted-42.74µs ± 0%2.73µs ± 0%~(p=1.000 n=1+1)
DecodeCCF/FlowFees.TokensWithdrawn-42.06µs ± 0%2.04µs ± 0%~(p=1.000 n=1+1)
DecodeCCF/FlowIDTableStaking.DelegatorRewardsPaid-42.71µs ± 0%2.70µs ± 0%~(p=1.000 n=1+1)
DecodeCCF/FlowIDTableStaking.EpochTotalRewardsPaid-43.02µs ± 0%3.02µs ± 0%~(p=1.000 n=1+1)
DecodeCCF/FlowIDTableStaking.NewWeeklyPayout-42.09µs ± 0%2.07µs ± 0%~(p=1.000 n=1+1)
DecodeCCF/FlowIDTableStaking.RewardsPaid-42.42µs ± 0%2.42µs ± 0%~(p=1.000 n=1+1)
DecodeCCF/FlowToken.TokensDeposited-42.49µs ± 0%2.47µs ± 0%~(p=1.000 n=1+1)
DecodeCCF/FlowToken.TokensDeposited_with_nil_receiver-42.40µs ± 0%2.40µs ± 0%~(p=1.000 n=1+1)
DecodeCCF/FlowToken.TokensMinted-42.05µs ± 0%2.05µs ± 0%~(p=1.000 n=1+1)
DecodeCCF/FlowToken.TokensWithdrawn-42.51µs ± 0%2.49µs ± 0%~(p=1.000 n=1+1)
DecodeJSON/FlowFees.FeesDeducted-48.98µs ± 0%25.82µs ± 0%~(p=1.000 n=1+1)
DecodeJSON/FlowFees.TokensWithdrawn-45.24µs ± 0%5.18µs ± 0%~(p=1.000 n=1+1)
DecodeJSON/FlowIDTableStaking.DelegatorRewardsPaid-48.11µs ± 0%8.19µs ± 0%~(p=1.000 n=1+1)
DecodeJSON/FlowIDTableStaking.EpochTotalRewardsPaid-411.2µs ± 0%11.1µs ± 0%~(p=1.000 n=1+1)
DecodeJSON/FlowIDTableStaking.NewWeeklyPayout-45.31µs ± 0%5.30µs ± 0%~(p=1.000 n=1+1)
DecodeJSON/FlowIDTableStaking.RewardsPaid-46.94µs ± 0%7.32µs ± 0%~(p=1.000 n=1+1)
DecodeJSON/FlowToken.TokensDeposited-47.15µs ± 0%7.13µs ± 0%~(p=1.000 n=1+1)
DecodeJSON/FlowToken.TokensDeposited_with_nil_receiver-46.50µs ± 0%6.58µs ± 0%~(p=1.000 n=1+1)
DecodeJSON/FlowToken.TokensMinted-45.28µs ± 0%5.27µs ± 0%~(p=1.000 n=1+1)
DecodeJSON/FlowToken.TokensWithdrawn-47.18µs ± 0%7.11µs ± 0%~(p=1.000 n=1+1)
EncodeBatchEventsCCF-481.8ms ± 0%85.6ms ± 0%~(p=1.000 n=1+1)
EncodeBatchEventsJSON-4101ms ± 0%101ms ± 0%~(p=1.000 n=1+1)
EncodeCCF/FlowFees.FeesDeducted-41.84µs ± 0%1.85µs ± 0%~(p=1.000 n=1+1)
EncodeCCF/FlowFees.TokensWithdrawn-41.46µs ± 0%1.49µs ± 0%~(p=1.000 n=1+1)
EncodeCCF/FlowIDTableStaking.DelegatorRewardsPaid-41.76µs ± 0%1.80µs ± 0%~(p=1.000 n=1+1)
EncodeCCF/FlowIDTableStaking.EpochTotalRewardsPaid-42.07µs ± 0%2.09µs ± 0%~(p=1.000 n=1+1)
EncodeCCF/FlowIDTableStaking.NewWeeklyPayout-41.50µs ± 0%1.51µs ± 0%~(p=1.000 n=1+1)
EncodeCCF/FlowIDTableStaking.RewardsPaid-41.62µs ± 0%1.64µs ± 0%~(p=1.000 n=1+1)
EncodeCCF/FlowToken.TokensDeposited-41.66µs ± 0%1.70µs ± 0%~(p=1.000 n=1+1)
EncodeCCF/FlowToken.TokensDeposited_with_nil_receiver-41.64µs ± 0%1.71µs ± 0%~(p=1.000 n=1+1)
EncodeCCF/FlowToken.TokensMinted-41.46µs ± 0%1.48µs ± 0%~(p=1.000 n=1+1)
EncodeCCF/FlowToken.TokensWithdrawn-41.66µs ± 0%1.70µs ± 0%~(p=1.000 n=1+1)
EncodeJSON/FlowFees.FeesDeducted-42.44µs ± 0%2.36µs ± 0%~(p=1.000 n=1+1)
EncodeJSON/FlowFees.TokensWithdrawn-41.35µs ± 0%1.36µs ± 0%~(p=1.000 n=1+1)
EncodeJSON/FlowIDTableStaking.DelegatorRewardsPaid-42.15µs ± 0%2.17µs ± 0%~(p=1.000 n=1+1)
EncodeJSON/FlowIDTableStaking.EpochTotalRewardsPaid-42.96µs ± 0%2.91µs ± 0%~(p=1.000 n=1+1)
EncodeJSON/FlowIDTableStaking.NewWeeklyPayout-41.40µs ± 0%1.39µs ± 0%~(p=1.000 n=1+1)
EncodeJSON/FlowIDTableStaking.RewardsPaid-41.79µs ± 0%1.79µs ± 0%~(all equal)
EncodeJSON/FlowToken.TokensDeposited-42.06µs ± 0%2.07µs ± 0%~(p=1.000 n=1+1)
EncodeJSON/FlowToken.TokensDeposited_with_nil_receiver-41.60µs ± 0%1.61µs ± 0%~(p=1.000 n=1+1)
EncodeJSON/FlowToken.TokensMinted-41.38µs ± 0%1.36µs ± 0%~(p=1.000 n=1+1)
EncodeJSON/FlowToken.TokensWithdrawn-42.05µs ± 0%2.04µs ± 0%~(p=1.000 n=1+1)
ExportType/composite_type-4258ns ± 0%282ns ± 0%~(p=1.000 n=1+1)
ExportType/simple_type-477.5ns ± 0%77.6ns ± 0%~(p=1.000 n=1+1)
InterpretRecursionFib-41.88ms ± 0%1.86ms ± 0%~(p=1.000 n=1+1)
NewInterpreter/new_interpreter-4856ns ± 0%851ns ± 0%~(p=1.000 n=1+1)
NewInterpreter/new_sub-interpreter-4355ns ± 0%354ns ± 0%~(p=1.000 n=1+1)
ParseArray-45.99ms ± 4%5.85ms ± 0%~(p=1.000 n=2+2)
ParseDeploy/byte_array-412.6ms ±29%8.9ms ± 1%~(p=0.667 n=2+2)
ParseDeploy/decode_hex-4992µs ± 1%994µs ± 1%~(p=0.667 n=2+2)
ParseFungibleToken/With_memory_metering-4178µs ±20%153µs ± 4%~(p=1.000 n=2+2)
ParseFungibleToken/Without_memory_metering-4146µs ±23%118µs ± 1%~(p=1.000 n=2+2)
ParseInfix-45.24µs ± 2%5.10µs ± 0%~(p=0.333 n=2+2)
QualifiedIdentifierCreation/One_level-42.17ns ± 0%2.17ns ± 0%~(p=1.000 n=1+1)
QualifiedIdentifierCreation/Three_levels-483.3ns ± 0%82.9ns ± 0%~(p=1.000 n=1+1)
RuntimeScriptNoop-43.58µs ± 0%3.61µs ± 0%~(p=1.000 n=1+1)
SuperTypeInference/arrays-4231ns ± 0%231ns ± 0%~(p=1.000 n=1+1)
SuperTypeInference/composites-491.9ns ± 0%88.0ns ± 0%~(p=1.000 n=1+1)
SuperTypeInference/integers-4297ns ± 0%297ns ± 0%~(all equal)
ValueIsSubtypeOfSemaType-465.3ns ± 0%62.8ns ± 0%~(p=1.000 n=1+1)
 
alloc/opdelta
DecodeBatchEventsCCF-466.5MB ± 0%66.5MB ± 0%~(p=1.000 n=1+1)
DecodeBatchEventsJSON-4242MB ± 0%242MB ± 0%~(p=1.000 n=1+1)
DecodeCCF/FlowFees.FeesDeducted-41.40kB ± 0%1.40kB ± 0%~(all equal)
DecodeCCF/FlowFees.TokensWithdrawn-41.21kB ± 0%1.21kB ± 0%~(all equal)
DecodeCCF/FlowIDTableStaking.DelegatorRewardsPaid-41.48kB ± 0%1.48kB ± 0%~(all equal)
DecodeCCF/FlowIDTableStaking.EpochTotalRewardsPaid-41.49kB ± 0%1.49kB ± 0%~(all equal)
DecodeCCF/FlowIDTableStaking.NewWeeklyPayout-41.26kB ± 0%1.26kB ± 0%~(all equal)
DecodeCCF/FlowIDTableStaking.RewardsPaid-41.38kB ± 0%1.38kB ± 0%~(all equal)
DecodeCCF/FlowToken.TokensDeposited-41.32kB ± 0%1.32kB ± 0%~(all equal)
DecodeCCF/FlowToken.TokensDeposited_with_nil_receiver-41.30kB ± 0%1.30kB ± 0%~(all equal)
DecodeCCF/FlowToken.TokensMinted-41.21kB ± 0%1.21kB ± 0%~(all equal)
DecodeCCF/FlowToken.TokensWithdrawn-41.33kB ± 0%1.33kB ± 0%~(all equal)
DecodeJSON/FlowFees.FeesDeducted-45.95kB ± 0%5.95kB ± 0%~(all equal)
DecodeJSON/FlowFees.TokensWithdrawn-43.55kB ± 0%3.55kB ± 0%~(all equal)
DecodeJSON/FlowIDTableStaking.DelegatorRewardsPaid-45.38kB ± 0%5.38kB ± 0%~(all equal)
DecodeJSON/FlowIDTableStaking.EpochTotalRewardsPaid-47.30kB ± 0%7.30kB ± 0%~(all equal)
DecodeJSON/FlowIDTableStaking.NewWeeklyPayout-43.59kB ± 0%3.59kB ± 0%~(all equal)
DecodeJSON/FlowIDTableStaking.RewardsPaid-44.49kB ± 0%4.49kB ± 0%~(all equal)
DecodeJSON/FlowToken.TokensDeposited-44.83kB ± 0%4.83kB ± 0%~(all equal)
DecodeJSON/FlowToken.TokensDeposited_with_nil_receiver-44.41kB ± 0%4.41kB ± 0%~(all equal)
DecodeJSON/FlowToken.TokensMinted-43.55kB ± 0%3.55kB ± 0%~(all equal)
DecodeJSON/FlowToken.TokensWithdrawn-44.83kB ± 0%4.83kB ± 0%~(all equal)
EncodeBatchEventsCCF-463.9MB ± 0%63.9MB ± 0%~(p=1.000 n=1+1)
EncodeBatchEventsJSON-440.7MB ± 0%40.7MB ± 0%~(p=1.000 n=1+1)
EncodeCCF/FlowFees.FeesDeducted-41.25kB ± 0%1.25kB ± 0%~(all equal)
EncodeCCF/FlowFees.TokensWithdrawn-41.20kB ± 0%1.20kB ± 0%~(all equal)
EncodeCCF/FlowIDTableStaking.DelegatorRewardsPaid-41.47kB ± 0%1.47kB ± 0%~(all equal)
EncodeCCF/FlowIDTableStaking.EpochTotalRewardsPaid-41.44kB ± 0%1.44kB ± 0%~(all equal)
EncodeCCF/FlowIDTableStaking.NewWeeklyPayout-41.38kB ± 0%1.38kB ± 0%~(all equal)
EncodeCCF/FlowIDTableStaking.RewardsPaid-41.46kB ± 0%1.46kB ± 0%~(all equal)
EncodeCCF/FlowToken.TokensDeposited-41.25kB ± 0%1.25kB ± 0%~(all equal)
EncodeCCF/FlowToken.TokensDeposited_with_nil_receiver-41.23kB ± 0%1.23kB ± 0%~(all equal)
EncodeCCF/FlowToken.TokensMinted-41.20kB ± 0%1.20kB ± 0%~(all equal)
EncodeCCF/FlowToken.TokensWithdrawn-41.25kB ± 0%1.25kB ± 0%~(all equal)
EncodeJSON/FlowFees.FeesDeducted-4896B ± 0%896B ± 0%~(all equal)
EncodeJSON/FlowFees.TokensWithdrawn-4536B ± 0%536B ± 0%~(all equal)
EncodeJSON/FlowIDTableStaking.DelegatorRewardsPaid-4920B ± 0%920B ± 0%~(all equal)
EncodeJSON/FlowIDTableStaking.EpochTotalRewardsPaid-41.11kB ± 0%1.11kB ± 0%~(all equal)
EncodeJSON/FlowIDTableStaking.NewWeeklyPayout-4584B ± 0%584B ± 0%~(all equal)
EncodeJSON/FlowIDTableStaking.RewardsPaid-4784B ± 0%784B ± 0%~(all equal)
EncodeJSON/FlowToken.TokensDeposited-4808B ± 0%808B ± 0%~(all equal)
EncodeJSON/FlowToken.TokensDeposited_with_nil_receiver-4672B ± 0%672B ± 0%~(all equal)
EncodeJSON/FlowToken.TokensMinted-4544B ± 0%544B ± 0%~(all equal)
EncodeJSON/FlowToken.TokensWithdrawn-4800B ± 0%800B ± 0%~(all equal)
ExportType/composite_type-4120B ± 0%120B ± 0%~(all equal)
ExportType/simple_type-40.00B 0.00B ~(all equal)
InterpretRecursionFib-41.19MB ± 0%1.19MB ± 0%~(p=1.000 n=1+1)
NewInterpreter/new_interpreter-4944B ± 0%944B ± 0%~(all equal)
NewInterpreter/new_sub-interpreter-4200B ± 0%200B ± 0%~(all equal)
ParseArray-42.69MB ± 0%2.69MB ± 0%~(p=1.000 n=2+2)
ParseDeploy/byte_array-44.13MB ± 2%4.13MB ± 2%~(p=0.667 n=2+2)
ParseDeploy/decode_hex-4214kB ± 0%214kB ± 0%~(p=1.000 n=2+2)
ParseFungibleToken/With_memory_metering-429.8kB ± 0%29.8kB ± 0%~(p=1.000 n=2+2)
ParseFungibleToken/Without_memory_metering-429.8kB ± 0%29.8kB ± 0%~(p=1.000 n=2+2)
ParseInfix-41.92kB ± 0%1.92kB ± 0%~(p=0.333 n=2+2)
QualifiedIdentifierCreation/One_level-40.00B 0.00B ~(all equal)
QualifiedIdentifierCreation/Three_levels-464.0B ± 0%64.0B ± 0%~(all equal)
RuntimeScriptNoop-43.36kB ± 0%3.36kB ± 0%~(all equal)
SuperTypeInference/arrays-496.0B ± 0%96.0B ± 0%~(all equal)
SuperTypeInference/composites-40.00B 0.00B ~(all equal)
SuperTypeInference/integers-40.00B 0.00B ~(all equal)
ValueIsSubtypeOfSemaType-448.0B ± 0%48.0B ± 0%~(all equal)
 
allocs/opdelta
DecodeBatchEventsCCF-41.48M ± 0%1.48M ± 0%~(p=1.000 n=1+1)
DecodeBatchEventsJSON-44.60M ± 0%4.60M ± 0%~(p=1.000 n=1+1)
DecodeCCF/FlowFees.FeesDeducted-430.0 ± 0%30.0 ± 0%~(all equal)
DecodeCCF/FlowFees.TokensWithdrawn-426.0 ± 0%26.0 ± 0%~(all equal)
DecodeCCF/FlowIDTableStaking.DelegatorRewardsPaid-430.0 ± 0%30.0 ± 0%~(all equal)
DecodeCCF/FlowIDTableStaking.EpochTotalRewardsPaid-432.0 ± 0%32.0 ± 0%~(all equal)
DecodeCCF/FlowIDTableStaking.NewWeeklyPayout-426.0 ± 0%26.0 ± 0%~(all equal)
DecodeCCF/FlowIDTableStaking.RewardsPaid-429.0 ± 0%29.0 ± 0%~(all equal)
DecodeCCF/FlowToken.TokensDeposited-431.0 ± 0%31.0 ± 0%~(all equal)
DecodeCCF/FlowToken.TokensDeposited_with_nil_receiver-429.0 ± 0%29.0 ± 0%~(all equal)
DecodeCCF/FlowToken.TokensMinted-426.0 ± 0%26.0 ± 0%~(all equal)
DecodeCCF/FlowToken.TokensWithdrawn-431.0 ± 0%31.0 ± 0%~(all equal)
DecodeJSON/FlowFees.FeesDeducted-4124 ± 0%124 ± 0%~(all equal)
DecodeJSON/FlowFees.TokensWithdrawn-469.0 ± 0%69.0 ± 0%~(all equal)
DecodeJSON/FlowIDTableStaking.DelegatorRewardsPaid-4100 ± 0%100 ± 0%~(all equal)
DecodeJSON/FlowIDTableStaking.EpochTotalRewardsPaid-4157 ± 0%157 ± 0%~(all equal)
DecodeJSON/FlowIDTableStaking.NewWeeklyPayout-468.0 ± 0%68.0 ± 0%~(all equal)
DecodeJSON/FlowIDTableStaking.RewardsPaid-485.0 ± 0%85.0 ± 0%~(all equal)
DecodeJSON/FlowToken.TokensDeposited-493.0 ± 0%93.0 ± 0%~(all equal)
DecodeJSON/FlowToken.TokensDeposited_with_nil_receiver-484.0 ± 0%84.0 ± 0%~(all equal)
DecodeJSON/FlowToken.TokensMinted-469.0 ± 0%69.0 ± 0%~(all equal)
DecodeJSON/FlowToken.TokensWithdrawn-493.0 ± 0%93.0 ± 0%~(all equal)
EncodeBatchEventsCCF-41.00M ± 0%1.00M ± 0%~(p=1.000 n=1+1)
EncodeBatchEventsJSON-4902k ± 0%902k ± 0%~(p=1.000 n=1+1)
EncodeCCF/FlowFees.FeesDeducted-420.0 ± 0%20.0 ± 0%~(all equal)
EncodeCCF/FlowFees.TokensWithdrawn-420.0 ± 0%20.0 ± 0%~(all equal)
EncodeCCF/FlowIDTableStaking.DelegatorRewardsPaid-420.0 ± 0%20.0 ± 0%~(all equal)
EncodeCCF/FlowIDTableStaking.EpochTotalRewardsPaid-420.0 ± 0%20.0 ± 0%~(all equal)
EncodeCCF/FlowIDTableStaking.NewWeeklyPayout-420.0 ± 0%20.0 ± 0%~(all equal)
EncodeCCF/FlowIDTableStaking.RewardsPaid-420.0 ± 0%20.0 ± 0%~(all equal)
EncodeCCF/FlowToken.TokensDeposited-421.0 ± 0%21.0 ± 0%~(all equal)
EncodeCCF/FlowToken.TokensDeposited_with_nil_receiver-421.0 ± 0%21.0 ± 0%~(all equal)
EncodeCCF/FlowToken.TokensMinted-420.0 ± 0%20.0 ± 0%~(all equal)
EncodeCCF/FlowToken.TokensWithdrawn-421.0 ± 0%21.0 ± 0%~(all equal)
EncodeJSON/FlowFees.FeesDeducted-420.0 ± 0%20.0 ± 0%~(all equal)
EncodeJSON/FlowFees.TokensWithdrawn-413.0 ± 0%13.0 ± 0%~(all equal)
EncodeJSON/FlowIDTableStaking.DelegatorRewardsPaid-417.0 ± 0%17.0 ± 0%~(all equal)
EncodeJSON/FlowIDTableStaking.EpochTotalRewardsPaid-426.0 ± 0%26.0 ± 0%~(all equal)
EncodeJSON/FlowIDTableStaking.NewWeeklyPayout-413.0 ± 0%13.0 ± 0%~(all equal)
EncodeJSON/FlowIDTableStaking.RewardsPaid-416.0 ± 0%16.0 ± 0%~(all equal)
EncodeJSON/FlowToken.TokensDeposited-420.0 ± 0%20.0 ± 0%~(all equal)
EncodeJSON/FlowToken.TokensDeposited_with_nil_receiver-415.0 ± 0%15.0 ± 0%~(all equal)
EncodeJSON/FlowToken.TokensMinted-414.0 ± 0%14.0 ± 0%~(all equal)
EncodeJSON/FlowToken.TokensWithdrawn-419.0 ± 0%19.0 ± 0%~(all equal)
ExportType/composite_type-43.00 ± 0%3.00 ± 0%~(all equal)
ExportType/simple_type-40.00 0.00 ~(all equal)
InterpretRecursionFib-418.9k ± 0%18.9k ± 0%~(all equal)
NewInterpreter/new_interpreter-415.0 ± 0%15.0 ± 0%~(all equal)
NewInterpreter/new_sub-interpreter-44.00 ± 0%4.00 ± 0%~(all equal)
ParseArray-459.6k ± 0%59.6k ± 0%~(p=1.000 n=2+2)
ParseDeploy/byte_array-489.4k ± 0%89.4k ± 0%~(p=0.667 n=2+2)
ParseDeploy/decode_hex-463.0 ± 0%63.0 ± 0%~(all equal)
ParseFungibleToken/With_memory_metering-4773 ± 1%773 ± 1%~(p=1.000 n=2+2)
ParseFungibleToken/Without_memory_metering-4773 ± 1%773 ± 1%~(p=1.000 n=2+2)
ParseInfix-448.0 ± 0%48.0 ± 0%~(all equal)
QualifiedIdentifierCreation/One_level-40.00 0.00 ~(all equal)
QualifiedIdentifierCreation/Three_levels-42.00 ± 0%2.00 ± 0%~(all equal)
RuntimeScriptNoop-450.0 ± 0%50.0 ± 0%~(all equal)
SuperTypeInference/arrays-43.00 ± 0%3.00 ± 0%~(all equal)
SuperTypeInference/composites-40.00 0.00 ~(all equal)
SuperTypeInference/integers-40.00 0.00 ~(all equal)
ValueIsSubtypeOfSemaType-41.00 ± 0%1.00 ± 0%~(all equal)