diff --git a/README.md b/README.md index 3dbdfb9..96565e9 100644 --- a/README.md +++ b/README.md @@ -74,6 +74,7 @@ Unknown builtin names make `NewWithNames` panic. For names supplied by callers, - `Reset()` discards runtime bindings and loaded-module state, while retaining configuration and the optional compiled-program cache. It does not undo host-side effects. - `RunScript` executes supplied bytes without the compiled-program cache. For reusable named scripts, use `SetScript` + `Run`, or `RunFile`; enable caching with `SetScriptCacheEnabled(true)` when needed. - Route script `print(...)` with `SetPrintFunc`; `nil` uses stderr and `NoopPrintFunc` discards output. See [file execution, output, and errors](USAGE.md). +- `SetMaxPrintBytes(n)` optionally limits messages sent through that handler; `0` is unlimited. See [print byte budgets](USAGE.md#print-byte-budget). - After loading function definitions, use `Call`, `CallWithContext`, or `CallWithTimeout` to invoke them from Go. Context cancellation is cooperative; it cannot preempt arbitrary Go callbacks. - `SetMaxExecutionSteps` bounds VM steps; `0` means unlimited. The counter resets for each Run/Call, and file-loaded module initialization receives a separate thread allowance. This is not a wall-clock or memory limit. diff --git a/SECURITY.md b/SECURITY.md index e663592..476ddf9 100644 --- a/SECURITY.md +++ b/SECURITY.md @@ -40,6 +40,15 @@ actual selection with `go list -m go.starlark.net` and review any change. An application's `replace` directive is local to that application and does not provide a backport to consumers of this library. +## Print output limits + +`Machine.SetMaxPrintBytes` limits message bytes delivered through the Machine's +print handler, with one allowance per Run/Call (or REPL session), shared with +Machine-loaded source modules. It does not bound temporary message allocations, +loggers, direct stderr output, returned values, or trusted loader helpers that +execute on their own threads. It does not supply the external process resource +limits described above. + ## Reporting Report vulnerabilities privately through diff --git a/USAGE.md b/USAGE.md index cbcc1dc..e84bff2 100644 --- a/USAGE.md +++ b/USAGE.md @@ -108,6 +108,14 @@ func main() { // Output: script: hello ``` +### Print byte budget + +`m.SetMaxPrintBytes(64 * 1024)` limits bytes delivered through the Machine's print handler. The default `0` is unlimited. Each Run or Call gets a fresh allowance; a REPL session shares one allowance across its inputs. Machine-loaded source modules consume the caller's allowance. `Reset` preserves the configured limit. + +The limit counts UTF-8 message bytes before host formatting or added newlines. A message that would exceed it is withheld in full, and Run/Call stop with the value error `PrintLimitExceededError`; use `errors.As` as shown below. An exceeded REPL allowance ends the session and reports the error. + +The limit does not bound loggers, direct stderr writes such as `eprint()`, returned values, message construction, or trusted source-loader helpers using separate threads. Those channels need separate host limits. A nonzero budget adds allocation and synchronized accounting work; configuring it can allocate before the first execution. Costs depend on the compiler, machine and workload. + ## Errors and cancellation Machine execution returns Go errors. `ExecError` unwraps its cause; use `errors.As` to classify the exported **value** error types, rather than matching message text: @@ -126,6 +134,7 @@ func describe(err error) { var missing starlet.ModuleNotFoundError var withheld starlet.ModuleWithheldError var steps starlet.MaxStepsExceededError + var printLimit starlet.PrintLimitExceededError switch { case errors.As(err, &missing): fmt.Println("missing:", missing.Name) @@ -133,6 +142,8 @@ func describe(err error) { fmt.Println("withheld:", withheld.Name) case errors.As(err, &steps): fmt.Println("step limit:", steps.Limit) + case errors.As(err, &printLimit): + fmt.Println("print byte limit:", printLimit.Limit) default: fmt.Println(err) } @@ -147,15 +158,20 @@ func main() { m.SetMaxExecutionSteps(1) _, err = m.RunScript([]byte(`answer = [i * i for i in range(10)]`), nil) describe(err) + m.SetMaxExecutionSteps(0) + m.SetMaxPrintBytes(2) + _, err = m.RunScript([]byte(`print("too long")`), nil) + describe(err) } // Output: // withheld: json // missing: missing_module // step limit: 1 +// print byte limit: 2 ``` -Use `errors.As(err, &evalErr)` with `var evalErr *starlark.EvalError` to obtain a Starlark backtrace. `SetMaxExecutionSteps(0)` removes the step limit. The budget is per Run/Call; file-loaded module initialization gets a separate allowance. +Use `errors.As(err, &evalErr)` with `var evalErr *starlark.EvalError` to obtain a Starlark backtrace. `SetMaxExecutionSteps(0)` removes the step limit. The step budget is per Run/Call; file-loaded module initialization gets a separate allowance. `RunWithContext` uses source previously set with `SetScript`; `CallWithContext` invokes an already-defined function. An already-cancelled context fails before main-script execution; module preparation happens first: diff --git a/cache.go b/cache.go index c8efb10..afb657b 100644 --- a/cache.go +++ b/cache.go @@ -18,6 +18,14 @@ import ( // with a BSD 3-clause license (see the LICENSE file in that repo). // Original source code: https://github.com/google/starlark-go/blob/master/starlark/example_test.go#L211 +// machineState keeps optional print configuration with the runtime load state. +// Runtime initialization reuses state created by SetMaxPrintBytes, keeping +// optional configuration out of the Machine value. +type machineState struct { + loads cache + maxPrintBytes uint64 +} + // cache is a concurrency-safe, duplicate-suppressing, // non-blocking cache of the doLoad function. // See Section 9.7 of gopl.io for an explanation of this structure. @@ -29,16 +37,10 @@ type cache struct { execOpts *syntax.FileOptions loadMod func(s string) (starlark.StringDict, error) // load from built-in module first readFile func(s string) ([]byte, error) // and then from file system - // newThread builds the thread that executes a loaded module, carrying the - // Machine's execution context (print func, step budget, run context). When - // nil the load path falls back to a bare thread — a module loaded that way - // would inherit none of the Machine's guards. - newThread func(load func(*starlark.Thread, string) (starlark.StringDict, error)) *starlark.Thread - // watchCancel, when set, arms cancellation of the load thread from the run's - // context and returns a stop func to disarm it once loading finishes, so a - // module executed by load() honours the run timeout instead of running past - // it on its own thread. When nil the load thread is not cancelled by context. - watchCancel func(thread *starlark.Thread) (stop func()) + // Both load-thread construction and cancellation belong to the same Machine. + // Keep one owner instead of allocating two bound method values. A standalone + // cache with no owner retains its bare-thread, uncancelled fallback. + machine *Machine } type entry struct { @@ -144,11 +146,11 @@ func (c *cache) doLoad(cc *cycleChecker, module string, loadThread **starlark.Th return c.get(cc, module) } var thread *starlark.Thread - if c.newThread != nil { + if c.machine != nil { // inherit the Machine's print func, step budget, and run context so a // loaded module cannot bypass them (e.g. a top-level loop escaping the // DoS guard, or print() going to stdout instead of the print func) - thread = c.newThread(loadFn) + thread = c.machine.newLoadThread(loadFn) } else { thread = &starlark.Thread{ Print: func(_ *starlark.Thread, msg string) { fmt.Println(msg) }, @@ -160,8 +162,8 @@ func (c *cache) doLoad(cc *cycleChecker, module string, loadThread **starlark.Th // Cancel this load thread when the run's context fires, so a long // computation inside a loaded module honours the run timeout rather than // running to completion on its own thread. Disarmed once loading finishes. - if c.watchCancel != nil { - defer c.watchCancel(thread)() + if c.machine != nil { + defer c.machine.watchLoadThread(thread)() } // 1: load from built-in module, the first field returns nil if not found diff --git a/call.go b/call.go index be99457..365f6a7 100644 --- a/call.go +++ b/call.go @@ -38,9 +38,12 @@ func (m *Machine) callInternal(ctx context.Context, name string, args []interfac defer func() { if r := recover(); r != nil { - if me, ok := r.(MaxStepsExceededError); ok { - err = errorStarlarkError("call", me) - } else { + switch e := r.(type) { + case MaxStepsExceededError: + err = errorStarlarkError("call", e) + case PrintLimitExceededError: + err = errorStarlarkError("call", e) + default: err = errorStarlarkPanic("call", r) } } @@ -77,6 +80,9 @@ func (m *Machine) callInternal(ctx context.Context, name string, args []interfac // reset the thread, arm the step budget, and wire the context m.thread.Uncancel() m.applyStepBudget() + if limit := m.state.maxPrintBytes; limit != 0 { + m.installPrintBudget(limit) + } if ctx == nil { // plain Call: no cancellation channel, as before m.thread.SetLocal("context", context.TODO()) diff --git a/call_test.go b/call_test.go index 4f45e37..1589977 100644 --- a/call_test.go +++ b/call_test.go @@ -4,15 +4,162 @@ import ( "context" "errors" "fmt" + "os" "reflect" "strings" "testing" + "testing/fstest" "time" "github.com/1set/starlet" "go.starlark.net/starlark" ) +// Print budget sections: per-call reset, overflow termination, and updating the +// handler/limit on a reused thread. +func TestMachine_CallPrintByteBudget(t *testing.T) { + m := starlet.NewDefault() + if _, err := m.RunScript([]byte("def emit(s):\n print(s)\n return 7"), nil); err != nil { + t.Fatal(err) + } + m.SetMaxPrintBytes(2) + delivered := 0 + m.SetPrintFunc(func(_ *starlark.Thread, msg string) { delivered += len(msg) }) + for i := 0; i < 2; i++ { + if _, err := m.CallWithTimeout(time.Second, "emit", "ok"); err != nil { + t.Fatalf("call %d did not get a fresh allowance: %v", i, err) + } + } + var limitErr starlet.PrintLimitExceededError + if out, err := m.Call("emit", "too long"); !errors.As(err, &limitErr) || out != nil { + t.Fatalf("want withheld result and typed limit: out=%v err=%v", out, err) + } + if delivered != 4 { + t.Fatalf("delivered %d bytes, want 4", delivered) + } + m.SetMaxPrintBytes(0) + m.SetPrintFunc(starlet.NoopPrintFunc) + if _, err := m.CallWithContext(context.Background(), "emit", "unlimited again"); err != nil { + t.Fatalf("clearing the limit after a rejected call: %v", err) + } +} + +// Call print configuration changes take effect on the next execution, including +// replacing handlers on an existing thread and clearing an exhausted allowance. +func TestMachine_CallPrintConfigurationChanges(t *testing.T) { + const script = "def emit(s):\n print(s)\n return 7" + t.Run("replace unlimited handler after Run", func(t *testing.T) { + m := starlet.NewDefault() + var original, replacement strings.Builder + m.SetPrintFunc(func(_ *starlark.Thread, msg string) { original.WriteString(msg) }) + if _, err := m.RunScript([]byte(script), nil); err != nil { + t.Fatal(err) + } + if _, err := m.Call("emit", "first"); err != nil { + t.Fatal(err) + } + m.SetPrintFunc(func(_ *starlark.Thread, msg string) { replacement.WriteString(msg) }) + if out, err := m.Call("emit", "second"); err != nil || out != int64(7) { + t.Fatalf("replacement call: out=%v err=%v", out, err) + } + if original.String() != "first" || replacement.String() != "second" { + t.Fatalf("handlers: original=%q replacement=%q", original.String(), replacement.String()) + } + }) + t.Run("disable limit after replacing handler", func(t *testing.T) { + m := starlet.NewDefault() + var original, replacement strings.Builder + m.SetMaxPrintBytes(2) + m.SetPrintFunc(func(_ *starlark.Thread, msg string) { original.WriteString(msg) }) + if _, err := m.RunScript([]byte(script), nil); err != nil { + t.Fatal(err) + } + if _, err := m.Call("emit", "ok"); err != nil { + t.Fatal(err) + } + m.SetPrintFunc(func(_ *starlark.Thread, msg string) { replacement.WriteString(msg) }) + if _, err := m.Call("emit", "hi"); err != nil { + t.Fatalf("limited replacement handler: %v", err) + } + var limitErr starlet.PrintLimitExceededError + if _, err := m.Call("emit", "rejected"); !errors.As(err, &limitErr) { + t.Fatalf("want rejected message before clearing allowance: %v", err) + } + m.SetMaxPrintBytes(0) + if _, err := m.Call("emit", "unlimited"); err != nil { + t.Fatalf("clearing limit: %v", err) + } + if original.String() != "ok" || replacement.String() != "hiunlimited" { + t.Fatalf("handlers: original=%q replacement=%q", original.String(), replacement.String()) + } + }) + t.Run("nil handler restores default stderr", func(t *testing.T) { + f, err := os.CreateTemp(t.TempDir(), "call-stderr-") + if err != nil { + t.Fatal(err) + } + previous := os.Stderr + os.Stderr = f + defer func() { os.Stderr = previous; _ = f.Close() }() + for _, limit := range []uint64{0, 2} { + m := starlet.NewDefault() + var original strings.Builder + m.SetPrintFunc(func(_ *starlark.Thread, msg string) { original.WriteString(msg) }) + m.SetMaxPrintBytes(limit) + if _, err := m.RunScript([]byte(script), nil); err != nil { + t.Fatal(err) + } + if _, err := m.Call("emit", "ok"); err != nil { + t.Fatal(err) + } + m.SetPrintFunc(nil) + if limit != 0 { + m.SetMaxPrintBytes(0) + } + if _, err := m.Call("emit", "default handler"); err != nil { + t.Fatalf("limit=%d default handler: %v", limit, err) + } + if original.String() != "ok" { + t.Fatalf("limit=%d retained previous handler: %q", limit, original.String()) + } + } + os.Stderr = previous + if err := f.Close(); err != nil { + t.Fatal(err) + } + got, err := os.ReadFile(f.Name()) + if err != nil || string(got) != "default handler\ndefault handler\n" { + t.Fatalf("default stderr: err=%v output=%q", err, got) + } + }) +} + +func TestMachine_CallLoadedFunctionPrintByteBudget(t *testing.T) { + for _, cached := range []bool{false, true} { + t.Run(fmt.Sprintf("compiled cache=%v", cached), func(t *testing.T) { + m := starlet.NewDefault() + m.SetScriptCacheEnabled(cached) + m.SetPrintFunc(starlet.NoopPrintFunc) + m.SetMaxPrintBytes(3) + m.SetScript("main.star", []byte("load(\"emit.star\", \"emit\")\nexported = emit"), fstest.MapFS{ + "emit.star": &fstest.MapFile{Data: []byte("print(\"abc\")\ndef emit(s):\n print(s)")}, + }) + for run := 0; run < 2; run++ { + if _, err := m.Run(); err != nil { + t.Fatalf("run %d: %v", run, err) + } + if _, err := m.Call("exported", "abc"); err != nil { + t.Fatalf("loaded function retained the previous allowance: %v", err) + } + var limitErr starlet.PrintLimitExceededError + if _, err := m.CallWithContext(context.Background(), "exported", "abcd"); !errors.As(err, &limitErr) { + t.Fatalf("loaded function bypassed the current allowance: %v", err) + } + } + }) + } +} + func TestMachine_Call_Preconditions(t *testing.T) { m := starlet.NewDefault() diff --git a/error.go b/error.go index 2fbeafe..e6f0116 100644 --- a/error.go +++ b/error.go @@ -133,3 +133,14 @@ type MaxStepsExceededError struct { func (e MaxStepsExceededError) Error() string { return fmt.Sprintf("execution exceeded the step limit (%d)", e.Limit) } + +// PrintLimitExceededError marks an execution stopped by SetMaxPrintBytes. +// Detect it with errors.As through the execution error chain. +type PrintLimitExceededError struct { + Limit uint64 +} + +// Error returns the error message. +func (e PrintLimitExceededError) Error() string { + return fmt.Sprintf("execution exceeded the print byte limit (%d)", e.Limit) +} diff --git a/internal_test.go b/internal_test.go index 17275e0..ae11c16 100644 --- a/internal_test.go +++ b/internal_test.go @@ -2,13 +2,80 @@ package starlet import ( "context" + "errors" "reflect" "strings" "testing" "go.starlark.net/starlark" + "go.starlark.net/syntax" ) +// REPL print-budget sections: separate input chunks share one allowance, the +// error boundary only handles its own budget sentinel, and zero is unlimited. +func TestREPLPrintByteBudget(t *testing.T) { + for _, limit := range []uint64{0, 2} { + m := NewDefault() + m.SetMaxPrintBytes(limit) + var delivered strings.Builder + m.SetPrintFunc(func(_ *starlark.Thread, msg string) { delivered.WriteString(msg) }) + var reported error + completed := false + m.replInternal(func(opts *syntax.FileOptions, thread *starlark.Thread, globals starlark.StringDict) { + for _, input := range []string{`print("a")`, `print("b")`, `print("c")`} { + if _, err := starlark.ExecFileOptions(opts, thread, "repl-input.star", input, globals); err != nil { + t.Fatal(err) + } + } + completed = true + }, func(err error) { reported = err }) + if limit == 0 { + if reported != nil || !completed || delivered.String() != "abc" { + t.Fatalf("unlimited session: completed=%v output=%q err=%v", completed, delivered.String(), reported) + } + } else { + var limitErr PrintLimitExceededError + if !errors.As(reported, &limitErr) || limitErr.Limit != limit || completed || delivered.String() != "ab" { + t.Fatalf("limited session: completed=%v output=%q err=%v", completed, delivered.String(), reported) + } + } + // The boundary must release the Machine lock after either outcome. + m.SetMaxPrintBytes(0) + } +} + +func TestREPLPrintHandlerPanicIsNotSwallowed(t *testing.T) { + m := NewDefault() + m.SetMaxPrintBytes(2) + sentinel := errors.New("host print callback failed") + m.SetPrintFunc(func(_ *starlark.Thread, _ string) { panic(sentinel) }) + defer func() { + if got := recover(); got != sentinel { + t.Fatalf("host panic changed: %v", got) + } + }() + m.replInternal(func(opts *syntax.FileOptions, thread *starlark.Thread, globals starlark.StringDict) { + _, _ = starlark.ExecFileOptions(opts, thread, "repl-input.star", `print("a")`, globals) + }, func(err error) { t.Fatalf("host panic was reported as a budget error: %v", err) }) +} + +func TestREPLNilPrintHandlerPanicIsNotSwallowed(t *testing.T) { + m := NewDefault() + m.SetMaxPrintBytes(2) + m.SetPrintFunc(func(_ *starlark.Thread, _ string) { panic(nil) }) + returned := false + func() { + defer func() { _ = recover() }() + m.replInternal(func(opts *syntax.FileOptions, thread *starlark.Thread, globals starlark.StringDict) { + _, _ = starlark.ExecFileOptions(opts, thread, "repl-input.star", `print("a")`, globals) + }, func(err error) { t.Fatalf("host panic was reported as a budget error: %v", err) }) + returned = true + }() + if returned { + t.Fatal("REPL swallowed the host callback's panic(nil)") + } +} + // TestWatchLoadThread covers the load-thread cancel watcher's two arms. A load // thread with no run context (e.g. a load during a REPL, which sets none) must // get a harmless no-op stop rather than watch a nil context; a thread carrying a diff --git a/machine.go b/machine.go index 9fa8aa5..8755aa4 100644 --- a/machine.go +++ b/machine.go @@ -44,7 +44,7 @@ type Machine struct { // runtime core progCache ByteCache runTimes uint - loadCache *cache + state *machineState thread *starlark.Thread predeclared starlark.StringDict } @@ -226,11 +226,43 @@ var ( // SetPrintFunc sets the print function of the Starlark runtime environment. // To disable printing, you can set it to NoopPrintFunc. Setting it to nil will invoke the default `fmt.Fprintln(os.Stderr, msg)` instead. +// The handler applies to subsequent Machine executions. When printing is +// unlimited, an initialized main thread is also updated immediately. func (m *Machine) SetPrintFunc(printFunc PrintFunc) { m.mu.Lock() defer m.mu.Unlock() m.printFunc = printFunc + if m.thread != nil && m.state.maxPrintBytes == 0 { + m.thread.Print = printFunc + } +} + +// SetMaxPrintBytes limits the total bytes of messages delivered to the print +// handler during each Run or Call, including messages from modules loaded by +// the Machine. Zero, the default, means unlimited. It counts UTF-8 message bytes, +// excluding any newline or formatting added by the host handler. An oversized +// message is withheld in full and execution fails with PrintLimitExceededError. +// A REPL session shares one budget; exceeding it ends the session. Changes apply +// to the next Machine execution. Setting zero also restores the configured +// handler on an initialized main thread. +// +// This limits the print channel, not loggers, direct stderr writes such as +// eprint, returned values, or memory allocated while constructing a message. +func (m *Machine) SetMaxPrintBytes(limit uint64) { + m.mu.Lock() + defer m.mu.Unlock() + + if m.state == nil { + if limit == 0 { + return + } + m.state = &machineState{} + } + m.state.maxPrintBytes = limit + if limit == 0 && m.thread != nil { + m.thread.Print = m.printFunc + } } // SetScript sets the script related things of the Starlark runtime environment. diff --git a/run.go b/run.go index a75dd22..98967fd 100644 --- a/run.go +++ b/run.go @@ -6,6 +6,7 @@ import ( "io" "io/fs" "math" + "os" "sync" "time" @@ -150,9 +151,12 @@ func (m *Machine) watchLoadThread(thread *starlark.Thread) (stop func()) { func (m *Machine) runInternal(ctx context.Context, extras StringAnyMap, allowCache bool) (out StringAnyMap, err error) { defer func() { if r := recover(); r != nil { - if me, ok := r.(MaxStepsExceededError); ok { - err = errorStarlarkError("exec", me) - } else { + switch e := r.(type) { + case MaxStepsExceededError: + err = errorStarlarkError("exec", e) + case PrintLimitExceededError: + err = errorStarlarkError("exec", e) + default: err = errorStarlarkPanic("exec", r) } } @@ -288,22 +292,23 @@ func (m *Machine) prepareThread(extras StringAnyMap) (err error) { } // cache load&read + printf -> thread - m.loadCache = &cache{ + if m.state == nil { + m.state = &machineState{} + } + m.state.loads = cache{ cache: make(map[string]*entry), execOpts: m.getFileOptions(), loadMod: m.lazyloadMods.GetLazyLoader(), readFile: func(name string) ([]byte, error) { return readScriptFile(name, m.scriptFS) }, - globals: m.predeclared, - newThread: m.newLoadThread, - watchCancel: m.watchLoadThread, + globals: m.predeclared, + machine: m, } m.thread = &starlark.Thread{ - Name: "starlet", - Print: m.printFunc, + Name: "starlet", Load: func(thread *starlark.Thread, module string) (starlark.StringDict, error) { - return m.loadCache.Load(module) + return m.state.loads.Load(module) }, } } else { @@ -315,19 +320,88 @@ func (m *Machine) prepareThread(extras StringAnyMap) (err error) { } // set globals for cache - m.loadCache.loadMod = m.lazyloadMods.GetLazyLoader() - m.loadCache.globals = m.predeclared + m.state.loads.loadMod = m.lazyloadMods.GetLazyLoader() + m.state.loads.globals = m.predeclared // reset for each run - m.thread.Print = m.printFunc m.thread.Uncancel() } // arm the per-run step budget m.applyStepBudget() + m.applyPrintBudget() return nil } +// applyPrintBudget installs one allowance shared by the main and load threads. +// The Machine serializes executions; the separate mutex protects the shared +// accounting without holding a lock while the host print handler runs. +func (m *Machine) applyPrintBudget() { + limit := m.state.maxPrintBytes + if limit == 0 { + m.thread.Print = m.printFunc + return + } + m.installPrintBudget(limit) +} + +// installPrintBudget keeps the limited-handler allocation off the unlimited path. +func (m *Machine) installPrintBudget(limit uint64) { + print := m.printFunc + var mu sync.Mutex + var used uint64 + m.thread.Print = func(thread *starlark.Thread, msg string) { + mu.Lock() + n := uint64(len(msg)) + exceeded := n > limit-used + if !exceeded { + used += n + } + mu.Unlock() + if exceeded { + // Like the step guard, unwind immediately even when this print is + // the last statement. Run/Call preserve the typed error, and the + // load cache evicts the interrupted entry before re-raising it. + panic(PrintLimitExceededError{Limit: limit}) + } + if print != nil { + print(thread, msg) + } else { + fmt.Fprintln(os.Stderr, msg) + } + } +} + +// replInternal owns the Machine for a complete REPL session. The native +// frontend supplies terminal input and error reporting; keeping execution here +// lets the same budget boundary be exercised with a scripted driver, without +// depending on platform-specific console readers or importing them on wasm. +func (m *Machine) replInternal(driver func(*syntax.FileOptions, *starlark.Thread, starlark.StringDict), report func(error)) { + m.mu.Lock() + defer m.mu.Unlock() + completed := false + defer func() { + if completed { + return + } + r := recover() + if limit, ok := r.(PrintLimitExceededError); ok { + report(errorStarlarkError("repl", limit)) + return + } + // A completion marker also preserves panic(nil) on the Go 1.19 + // floor, where recover() cannot distinguish it from normal return. + panic(r) + }() + if err := m.prepareThread(nil); err != nil { + completed = true + report(err) + return + } + driver(m.getFileOptions(), m.thread, m.predeclared) + completed = true +} + // newLoadThread builds the thread that runs a module executed by load(), // mirroring the main thread's execution context: the same print func, an // independent copy of the step budget (so a loaded module's work is bounded @@ -353,6 +427,8 @@ func (m *Machine) newLoadThread(load func(*starlark.Thread, string) (starlark.St } } if m.thread != nil { + // Reuse the exact handler so loads consume the same print allowance. + t.Print = m.thread.Print if ctx := m.thread.Local("context"); ctx != nil { t.SetLocal("context", ctx) } @@ -394,7 +470,17 @@ func (m *Machine) Reset() { m.runTimes = 0 m.thread = nil - m.loadCache = nil + if m.state != nil { + limit := m.state.maxPrintBytes + if limit == 0 { + m.state = nil + } else if m.state.loads.machine != nil { + // A loaded thread may have exposed a Load closure that still owns + // the completed cache. Replace it instead of rewriting its mutex, + // entries or globals in place; only the configuration survives. + m.state = &machineState{maxPrintBytes: limit} + } + } m.predeclared = nil } diff --git a/run_repl.go b/run_repl.go index 73b4995..db798f1 100644 --- a/run_repl.go +++ b/run_repl.go @@ -11,19 +11,5 @@ import "go.starlark.net/repl" // chzyer/readline, a terminal library that does not compile for browser // js/wasm or WASI; see run_repl_stub.go for the no-terminal stub. func (m *Machine) REPL() { - // Hold the write lock for the whole REPL session, matching runInternal: - // prepareThread and repl.REPLOptions read and mutate m.thread/m.predeclared, - // and every other execution path already serializes on m.mu (String() uses - // TryRLock and returns the running snapshot rather than blocking). A REPL - // owns the Machine exclusively for its duration, so holding the lock until it - // exits is the right scope — without it, a REPL concurrent with Run/Reset - // races on m.thread. - m.mu.Lock() - defer m.mu.Unlock() - - if err := m.prepareThread(nil); err != nil { - repl.PrintError(err) - return - } - repl.REPLOptions(m.getFileOptions(), m.thread, m.predeclared) + m.replInternal(repl.REPLOptions, repl.PrintError) } diff --git a/run_test.go b/run_test.go index b3145b8..fd3667e 100644 --- a/run_test.go +++ b/run_test.go @@ -18,6 +18,241 @@ import ( "go.starlark.net/starlark" ) +// Print budget sections: message byte accounting, execution reuse, loaded +// module aggregation/retry and default stderr. REPL budget execution is +// exercised through the shared driver in internal_test.go. +func TestMachine_PrintByteBudget(t *testing.T) { + for _, tc := range []struct { + name string + limit uint64 + code string + want string + fail bool + }{ + {"default unlimited", 0, `print("x" * 1048576)`, strings.Repeat("x", 1048576), false}, + {"exact UTF8 boundary", 5, `print("界"); print("é")`, "界é", false}, + {"UTF8 is bytes", 4, `print("界"); print("é")`, "界", true}, + {"cumulative messages", 4, `print("ab"); print("cd"); print("e")`, "abcd", true}, + {"oversized message withheld", 8, `print("x" * 1048576)`, "", true}, + {"empty message costs zero", 1, `print(""); print("x"); print("")`, "x", false}, + } { + t.Run(tc.name, func(t *testing.T) { + m := starlet.NewDefault() + var got strings.Builder + m.SetPrintFunc(func(_ *starlark.Thread, msg string) { got.WriteString(msg) }) + m.SetMaxPrintBytes(tc.limit) + out, err := m.RunScript([]byte(tc.code), nil) + var limitErr starlet.PrintLimitExceededError + if tc.fail { + if !errors.As(err, &limitErr) || limitErr.Limit != tc.limit { + t.Fatalf("want typed print limit %d, got %v", tc.limit, err) + } + if !strings.Contains(err.Error(), "print byte limit") { + t.Fatalf("missing print-limit diagnostic: %v", err) + } + if out != nil { + t.Fatal("failed execution returned output") + } + } else if err != nil { + t.Fatal(err) + } + if got.String() != tc.want { + t.Fatalf("delivered %d bytes, want %d", got.Len(), len(tc.want)) + } + }) + } + + t.Run("execution stops after rejected print", func(t *testing.T) { + marked := false + m := starlet.NewWithGlobals(starlet.StringAnyMap{"mark": func() { marked = true }}) + m.SetMaxPrintBytes(1) + m.SetPrintFunc(starlet.NoopPrintFunc) + _, err := m.RunScript([]byte(`print("too long"); mark()`), nil) + var limitErr starlet.PrintLimitExceededError + if !errors.As(err, &limitErr) || marked { + t.Fatalf("execution continued: marked=%v err=%v", marked, err) + } + }) + + t.Run("pretty printing shares the handler allowance", func(t *testing.T) { + m := starlet.NewWithNames(nil, []string{"go_idiomatic"}, nil) + m.SetPrintFunc(starlet.NoopPrintFunc) + m.SetMaxPrintBytes(2) + _, err := m.RunScript([]byte(`pprint([1, 2, 3])`), nil) + var limitErr starlet.PrintLimitExceededError + if !errors.As(err, &limitErr) { + t.Fatalf("pprint escaped the handler allowance: %v", err) + } + }) + + t.Run("budget resets and configuration survives Reset", func(t *testing.T) { + m := starlet.NewDefault() + m.SetPrintFunc(starlet.NoopPrintFunc) + m.SetMaxPrintBytes(2) + for i := 0; i < 2; i++ { + if _, err := m.RunScript([]byte(`print("ok")`), nil); err != nil { + t.Fatalf("run %d: %v", i, err) + } + } + m.Reset() + var limitErr starlet.PrintLimitExceededError + if _, err := m.RunScript([]byte(`print("bad")`), nil); !errors.As(err, &limitErr) { + t.Fatalf("Reset lost configured budget: %v", err) + } + m.SetMaxPrintBytes(0) + if _, err := m.RunScript([]byte(`print("now unlimited")`), nil); err != nil { + t.Fatalf("clearing limit on reused thread: %v", err) + } + }) + + t.Run("configuration survives Reset before the first execution", func(t *testing.T) { + m := starlet.NewDefault() + m.SetPrintFunc(starlet.NoopPrintFunc) + m.SetMaxPrintBytes(2) + m.Reset() + m.Reset() + var limitErr starlet.PrintLimitExceededError + if _, err := m.RunScript([]byte(`print("bad")`), nil); !errors.As(err, &limitErr) { + t.Fatalf("Reset before execution lost the allowance: %v", err) + } + m.Reset() + m.SetMaxPrintBytes(0) + m.Reset() + if _, err := m.RunScript([]byte(`print("unlimited again")`), nil); err != nil { + t.Fatalf("clearing the allowance after Reset: %v", err) + } + }) + + t.Run("initialization failure does not lose configuration", func(t *testing.T) { + m := starlet.NewDefault() + m.SetPrintFunc(starlet.NoopPrintFunc) + m.SetMaxPrintBytes(2) + bootstrapErr := errors.New("preload unavailable") + m.SetPreloadModules(starlet.ModuleLoaderList{func() (starlark.StringDict, error) { + return nil, bootstrapErr + }}) + if _, err := m.RunScript([]byte(`print("ok")`), nil); !errors.Is(err, bootstrapErr) { + t.Fatalf("initialization control: %v", err) + } + m.SetPreloadModules(nil) + m.Reset() + var limitErr starlet.PrintLimitExceededError + if _, err := m.RunScript([]byte(`print("bad")`), nil); !errors.As(err, &limitErr) { + t.Fatalf("failed initialization lost the allowance: %v", err) + } + }) +} + +func TestMachine_LoadPrintByteBudget(t *testing.T) { + t.Run("Reset separates loaded state while retaining the allowance", func(t *testing.T) { + m := starlet.NewDefault() + var previousLoad starlet.LoadFunc + m.SetPrintFunc(func(thread *starlark.Thread, message string) { + if previousLoad == nil && message == "capture" { + previousLoad = thread.Load + } + }) + m.SetMaxPrintBytes(7) + files := fstest.MapFS{ + "a.star": &fstest.MapFile{Data: []byte("load(\"b.star\", \"value\")\nprint(\"capture\")\na = value")}, + "b.star": &fstest.MapFile{Data: []byte("value = 1")}, + } + m.SetScript("main.star", []byte(`load("a.star", "a"); result = a`), files) + if out, err := m.Run(); err != nil || out["result"] != int64(1) { + t.Fatalf("first load: %v, %v", out, err) + } + if previousLoad == nil { + t.Fatal("loaded print did not publish its load callback") + } + m.Reset() + files["b.star"].Data = []byte("value = 2") + if out, err := m.Run(); err != nil || out["result"] != int64(2) { + t.Fatalf("Reset retained the old module globals: %v, %v", out, err) + } + // A callback already exposed by the old loaded thread still refers to + // that completed cache; Reset must not rewrite a live cache in place. + old, err := previousLoad(nil, "b.star") + if err != nil || old["value"] != starlark.MakeInt(1) { + t.Fatalf("Reset rewrote an earlier load callback's cache: %v, %v", old, err) + } + m.Reset() + files["a.star"].Data = []byte("print(\"too long\")\na = 3") + var limitErr starlet.PrintLimitExceededError + if _, err := m.Run(); !errors.As(err, &limitErr) { + t.Fatalf("Reset discarded the loaded-module allowance: %v", err) + } + }) + + t.Run("all loaded modules share the run allowance", func(t *testing.T) { + m := starlet.NewDefault() + m.SetPrintFunc(starlet.NoopPrintFunc) + m.SetMaxPrintBytes(5) + m.SetScript("main.star", []byte("load(\"a.star\", \"a\")\nload(\"b.star\", \"b\")"), fstest.MapFS{ + "a.star": &fstest.MapFile{Data: []byte("print(\"abc\")\na = 1")}, + "b.star": &fstest.MapFile{Data: []byte("print(\"def\")\nb = 2")}, + }) + var limitErr starlet.PrintLimitExceededError + if _, err := m.Run(); !errors.As(err, &limitErr) { + t.Fatalf("loaded module escaped shared allowance: %v", err) + } + }) + + t.Run("loaded and main source share the run allowance", func(t *testing.T) { + m := starlet.NewDefault() + m.SetPrintFunc(starlet.NoopPrintFunc) + m.SetMaxPrintBytes(4) + m.SetScript("main.star", []byte("load(\"a.star\", \"a\")\nprint(\"de\")"), fstest.MapFS{ + "a.star": &fstest.MapFile{Data: []byte("print(\"abc\")\na = 1")}, + }) + var limitErr starlet.PrintLimitExceededError + if _, err := m.Run(); !errors.As(err, &limitErr) { + t.Fatalf("main source escaped module allowance: %v", err) + } + }) + + t.Run("a rejected loaded module can be retried", func(t *testing.T) { + m := starlet.NewDefault() + m.SetPrintFunc(starlet.NoopPrintFunc) + m.SetMaxPrintBytes(2) + m.SetScript("main.star", []byte(`load("a.star", "a")`), fstest.MapFS{ + "a.star": &fstest.MapFile{Data: []byte("print(\"abc\")\na = 1")}, + }) + var limitErr starlet.PrintLimitExceededError + if _, err := m.Run(); !errors.As(err, &limitErr) { + t.Fatalf("first run: %v", err) + } + m.SetMaxPrintBytes(3) + if _, err := m.Run(); err != nil { + t.Fatalf("retry replayed failed load: %v", err) + } + }) +} + +func TestMachine_PrintByteBudgetDefaultStderr(t *testing.T) { + f, err := os.CreateTemp(t.TempDir(), "stderr-") + if err != nil { + t.Fatal(err) + } + previous := os.Stderr + os.Stderr = f + defer func() { os.Stderr = previous; _ = f.Close() }() + m := starlet.NewDefault() + m.SetMaxPrintBytes(2) + _, runErr := m.RunScript([]byte(`print("ok"); print("withheld")`), nil) + os.Stderr = previous + if err := f.Close(); err != nil { + t.Fatal(err) + } + var limitErr starlet.PrintLimitExceededError + if !errors.As(runErr, &limitErr) { + t.Fatalf("want typed limit, got %v", runErr) + } + got, err := os.ReadFile(f.Name()) + if err != nil || string(got) != "ok\n" { + t.Fatalf("default stderr: err=%v output=%q", err, got) + } +} + // TestMachine_LoadInheritsMachineContext: a module executed by load() ran in a // bare thread that inherited none of the Machine's execution context, so the // step budget did not apply (a top-level loop in a loaded module bypassed the