Transaction
11542C721A7E9D…1F8933770A1F
Block 923,219 · index 0 · indexed
Summary
- Hash
- 11542C721A7E9D8965D3F44C50968340855024B6443935B83E091F8933770A1F
- Block
- 923,219
- Size
- 60623 bytes
- Gas used
- 76,783,478 / 180,236,600
- Fee
- 540709 ugnot
- Status
- success
Messages
- Attached funds
- 10000000 ugnot
Arguments · 11
- #1bfdemo
- #2README.md
- #3# r/moul/x/vm/bfdemo A realm that runs Brainfuck programs on chain, a slice at a time. It is a demo of [`p/moul/x/vm/bf`](/p/moul/x/vm/bf/v0) (the machine) and [`p/moul/x/vm/vmkit`](/p/moul/x/vm/vmkit/v0) (the host ABI, the fuel meter, the instance store), and carries no logic of its own. What it exists to show is the thing gno realm code cannot do for itself: a program that runs out of fuel does not fail, it pauses. The realm keeps the snapshot, and the next caller pays for the next slice. Upload a program, call `Step` a few times, and watch one computation finish across several transactions. ## Calls | function | what | |---|---| | `Upload(src, input, budget)` | compile and store a program, returns its id. Compilation happens here, so a malformed program is rejected by the transaction that submitted it | | `Step(id, fuel)` | run one slice and keep the snapshot. Anyone may pay for a slice, not only the owner | | `Remove(id)` | delete an instance. Owner only | `Render("/")` lists the instances and offers three sample programs. `Render("/<id>")` shows one instance: its compiled program, status, fuel, and output. ## Limits Everything a caller can grow is bounded, because all of it is storage somebody pays a deposit on: 64 instances, 64 KiB of source, 1 KiB of input, 4 KiB of output, 5,000,000 fuel per slice, 30,000 tape cells. A guest that writes past the output cap is trapped rather than truncated, so the rendered output is never a lie. ## Untrusted input A program's source is caller-supplied, and everything outside the eight operators is a comment that may hold anything at all. The page never renders it: it shows the program the machine actually compiled, which cannot contain a backtick or a newline and so cannot break out of its code fence. Guest output is arbitrary bytes and is escaped the same way, backtick included. No instance is funded, so `Host.Send` always returns `ErrNotGranted`. That is the capability rule doing its job, not a missing feature. <!-- BEGIN GNOCONTRACTS FOOTER (generated by `make readmes`; do not edit below) --> --- Part of **[moul/gno-contracts](https://github.com/moul/gno-contracts)** — moul's versioned gno.land contracts. See the repository for the full catalog, build/test tooling, and usage. **On mainnet:** [](https://gnoscope.com/realm/r/moul/x/vm/bfdemo/v0) [](https://gnoscope.com/realm/r/moul/x/vm/bfdemo/v0) [](https://gnoscope.com/realm/r/moul/x/vm/bfdemo/v0) [](https://gnoscope.com/realm/r/moul/x/vm/bfdemo/v0) **Dependency graph:**  > 🧪 **Highly experimental — potentially vibe-coded.** Not audited; may break, change, or be removed at any time. Do not use with anything of value. Full disclaimer: [DISCLAIMER](https://github.com/moul/gno-contracts/blob/main/DISCLAIMER.md). <!-- END GNOCONTRACTS FOOTER -->
- #4bfdemo.gno
- #5// Package bfdemo is the playground for the guest-VM work: a realm that runs // Brainfuck programs on chain, a slice at a time. // // It is a demo of two libraries and carries no logic of its own: // [p/moul/x/vm/bf](/p/moul/x/vm/bf/v0) is the machine, and // [p/moul/x/vm/vmkit](/p/moul/x/vm/vmkit/v0) is the host ABI, the fuel meter // and the instance store. // // What it exists to show is the thing gno realm code cannot do for itself: a // program that runs out of fuel does not fail, it pauses. The realm keeps the // snapshot, and the next caller pays for the next slice. Upload a program, // call Step a few times, and watch one computation finish across several // transactions. package bfdemo import ( "chain" "chain/runtime" "chain/runtime/unsafe" "time" "gno.land/p/moul/x/vm/bf/v0" "gno.land/p/moul/x/vm/vmkit/v0" "gno.land/p/nt/avl/v0" "gno.land/p/nt/seqid/v0" "gno.land/p/nt/ufmt/v0" ) // Caps. Everything a caller can grow is bounded, because all of it is storage // somebody pays a deposit on. const ( // MaxInstances is how many programs the realm keeps at once. Past it, // an upload has to wait for someone to remove one. MaxInstances = 64 // MaxOutput caps the bytes one program may write. Past it the guest is // trapped rather than truncated, so rendered output is never a lie. MaxOutput = 4096 // MaxInput caps the call input a program can be given. MaxInput = 1024 // DefaultFuel is the budget an upload gets when it asks for none, and // the slice size Step uses when asked for none. DefaultFuel = 100000 // MaxSliceFuel bounds one transaction's work regardless of what the // caller asked for. MaxSliceFuel = 5000000 ) var ( // store holds the instances: program, snapshot, status, accounting. store = vmkit.NewStore() // inputs holds each instance's call input, keyed by id. It is separate // from the instance so vmkit.Instance stays the VM-agnostic record it // is meant to be. inputs = avl.NewTree() idgen seqid.ID count int ) // host is the realm-backed [vmkit.Host]. One is built per call, wrapping the // instance being stepped, so a guest's output and authority are scoped to it // and nothing ambient leaks in. type host struct { inst *vmkit.Instance in []byte kv *avl.Tree overrun bool // the guest wrote past MaxOutput } func (h *host) Caller() address { return h.inst.Owner } func (h *host) Origin() address { return h.inst.Owner } func (h *host) Now() int64 { return time.Now().Unix() } func (h *host) Height() int64 { return runtime.ChainHeight() } func (h *host) Input() []byte { return h.in } // Get and Set are scoped to the instance by construction: the tree belongs to // the instance being stepped, so one program cannot reach another's storage // even though both live in this one realm. bf never calls them; they are here // because the ABI is the point. func (h *host) Get(key []byte) []byte { v := h.kv.Get(string(key)) if v == nil { return nil } return v.([]byte) } func (h *host) Set(key, val []byte) { h.kv.Set(string(key), val) } func (h *host) Output(p []byte) { if len(h.inst.Output)+len(p) > MaxOutput { h.overrun = true return } h.inst.Output = append(h.inst.Output, p...) } func (h *host) Emit(typ string, kv ...string) { chain.Emit(typ, kv...) } // Send is never granted here. The demo funds no instance, so a guest that // tries to move coins is refused. That is the capability rule doing its job, // not a missing feature. func (h *host) Send(to address, amount int64) error { return vmkit.ErrNotGranted } func (h *host) Log(msg string) {} // Upload compiles src and stores it as a new instance, returning its id. // // Compilation happens here rather than at the first Step, so an unbalanced // program is rejected by the transaction that submitted it instead of costing // somebody else the gas later. func Upload(cur realm, src, input string, budget int64) string { if count >= MaxInstances { panic("bfdemo: too many instances, remove one first") } if len(input) > MaxInput { panic("bfdemo: input too long") } if _, err := bf.CompileDefault(src); err != nil { panic("bfdemo: " + err.Error()) } if budget <= 0 { budget = DefaultFuel } id := idgen.Next().String() owner := unsafe.PreviousRealm().Address() store.Set(vmkit.NewInstance(id, owner, bf.VMName, []byte(src), budget)) if input != "" { inputs.Set(id, input) } count++ chain.Emit("bf_upload", "id", id, "bytes", ufmt.Sprintf("%d", len(src))) return id } // Step runs one slice of the instance: up to `fuel` guest ops, then stop and // keep the snapshot. Anyone may pay for a slice, not only the owner: a paused // program that only its owner can advance is a worse demo and no safer, since // the program and its budget were both fixed at upload. func Step(cur realm, id string, fuel int64) string { inst := store.Get(id) if inst == nil { panic("bfdemo: no such instance") } if inst.Status != vmkit.Running { panic("bfdemo: instance is " + inst.Status.String()) } if fuel <= 0 { fuel = DefaultFuel } if fuel > MaxSliceFuel { fuel = MaxSliceFuel } prog, err := bf.CompileDefault(string(inst.Program)) if err != nil { panic("bfdemo: " + err.Error()) } h := &host{inst: inst, in: []byte(inputOf(id)), kv: avl.NewTree()} if err := inst.Run(bf.NewMachine(prog), h, fuel); err != nil { panic("bfdemo: " + err.Error()) } if h.overrun { inst.Status = vmkit.Trapped inst.Trap = "output limit reached" } chain.Emit("bf_step", "id", id, "status", inst.Status.String(), "fuel", ufmt.Sprintf("%d", inst.FuelUsed), ) return inst.Status.String() } // Remove deletes an instance. Owner only. func Remove(cur realm, id string) { inst := store.Get(id) if inst == nil { panic("bfdemo: no such instance") } if inst.Owner != unsafe.PreviousRealm().Address() { panic("bfdemo: not your instance") } store.Remove(id) inputs.Remove(id) count-- } func inputOf(id string) string { v := inputs.Get(id) if v == nil { return "" } return v.(string) }
- #6bfdemo_test.gno
- #7package bfdemo import ( "strings" "testing" "gno.land/p/moul/x/vm/vmkit/v0" "gno.land/p/nt/avl/v0" "gno.land/p/nt/seqid/v0" "gno.land/p/nt/testutils/v0" "gno.land/p/nt/uassert/v0" ) const hello = "++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------." // resetState puts the realm globals back where init() left them. Realm state // persists for the whole test binary, so every test that asserts on ids or on // the rendered instance list has to start from here. func resetState() { store = vmkit.NewStore() inputs = avl.NewTree() idgen = seqid.ID(0) count = 0 } func TestUploadAndRunToCompletion(cur realm, t *testing.T) { resetState() alice := testutils.TestAddress("alice") testing.SetRealm(testing.NewUserRealm(alice)) id := Upload(cross(cur), hello, "", 0) uassert.Equal(t, "halted", Step(cross(cur), id, 0)) inst := store.Get(id) uassert.Equal(t, "Hello World", string(inst.Output)) uassert.Equal(t, int64(1), inst.Slices) uassert.Equal(t, alice, inst.Owner) } // TestOneProgramAcrossManyTransactions is what the realm exists to // demonstrate: the same computation, finished over several calls, each paying // for its own slice. func TestOneProgramAcrossManyTransactions(cur realm, t *testing.T) { resetState() testing.SetRealm(testing.NewUserRealm(testutils.TestAddress("alice"))) id := Upload(cross(cur), hello, "", 0) status := "" slices := 0 for slices < 500 { status = Step(cross(cur), id, 7) slices++ if status != "running" { break } } inst := store.Get(id) uassert.Equal(t, "halted", status) uassert.Equal(t, "Hello World", string(inst.Output)) uassert.True(t, inst.Slices > 1, "took more than one slice") } func TestInputIsReadThroughTheHost(cur realm, t *testing.T) { resetState() testing.SetRealm(testing.NewUserRealm(testutils.TestAddress("alice"))) // ",[.,]" echoes its input back. id := Upload(cross(cur), ",[.,]", "gno.land", 0) uassert.Equal(t, "halted", Step(cross(cur), id, 0)) uassert.Equal(t, "gno.land", string(store.Get(id).Output)) } func TestBudgetStopsAProgram(cur realm, t *testing.T) { resetState() testing.SetRealm(testing.NewUserRealm(testutils.TestAddress("alice"))) id := Upload(cross(cur), hello, "", 12) uassert.Equal(t, "out of fuel", Step(cross(cur), id, 0)) uassert.Equal(t, int64(12), store.Get(id).FuelUsed) // And it stays stopped rather than quietly continuing. uassert.AbortsContains(t, cur, "instance is out of fuel", func() { Step(cross(cur), id, 0) }) } func TestUploadRejectsAMalformedProgram(cur realm, t *testing.T) { resetState() testing.SetRealm(testing.NewUserRealm(testutils.TestAddress("alice"))) // Rejected by the transaction that submitted it, not by whoever pays // for the first slice later. uassert.AbortsContains(t, cur, "unmatched", func() { Upload(cross(cur), "+++[", "", 0) }) uassert.Equal(t, 0, count) } func TestRemoveIsOwnerOnly(cur realm, t *testing.T) { resetState() alice := testutils.TestAddress("alice") bob := testutils.TestAddress("bob") testing.SetRealm(testing.NewUserRealm(alice)) id := Upload(cross(cur), hello, "", 0) testing.SetRealm(testing.NewUserRealm(bob)) uassert.AbortsContains(t, cur, "not your instance", func() { Remove(cross(cur), id) }) // Anyone may pay for a slice, though: the program and its budget were // both fixed at upload. uassert.Equal(t, "halted", Step(cross(cur), id, 0)) testing.SetRealm(testing.NewUserRealm(alice)) Remove(cross(cur), id) uassert.Equal(t, 0, count) uassert.True(t, store.Get(id) == nil) } func TestOutputIsCappedAndTrapped(cur realm, t *testing.T) { resetState() testing.SetRealm(testing.NewUserRealm(testutils.TestAddress("alice"))) // A cell set to 1 and printed forever: an infinite writer. id := Upload(cross(cur), "+[.]", "", 0) status := Step(cross(cur), id, 0) inst := store.Get(id) uassert.Equal(t, "trapped", status) uassert.Equal(t, "output limit reached", inst.Trap) uassert.Equal(t, MaxOutput, len(inst.Output)) } func TestRenderUnknownInstance(t *testing.T) { resetState() uassert.True(t, strings.Contains(Render("/nope"), "No such instance")) } // TestRenderEscapesGuestOutput pins the rule that a realm cannot be fixed in // place once it is live: everything a caller controls is escaped before it // reaches the page. func TestRenderEscapesGuestOutput(cur realm, t *testing.T) { resetState() testing.SetRealm(testing.NewUserRealm(testutils.TestAddress("alice"))) // Print a backtick (96), which would otherwise close the code fence. src := strings.Repeat("+", 96) + "." id := Upload(cross(cur), src, "", 0) Step(cross(cur), id, 0) page := Render("/" + id) uassert.Equal(t, "`", string(store.Get(id).Output)) uassert.True(t, strings.Contains(page, "\\x60"), "the backtick is escaped") // The comment text in a program never reaches the page at all: only // the eight operators do. uassert.Equal(t, "+++[-]<>", operatorsOnly("+++ this is a comment [-] </script>")) } // Output sits in a code span, where markdown escapes show verbatim: only the // pipe is escaped there, and nothing else. func TestCodeCell(t *testing.T) { uassert.Equal(t, "`Hello, gno.land\\n`", codeCell("Hello, gno.land\\n")) uassert.Equal(t, "`a\\|b`", codeCell("a|b")) } // emit returns a program that prints s, one byte at a time, so a test can put // any text in the output column without hand-writing Brainfuck. func emit(s string) string { var sb strings.Builder for i := 0; i < len(s); i++ { sb.WriteString("[-]" + strings.Repeat("+", int(s[i])) + ".") } return sb.String() } // instanceRow returns the first instance row of the home page. func instanceRow(t *testing.T) string { for _, line := range strings.Split(Render(""), "\n") { if strings.HasPrefix(line, "| [") { return line } } t.Fatalf("no instance row on the home page") return "" } // The output column is pinned on the rendered page, not only through the // helper, so the call site cannot drift back to ui.Cell inside backticks // (which would show `a\.b`) with the suite green. func TestRenderOutputCell(cur realm, t *testing.T) { cases := []struct { name, out, cell string }{ {"an escapable character shows as written", "a.b", "| `a.b` |"}, {"no output is an empty cell, not two backticks", "", "| 0 | |"}, {"a space at each end is padded in the markdown, past CommonMark's strip", " a ", "| ` a ` |"}, {"a guest pipe stays inside its cell", "a|b", "| `a\\|b` |"}, } for _, c := range cases { resetState() testing.SetRealm(testing.NewUserRealm(testutils.TestAddress("alice"))) id := Upload(cross(cur), emit(c.out), "", 0) if c.out != "" { Step(cross(cur), id, 0) uassert.Equal(t, c.out, string(store.Get(id).Output)) } row := instanceRow(t) uassert.True(t, strings.HasSuffix(row, c.cell), c.name+": "+row) } } // codeCell follows the three rules of a code span, not only the pipe: empty // content is not a span, a space at either end needs a pad, and the fence // outscans any backtick run in the content. func TestCodeCellRules(t *testing.T) { cases := []struct{ in, want string }{ {"", ""}, {"Hello World", "`Hello World`"}, {"a|b", "`a\\|b`"}, {" hi ", "` hi `"}, {" hi", "` hi `"}, {"hi ", "` hi `"}, {"a`b", "``a`b``"}, } for _, c := range cases { uassert.Equal(t, c.want, codeCell(c.in), "codeCell("+c.in+")") } }
- #8gnomod.toml
- #9module = "gno.land/r/moul/x/vm/bfdemo/v0" gno = "0.9" private = true [source] repository = "https://github.com/moul/gno-contracts" path = "r/moul/x/vm/bfdemo" revision = "507056d812b505ad16a009f0d60786644ceeb19d"
- #10render.gno
- #11package bfdemo import ( "strings" "gno.land/p/moul/kit/ui/v0" "gno.land/p/moul/md/v0" "gno.land/p/moul/x/vm/bf/v0" "gno.land/p/moul/x/vm/vmkit/v0" "gno.land/p/nt/ufmt/v0" ) // samples are the programs the home page offers, so the realm is usable // without writing Brainfuck by hand. var samples = []struct { name string src string input string note string }{ { "Hello World", "++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.", "", "385 guest ops, five tape cells. Finishes in one slice.", }, { "Echo the input", ",[.,]", "gno.land", "Reads through Host.Input, which is what the naive interpreter could not do at all: it panicked on `,`.", }, { "Heavy loop", "++++++++++[->++++++++++[->++++++++++[-]<]<]", "", "About 111k guest ops. Give it 20000 fuel and it takes six slices, which is the point.", }, } // Render is the realm's gnoweb view. // // - "/" the samples, the instance list, and how the thing works. // - "/<id>" one instance: its program, status, fuel and output. func Render(path string) string { id := strings.TrimPrefix(path, "/") id = strings.TrimSpace(id) if id == "" { return renderHome() } return renderInstance(id) } func renderHome() string { var sb strings.Builder sb.WriteString("# Brainfuck, on chain, a slice at a time\n\n") sb.WriteString("Demo of [`p/moul/x/vm/bf`](/p/moul/x/vm/bf/v0) (the machine) and ") sb.WriteString("[`p/moul/x/vm/vmkit`](/p/moul/x/vm/vmkit/v0) (the host ABI, the fuel meter, ") sb.WriteString("the instance store). This realm holds no logic of its own.\n\n") sb.WriteString("A program here does not run to completion. It runs until its fuel slice is spent, ") sb.WriteString("then pauses: the realm keeps the snapshot and the next caller pays for the next slice. ") sb.WriteString("Realm code cannot pause itself, and a guest can.\n\n") sb.WriteString("## Samples\n\n") for _, s := range samples { sb.WriteString("**" + ui.Inline(s.name) + "**: " + s.note + "\n\n") sb.WriteString("```\n" + s.src + "\n```\n\n") sb.WriteString(ui.Action("Upload "+s.name, "Upload", "src", s.src, "input", s.input, "budget", "0") + "\n\n") } sb.WriteString("## Instances\n\n") t := ui.NewTable("id", "status", "fuel used", "slices", "output") store.ReverseIterate(func(i *vmkit.Instance) bool { t.Row( "["+i.ID+"](/r/moul/x/vm/bfdemo/v0:"+i.ID+")", i.Status.String(), ufmt.Sprintf("%d", i.FuelUsed), ufmt.Sprintf("%d", i.Slices), codeCell(printable(i.Output, 24)), ) return false }) sb.WriteString(t.OrEmpty("No instances yet. Upload one of the samples above.")) sb.WriteString("\n\n") sb.WriteString("## Limits\n\n") lt := ui.NewTable("limit", "value") lt.Row("instances", ufmt.Sprintf("%d", MaxInstances)) lt.Row("source bytes", ufmt.Sprintf("%d", bf.MaxSource)) lt.Row("input bytes", ufmt.Sprintf("%d", MaxInput)) lt.Row("output bytes", ufmt.Sprintf("%d", MaxOutput)) lt.Row("fuel per slice", ufmt.Sprintf("%d", MaxSliceFuel)) lt.Row("tape cells", ufmt.Sprintf("%d", bf.TapeSize)) sb.WriteString(lt.String()) return sb.String() } func renderInstance(id string) string { var sb strings.Builder sb.WriteString("# Instance " + ui.Inline(id) + "\n\n") sb.WriteString("[← all instances](/r/moul/x/vm/bfdemo/v0)\n\n") inst := store.Get(id) if inst == nil { sb.WriteString("**No such instance.** It was never uploaded, or its owner removed it.\n") return sb.String() } t := ui.NewTable("field", "value") t.Row("owner", ui.Addr(inst.Owner)) t.Row("vm", ui.Cell(inst.VM)) t.Row("status", inst.Status.String()) if inst.Trap != "" { t.Row("trap", ui.Cell(inst.Trap)) } t.Row("fuel used", ufmt.Sprintf("%d", inst.FuelUsed)) t.Row("fuel budget", fuelText(inst.FuelBudget)) t.Row("slices", ufmt.Sprintf("%d", inst.Slices)) t.Row("source bytes", ufmt.Sprintf("%d", len(inst.Program))) t.Row("snapshot bytes", ufmt.Sprintf("%d", len(inst.Snapshot))) sb.WriteString(t.String()) sb.WriteString("\n") // The source is caller-supplied, and everything outside the eight // operators is a comment that may hold anything at all. Show the // program the machine actually compiled, which cannot contain a // backtick or a newline and so cannot break out of the fence. sb.WriteString("## Program\n\n```\n" + operatorsOnly(string(inst.Program)) + "\n```\n\n") sb.WriteString("## Output\n\n") if len(inst.Output) == 0 { sb.WriteString(ui.Empty("Nothing written yet.")) } else { sb.WriteString("```\n" + printable(inst.Output, MaxOutput) + "\n```\n") } sb.WriteString("\n") if inst.Status == vmkit.Running { sb.WriteString(ui.Action("Run 10000 more fuel", "Step", "id", id, "fuel", "10000")) sb.WriteString(" · ") sb.WriteString(ui.Action("Run 100000 more fuel", "Step", "id", id, "fuel", "100000")) sb.WriteString("\n") } else { sb.WriteString("This instance is **" + inst.Status.String() + "** and cannot be stepped again.\n") } return sb.String() } func fuelText(n int64) string { if n == vmkit.Unmetered { return "unmetered" } return ufmt.Sprintf("%d", n) } // operatorsOnly strips a program down to the eight characters the language // defines. Everything else is a comment by definition, and a comment is // attacker-controlled text that has no business reaching a renderer. func operatorsOnly(src string) string { var sb strings.Builder for i := 0; i < len(src); i++ { switch src[i] { case '+', '-', '<', '>', '[', ']', '.', ',': sb.WriteByte(src[i]) } } if sb.Len() == 0 { return "(no operators: this program does nothing)" } return sb.String() } // printable renders guest output for a code fence: printable ASCII as itself, // everything else as an escape. A guest writes arbitrary bytes, so this is // the only form of it that is safe to show. func printable(b []byte, max int) string { if len(b) == 0 { return "" } truncated := false if len(b) > max { b, truncated = b[:max], true } const hexDigits = "0123456789abcdef" var sb strings.Builder for _, c := range b { switch { case c == '\n': sb.WriteString("\\n") case c == '\t': sb.WriteString("\\t") case c == '\\': sb.WriteString("\\\\") case c == '`': sb.WriteString("\\x60") case c >= 0x20 && c < 0x7f: sb.WriteByte(c) default: sb.WriteString("\\x") sb.WriteByte(hexDigits[c>>4]) sb.WriteByte(hexDigits[c&0x0f]) } } if truncated { sb.WriteString("…") } return sb.String() } // codeCell puts already-printable text in a code span inside a table cell. // Inside backticks markdown escapes are not applied, so ui.Cell's backslashes // would reach the reader verbatim ("Hello, gno\.land"). md.InlineCode builds // the span: empty text is no span at all (two bare backticks render as two // backticks), a space at either end is padded so CommonMark's strip does not // eat it, and the fence outscans any backtick run. The GFM row splitter runs // before inline parsing, so the pipe is escaped on top. func codeCell(s string) string { return strings.ReplaceAll(md.InlineCode(s), "|", "\\|") }
- Attached funds
- 15000000 ugnot
Arguments · 13
- #1riscvdemo
- #2README.md
- #3# r/moul/x/vm/riscvdemo Demo of [`p/moul/x/vm/riscv`](/p/moul/x/vm/riscv/v0), the RV32IM hart, and [`p/moul/x/vm/vmkit`](/p/moul/x/vm/vmkit/v0), the host ABI. This realm holds no logic of its own: it stores images, builds a `vmkit.Host`, and renders. What it exists to show is two things realm code cannot do for itself. **The guest was not written for gno.** The programs on the front page are flat `.text` images, the bytes a cross compiler emits for `riscv32im-unknown-none-elf`. `Upload` takes hex, so anything you can build with `clang`, `rustc`, TinyGo or Zig and strip to its text segment goes in unchanged. Two samples are exactly that and neither is a mock up. **Compiled by clang** is a freestanding C program; **A token, in Rust** is `#![no_std]` Rust that mints, sends and burns, linking Rust's real `core` and `compiler_builtins`, so a 64-bit divide in it runs `__udivdi3` on a machine with no 64-bit divide instruction. Both are shipped as the bytes the compiler emitted, and the page shows their source rather than a disassembly, because a disassembly would bury the only interesting fact: nobody wrote the machine code. Sources and build commands: `tools/riscv-guests`. **A program pauses instead of failing.** It runs until its fuel slice is spent, then stops with a snapshot the realm keeps, and the next caller pays for the next slice. The "Heavy loop" sample is 200,006 instructions and takes three transactions at the default slice, which is the whole point. ``` UploadSample("heavy", "", 0) -> id Step(id, 80000) -> "running" Step(id, 80000) -> "running" Step(id, 80000) -> "halted" ``` ## Reading the instance page A guest that writes nothing has still computed something, so the instance page restores the snapshot and shows the register file under its ABI names. That is the only place the state is legible: the hart itself does not survive the transaction, only its snapshot does. ## What a slice costs An RV32IM instruction costs about **8,500 gas**, measured rather than estimated, so a block buys roughly **350,000 guest instructions**. Loading the image costs about **19,600 gas per instruction word** and is paid again on every resume, which is why `MaxImage` is 8 KiB and not a megabyte. The measurements and how they were taken are in the [library README](/p/moul/x/vm/riscv/v0). ## What this realm does not grant `Send` always returns `vmkit.ErrNotGranted`. No instance here is funded, so a guest that tries to move coins is refused, and that is the capability rule working rather than a missing feature. Guest key-value storage is scoped to the instance being stepped by construction: the tree belongs to the instance, so one program cannot reach another's even though both live in this realm. <!-- BEGIN GNOCONTRACTS FOOTER (generated by `make readmes`; do not edit below) --> --- Part of **[moul/gno-contracts](https://github.com/moul/gno-contracts)** — moul's versioned gno.land contracts. See the repository for the full catalog, build/test tooling, and usage. **On mainnet:** [](https://gnoscope.com/realm/r/moul/x/vm/riscvdemo/v0) [](https://gnoscope.com/realm/r/moul/x/vm/riscvdemo/v0) [](https://gnoscope.com/realm/r/moul/x/vm/riscvdemo/v0) [](https://gnoscope.com/realm/r/moul/x/vm/riscvdemo/v0) **Dependency graph:**  > 🧪 **Highly experimental — potentially vibe-coded.** Not audited; may break, change, or be removed at any time. Do not use with anything of value. Full disclaimer: [DISCLAIMER](https://github.com/moul/gno-contracts/blob/main/DISCLAIMER.md). <!-- END GNOCONTRACTS FOOTER -->
- #4gnomod.toml
- #5module = "gno.land/r/moul/x/vm/riscvdemo/v0" gno = "0.9" private = true [source] repository = "https://github.com/moul/gno-contracts" path = "r/moul/x/vm/riscvdemo" revision = "507056d812b505ad16a009f0d60786644ceeb19d"
- #6render.gno
- #7package riscvdemo import ( "strings" "gno.land/p/moul/kit/ui/v0" "gno.land/p/moul/md/v0" "gno.land/p/moul/x/vm/riscv/v0" "gno.land/p/moul/x/vm/vmkit/v0" "gno.land/p/moul/vendor/nt/ufmt/v2" ) // regNames are the ABI names, because nobody reading a register dump thinks in // x-numbers and every compiler's output is annotated with these. var regNames = [32]string{ "zero", "ra", "sp", "gp", "tp", "t0", "t1", "t2", "s0", "s1", "a0", "a1", "a2", "a3", "a4", "a5", "a6", "a7", "s2", "s3", "s4", "s5", "s6", "s7", "s8", "s9", "s10", "s11", "t3", "t4", "t5", "t6", } // Render is the realm's gnoweb view. // // - "/" the samples, the instance list, and what the numbers mean. // - "/<id>" one instance: its status, fuel, output and register file. func Render(path string) string { id := strings.TrimSpace(strings.TrimPrefix(path, "/")) if id == "" { return renderHome() } return renderInstance(id) } func renderHome() string { var sb strings.Builder sb.WriteString("# RV32IM, on chain, a slice at a time\n\n") sb.WriteString("Demo of [`p/moul/x/vm/riscv`](/p/moul/x/vm/riscv/v0) (the hart) and ") sb.WriteString("[`p/moul/x/vm/vmkit`](/p/moul/x/vm/vmkit/v0) (the host ABI, the fuel meter, ") sb.WriteString("the instance store). This realm holds no logic of its own.\n\n") sb.WriteString("The programs below are flat `.text` images: the same bytes a cross compiler ") sb.WriteString("emits for `riscv32im-unknown-none-elf`. Nothing here was written in a ") sb.WriteString("chain language, and nothing translated it.\n\n") sb.WriteString("A program does not run to completion. It runs until its fuel slice is spent, ") sb.WriteString("then pauses: the realm keeps the snapshot and the next caller pays for the ") sb.WriteString("next slice. Realm code cannot pause itself, and a guest can.\n\n") sb.WriteString("## Samples\n\n") for i := range samples { s := &samples[i] sb.WriteString("**" + ui.Inline(s.name) + "**: " + s.note + "\n\n") sb.WriteString("```" + s.lang() + "\n" + s.asm + "\n```\n\n") sb.WriteString(ui.Action("Upload "+s.name, "UploadSample", "name", s.slug, "input", s.input, "budget", ufmt.Sprintf("%d", s.budget)) + "\n\n") } sb.WriteString("## Instances\n\n") t := ui.NewTable("id", "status", "instructions", "slices", "output") store.ReverseIterate(func(i *vmkit.Instance) bool { t.Row( "["+i.ID+"](/r/moul/x/vm/riscvdemo/v0:"+i.ID+")", i.Status.String(), ufmt.Sprintf("%d", i.FuelUsed), ufmt.Sprintf("%d", i.Slices), codeCell(printable(i.Output, 24)), ) return false }) sb.WriteString(t.OrEmpty("No instances yet. Upload one of the samples above.")) sb.WriteString("\n\n") sb.WriteString("## What a slice costs\n\n") sb.WriteString("An RV32IM instruction costs about **8,500 gas** here, measured rather than ") sb.WriteString("estimated, so a block buys roughly **350,000 guest instructions**. Loading ") sb.WriteString("the image costs about **19,600 gas per instruction word** and is paid again ") sb.WriteString("on every resume, which is why the image cap below is small. The numbers and ") sb.WriteString("how they were taken are in the [library README](/p/moul/x/vm/riscv/v0).\n\n") sb.WriteString("## Limits\n\n") lt := ui.NewTable("limit", "value") lt.Row("instances", ufmt.Sprintf("%d", MaxInstances)) lt.Row("image bytes", ufmt.Sprintf("%d", MaxImage)) lt.Row("input bytes", ufmt.Sprintf("%d", MaxInput)) lt.Row("output bytes", ufmt.Sprintf("%d", MaxOutput)) lt.Row("instructions per slice", ufmt.Sprintf("%d", MaxSliceFuel)) lt.Row("address space", ufmt.Sprintf("%d", riscv.MemSize)) sb.WriteString(lt.String()) return sb.String() } func renderInstance(id string) string { var sb strings.Builder sb.WriteString("# Instance " + ui.Inline(id) + "\n\n") sb.WriteString("[← all instances](/r/moul/x/vm/riscvdemo/v0)\n\n") inst := store.Get(id) if inst == nil { sb.WriteString("**No such instance.** It was never uploaded, or its owner removed it.\n") return sb.String() } t := ui.NewTable("field", "value") t.Row("owner", ui.Addr(inst.Owner)) t.Row("vm", ui.Cell(inst.VM)) t.Row("status", inst.Status.String()) if inst.Trap != "" { t.Row("trap", ui.Cell(inst.Trap)) } t.Row("instructions run", ufmt.Sprintf("%d", inst.FuelUsed)) t.Row("budget", fuelText(inst.FuelBudget)) t.Row("slices", ufmt.Sprintf("%d", inst.Slices)) t.Row("image bytes", ufmt.Sprintf("%d", len(inst.Program))) t.Row("image words", ufmt.Sprintf("%d", len(inst.Program)/4)) t.Row("snapshot bytes", ufmt.Sprintf("%d", len(inst.Snapshot))) sb.WriteString(t.String()) sb.WriteString("\n") sb.WriteString("## Registers\n\n") sb.WriteString(renderRegisters(inst)) sb.WriteString("## Output\n\n") if len(inst.Output) == 0 { sb.WriteString(ui.Empty("Nothing written yet.")) } else { sb.WriteString("```\n" + printable(inst.Output, MaxOutput) + "\n```\n") } sb.WriteString("\n") sb.WriteString("## Image\n\n```\n" + hexDump(inst.Program, 64) + "\n```\n\n") if inst.Status == vmkit.Running { sb.WriteString(ui.Action("Run 25000 instructions", "Step", "id", id, "fuel", "25000")) sb.WriteString(" · ") sb.WriteString(ui.Action("Run 100000 instructions", "Step", "id", id, "fuel", "100000")) sb.WriteString("\n") } else { sb.WriteString("This instance is **" + inst.Status.String() + "** and cannot be stepped again.\n") } return sb.String() } // renderRegisters restores the snapshot to read the register file out of it. // The hart is not kept between transactions, only its snapshot is, so this is // the only place the state is legible, and it is the thing worth seeing: a // program that wrote nothing still computed something. func renderRegisters(inst *vmkit.Instance) string { if len(inst.Snapshot) == 0 { return ui.Empty("Not started yet: no snapshot to read.") + "\n\n" } var m riscv.Machine if err := m.Restore(inst.Snapshot); err != nil { return ui.Empty("The snapshot could not be read.") + "\n\n" } reg := m.Registers() var sb strings.Builder t := ui.NewTable("register", "hex", "unsigned", "signed") t.Row("pc", ufmt.Sprintf("0x%08X", m.PC()), "", "") shown := 0 for i := 1; i < 32; i++ { if reg[i] == 0 { continue } t.Row( regNames[i], ufmt.Sprintf("0x%08X", reg[i]), ufmt.Sprintf("%d", reg[i]), ufmt.Sprintf("%d", int64(int32(reg[i]))), ) shown++ } sb.WriteString(t.String()) if shown == 0 { sb.WriteString("\nEvery register except `pc` is zero.\n") } sb.WriteString("\n") return sb.String() } func fuelText(n int64) string { if n == vmkit.Unmetered { return "unmetered" } return ufmt.Sprintf("%d", n) } const hexDigits = "0123456789abcdef" // hexDump shows the image as words, which is how anyone reading RV32 reads it. func hexDump(b []byte, max int) string { truncated := false if len(b) > max { b, truncated = b[:max], true } var sb strings.Builder for i := 0; i+3 < len(b); i += 4 { if i > 0 && i%16 == 0 { sb.WriteString("\n") } else if i > 0 { sb.WriteString(" ") } w := uint32(b[i]) | uint32(b[i+1])<<8 | uint32(b[i+2])<<16 | uint32(b[i+3])<<24 sb.WriteString(ufmt.Sprintf("%08x", w)) } if truncated { sb.WriteString("\n...") } return sb.String() } // printable renders guest output for a code fence: printable ASCII as itself, // everything else as an escape. A guest writes arbitrary bytes, so this is the // only form of it that is safe to show. func printable(b []byte, max int) string { if len(b) == 0 { return "" } truncated := false if len(b) > max { b, truncated = b[:max], true } var sb strings.Builder for _, c := range b { switch { case c == '\n': sb.WriteString("\\n") case c == '\t': sb.WriteString("\\t") case c == '\\': sb.WriteString("\\\\") case c == '`': sb.WriteString("\\x60") case c >= 0x20 && c < 0x7f: sb.WriteByte(c) default: sb.WriteString("\\x") sb.WriteByte(hexDigits[c>>4]) sb.WriteByte(hexDigits[c&0x0f]) } } if truncated { sb.WriteString("…") } return sb.String() } // codeCell puts already-printable text in a code span inside a table cell. // Inside backticks markdown escapes are not applied, so ui.Cell's backslashes // would reach the reader verbatim ("Hello, gno\.land"). md.InlineCode builds // the span: empty text is no span at all (two bare backticks render as two // backticks), a space at either end is padded so CommonMark's strip does not // eat it, and the fence outscans any backtick run. The GFM row splitter runs // before inline parsing, so the pipe is escaped on top. func codeCell(s string) string { return strings.ReplaceAll(md.InlineCode(s), "|", "\\|") }
- #8riscvdemo.gno
- #9// Package riscvdemo runs real compiled machine code on chain, a slice at a // time. // // It is a demo of two libraries and carries no logic of its own: // [p/moul/x/vm/riscv](/p/moul/x/vm/riscv/v0) is the RV32IM hart, and // [p/moul/x/vm/vmkit](/p/moul/x/vm/vmkit/v0) is the host ABI, the fuel meter // and the instance store. // // What it exists to show is that the guest was not written for gno. The // programs on the front page are flat .text images: the same bytes // `rustc --target riscv32im-unknown-none-elf` emits, uploaded as hex. The // realm holds the snapshot between transactions, so one computation finishes // across several of them, and realm code cannot do that for itself. package riscvdemo import ( "chain" "chain/runtime" "chain/runtime/unsafe" "time" "gno.land/p/moul/x/vm/riscv/v0" "gno.land/p/moul/x/vm/vmkit/v0" "gno.land/p/nt/avl/v0" "gno.land/p/nt/seqid/v0" "gno.land/p/moul/vendor/nt/ufmt/v2" ) // Caps. Everything a caller can grow is bounded, because all of it is storage // somebody pays a deposit on. const ( // MaxInstances is how many programs the realm keeps at once. MaxInstances = 64 // MaxImage caps an uploaded text segment at 2,048 instructions. // // This one is not arbitrary. Predecoding costs about 19,600 gas per word // and is redone on every resume, so the image size is a tax on every // transaction that touches the instance, not just the upload. 8 KiB works // out to roughly 40M gas per slice before the guest executes anything, // which is about 1% of a block. MaxImage = 8192 // MaxOutput caps the bytes one program may write. Past it the guest is // trapped rather than truncated, so rendered output is never a lie. MaxOutput = 4096 // MaxInput caps the call input a program can be given. MaxInput = 1024 // DefaultFuel is the budget an upload gets when it asks for none, and the // slice size Step uses when asked for none. DefaultFuel = 100000 // MaxSliceFuel bounds one transaction's work regardless of what the // caller asked for, and it is derived from the measurement rather than // picked: an RV32IM instruction costs about 8,500 gas, so 100,000 of them // is about 850M, a little under a third of a 3G block. Twice this would // still be a legal transaction and a rude one, and it would leave no room // for the predecode and the realm's own storage writes on top. MaxSliceFuel = 100000 ) var ( store = vmkit.NewStore() inputs = avl.NewTree() idgen seqid.ID count int ) // host is the realm-backed [vmkit.Host]. One is built per call, wrapping the // instance being stepped, so a guest's output and authority are scoped to it // and nothing ambient leaks in. type host struct { inst *vmkit.Instance in []byte kv *avl.Tree overrun bool // the guest wrote past MaxOutput } func (h *host) Caller() address { return h.inst.Owner } func (h *host) Origin() address { return h.inst.Owner } func (h *host) Now() int64 { return time.Now().Unix() } func (h *host) Height() int64 { return runtime.ChainHeight() } func (h *host) Input() []byte { return h.in } // Get and Set are scoped to the instance by construction: the tree belongs to // the instance being stepped, so one program cannot reach another's storage // even though both live in this one realm. func (h *host) Get(key []byte) []byte { v := h.kv.Get(string(key)) if v == nil { return nil } return v.([]byte) } func (h *host) Set(key, val []byte) { h.kv.Set(string(key), val) } func (h *host) Output(p []byte) { if len(h.inst.Output)+len(p) > MaxOutput { h.overrun = true return } h.inst.Output = append(h.inst.Output, p...) } func (h *host) Emit(typ string, kv ...string) { chain.Emit(typ, kv...) } // Send is never granted here. The demo funds no instance, so a guest that // tries to move coins is refused. That is the capability rule doing its job, // not a missing feature. func (h *host) Send(to address, amount int64) error { return vmkit.ErrNotGranted } func (h *host) Log(msg string) {} // Upload stores a hex-encoded text image as a new instance and returns its id. // // The image is loaded here rather than at the first Step, so a misaligned or // oversized one is rejected by the transaction that submitted it instead of // costing somebody else the gas later. func Upload(cur realm, hexImage, input string, budget int64) string { img, ok := decodeHex(hexImage) if !ok { panic("riscvdemo: image is not valid hex") } return upload(img, input, budget) } // UploadSample stores one of the programs the front page offers. Writing RV32IM // by hand is not the point of this realm, and without this nobody without a // cross compiler could press a button. func UploadSample(cur realm, name, input string, budget int64) string { s := sampleByName(name) if s == nil { panic("riscvdemo: no such sample") } // A sample carries its own budget because the default is a total, not a // slice: the heavy loop needs 200,006 instructions, and offering a button // that runs out of fuel halfway is a worse demo than no button. if budget <= 0 { budget = s.budget } return upload(s.image(), input, budget) } func upload(img []byte, input string, budget int64) string { if count >= MaxInstances { panic("riscvdemo: too many instances, remove one first") } if len(input) > MaxInput { panic("riscvdemo: input too long") } if len(img) == 0 { panic("riscvdemo: empty image") } if len(img) > MaxImage { panic("riscvdemo: image too large") } // Loading it now is the validation: NewMachine is what rejects an image // that is not a whole number of instructions. if _, err := riscv.NewMachine(img, riscv.DefaultEntry); err != nil { panic("riscvdemo: " + err.Error()) } if budget <= 0 { budget = DefaultFuel } id := idgen.Next().String() owner := unsafe.PreviousRealm().Address() store.Set(vmkit.NewInstance(id, owner, riscv.VMName, img, budget)) if input != "" { inputs.Set(id, input) } count++ chain.Emit("riscv_upload", "id", id, "bytes", ufmt.Sprintf("%d", len(img))) return id } // Step runs one slice of the instance: up to `fuel` guest instructions, then // stop and keep the snapshot. Anyone may pay for a slice, not only the owner: // a paused program that only its owner can advance is a worse demo and no // safer, since the program and its budget were both fixed at upload. func Step(cur realm, id string, fuel int64) string { inst := store.Get(id) if inst == nil { panic("riscvdemo: no such instance") } if inst.Status != vmkit.Running { panic("riscvdemo: instance is " + inst.Status.String()) } if fuel <= 0 { fuel = DefaultFuel } if fuel > MaxSliceFuel { fuel = MaxSliceFuel } m, err := riscv.NewMachine(inst.Program, riscv.DefaultEntry) if err != nil { panic("riscvdemo: " + err.Error()) } h := &host{inst: inst, in: []byte(inputOf(id)), kv: avl.NewTree()} if err := inst.Run(m, h, fuel); err != nil { panic("riscvdemo: " + err.Error()) } if h.overrun { inst.Status = vmkit.Trapped inst.Trap = "output limit reached" } chain.Emit("riscv_step", "id", id, "status", inst.Status.String(), "fuel", ufmt.Sprintf("%d", inst.FuelUsed), ) return inst.Status.String() } // Remove deletes an instance. Owner only. func Remove(cur realm, id string) { inst := store.Get(id) if inst == nil { panic("riscvdemo: no such instance") } if inst.Owner != unsafe.PreviousRealm().Address() { panic("riscvdemo: not your instance") } store.Remove(id) inputs.Remove(id) count-- } func inputOf(id string) string { v := inputs.Get(id) if v == nil { return "" } return v.(string) } // decodeHex accepts the output of any hexdump: an even number of hex digits, // with an optional 0x prefix, and whitespace anywhere. Whitespace is allowed // because that is what a pasted hexdump has in it; anything else is refused, // because a silently mangled image would trap somewhere unrelated later. func decodeHex(s string) ([]byte, bool) { if len(s) >= 2 && s[0] == '0' && (s[1] == 'x' || s[1] == 'X') { s = s[2:] } digits := make([]byte, 0, len(s)) for i := 0; i < len(s); i++ { c := s[i] if c == ' ' || c == '\t' || c == '\n' || c == '\r' { continue } v, ok := hexVal(c) if !ok { return nil, false } digits = append(digits, v) } if len(digits) == 0 || len(digits)%2 != 0 { return nil, false } out := make([]byte, len(digits)/2) for i := 0; i < len(out); i++ { out[i] = digits[i*2]<<4 | digits[i*2+1] } return out, true } func hexVal(c byte) (byte, bool) { switch { case c >= '0' && c <= '9': return c - '0', true case c >= 'a' && c <= 'f': return c - 'a' + 10, true case c >= 'A' && c <= 'F': return c - 'A' + 10, true } return 0, false }
- #10riscvdemo_test.gno
- #11package riscvdemo import ( "strings" "testing" "gno.land/p/moul/x/vm/riscv/v0" "gno.land/p/moul/x/vm/vmkit/v0" "gno.land/p/nt/avl/v0" "gno.land/p/nt/seqid/v0" "gno.land/p/nt/testutils/v0" "gno.land/p/nt/uassert/v0" ) // resetState puts the realm globals back where init() left them. Realm state // persists for the whole test binary, so every test that asserts on ids or on // the rendered instance list has to start from here. func resetState() { store = vmkit.NewStore() inputs = avl.NewTree() idgen = seqid.ID(0) count = 0 } func alice() address { return testutils.TestAddress("alice") } func TestUploadAndRunToCompletion(cur realm, t *testing.T) { resetState() testing.SetRealm(testing.NewUserRealm(alice())) id := UploadSample(cross(cur), "hello", "", 0) uassert.Equal(t, "halted", Step(cross(cur), id, 0)) inst := store.Get(id) uassert.Equal(t, "Hello, gno.land\n", string(inst.Output)) uassert.Equal(t, int64(1), inst.Slices) uassert.Equal(t, alice(), inst.Owner) uassert.Equal(t, "riscv32im", inst.VM) } // Every sample on the front page has to reach its documented answer, or the // page offers a button that traps when pressed. The words are assembled // outside this repo, so this is the test that catches a mistyped one. func TestEverySampleReachesItsAnswer(cur realm, t *testing.T) { resetState() testing.SetRealm(testing.NewUserRealm(alice())) cases := []struct { slug string reg int want uint32 output string }{ {"hello", 0, 0, "Hello, gno.land\n"}, {"sum100", 5, 5050, ""}, {"heavy", 5, 1250025000, ""}, {"mext", 5, 3000000021, ""}, {"clang", 0, 0, "fb7ffba0"}, {"ledger", 0, 0, "alice 70\nbob 80\n"}, } for _, c := range cases { id := UploadSample(cross(cur), c.slug, sampleByName(c.slug).input, 0) status := "" for i := 0; i < 20; i++ { status = Step(cross(cur), id, 0) if status != "running" { break } } uassert.Equal(t, "halted", status, c.slug+" must halt") inst := store.Get(id) uassert.Equal(t, c.output, string(inst.Output), c.slug+" output") if c.reg != 0 { var m riscv.Machine uassert.NoError(t, m.Restore(inst.Snapshot)) uassert.Equal(t, uint64(c.want), uint64(m.Registers()[c.reg]), c.slug+" result") } } } // TestOneProgramAcrossManyTransactions is what the realm exists to // demonstrate: the same computation, finished over several calls, each paying // for its own slice. func TestOneProgramAcrossManyTransactions(cur realm, t *testing.T) { resetState() testing.SetRealm(testing.NewUserRealm(alice())) id := UploadSample(cross(cur), "heavy", "", 0) status := "" for i := 0; i < 50; i++ { status = Step(cross(cur), id, 80000) if status != "running" { break } } inst := store.Get(id) uassert.Equal(t, "halted", status) uassert.True(t, inst.Slices > 1, "a 200k instruction program must not finish in one slice") uassert.Equal(t, int64(200006), inst.FuelUsed) } func TestUploadTakesHex(cur realm, t *testing.T) { resetState() testing.SetRealm(testing.NewUserRealm(alice())) // addi a7, zero, 93 ; ecall id := Upload(cross(cur), "0x9308d005 73000000", "", 0) _ = id } func TestUploadRejectsBadImages(cur realm, t *testing.T) { resetState() testing.SetRealm(testing.NewUserRealm(alice())) // Called without cross(): a panic raised through a crossing call cannot be // recovered, so uassert would never see it. uassert.PanicsWithMessage(t, cur, "riscvdemo: image is not valid hex", func() { Upload(cur, "zzzz", "", 0) }) uassert.PanicsWithMessage(t, cur, "riscvdemo: image is not valid hex", func() { Upload(cur, "abc", "", 0) }) uassert.PanicsWithMessage(t, cur, "riscvdemo: no such sample", func() { UploadSample(cur, "nope", "", 0) }) uassert.PanicsWithMessage(t, cur, "riscvdemo: riscv: image length is not a whole number of instructions", func() { Upload(cur, "0011", "", 0) }) } func TestDecodeHex(t *testing.T) { b, ok := decodeHex("0xDEADbeef") uassert.True(t, ok) uassert.Equal(t, 4, len(b)) uassert.Equal(t, uint64(0xDE), uint64(b[0])) uassert.Equal(t, uint64(0xEF), uint64(b[3])) _, ok = decodeHex("") uassert.False(t, ok) _, ok = decodeHex("0x") uassert.False(t, ok) // Whitespace is skipped, because a pasted hexdump has it. b, ok = decodeHex("00 11\n22\t33") uassert.True(t, ok) uassert.Equal(t, 4, len(b)) // An odd number of digits is still a refusal, whitespace or not. _, ok = decodeHex("00 1") uassert.False(t, ok) } func TestRenderHomeOffersEverySample(cur realm, t *testing.T) { resetState() out := Render("") uassert.True(t, strings.Contains(out, "RV32IM, on chain"), "the title") for _, s := range samples { uassert.True(t, strings.Contains(out, s.name), "home must offer "+s.slug) } uassert.True(t, strings.Contains(out, "No instances yet"), "the empty state") } // The register file is the interesting half of this VM: a program that writes // nothing still computed something, and the snapshot is the only place it is // legible between transactions. func TestRenderInstanceShowsTheRegisterFile(cur realm, t *testing.T) { resetState() testing.SetRealm(testing.NewUserRealm(alice())) id := UploadSample(cross(cur), "sum100", "", 0) Step(cross(cur), id, 0) out := Render("/" + id) uassert.True(t, strings.Contains(out, "## Registers"), "the section") uassert.True(t, strings.Contains(out, "5050"), "t0 must show the answer") uassert.True(t, strings.Contains(out, "halted"), "the status") } // The instance page is all hex: the pc, every register, the image dump. // gno.land/p/nt/ufmt/v0 has no %X and no width flags, so this page shipped // printing "0x(unhandled verb: %X)" in every cell; the test pins the fix. func TestRenderInstanceHex(cur realm, t *testing.T) { resetState() testing.SetRealm(testing.NewUserRealm(alice())) // addi a7, zero, 93 ; ecall (exit) id := Upload(cross(cur), "0x9308d005 73000000", "", 0) Step(cross(cur), id, 0) out := Render("/" + id) uassert.False(t, strings.Contains(out, "unhandled"), out) uassert.True(t, strings.Contains(out, "| a7 | 0x0000005D | 93 | 93 |"), out) uassert.True(t, strings.Contains(out, "05d00893 00000073"), out) } // The instance list shows output in a code span, where markdown escapes do // not apply: ui.Cell's backslashes used to reach the reader as // `Hello, gno\.land\\n`. Only the pipe needs escaping inside the span. func TestRenderOutputCell(cur realm, t *testing.T) { resetState() testing.SetRealm(testing.NewUserRealm(alice())) id := UploadSample(cross(cur), "hello", "", 0) Step(cross(cur), id, 0) out := Render("") uassert.True(t, strings.Contains(out, "| `Hello, gno.land\\n` |"), out) uassert.Equal(t, "`a\\|b`", codeCell("a|b")) } func TestRenderUnknownInstance(t *testing.T) { uassert.True(t, strings.Contains(Render("/nope"), "No such instance")) } // ExampleRender pins the realm's output for a path whose content cannot drift // with the instance list. The home page and the instance page both produce // consecutive blank lines, which an example block cannot represent, so they // are asserted in the tests above instead. func ExampleRender() { print(Render("/missing")) // Output: // # Instance missing // // [← all instances](/r/moul/x/vm/riscvdemo/v0) // // **No such instance.** It was never uploaded, or its owner removed it. } // An instance that has written nothing yet shows an empty output cell. Two // adjacent backticks are not an empty code span, they render as two // backticks. func TestRenderEmptyOutputCell(cur realm, t *testing.T) { resetState() testing.SetRealm(testing.NewUserRealm(alice())) UploadSample(cross(cur), "hello", "", 0) row := "" for _, line := range strings.Split(Render(""), "\n") { if strings.HasPrefix(line, "| [") { row = line } } uassert.True(t, row != "", "an instance row is rendered") uassert.True(t, strings.HasSuffix(row, "| 0 | |"), row) } // codeCell follows the three rules of a code span, not only the pipe: empty // content is not a span, a space at either end needs a pad, and the fence // outscans any backtick run in the content. func TestCodeCellRules(t *testing.T) { cases := []struct{ in, want string }{ {"", ""}, {"Hello, gno.land\\n", "`Hello, gno.land\\n`"}, {"a|b", "`a\\|b`"}, {" hi ", "` hi `"}, {" hi", "` hi `"}, {"hi ", "` hi `"}, {"a`b", "``a`b``"}, } for _, c := range cases { uassert.Equal(t, c.want, codeCell(c.in), "codeCell("+c.in+")") } }
- #12samples.gno
- #13package riscvdemo import "gno.land/p/moul/x/vm/riscv/v0" // The programs the front page offers, as the machine words a cross compiler // would have emitted. They are assembled outside this repo and pinned here, // and riscvdemo_test.gno runs every one of them to the expected answer, so a // wrong word is a failing test rather than a page that traps when clicked. type sample struct { slug string // what UploadSample takes: short, no punctuation, stable name string budget int64 // the instance's total instruction budget, not the slice // builtin names an image the library ships compiled, instead of the words // below. Empty means the words are the program. builtin string words []uint32 input string note string asm string } var samples = []sample{ { slug: "hello", name: "Hello, gno.land", words: []uint32{ 0x00100513, 0x000015B7, 0x02058593, 0x01000613, 0x04000893, 0x00000073, 0x05D00893, 0x00000073, 0x6C6C6548, 0x67202C6F, 0x6C2E6F6E, 0x0A646E61, }, budget: 1000, note: "Eight instructions and a string constant. The write syscall reads " + "the bytes straight out of the text segment and hands them to Host.Output.", asm: `addi a0, zero, 1 # fd lui a1, 0x1000 # buf addi a1, a1, 0x20 addi a2, zero, 16 # len addi a7, zero, 64 # write ecall addi a7, zero, 93 # exit ecall .ascii "Hello, gno.land\n"`, }, { slug: "sum100", name: "Sum 1 to 100", words: []uint32{ 0x06400413, 0x00000293, 0x00000313, 0x00130313, 0x006282B3, 0xFE831CE3, 0x05D00893, 0x00000073, }, budget: 1000, note: "A real loop, 303 instructions. Writes nothing: the answer is in t0, " + "and the instance page shows the register file.", asm: `addi s0, zero, 100 addi t0, zero, 0 addi t1, zero, 0 loop: addi t1, t1, 1 add t0, t0, t1 bne t1, s0, loop addi a7, zero, 93 ecall`, }, { slug: "heavy", name: "Heavy loop", words: []uint32{ 0x0000C437, 0x35040413, 0x00000293, 0x00000313, 0x00130313, 0x006282B3, 0x00000393, 0xFE831AE3, 0x05D00893, 0x00000073, }, budget: 250000, note: "200,006 instructions, four per iteration. At the largest slice this realm " + "allows it takes three transactions to finish, which is the thing it exists to show.", asm: `lui s0, 0xc000 # s0 = 50000 addi s0, s0, 0x350 addi t0, zero, 0 addi t1, zero, 0 loop: addi t1, t1, 1 add t0, t0, t1 addi t2, zero, 0 bne t1, s0, loop addi a7, zero, 93 ecall`, }, { slug: "mext", name: "Multiply and divide", words: []uint32{ 0x3B9AD2B7, 0xA0728293, 0x00300313, 0x026282B3, 0x00700393, 0x0272D333, 0x05D00893, 0x00000073, }, budget: 1000, note: "The M extension. t0 = 1000000007 * 3 truncated to 32 bits, then t1 = t0 / 7. " + "Division by zero and the one signed overflow case return values here " + "rather than trapping, which is what the spec says and what a chain needs.", asm: `lui t0, 0x3b9ad000 # t0 = 1000000007 addi t0, t0, -1529 addi t1, zero, 3 mul t0, t0, t1 addi t2, zero, 7 divu t1, t0, t2 addi a7, zero, 93 ecall`, }, { slug: "clang", name: "Compiled by clang", input: "gno.land", budget: 20000, note: "Not written for gno and not written by hand: a freestanding C program, " + "compiled by clang for riscv32im and shipped as the bytes LLVM emitted. It reads " + "the call input, hashes it with FNV-1a and writes eight hex digits back. " + "With the default input it prints fb7ffba0. Source and build command: " + "tools/riscv-guests/fnv.", builtin: "fnv", asm: `long n = syscall3(SYS_READ, 0, (long)in, sizeof(in)); unsigned int h = 2166136261u; for (long i = 0; i < n; i++) { h ^= in[i]; h *= 16777619u; /* the reason this is rv32im */ } for (int i = 0; i < 8; i++) { unsigned int nib = (h >> (28 - i * 4)) & 0xF; out[i] = nib < 10 ? '0' + nib : 'a' + (nib - 10); } syscall3(SYS_WRITE, 1, (long)out, 8); syscall3(SYS_EXIT, 0, 0, 0);`, }, { slug: "ledger", name: "A token, in Rust", input: "mint alice 100\nmint bob 50\nsend alice bob 30\n", budget: 20000, note: "no_std Rust, compiled by rustc for riscv32im: mint, send and burn, applied " + "from the call input. It links Rust's real core and compiler_builtins, so the " + "balances being 64-bit means a divide runs __udivdi3 here, because RV32 has no " + "64-bit divide instruction. With the default input it prints alice 70 and bob 80. " + "Source: tools/riscv-guests/ledger.", builtin: "ledger", asm: `match cmd { b"send" => { let (from, rest) = field(rest); let (to, rest) = field(rest); let (amount, _) = field(rest); let n = parse_u32(amount).ok_or("bad amount")?; let fi = led.find(from).ok_or("unknown sender")?; if led.balances[fi] < n { return Err("insufficient balance"); } let ti = led.intern(to).ok_or("ledger full or bad name")?; led.balances[fi] -= n; led.balances[ti] += n; Ok(()) } _ => Err("unknown command"), }`, }, } // image is what gets uploaded: either the words assembled here, or a program // the library ships because a compiler produced it. func (s *sample) image() []byte { switch s.builtin { case "fnv": return riscv.GuestFNV() case "ledger": return riscv.GuestLedger() } return riscv.Image(s.words) } // lang is the fence tag for what the sample shows. The compiled guest shows // its C, because showing a disassembly of what LLVM did with it would bury the // only interesting fact: nobody wrote the machine code. func (s *sample) lang() string { switch s.builtin { case "fnv": return "c" case "ledger": return "rust" } return "asm" } func sampleByName(slug string) *sample { for i := range samples { if samples[i].slug == slug { return &samples[i] } } return nil }
Result log
msg:0,success:true,log:,events:[] msg:1,success:true,log:,events:[]