Skip to content

Commit

Permalink
Enables debug info (a.k.a. DWARF) by default (#924)
Browse files Browse the repository at this point in the history
This makes DWARF enabled by default, with an opt-out flag. This didn't
need to be experimental as the feature was requested for a long time and
there's no API impact to using it.

Signed-off-by: Adrian Cole <[email protected]>
  • Loading branch information
codefromthecrypt authored Dec 15, 2022
1 parent 126bd90 commit 5f7467b
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 65 deletions.
3 changes: 1 addition & 2 deletions cmd/wazero/wazero.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,11 @@ import (
"strings"

"github.com/tetratelabs/wazero"
"github.com/tetratelabs/wazero/experimental"
"github.com/tetratelabs/wazero/imports/wasi_snapshot_preview1"
"github.com/tetratelabs/wazero/sys"
)

var ctx = experimental.WithDWARFBasedStackTrace(context.Background())
var ctx = context.Background()

func main() {
doMain(os.Stdout, os.Stderr, os.Exit)
Expand Down
5 changes: 0 additions & 5 deletions cmd/wazero/wazero_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"path/filepath"
"testing"

"github.com/tetratelabs/wazero/experimental"
"github.com/tetratelabs/wazero/internal/testing/require"
)

Expand Down Expand Up @@ -158,7 +157,3 @@ func runMain(t *testing.T, args []string) (int, string, string) {

return exitCode, stdOut.String(), stdErr.String()
}

func TestContext(t *testing.T) {
require.True(t, experimental.DWARFBasedStackTraceEnabled(ctx))
}
44 changes: 44 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,41 @@ type RuntimeConfig interface {
//
// See https://www.w3.org/TR/2019/REC-wasm-core-1-20191205/#grow-mem
WithMemoryCapacityFromMax(memoryCapacityFromMax bool) RuntimeConfig

// WithDebugInfoEnabled toggles DWARF based stack traces in the face of
// runtime errors. Defaults to true.
//
// Those who wish to disable this, can like so:
//
// r := wazero.NewRuntimeWithConfig(wazero.NewRuntimeConfig().WithDebugInfoEnabled(false)
//
// When disabled, a stack trace message looks like:
//
// wasm stack trace:
// .runtime._panic(i32)
// .myFunc()
// .main.main()
// .runtime.run()
// ._start()
//
// When enabled, the stack trace includes source code information:
//
// wasm stack trace:
// .runtime._panic(i32)
// 0x16e2: /opt/homebrew/Cellar/tinygo/0.26.0/src/runtime/runtime_tinygowasm.go:73:6
// .myFunc()
// 0x190b: /Users/XXXXX/wazero/internal/testing/dwarftestdata/testdata/main.go:19:7
// .main.main()
// 0x18ed: /Users/XXXXX/wazero/internal/testing/dwarftestdata/testdata/main.go:4:3
// .runtime.run()
// 0x18cc: /opt/homebrew/Cellar/tinygo/0.26.0/src/runtime/scheduler_none.go:26:10
// ._start()
// 0x18b6: /opt/homebrew/Cellar/tinygo/0.26.0/src/runtime/runtime_wasm_wasi.go:22:5
//
// Note: This only takes into effect when the original Wasm binary has the
// DWARF "custom sections" that are often stripped, depending on
// optimization flags passed to the compiler.
WithDebugInfoEnabled(bool) RuntimeConfig
}

// NewRuntimeConfig returns a RuntimeConfig using the compiler if it is supported in this environment,
Expand All @@ -79,6 +114,7 @@ type runtimeConfig struct {
memoryLimitPages uint32
memoryCapacityFromMax bool
isInterpreter bool
dwarfDisabled bool // negative as defaults to enabled
newEngine func(context.Context, api.CoreFeatures) wasm.Engine
}

Expand All @@ -87,6 +123,7 @@ var engineLessConfig = &runtimeConfig{
enabledFeatures: api.CoreFeaturesV2,
memoryLimitPages: wasm.MemoryLimitPages,
memoryCapacityFromMax: false,
dwarfDisabled: false,
}

// NewRuntimeConfigCompiler compiles WebAssembly modules into
Expand Down Expand Up @@ -148,6 +185,13 @@ func (c *runtimeConfig) WithMemoryCapacityFromMax(memoryCapacityFromMax bool) Ru
return ret
}

// WithDebugInfoEnabled implements RuntimeConfig.WithDebugInfoEnabled
func (c *runtimeConfig) WithDebugInfoEnabled(dwarfEnabled bool) RuntimeConfig {
ret := c.clone()
ret.dwarfDisabled = !dwarfEnabled
return ret
}

// CompiledModule is a WebAssembly module ready to be instantiated (Runtime.InstantiateModule) as an api.Module.
//
// In WebAssembly terminology, this is a decoded, validated, and possibly also compiled module. wazero avoids using
Expand Down
9 changes: 9 additions & 0 deletions config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,15 @@ func TestRuntimeConfig(t *testing.T) {
memoryCapacityFromMax: true,
},
},
{
name: "WithDebugInfoEnabled",
with: func(c RuntimeConfig) RuntimeConfig {
return c.WithDebugInfoEnabled(false)
},
expected: &runtimeConfig{
dwarfDisabled: true, // dwarf is a more technical name and ok here.
},
},
}

for _, tt := range tests {
Expand Down
16 changes: 7 additions & 9 deletions experimental/dwarf_test.go → dwarf_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package experimental_test
package wazero_test

import (
"bufio"
Expand All @@ -8,25 +8,24 @@ import (
"testing"

"github.com/tetratelabs/wazero"
"github.com/tetratelabs/wazero/experimental"
"github.com/tetratelabs/wazero/imports/wasi_snapshot_preview1"
"github.com/tetratelabs/wazero/internal/platform"
"github.com/tetratelabs/wazero/internal/testing/dwarftestdata"
"github.com/tetratelabs/wazero/internal/testing/require"
)

func TestWithDWARFBasedStackTrace(t *testing.T) {
func TestWithDebugInfo(t *testing.T) {
ctx := context.Background()
require.False(t, experimental.DWARFBasedStackTraceEnabled(ctx))
ctx = experimental.WithDWARFBasedStackTrace(ctx)
require.True(t, experimental.DWARFBasedStackTraceEnabled(ctx))

type testCase struct {
name string
r wazero.Runtime
}

tests := []testCase{{name: "interpreter", r: wazero.NewRuntimeWithConfig(ctx, wazero.NewRuntimeConfigInterpreter())}}
tests := []testCase{{
name: "interpreter",
r: wazero.NewRuntimeWithConfig(ctx, wazero.NewRuntimeConfigInterpreter()),
}}

if platform.CompilerSupported() {
tests = append(tests, testCase{
Expand Down Expand Up @@ -167,8 +166,7 @@ wasm stack trace:
compiled, err := r.CompileModule(ctx, lang.bin)
require.NoError(t, err)

// Use context.Background to ensure that DWARF is a compile-time option.
_, err = r.InstantiateModule(context.Background(), compiled, wazero.NewModuleConfig())
_, err = r.InstantiateModule(ctx, compiled, wazero.NewModuleConfig())
require.Error(t, err)

errStr := err.Error()
Expand Down
47 changes: 0 additions & 47 deletions experimental/dwarf.go

This file was deleted.

5 changes: 3 additions & 2 deletions runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ func NewRuntimeWithConfig(ctx context.Context, rConfig RuntimeConfig) Runtime {
memoryLimitPages: config.memoryLimitPages,
memoryCapacityFromMax: config.memoryCapacityFromMax,
isInterpreter: config.isInterpreter,
dwarfDisabled: config.dwarfDisabled,
}
}

Expand All @@ -149,6 +150,7 @@ type runtime struct {
memoryLimitPages uint32
memoryCapacityFromMax bool
isInterpreter bool
dwarfDisabled bool
compiledModules []*compiledModule
}

Expand All @@ -172,9 +174,8 @@ func (r *runtime) CompileModule(ctx context.Context, binary []byte) (CompiledMod
return nil, errors.New("invalid binary")
}

dwarfEnabled := experimentalapi.DWARFBasedStackTraceEnabled(ctx)
internal, err := binaryformat.DecodeModule(binary, r.enabledFeatures,
r.memoryLimitPages, r.memoryCapacityFromMax, dwarfEnabled, false)
r.memoryLimitPages, r.memoryCapacityFromMax, !r.dwarfDisabled, false)
if err != nil {
return nil, err
} else if err = internal.Validate(r.enabledFeatures); err != nil {
Expand Down

0 comments on commit 5f7467b

Please sign in to comment.