go-vise

Constrained Size Output Virtual Machine
Info | Log | Files | Refs | README | LICENSE

commit 7bb678ca54afa29cd8a7073ef968384828c1614f
parent 3f27d54f75472fe30bc86d335ba10ff297a98361
Author: lash <dev@holbrook.no>
Date:   Thu, 22 Aug 2024 03:01:19 +0100

Fix code length check overflow + pinentry example

Pin check example

Remove useless invalidpin

Avoid wrap on code length comparison

Diffstat:
Aexamples/pincheck/Makefile | 10++++++++++
Aexamples/pincheck/cancel.vis | 2++
Aexamples/pincheck/check.vis | 2++
Aexamples/pincheck/continue | 2++
Aexamples/pincheck/continue.vis | 3+++
Aexamples/pincheck/main.go | 97+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Aexamples/pincheck/root.vis | 1+
Aexamples/pincheck/shut | 2++
Aexamples/pincheck/shut.vis | 7+++++++
Mvm/vm.go | 4++--
10 files changed, 128 insertions(+), 2 deletions(-)

diff --git a/examples/pincheck/Makefile b/examples/pincheck/Makefile @@ -0,0 +1,10 @@ +INPUTS = $(wildcard ./*.vis) +TXTS = $(wildcard ./*.txt.orig) + +%.vis: + go run ../../dev/asm $(basename $@).vis > $(basename $@).bin + +all: $(INPUTS) $(TXTS) + +%.txt.orig: + cp -v $(basename $@).orig $(basename $@) diff --git a/examples/pincheck/cancel.vis b/examples/pincheck/cancel.vis @@ -0,0 +1,2 @@ +LOAD pinclear 0 +HALT diff --git a/examples/pincheck/check.vis b/examples/pincheck/check.vis @@ -0,0 +1,2 @@ +RELOAD pincheck +MOVE _ diff --git a/examples/pincheck/continue b/examples/pincheck/continue @@ -0,0 +1,2 @@ +Congrats you made it! +Any input to return to top. diff --git a/examples/pincheck/continue.vis b/examples/pincheck/continue.vis @@ -0,0 +1,3 @@ +LOAD pinclear 0 +HALT +MOVE ^ diff --git a/examples/pincheck/main.go b/examples/pincheck/main.go @@ -0,0 +1,97 @@ +package main + +import ( + "bytes" + "context" + "flag" + "fmt" + "os" + "path" + + testdataloader "github.com/peteole/testdata-loader" + + "git.defalsify.org/vise.git/cache" + "git.defalsify.org/vise.git/engine" + "git.defalsify.org/vise.git/resource" + "git.defalsify.org/vise.git/state" +) + +const ( + USERFLAG_VALIDPIN = iota + state.FLAG_USERSTART + USERFLAG_QUERYPIN +) + +var ( + baseDir = testdataloader.GetBasePath() + scriptDir = path.Join(baseDir, "examples", "pincheck") + pin = []byte("1234") +) + +type pinResource struct{ + resource.Resource + st *state.State +} + +func newPinResource(resource resource.Resource, state *state.State) *pinResource { + return &pinResource{ + resource, + state, + } +} + +func(rs *pinResource) pinCheck(ctx context.Context, sym string, input []byte) (resource.Result, error) { + var r resource.Result + + if rs.st.MatchFlag(USERFLAG_QUERYPIN, false) { + r.Content = "Please enter PIN" + r.FlagReset = []uint32{USERFLAG_VALIDPIN} + r.FlagSet = []uint32{USERFLAG_QUERYPIN} + return r, nil + } + if bytes.Equal(input, pin) { + r.FlagSet = []uint32{USERFLAG_VALIDPIN} + engine.Logg.DebugCtxf(ctx, "pin match", "state", rs.st, "rs", r.FlagSet, "rr", r.FlagReset) + } else { + r.Content = "Wrong PIN please try again" + } + return r, nil +} + +func(rs *pinResource) pinClear(ctx context.Context, sym string, input []byte) (resource.Result, error) { + var r resource.Result + r.FlagReset = []uint32{USERFLAG_VALIDPIN, USERFLAG_QUERYPIN} + return r, nil +} + +func main() { + root := "root" + flag.Parse() + fmt.Fprintf(os.Stderr, "starting session at symbol '%s' using resource dir: %s\n", root, scriptDir) + + st := state.NewState(3) + st.UseDebug() + state.FlagDebugger.Register(USERFLAG_VALIDPIN, "VALIDPIN") + state.FlagDebugger.Register(USERFLAG_QUERYPIN, "QUERYPIN") + rsf := resource.NewFsResource(scriptDir) + rs := newPinResource(rsf, &st) + rsf.AddLocalFunc("pincheck", rs.pinCheck) + rsf.AddLocalFunc("pinclear", rs.pinClear) + ca := cache.NewCache() + cfg := engine.Config{ + Root: "root", + } + ctx := context.Background() + en := engine.NewEngine(ctx, cfg, &st, rs, ca) + var err error + _, err = en.Init(ctx) + if err != nil { + fmt.Fprintf(os.Stderr, "engine init fail: %v\n", err) + os.Exit(1) + } + + err = engine.Loop(ctx, &en, os.Stdin, os.Stdout) + if err != nil { + fmt.Fprintf(os.Stderr, "loop exited with error: %v\n", err) + os.Exit(1) + } +} diff --git a/examples/pincheck/root.vis b/examples/pincheck/root.vis @@ -0,0 +1 @@ +MOVE shut diff --git a/examples/pincheck/shut b/examples/pincheck/shut @@ -0,0 +1,2 @@ +The way is shut. +{{.pincheck}} diff --git a/examples/pincheck/shut.vis b/examples/pincheck/shut.vis @@ -0,0 +1,7 @@ +LOAD pincheck 0 +CATCH continue 8 1 +MOUT cancel 0 +MAP pincheck +HALT +INCMP cancel 0 +INCMP check * diff --git a/vm/vm.go b/vm/vm.go @@ -216,8 +216,8 @@ func instructionSplit(b []byte) (string, []byte, error) { if sz == 0 { return "", nil, fmt.Errorf("zero-length argument") } - bSz := uint8(len(b)) - if bSz < sz { + bSz := len(b) + if bSz < int(sz) { return "", nil, fmt.Errorf("corrupt instruction, len %v less than symbol length: %v", bSz, sz) } r := string(b[1:1+sz])