Skip to content Search Apps Documentation Source Content Run Reset Eval Playground File Folder Download Copy Actions State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

Playground

exploit.gno

package vm

var MyFoo *Foo

type Foo struct {
	A int
	B *Foo
}

// method to mutate

func (f *Foo) UpdateFoo(x int) {
	f.A = x
}

func init() {
	MyFoo = &Foo{
		A: 1,
		B: &Foo{
			A: 2,
		},
	}
}

gnomod.toml

module = "gno.land/r/tests/vm"
gno = "0.9"

[addpkg]
  creator = "g1edq4dugw0sgat4zxcw9xardvuydqf6cgleuc8p"

interfaces.gno

package vm

import (
	"strconv"
)

type Stringer interface {
	String() string
}

var stringers []Stringer

func AddStringer(cur realm, str Stringer) {
	// NOTE: this is ridiculous, a slice that will become too long
	// eventually.  Don't do this in production programs; use
	// gno.land/p/nt/avl/v0 or similar structures.
	stringers = append(stringers, str)
}

func Render(path string) string {
	res := ""
	// NOTE: like the function above, this function too will eventually
	// become too expensive to call.
	for i, stringer := range stringers {
		res += strconv.Itoa(i) + ": " + stringer.String() + "\n"
	}
	return res
}

nestedpkg_test.gno

package vm

import (
	"testing"
)

func TestNestedPkg(cur realm, t *testing.T) {
	// direct child
	curPath := "gno.land/r/tests/vm/foo"
	testing.SetRealm(testing.NewCodeRealm(curPath))
	if !IsCallerSubPath(cross(cur)) {
		t.Errorf(curPath + " should be a sub path")
	}
	if IsCallerParentPath(cross(cur)) {
		t.Errorf(curPath + " should not be a parent path")
	}
	if !HasCallerSameNamespace(cross(cur)) {
		t.Errorf(curPath + " should be from the same namespace")
	}

	// grand-grand-child
	curPath = "gno.land/r/tests/vm/foo/bar/baz"
	testing.SetRealm(testing.NewCodeRealm(curPath))
	if !IsCallerSubPath(cross(cur)) {
		t.Errorf(curPath + " should be a sub path")
	}
	if IsCallerParentPath(cross(cur)) {
		t.Errorf(curPath + " should not be a parent path")
	}
	if !HasCallerSameNamespace(cross(cur)) {
		t.Errorf(curPath + " should be from the same namespace")
	}

	// NOTE: This is now back in the gno.land/r/tests/vm structure,
	// so the direct parent test case is valid again.

	// direct parent (was previously fake parent)
	curPath = "gno.land/r/test" // without the 's' at the end
	testing.SetRealm(testing.NewCodeRealm(curPath))
	if IsCallerSubPath(cross(cur)) {
		t.Errorf(curPath + " should not be a sub path")
	}
	if IsCallerParentPath(cross(cur)) {
		t.Errorf(curPath + " should not be a parent path")
	}
	if HasCallerSameNamespace(cross(cur)) {
		t.Errorf(curPath + " should not be from the same namespace")
	}

	// fake parent (prefix)
	curPath = "gno.land/r/dem"
	testing.SetRealm(testing.NewCodeRealm(curPath))
	if IsCallerSubPath(cross(cur)) {
		t.Errorf(curPath + " should not be a sub path")
	}
	if IsCallerParentPath(cross(cur)) {
		t.Errorf(curPath + " should not be a parent path")
	}
	if HasCallerSameNamespace(cross(cur)) {
		t.Errorf(curPath + " should not be from the same namespace")
	}

	// different namespace
	curPath = "gno.land/r/foo"
	testing.SetRealm(testing.NewCodeRealm(curPath))
	if IsCallerSubPath(cross(cur)) {
		t.Errorf(curPath + " should not be a sub path")
	}
	if IsCallerParentPath(cross(cur)) {
		t.Errorf(curPath + " should not be a parent path")
	}
	if HasCallerSameNamespace(cross(cur)) {
		t.Errorf(curPath + " should not be from the same namespace")
	}
}

realm_compositelit.gno

package vm

type (
	Word uint
	nat  []Word
)

var zero = &Int{
	neg: true,
	abs: []Word{0},
}

// structLit
type Int struct {
	neg bool
	abs nat
}

func GetZeroType() nat {
	a := zero.abs
	return a
}

realm_method38d.gno

package vm

var abs nat

func (n nat) Add() nat {
	return []Word{0}
}

func GetAbs(cur realm) nat {
	abs = []Word{0}
	return abs
}

func AbsAdd(cur realm) nat {
	rt := GetAbs(cur).Add()
	return rt
}

tests.gno

package vm

import (
	"chain/runtime"
	"chain/runtime/unsafe"

	"gno.land/p/nt/nestedpkg/v0"
	rsubtests "gno.land/r/tests/vm/subtests"
)

var counter int

func IncCounter(cur realm) {
	counter++
}

func Counter(cur realm) int {
	return counter
}

func CurrentRealmPath(cur realm) string {
	return unsafe.CurrentRealm().PkgPath()
}

var initOriginCaller = unsafe.OriginCaller()

func InitOriginCaller(cur realm) address {
	return initOriginCaller
}

func CallAssertOriginCall(cur realm) {
	runtime.AssertOriginCall()
}

func CallIsOriginCall(cur realm) bool {
	// XXX: consider return !unsafe.PreviousRealm().IsCode()
	return unsafe.PreviousRealm().IsUser()
}

func CallSubtestsAssertOriginCall(cur realm) {
	rsubtests.CallAssertOriginCall(cross(cur))
}

func CallSubtestsIsOriginCall(cur realm) bool {
	return rsubtests.CallIsOriginCall(cross(cur))
}

//----------------------------------------
// Test structure to ensure cross-realm modification is prevented.

type TestRealmObject struct {
	Field string
}

var TestRealmObjectValue TestRealmObject

// NewTestRealmObject returns a fresh heap-allocated TestRealmObject.
// Non-crossing — relies on borrow rule #1 to set m.Realm = /r/tests/vm
// inside the body so the composite literal passes checkConstructionTime.
func NewTestRealmObject() *TestRealmObject {
	return &TestRealmObject{Field: "initial"}
}

func ModifyTestRealmObject(cur realm, t *TestRealmObject) {
	t.Field += "_modified"
}

func (t *TestRealmObject) Modify() {
	t.Field += "_modified"
}

//----------------------------------------
// Test helpers to test a particular realm bug.

type TestNode struct {
	Name  string
	Child *TestNode
}

var (
	gTestNode1 *TestNode
	gTestNode2 *TestNode
	gTestNode3 *TestNode
)

func InitTestNodes(cur realm) {
	gTestNode1 = &TestNode{Name: "first"}
	gTestNode2 = &TestNode{Name: "second", Child: &TestNode{Name: "second's child"}}
}

func ModTestNodes(cur realm) {
	tmp := &TestNode{}
	tmp.Child = gTestNode2.Child
	gTestNode3 = tmp // set to new-real
	// gTestNode1 = tmp.Child // set back to original is-real
	gTestNode3 = nil // delete.
}

func PrintTestNodes() {
	println(gTestNode2.Child.Name)
}

func GetPreviousRealm(cur realm) runtime.Realm {
	return unsafe.PreviousRealm()
}

func GetRSubtestsPreviousRealm(cur realm) runtime.Realm {
	return rsubtests.GetPreviousRealm(cross(cur))
}

func Exec(fn func()) {
	// no realm switching.
	fn()
}

// ExecRlm mirrors Exec but threads the caller's rlm into the callback
// so the callback can use `cross(rlm)` instead of bare `cross`.
func ExecRlm(_ int, rlm realm, fn func(_ int, rlm realm)) {
	fn(0, rlm)
}

func ExecSwitch(cur realm, fn func()) {
	fn()
}

// ExecSwitchRlm is the rlm-threaded variant of ExecSwitch — crosses
// into this realm and passes the callee's cur to the callback so
// the callback can `cross(rlm)` against the switched realm.
func ExecSwitchRlm(cur realm, fn func(_ int, rlm realm)) {
	fn(0, cur)
}

func IsCallerSubPath(cur realm) bool {
	return nestedpkg.IsCallerSubPath(0, cur)
}

func IsCallerParentPath(cur realm) bool {
	return nestedpkg.IsCallerParentPath(0, cur)
}

func HasCallerSameNamespace(cur realm) bool {
	return nestedpkg.IsSameNamespace(0, cur)
}

func BankerOriginSend(cur realm) string {
	return unsafe.OriginSend().String()
}

func RTestsOriginSend(cur realm) string {
	return rsubtests.BankerOriginSend(cross(cur))
}

tests_test.gno

package vm_test

import (
	"chain"
	"fmt"
	"testing"

	"gno.land/p/nt/testutils/v0"
	tests "gno.land/r/tests/vm"
)

func TestAssertOriginCall(cur realm, t *testing.T) {
	// CallAssertOriginCall(): no panic
	caller := testutils.TestAddress("caller")
	testing.SetRealm(testing.NewUserRealm(caller))
	tests.CallAssertOriginCall(cross(cur))
	if !tests.CallIsOriginCall(cross(cur)) {
		t.Errorf("expected IsOriginCall=true but got false")
	}

	testing.SetRealm(testing.NewCodeRealm("gno.land/r/tests/vm"))
	// CallAssertOriginCall() from a block: abort
	r := revive(func() {
		// if called inside a function literal, this is no longer an origin call
		// because there's one additional frame (the function literal block).
		if tests.CallIsOriginCall(cross(cur)) {
			t.Errorf("expected IsOriginCall=false but got true")
		}
		tests.CallAssertOriginCall(cross(cur)) // <---
	})
	if fmt.Sprintf("%v", r) != "invalid non-origin call" {
		t.Error("expected abort but did not")
	}
	// CallSubtestsAssertOriginCall(): abort
	r = revive(func() {
		// if called inside a function literal, this is no longer an origin call
		// because there's one additional frame (the function literal block).
		if tests.CallSubtestsIsOriginCall(cross(cur)) {
			t.Errorf("expected IsOriginCall=false but got true")
		}
		tests.CallSubtestsAssertOriginCall(cross(cur))
	})
	if fmt.Sprintf("%v", r) != "invalid non-origin call" {
		t.Error("expected abort but did not")
	}
}

func TestPreviousRealm(cur realm, t *testing.T) {
	var (
		firstRealm = chain.PackageAddress("gno.land/r/tests/vm_test")
		rTestsAddr = chain.PackageAddress("gno.land/r/tests/vm")
	)
	// When only one realm in the frames, PreviousRealm returns the same realm
	if addr := tests.GetPreviousRealm(cross(cur)).Address(); addr != firstRealm {
		println(tests.GetPreviousRealm(cross(cur)))
		t.Errorf("want GetPreviousRealm().Address==%s, got %s", firstRealm, addr)
	}
	// When 2 or more realms in the frames, PreviousRealm returns the second to last
	if addr := tests.GetRSubtestsPreviousRealm(cross(cur)).Address(); addr != rTestsAddr {
		t.Errorf("want GetRSubtestsPreviousRealm().Address==%s, got %s", rTestsAddr, addr)
	}
}