Skip to content

panic: runtime error: invalid memory address or nil pointer dereference #4895

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Open
helperShang opened this issue May 15, 2025 · 3 comments
Open
Labels
bug Something isn't working next-release Will be part of next release

Comments

@helperShang
Copy link

helperShang commented May 15, 2025

tinygo version

tinygo version 0.37.0 darwin/arm64 (using go version go1.24.3 and LLVM version 19.1.2)

source code

package main

import (
	"fmt"
	"io"

	"github.com/alecthomas/chroma/v2"
	"github.com/alecthomas/chroma/v2/formatters/html"
	"github.com/alecthomas/chroma/v2/lexers"
	"github.com/alecthomas/chroma/v2/styles"
	"github.com/gomarkdown/markdown"
	"github.com/gomarkdown/markdown/ast"
	mdhtml "github.com/gomarkdown/markdown/html"
)

func htmlHighlight(w io.Writer, source, lang, defaultLang string) error {
	if lang == "" {
		lang = defaultLang
	}
	l := lexers.Get(lang)
	if l == nil {
		l = lexers.Analyse(source)
	}
	if l == nil {
		l = lexers.Fallback
	}
	l = chroma.Coalesce(l)

	it, err := l.Tokenise(nil, source)
	if err != nil {
		return err
	}
	htmlFormatter := html.New(html.WithClasses(true), html.TabWidth(2))
	if htmlFormatter == nil {
		panic("couldn't create html formatter")
	}
	styleName := "monokailight"
	highlightStyle := styles.Get(styleName)
	if highlightStyle == nil {
		panic(fmt.Sprintf("didn't find style '%s'", styleName))
	}
	return htmlFormatter.Format(w, highlightStyle, it)
}

// an actual rendering of Paragraph is more complicated
func renderCode(w io.Writer, codeBlock *ast.CodeBlock, entering bool) {
	defaultLang := ""
	lang := string(codeBlock.Info)
	htmlHighlight(w, string(codeBlock.Literal), lang, defaultLang)
}

func myRenderHook(w io.Writer, node ast.Node, entering bool) (ast.WalkStatus, bool) {
	if code, ok := node.(*ast.CodeBlock); ok {
		renderCode(w, code, entering)
		return ast.GoToNext, true
	}
	return ast.GoToNext, false
}

func newCustomizedRender() *mdhtml.Renderer {
	opts := mdhtml.RendererOptions{
		Flags:          mdhtml.CommonFlags,
		RenderNodeHook: myRenderHook,
	}
	return mdhtml.NewRenderer(opts)
}

func main() {
	markdown.ToHTML([]byte(`# asfasdfs`), nil, newCustomizedRender())
}

build

tinygo build -target wasm -x

out

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x2 addr=0x38 pc=0x102432e30]

goroutine 241 [running]:
github.com/tinygo-org/tinygo/interp.(*runner).run(0x14006a6b4a0, 0x140027a2870, {0x140088a8290, 0x6, 0x1400887fa28?}, 0x1400393ecf0, {0x1400846d2c0, 0x28})
        /Users/runner/work/tinygo/tinygo/interp/interpreter.go:369 +0x3470
github.com/tinygo-org/tinygo/interp.(*runner).run(0x14006a6b4a0, 0x140027a27d0, {0x1400889b390, 0x2, 0x1400887eb28?}, 0x140039113b0, {0x1400846d1d0, 0x24})
        /Users/runner/work/tinygo/tinygo/interp/interpreter.go:560 +0x669c
github.com/tinygo-org/tinygo/interp.(*runner).run(0x14006a6b4a0, 0x140027a2780, {0x1400888bc10, 0x3, 0x1400887e408?}, 0x1400367fb00, {0x140088969c0, 0x20})
        /Users/runner/work/tinygo/tinygo/interp/interpreter.go:560 +0x669c
github.com/tinygo-org/tinygo/interp.(*runner).run(0x14006a6b4a0, 0x140027a26e0, {0x1400887d790, 0x2, 0x14008665bf0?}, 0x140035d3410, {0x14008896400, 0x1c})
        /Users/runner/work/tinygo/tinygo/interp/interpreter.go:560 +0x669c
github.com/tinygo-org/tinygo/interp.(*runner).run(0x14006a6b4a0, 0x140027a2690, {0x14008804290, 0x2, 0x140086644f8?}, 0x14003580990, {0x14008833758, 0x18})
        /Users/runner/work/tinygo/tinygo/interp/interpreter.go:560 +0x669c
github.com/tinygo-org/tinygo/interp.(*runner).run(0x14006a6b4a0, 0x140027a2500, {0x1400875ab10, 0x2, 0x14008746930?}, 0x14002a44600, {0x14008629800, 0x14})
        /Users/runner/work/tinygo/tinygo/interp/interpreter.go:560 +0x669c
github.com/tinygo-org/tinygo/interp.(*runner).run(0x14006a6b4a0, 0x14003787180, {0x14008749310, 0x5, 0x10855fbc0?}, 0x140018d4780, {0x14008758c50, 0x10})
        /Users/runner/work/tinygo/tinygo/interp/interpreter.go:560 +0x669c
github.com/tinygo-org/tinygo/interp.(*runner).run(0x14006a6b4a0, 0x14003787130, {0x14007d1ea10, 0x5, 0x10855fbc0?}, 0x140048671a0, {0x14008743090, 0xc})
        /Users/runner/work/tinygo/tinygo/interp/interpreter.go:560 +0x669c
github.com/tinygo-org/tinygo/interp.(*runner).run(0x14006a6b4a0, 0x14003787040, {0x14007d1e810, 0x5, 0x10855fbc0?}, 0x14004df83f0, {0x1400873aa68, 0x8})
        /Users/runner/work/tinygo/tinygo/interp/interpreter.go:560 +0x669c
github.com/tinygo-org/tinygo/interp.(*runner).run(0x14006a6b4a0, 0x1400378b4a0, {0x0, 0x0, 0x140000839a8?}, 0x0, {0x106c550b3, 0x4})
        /Users/runner/work/tinygo/tinygo/interp/interpreter.go:560 +0x669c
github.com/tinygo-org/tinygo/interp.Run({0x14000083dd8?}, 0x1024974f8?, 0xf0?)
        /Users/runner/work/tinygo/tinygo/interp/interp.go:121 +0x558
github.com/tinygo-org/tinygo/builder.optimizeProgram({0x1180a3f60?}, 0x140002a4000)
        /Users/runner/work/tinygo/tinygo/builder/build.go:1180 +0x30
github.com/tinygo-org/tinygo/builder.Build.func5(0x14005e294a0?)
        /Users/runner/work/tinygo/tinygo/builder/build.go:611 +0x504
github.com/tinygo-org/tinygo/builder.runJob(0x14005e29500, 0x14000113b20)
        /Users/runner/work/tinygo/tinygo/builder/jobs.go:212 +0x48
created by github.com/tinygo-org/tinygo/builder.runJobs in goroutine 1
        /Users/runner/work/tinygo/tinygo/builder/jobs.go:113 +0x45c
@aykevl
Copy link
Member

aykevl commented May 21, 2025

I can reproduce this issue, with the following go.mod:

module tinygo/tinygo/tmp/issue4895

go 1.24.0

require (
        github.com/alecthomas/chroma/v2 v2.18.0
        github.com/gomarkdown/markdown v0.0.0-20250311123330-531bef5e742b
)

require github.com/dlclark/regexp2 v1.11.5 // indirect

aykevl added a commit that referenced this issue May 21, 2025
This includes copying from a //go:embed slice for example. The slice
with data is not available at interp time.

See: #4895
@aykevl
Copy link
Member

aykevl commented May 21, 2025

Thank you, here is a fix: #4899

deadprogram pushed a commit that referenced this issue May 22, 2025
This includes copying from a //go:embed slice for example. The slice
with data is not available at interp time.

See: #4895
@deadprogram deadprogram added bug Something isn't working next-release Will be part of next release labels May 22, 2025
@deadprogram
Copy link
Member

Labeled to close this issue after next release.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
bug Something isn't working next-release Will be part of next release
Projects
None yet
Development

No branches or pull requests

3 participants