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

feat(gnovm): implement overflow checking at VM level #3250

Merged
merged 21 commits into from
Jan 9, 2025
Merged
Show file tree
Hide file tree
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
22 changes: 14 additions & 8 deletions examples/gno.land/p/demo/grc/grc20/token.gno
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package grc20

import (
"math/overflow"
"std"
"strconv"

Expand Down Expand Up @@ -170,17 +169,24 @@ func (led *PrivateLedger) Approve(owner, spender std.Address, amount uint64) err
}

// Mint increases the total supply of the token and adds the specified amount to the specified address.
func (led *PrivateLedger) Mint(address std.Address, amount uint64) error {
func (led *PrivateLedger) Mint(address std.Address, amount uint64) (err error) {
if !address.IsValid() {
return ErrInvalidAddress
}

// XXX: math/overflow is not supporting uint64.
// This checks prevents overflow but makes the totalSupply limited to a uint63.
sum, ok := overflow.Add64(int64(led.totalSupply), int64(amount))
if !ok {
return ErrOverflow
}
defer func() {
if r := recover(); r != nil {
if r != "addition overflow" {
panic(r)
}
err = ErrOverflow
}
}()

// Convert amount and totalSupply to signed integers to enable
// overflow checking (not occuring on unsigned) when computing the sum.
// The maximum value for totalSupply is therefore 1<<63.
sum := int64(led.totalSupply) + int64(amount)

led.totalSupply = uint64(sum)
currentBalance := led.balanceOf(address)
Expand Down
70 changes: 70 additions & 0 deletions gnovm/pkg/gnolang/op_bench_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package gnolang

import (
"testing"

"github.com/gnolang/gno/tm2/pkg/overflow"
)

func BenchmarkOpAdd(b *testing.B) {
m := NewMachine("bench", nil)
x := TypedValue{T: IntType}
x.SetInt(4)
y := TypedValue{T: IntType}
y.SetInt(3)

b.ResetTimer()

for range b.N {
m.PushOp(OpHalt)
m.PushExpr(&BinaryExpr{})
m.PushValue(x)
m.PushValue(y)
m.PushOp(OpAdd)
m.Run()
}
}

//go:noinline
func AddNoOverflow(x, y int) int { return x + y }

func BenchmarkAddNoOverflow(b *testing.B) {
x, y := 4, 3
c := 0
for range b.N {
c = AddNoOverflow(x, y)
}
if c != 7 {
b.Error("invalid result")
}
}

func BenchmarkAddOverflow(b *testing.B) {
x, y := 4, 3
c := 0
for range b.N {
c = overflow.Addp(x, y)
}
if c != 7 {
b.Error("invalid result")
}
}

func TestOpAdd1(t *testing.T) {
m := NewMachine("test", nil)
a := TypedValue{T: IntType}
a.SetInt(4)
b := TypedValue{T: IntType}
b.SetInt(3)
t.Log("a:", a, "b:", b)

start := m.NumValues
m.PushOp(OpHalt)
m.PushExpr(&BinaryExpr{})
m.PushValue(a)
m.PushValue(b)
m.PushOp(OpAdd)
m.Run()
res := m.ReapValues(start)
t.Log("res:", res)
}
Loading
Loading