cosmos-sdk/codec/types
Emmanuel T Odeke 56fc3fc572
codec/types: avoid unnecessary allocations for NewAnyWithCustomTypeURL on error (#8605)
Avoids a bleed out attack in which a node can be made to allocate
memory slowly or very fast in small strides, by sending bad data
to code that invokes NewAnyWithCustomTypeURL, in which we
unconditionally returned a new Any object. On a 64-bit machine,
this would waste 96 bytes per invocation even on error.

Added a test to ensure zero allocations with a fixed error returned.
Also added a benchmark which shows reduction in wasted allocations and
wasted CPU time:

```shell
$ benchstat before.txt after.txt
name                                            old time/op    new time/op    delta
NewAnyWithCustomTypeURLWithErrorReturned-8      142ns ± 6%      55ns ±12%   -61.65%  (p=0.000 n=9+10)

name                                            old alloc/op   new alloc/op   delta
NewAnyWithCustomTypeURLWithErrorReturned-8      96.0B ± 0%      0.0B       -100.00%  (p=0.000 n=10+10)

name                                            old allocs/op  new allocs/op  delta
NewAnyWithCustomTypeURLWithErrorReturned-8      1.00 ± 0%      0.00       -100.00%  (p=0.000 n=10+10)
```

Fixes #8537
2021-02-17 10:13:00 +00:00
..
any.go codec/types: avoid unnecessary allocations for NewAnyWithCustomTypeURL on error (#8605) 2021-02-17 10:13:00 +00:00
any.pb.go All Makefile proto commands use Docker (#7931) 2020-11-13 16:36:58 +00:00
any_internal_test.go remove any.ClearCachedValue method (#8402) 2021-01-21 12:35:40 +00:00
any_test.go codec/types: avoid unnecessary allocations for NewAnyWithCustomTypeURL on error (#8605) 2021-02-17 10:13:00 +00:00
compat.go Clean Any interface (#8167) 2020-12-18 14:55:25 +00:00
compat_test.go x/auth/ante: Migrate tests to use the new client.TxConfig (#6661) 2020-07-20 08:30:12 -04:00
doc.go Merge PR #6076: Proto Any init + evidence 2020-05-05 10:28:20 -04:00
interface_registry.go Throw an error on duplicate registration (#7729) 2020-10-29 15:32:47 +00:00
types_test.go remove any.ClearCachedValue method (#8402) 2021-01-21 12:35:40 +00:00