Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(gnovm): allow multiple NaN keys in maps #3932

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions gnovm/pkg/gnolang/values.go
Original file line number Diff line number Diff line change
@@ -3,7 +3,9 @@
import (
"encoding/binary"
"fmt"
"math"
"math/big"
"math/rand/v2"
"reflect"
"strconv"
"strings"
@@ -1458,6 +1460,18 @@
}
}

// randNaN64 returns an uint64 representation of NaN with a random payload.
func randNaN64() uint64 {
const uvnan64 = 0x7FF8000000000001 // math.Float64bits(math.NaN())
return rand.Uint64()&^uvnan64 | uvnan64 //nolint:gosec
}

// randNaN32 returns an uint32 representation of NaN with a random payload.
func randNaN32() uint32 {
const uvnan32 = 0x7FC00000 // math.Float32bits(float32(math.NaN()))
return rand.Uint32()&^uvnan32 | uvnan32 //nolint:gosec
Comment on lines +1471 to +1472
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, so I'm not convinced by this approach in general, but especially for float32's.

We'd have (32-9) bits to play with for a random value, ie. 8388608. With this amount of values, finding a "birthday" isn't that absurd probability; for a 1% probability, you need 412 values; for 50%, 3411; for 99%, 8789.

https://www.bdayprob.com/

I remain on my proposal for a solution for this; ComputeMapKey should return a second field saying that the value is not comparable, and the computed key should not be stored in the vmap or used for comparisons.

}

func (tv *TypedValue) ComputeMapKey(store Store, omitType bool) MapKey {
// Special case when nil: has no separator.
if tv.T == nil {
@@ -1476,6 +1490,27 @@
}
switch bt := baseOf(tv.T).(type) {
case PrimitiveType:
if tv.HasKind(Float64Kind) {
f := math.Float64frombits(tv.GetFloat64())
if f != f {
// If NaN, shuffle its value, thus allowing several NaN per map
// and ensuring that a value cannot be retrieved by NaN.
tv.SetFloat64(randNaN64())
} else if f == 0.0 {
// If zero, clear the sign, so +0.0 and -0.0 match the same key.
tv.SetFloat64(math.Float64bits(+0.0))
}

Check warning on line 1502 in gnovm/pkg/gnolang/values.go

Codecov / codecov/patch

gnovm/pkg/gnolang/values.go#L1500-L1502

Added lines #L1500 - L1502 were not covered by tests
} else if tv.HasKind(Float32Kind) {
f := math.Float32frombits(tv.GetFloat32())
if f != f {
// If NaN, shuffle its value, thus allowing several NaN per map
// and ensuring that a value cannot be retrieved by NaN.
tv.SetFloat32(randNaN32())
} else if f == 0.0 {
// If zero, clear the sign, so +0.0 and -0.0 match the same key.
tv.SetFloat32(math.Float32bits(+0.0))
}

Check warning on line 1512 in gnovm/pkg/gnolang/values.go

Codecov / codecov/patch

gnovm/pkg/gnolang/values.go#L1510-L1512

Added lines #L1510 - L1512 were not covered by tests
}
pbz := tv.PrimitiveBytes()
bz = append(bz, pbz...)
case *PointerType:
15 changes: 15 additions & 0 deletions gnovm/tests/files/map30.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package main

import (
"fmt"
"math"
)

var NaN = math.NaN()

func main() {
fmt.Println(map[float64]int{NaN: 1, NaN: 2})
}

// Output:
// map[NaN:1 NaN:2]
15 changes: 15 additions & 0 deletions gnovm/tests/files/map31.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package main

import (
"fmt"
"math"
)

var NaN = float32(math.NaN())

func main() {
fmt.Println(map[float32]int{NaN: 1, NaN: 2})
}

// Output:
// map[NaN:1 NaN:2]