Skip to content

Commit

Permalink
chore: demonstrate ownership bug
Browse files Browse the repository at this point in the history
Signed-off-by: Norman Meier <norman@berty.tech>
  • Loading branch information
n0izn0iz committed Jul 13, 2023
1 parent 52ea535 commit 617c208
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 2 deletions.
5 changes: 5 additions & 0 deletions examples/gno.land/r/demo/bugs/mis_ownership/gno.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module "gno.land/r/demo/bugs/mis_ownership"

require (
"gno.land/r/demo/bugs/steal_ownership" v0.0.0-latest
)
16 changes: 16 additions & 0 deletions examples/gno.land/r/demo/bugs/mis_ownership/mis_ownership.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package mis_ownership

import "gno.land/r/demo/bugs/steal_ownership"

var (
x = uint32(42)
ptr = &x
)

func init() {
steal_ownership.Steal(ptr)
}

func Mutate() {
*ptr = 21
}
1 change: 1 addition & 0 deletions examples/gno.land/r/demo/bugs/steal_ownership/gno.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module "gno.land/r/demo/bugs/steal_ownership"
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package steal_ownership

var ptr *uint32

func Steal(xptr *uint32) {
ptr = xptr
}
17 changes: 15 additions & 2 deletions gnovm/pkg/gnolang/realm.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,16 @@ func (pid PkgID) Bytes() []byte {
return pid.Hashlet[:]
}

var pathsFromIds = make(map[string]string)

func PkgIDFromPkgPath(path string) PkgID {
return PkgID{HashBytes([]byte(path))}
id := PkgID{HashBytes([]byte(path))}
pathsFromIds[id.String()] = path
return id
}

func PkgPathFromPkgID(id PkgID) string {
return pathsFromIds[id.String()]
}

func ObjectIDFromPkgPath(path string) ObjectID {
Expand Down Expand Up @@ -145,7 +153,12 @@ func (rlm *Realm) DidUpdate(po, xo, co Object) {
return // do nothing.
}
if po.GetObjectID().PkgID != rlm.ID {
panic("cannot modify external-realm or non-realm object")
opid := po.GetObjectID().PkgID
prettyName := PkgPathFromPkgID(opid)
if prettyName == "" {
prettyName = opid.String()
}
panic(fmt.Sprintf("cannot modify external-realm or non-realm object: object pkg path: %s, realm: %s", prettyName, rlm.Path))
}
// From here on, po is real (not new-real).
// Updates to .newCreated/.newEscaped /.newDeleted made here. (first gen)
Expand Down

0 comments on commit 617c208

Please sign in to comment.