Search Apps Documentation Source Content File Folder Download Copy Actions Download State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

crossrealm.gno

1.38 Kb · 47 lines
 1package crossrealm_d
 2
 3// Simple stateful realm for cross-realm consistency tests.
 4// Separated from crossrealm_b to avoid perturbing its object IDs.
 5//
 6// Contains both crossing and non-crossing setters so tests can
 7// demonstrate that non-crossing calls from another realm cannot
 8// silently mutate state via assign+recover.
 9
10var counter int
11
12func init() {
13	counter = 100
14}
15
16// SetCounter: non-crossing. Calling this cross-realm triggers
17// the readonly check because it directly assigns a package var.
18func SetCounter(n int) {
19	counter = n
20}
21
22// SetCounterCrossing: crossing version. This works correctly
23// cross-realm because the caller enters this realm's context.
24func SetCounterCrossing(cur realm, n int) {
25	counter = n
26}
27
28func GetCounter(cur realm) int {
29	return counter
30}
31
32// DoubleCounter reads counter and doubles it. Used to show that
33// if counter were silently corrupted in memory, subsequent crossing
34// calls would act on the wrong value.
35func DoubleCounter(cur realm) int {
36	counter = counter * 2
37	return counter
38}
39
40// MutateBytes mutates the first byte of bz to 0xff. Used to test
41// whether a foreign realm can write to caller-allocated bytes.
42// Under storage=authority, bz has PkgID=caller; borrow rule #1 here
43// makes m.Realm=crossrealm_d (declaring realm); the write site
44// (bz[0] = ...) should fire readonly because bz.PkgID != m.Realm.ID.
45func MutateBytes(bz []byte) {
46	bz[0] = 0xff
47}