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): enforce size of int and uint to 64 bits on all arch. #3591

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions gno.land/pkg/sdk/vm/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
"error parsing int %q: %v",
arg, err))
}
tv.SetInt(int(i64))
tv.SetInt(i64)
return
case gno.Int8Type:
assertNoPlusPrefix(arg)
Expand Down Expand Up @@ -100,7 +100,7 @@
"error parsing uint %q: %v",
arg, err))
}
tv.SetUint(uint(u64))
tv.SetUint(u64)

Check warning on line 103 in gno.land/pkg/sdk/vm/convert.go

View check run for this annotation

Codecov / codecov/patch

gno.land/pkg/sdk/vm/convert.go#L103

Added line #L103 was not covered by tests
return
case gno.Uint8Type:
assertNoPlusPrefix(arg)
Expand Down
20 changes: 10 additions & 10 deletions gnovm/pkg/gnolang/gonative.go
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@
case reflect.String:
tv.V = alloc.NewString(rv.String())
case reflect.Int:
tv.SetInt(int(rv.Int()))
tv.SetInt(rv.Int())
case reflect.Int8:
tv.SetInt8(int8(rv.Int()))
case reflect.Int16:
Expand All @@ -322,7 +322,7 @@
case reflect.Int64:
tv.SetInt64(rv.Int())
case reflect.Uint:
tv.SetUint(uint(rv.Uint()))
tv.SetUint(rv.Uint())

Check warning on line 325 in gnovm/pkg/gnolang/gonative.go

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/gonative.go#L325

Added line #L325 was not covered by tests
case reflect.Uint8:
tv.SetUint8(uint8(rv.Uint()))
case reflect.Uint16:
Expand Down Expand Up @@ -391,7 +391,7 @@
}
case IntKind:
if lvl != 0 {
tv.SetInt(int(rv.Int()))
tv.SetInt(rv.Int())
}
case Int8Kind:
if lvl != 0 {
Expand All @@ -411,7 +411,7 @@
}
case UintKind:
if lvl != 0 {
tv.SetUint(uint(rv.Uint()))
tv.SetUint(rv.Uint())

Check warning on line 414 in gnovm/pkg/gnolang/gonative.go

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/gonative.go#L414

Added line #L414 was not covered by tests
}
case Uint8Kind:
if lvl != 0 {
Expand Down Expand Up @@ -627,7 +627,7 @@
case reflect.String:
tv.V = alloc.NewString(rv.String())
case reflect.Int:
tv.SetInt(int(rv.Int()))
tv.SetInt(rv.Int())

Check warning on line 630 in gnovm/pkg/gnolang/gonative.go

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/gonative.go#L630

Added line #L630 was not covered by tests
case reflect.Int8:
tv.SetInt8(int8(rv.Int()))
case reflect.Int16:
Expand All @@ -637,7 +637,7 @@
case reflect.Int64:
tv.SetInt64(rv.Int())
case reflect.Uint:
tv.SetUint(uint(rv.Uint()))
tv.SetUint(rv.Uint())

Check warning on line 640 in gnovm/pkg/gnolang/gonative.go

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/gonative.go#L640

Added line #L640 was not covered by tests
case reflect.Uint8:
tv.SetUint8(uint8(rv.Uint()))
case reflect.Uint16:
Expand Down Expand Up @@ -1032,7 +1032,7 @@
case StringType, UntypedStringType:
rv.SetString(tv.GetString())
case IntType:
rv.SetInt(int64(tv.GetInt()))
rv.SetInt(tv.GetInt())
case Int8Type:
rv.SetInt(int64(tv.GetInt8()))
case Int16Type:
Expand All @@ -1042,7 +1042,7 @@
case Int64Type:
rv.SetInt(tv.GetInt64())
case UintType:
rv.SetUint(uint64(tv.GetUint()))
rv.SetUint(tv.GetUint())
case Uint8Type:
rv.SetUint(uint64(tv.GetUint8()))
case Uint16Type:
Expand Down Expand Up @@ -1222,7 +1222,7 @@
if kx := x.Elts[i].Key; kx != nil {
// XXX why convert? (also see doOpArrayLit())
k := kx.(*ConstExpr).ConvertGetInt()
rf := rv.Index(k)
rf := rv.Index(int(k)) // XXX: k may overflow on 32 bits plaforms.

Check warning on line 1225 in gnovm/pkg/gnolang/gonative.go

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/gonative.go#L1225

Added line #L1225 was not covered by tests
gno2GoValue(&itvs[i], rf)
} else {
rf := rv.Index(i)
Expand Down Expand Up @@ -1261,7 +1261,7 @@
if kx := x.Elts[i].Key; kx != nil {
// XXX why convert? (also see doOpArrayLit())
k := kx.(*ConstExpr).ConvertGetInt()
rf := rv.Index(k)
rf := rv.Index(int(k)) // XXX: k may overflow on 32 bits plaforms.

Check warning on line 1264 in gnovm/pkg/gnolang/gonative.go

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/gonative.go#L1264

Added line #L1264 was not covered by tests
gno2GoValue(&itvs[i], rf)
} else {
rf := rv.Index(i)
Expand Down
56 changes: 28 additions & 28 deletions gnovm/pkg/gnolang/op_binary.go
Original file line number Diff line number Diff line change
Expand Up @@ -1211,8 +1211,8 @@
switch baseOf(lv.T) {
case IntType:
checkOverflow(func() bool {
l := big.NewInt(int64(lv.GetInt()))
r := big.NewInt(0).Lsh(l, rv.GetUint())
l := big.NewInt(lv.GetInt())
r := big.NewInt(0).Lsh(l, uint(rv.GetUint()))

return r.Cmp(big.NewInt(math.MaxInt)) != 1
})
Expand All @@ -1221,7 +1221,7 @@
case Int8Type:
checkOverflow(func() bool {
l := big.NewInt(int64(lv.GetInt8()))
r := big.NewInt(0).Lsh(l, rv.GetUint())
r := big.NewInt(0).Lsh(l, uint(rv.GetUint()))

return r.Cmp(big.NewInt(math.MaxInt8)) != 1
})
Expand All @@ -1230,7 +1230,7 @@
case Int16Type:
checkOverflow(func() bool {
l := big.NewInt(int64(lv.GetInt16()))
r := big.NewInt(0).Lsh(l, rv.GetUint())
r := big.NewInt(0).Lsh(l, uint(rv.GetUint()))

return r.Cmp(big.NewInt(math.MaxInt16)) != 1
})
Expand All @@ -1239,7 +1239,7 @@
case Int32Type, UntypedRuneType:
checkOverflow(func() bool {
l := big.NewInt(int64(lv.GetInt32()))
r := big.NewInt(0).Lsh(l, rv.GetUint())
r := big.NewInt(0).Lsh(l, uint(rv.GetUint()))

return r.Cmp(big.NewInt(math.MaxInt32)) != 1
})
Expand All @@ -1248,16 +1248,16 @@
case Int64Type:
checkOverflow(func() bool {
l := big.NewInt(lv.GetInt64())
r := big.NewInt(0).Lsh(l, rv.GetUint())
r := big.NewInt(0).Lsh(l, uint(rv.GetUint()))

return r.Cmp(big.NewInt(math.MaxInt64)) != 1
})

lv.SetInt64(lv.GetInt64() << rv.GetUint())
case UintType:
checkOverflow(func() bool {
l := big.NewInt(0).SetUint64(uint64(lv.GetUint()))
r := big.NewInt(0).Lsh(l, rv.GetUint())
l := big.NewInt(0).SetUint64(lv.GetUint())
r := big.NewInt(0).Lsh(l, uint(rv.GetUint()))

Check warning on line 1260 in gnovm/pkg/gnolang/op_binary.go

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/op_binary.go#L1259-L1260

Added lines #L1259 - L1260 were not covered by tests

return r.Cmp(big.NewInt(0).SetUint64(math.MaxUint)) != 1
})
Expand All @@ -1266,7 +1266,7 @@
case Uint8Type:
checkOverflow(func() bool {
l := big.NewInt(0).SetUint64(uint64(lv.GetUint8()))
r := big.NewInt(0).Lsh(l, rv.GetUint())
r := big.NewInt(0).Lsh(l, uint(rv.GetUint()))

return r.Cmp(big.NewInt(math.MaxUint8)) != 1
})
Expand All @@ -1275,7 +1275,7 @@
case DataByteType:
checkOverflow(func() bool {
l := big.NewInt(0).SetUint64(uint64(lv.GetDataByte()))
r := big.NewInt(0).Lsh(l, rv.GetUint())
r := big.NewInt(0).Lsh(l, uint(rv.GetUint()))

Check warning on line 1278 in gnovm/pkg/gnolang/op_binary.go

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/op_binary.go#L1278

Added line #L1278 was not covered by tests

return r.Cmp(big.NewInt(math.MaxUint8)) != 1
})
Expand All @@ -1284,7 +1284,7 @@
case Uint16Type:
checkOverflow(func() bool {
l := big.NewInt(0).SetUint64(uint64(lv.GetUint16()))
r := big.NewInt(0).Lsh(l, rv.GetUint())
r := big.NewInt(0).Lsh(l, uint(rv.GetUint()))

return r.Cmp(big.NewInt(math.MaxUint16)) != 1
})
Expand All @@ -1293,7 +1293,7 @@
case Uint32Type:
checkOverflow(func() bool {
l := big.NewInt(0).SetUint64(uint64(lv.GetUint32()))
r := big.NewInt(0).Lsh(l, rv.GetUint())
r := big.NewInt(0).Lsh(l, uint(rv.GetUint()))

return r.Cmp(big.NewInt(math.MaxUint32)) != 1
})
Expand All @@ -1302,15 +1302,15 @@
case Uint64Type:
checkOverflow(func() bool {
l := big.NewInt(0).SetUint64(lv.GetUint64())
r := big.NewInt(0).Lsh(l, rv.GetUint())
r := big.NewInt(0).Lsh(l, uint(rv.GetUint()))

return r.Cmp(big.NewInt(0).SetUint64(math.MaxUint64)) != 1
})

lv.SetUint64(lv.GetUint64() << rv.GetUint())
case BigintType, UntypedBigintType:
lb := lv.GetBigInt()
lb = big.NewInt(0).Lsh(lb, rv.GetUint())
lb = big.NewInt(0).Lsh(lb, uint(rv.GetUint()))
lv.V = BigintValue{V: lb}
default:
panic(fmt.Sprintf(
Expand All @@ -1335,8 +1335,8 @@
switch baseOf(lv.T) {
case IntType:
checkOverflow(func() bool {
l := big.NewInt(int64(lv.GetInt()))
r := big.NewInt(0).Rsh(l, rv.GetUint())
l := big.NewInt(lv.GetInt())
r := big.NewInt(0).Rsh(l, uint(rv.GetUint()))

return r.Cmp(big.NewInt(math.MaxInt)) != 1
})
Expand All @@ -1345,7 +1345,7 @@
case Int8Type:
checkOverflow(func() bool {
l := big.NewInt(int64(lv.GetInt8()))
r := big.NewInt(0).Rsh(l, rv.GetUint())
r := big.NewInt(0).Rsh(l, uint(rv.GetUint()))

Check warning on line 1348 in gnovm/pkg/gnolang/op_binary.go

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/op_binary.go#L1348

Added line #L1348 was not covered by tests

return r.Cmp(big.NewInt(math.MaxInt8)) != 1
})
Expand All @@ -1354,7 +1354,7 @@
case Int16Type:
checkOverflow(func() bool {
l := big.NewInt(int64(lv.GetInt16()))
r := big.NewInt(0).Rsh(l, rv.GetUint())
r := big.NewInt(0).Rsh(l, uint(rv.GetUint()))

Check warning on line 1357 in gnovm/pkg/gnolang/op_binary.go

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/op_binary.go#L1357

Added line #L1357 was not covered by tests

return r.Cmp(big.NewInt(math.MaxInt16)) != 1
})
Expand All @@ -1363,7 +1363,7 @@
case Int32Type, UntypedRuneType:
checkOverflow(func() bool {
l := big.NewInt(int64(lv.GetInt32()))
r := big.NewInt(0).Rsh(l, rv.GetUint())
r := big.NewInt(0).Rsh(l, uint(rv.GetUint()))

Check warning on line 1366 in gnovm/pkg/gnolang/op_binary.go

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/op_binary.go#L1366

Added line #L1366 was not covered by tests

return r.Cmp(big.NewInt(math.MaxInt32)) != 1
})
Expand All @@ -1372,16 +1372,16 @@
case Int64Type:
checkOverflow(func() bool {
l := big.NewInt(lv.GetInt64())
r := big.NewInt(0).Rsh(l, rv.GetUint())
r := big.NewInt(0).Rsh(l, uint(rv.GetUint()))

Check warning on line 1375 in gnovm/pkg/gnolang/op_binary.go

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/op_binary.go#L1375

Added line #L1375 was not covered by tests

return r.Cmp(big.NewInt(math.MaxInt64)) != 1
})

lv.SetInt64(lv.GetInt64() >> rv.GetUint())
case UintType:
checkOverflow(func() bool {
l := big.NewInt(0).SetUint64(uint64(lv.GetUint()))
r := big.NewInt(0).Rsh(l, rv.GetUint())
l := big.NewInt(0).SetUint64(lv.GetUint())
r := big.NewInt(0).Rsh(l, uint(rv.GetUint()))

return r.Cmp(big.NewInt(0).SetUint64(math.MaxUint)) != 1
})
Expand All @@ -1390,7 +1390,7 @@
case Uint8Type:
checkOverflow(func() bool {
l := big.NewInt(0).SetUint64(uint64(lv.GetUint8()))
r := big.NewInt(0).Rsh(l, rv.GetUint())
r := big.NewInt(0).Rsh(l, uint(rv.GetUint()))

Check warning on line 1393 in gnovm/pkg/gnolang/op_binary.go

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/op_binary.go#L1393

Added line #L1393 was not covered by tests

return r.Cmp(big.NewInt(math.MaxUint8)) != 1
})
Expand All @@ -1399,7 +1399,7 @@
case DataByteType:
checkOverflow(func() bool {
l := big.NewInt(0).SetUint64(uint64(lv.GetDataByte()))
r := big.NewInt(0).Rsh(l, rv.GetUint())
r := big.NewInt(0).Rsh(l, uint(rv.GetUint()))

Check warning on line 1402 in gnovm/pkg/gnolang/op_binary.go

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/op_binary.go#L1402

Added line #L1402 was not covered by tests

return r.Cmp(big.NewInt(math.MaxUint8)) != 1
})
Expand All @@ -1408,7 +1408,7 @@
case Uint16Type:
checkOverflow(func() bool {
l := big.NewInt(0).SetUint64(uint64(lv.GetUint16()))
r := big.NewInt(0).Rsh(l, rv.GetUint())
r := big.NewInt(0).Rsh(l, uint(rv.GetUint()))

Check warning on line 1411 in gnovm/pkg/gnolang/op_binary.go

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/op_binary.go#L1411

Added line #L1411 was not covered by tests

return r.Cmp(big.NewInt(math.MaxUint16)) != 1
})
Expand All @@ -1417,7 +1417,7 @@
case Uint32Type:
checkOverflow(func() bool {
l := big.NewInt(0).SetUint64(uint64(lv.GetUint32()))
r := big.NewInt(0).Rsh(l, rv.GetUint())
r := big.NewInt(0).Rsh(l, uint(rv.GetUint()))

Check warning on line 1420 in gnovm/pkg/gnolang/op_binary.go

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/op_binary.go#L1420

Added line #L1420 was not covered by tests

return r.Cmp(big.NewInt(math.MaxUint32)) != 1
})
Expand All @@ -1426,15 +1426,15 @@
case Uint64Type:
checkOverflow(func() bool {
l := big.NewInt(0).SetUint64(lv.GetUint64())
r := big.NewInt(0).Rsh(l, rv.GetUint())
r := big.NewInt(0).Rsh(l, uint(rv.GetUint()))

Check warning on line 1429 in gnovm/pkg/gnolang/op_binary.go

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/op_binary.go#L1429

Added line #L1429 was not covered by tests

return r.Cmp(big.NewInt(0).SetUint64(math.MaxUint64)) != 1
})

lv.SetUint64(lv.GetUint64() >> rv.GetUint())
case BigintType, UntypedBigintType:
lb := lv.GetBigInt()
lb = big.NewInt(0).Rsh(lb, rv.GetUint())
lb = big.NewInt(0).Rsh(lb, uint(rv.GetUint()))
lv.V = BigintValue{V: lb}
default:
panic(fmt.Sprintf(
Expand Down
10 changes: 5 additions & 5 deletions gnovm/pkg/gnolang/op_exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ func (m *Machine) doOpExec(op Op) {
case -1: // assign list element.
if bs.Key != nil {
iv := TypedValue{T: IntType}
iv.SetInt(bs.ListIndex)
iv.SetInt(int64(bs.ListIndex))
switch bs.Op {
case ASSIGN:
m.PopAsPointer(bs.Key).Assign2(m.Alloc, m.Store, m.Realm, iv, false)
Expand All @@ -180,7 +180,7 @@ func (m *Machine) doOpExec(op Op) {
}
if bs.Value != nil {
iv := TypedValue{T: IntType}
iv.SetInt(bs.ListIndex)
iv.SetInt(int64(bs.ListIndex))
ev := xv.GetPointerAtIndex(m.Alloc, m.Store, &iv).Deref()
switch bs.Op {
case ASSIGN:
Expand Down Expand Up @@ -262,7 +262,7 @@ func (m *Machine) doOpExec(op Op) {
case -1: // assign list element.
if bs.Key != nil {
iv := TypedValue{T: IntType}
iv.SetInt(bs.ListIndex)
iv.SetInt(int64(bs.ListIndex))
switch bs.Op {
case ASSIGN:
m.PopAsPointer(bs.Key).Assign2(m.Alloc, m.Store, m.Realm, iv, false)
Expand Down Expand Up @@ -904,7 +904,7 @@ func (m *Machine) doOpSwitchClause() {
// caiv := m.PeekValue(2) // switch clause case index (reuse)
cliv := m.PeekValue(3) // switch clause index (reuse)
idx := cliv.GetInt()
if idx >= len(ss.Clauses) {
if int(idx) >= len(ss.Clauses) { // XXX: idx may overflow on 32 bits platforms.
// no clauses matched: do nothing.
m.PopStmt() // pop switch stmt
m.PopValue() // pop switch tag value
Expand Down Expand Up @@ -980,7 +980,7 @@ func (m *Machine) doOpSwitchClauseCase() {
clidx := cliv.GetInt()
cl := ss.Clauses[clidx]
caidx := caiv.GetInt()
if (caidx + 1) < len(cl.Cases) {
if int(caidx+1) < len(cl.Cases) {
// try next clause case.
m.PushOp(OpSwitchClauseCase) // TODO consider sticky
caiv.SetInt(caidx + 1)
Expand Down
Loading
Loading