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: invoke user recover with implicit panics #3067

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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
24 changes: 23 additions & 1 deletion gnovm/pkg/gnolang/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -829,7 +829,9 @@

func (m *Machine) RunMain() {
defer func() {
if r := recover(); r != nil {
r := recover()

if r != nil {
switch r := r.(type) {
case UnhandledPanicError:
fmt.Printf("Machine.RunMain() panic: %s\nStacktrace: %s\n",
Expand Down Expand Up @@ -1280,6 +1282,26 @@
// main run loop.

func (m *Machine) Run() {
defer func() {
r := recover()

if r != nil {
switch r := r.(type) {
case *Exception:
panicStmt := &PanicStmt{
Exception: &BasicLitExpr{Value: `"` + r.Sprint(m) + `"`, Kind: STRING},
Dismissed Show dismissed Hide dismissed
}

m.PushStmt(panicStmt)
m.PushOp(OpExec)
Comment on lines +1291 to +1296
Copy link
Member

Choose a reason for hiding this comment

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

Maybe this ?

Suggested change
panicStmt := &PanicStmt{
Exception: &BasicLitExpr{Value: `"` + r.Sprint(m) + `"`, Kind: STRING},
}
m.PushStmt(panicStmt)
m.PushOp(OpExec)
m.Panic(r.Value)

Or Instead of panicking upon detecting a runtime panic, you can use m.Panic.


m.Run()
default:
panic(r)
}
}
}()

for {
if m.Debugger.enabled {
m.Debug()
Expand Down
6 changes: 5 additions & 1 deletion gnovm/pkg/gnolang/op_assign.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,11 @@ func (m *Machine) doOpQuoAssign() {
}
}
// lv /= rv
quoAssign(lv.TV, rv)
err := quoAssign(m, lv.TV, rv)
if err != nil {
panic(err)
}

if lv.Base != nil {
m.Realm.DidUpdate(lv.Base.(Object), nil, nil)
}
Expand Down
60 changes: 58 additions & 2 deletions gnovm/pkg/gnolang/op_binary.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,10 @@ func (m *Machine) doOpQuo() {
}

// lv / rv
quoAssign(lv, rv)
err := quoAssign(m, lv, rv)
if err != nil {
panic(err)
}
}

func (m *Machine) doOpRem() {
Expand Down Expand Up @@ -845,45 +848,96 @@ func mulAssign(lv, rv *TypedValue) {
}

// for doOpQuo and doOpQuoAssign.
func quoAssign(lv, rv *TypedValue) {
func quoAssign(m *Machine, lv, rv *TypedValue) *Exception {
expt := &Exception{
Value: typedString("division by zero"),
Frame: m.LastFrame(),
Stacktrace: m.Stacktrace(),
}

// set the result in lv.
// NOTE this block is replicated in op_assign.go
switch baseOf(lv.T) {
case IntType:
if rv.GetInt() == 0 {
return expt
}
lv.SetInt(lv.GetInt() / rv.GetInt())
case Int8Type:
if rv.GetInt8() == 0 {
return expt
}
lv.SetInt8(lv.GetInt8() / rv.GetInt8())
case Int16Type:
if rv.GetInt16() == 0 {
return expt
}
lv.SetInt16(lv.GetInt16() / rv.GetInt16())
case Int32Type, UntypedRuneType:
if rv.GetInt32() == 0 {
return expt
}
lv.SetInt32(lv.GetInt32() / rv.GetInt32())
case Int64Type:
if rv.GetInt64() == 0 {
return expt
}
lv.SetInt64(lv.GetInt64() / rv.GetInt64())
case UintType:
if rv.GetUint() == 0 {
return expt
}
lv.SetUint(lv.GetUint() / rv.GetUint())
case Uint8Type:
if rv.GetUint8() == 0 {
return expt
}
lv.SetUint8(lv.GetUint8() / rv.GetUint8())
case DataByteType:
if rv.GetUint8() == 0 {
return expt
}
lv.SetDataByte(lv.GetDataByte() / rv.GetUint8())
case Uint16Type:
if rv.GetUint16() == 0 {
return expt
}
lv.SetUint16(lv.GetUint16() / rv.GetUint16())
case Uint32Type:
if rv.GetUint32() == 0 {
return expt
}
lv.SetUint32(lv.GetUint32() / rv.GetUint32())
case Uint64Type:
if rv.GetUint64() == 0 {
return expt
}
lv.SetUint64(lv.GetUint64() / rv.GetUint64())
case Float32Type:
// NOTE: gno doesn't fuse *+.
if rv.GetFloat32() == 0 {
return expt
}
lv.SetFloat32(lv.GetFloat32() / rv.GetFloat32())
// XXX FOR DETERMINISM, PANIC IF NAN.
case Float64Type:
// NOTE: gno doesn't fuse *+.
if rv.GetFloat64() == 0 {
return expt
}
lv.SetFloat64(lv.GetFloat64() / rv.GetFloat64())
// XXX FOR DETERMINISM, PANIC IF NAN.
case BigintType, UntypedBigintType:
if rv.GetBigInt().Sign() == 0 {
return expt
}
lb := lv.GetBigInt()
lb = big.NewInt(0).Quo(lb, rv.GetBigInt())
lv.V = BigintValue{V: lb}
case BigdecType, UntypedBigdecType:
if rv.GetBigDec().Cmp(apd.New(0, 0)) == 0 {
return expt
}
lb := lv.GetBigDec()
rb := rv.GetBigDec()
quo := apd.New(0, 0)
Expand All @@ -898,6 +952,8 @@ func quoAssign(lv, rv *TypedValue) {
lv.T,
))
}

return nil
}

// for doOpRem and doOpRemAssign.
Expand Down
Loading