z_evil_filetest.gno
1.74 Kb · 57 lines
1// PKGPATH: gno.land/r/test/spikeevil
2
3// Spike (a), adversarial case. This file IS the foreign engine realm: its
4// evil type is declared in /r/test/spikeevil, so by interrealm v2 borrow
5// rule #1 its methods run with spikeevil's authority even when the registry
6// calls them. The expected output below is the hypothesis under test:
7//
8// - a direct write through a pointer into registry state is refused;
9// - a call to the registry's non-crossing mutator is NOT refused (the
10// hazard that core's invariant I-12 exists to rule out);
11// - a panic only aborts the read that ran the engine.
12package spikeevil
13
14import (
15 "gno.land/r/g19dzk54hhsdc3gllpksuj6gu4a3e6fl8cr6rn4w/minhwa/spike/registry"
16)
17
18type evil struct{}
19
20func (evil) Name() string { return "evil" }
21
22func (evil) Render(seed string) string {
23 switch seed {
24 case "write":
25 registry.SharedBoard().Count = 999
26 return "<svg>wrote</svg>"
27 case "bump":
28 registry.UnsafeBump()
29 return "<svg>bumped</svg>"
30 case "panic":
31 panic("evil: render refused")
32 }
33 return "<svg>" + seed + "</svg>"
34}
35
36func main(cur realm) {
37 id := registry.Register(cross(cur), evil{})
38 println("registered:", id)
39
40 r := revive(func() { registry.RenderWith(id, "write") })
41 println("write aborted:", r != nil, "board:", registry.BoardCount())
42
43 r = revive(func() { registry.RenderWith(id, "bump") })
44 println("bump aborted:", r != nil, "bumps:", registry.Bumps())
45
46 r = revive(func() { registry.RenderWith(id, "panic") })
47 println("panic aborted:", r != nil, "registered:", registry.IsRegistered(id))
48
49 println("normal:", registry.RenderWith(id, "x"))
50}
51
52// Output:
53// registered: gno.land/r/test/spikeevil
54// write aborted: true board: 0
55// bump aborted: false bumps: 1
56// panic aborted: true registered: true
57// normal: <svg>x</svg>