Skip to content

sha256 panics in reactor module exports in wasm #4847

Open
@mhmd-azeez

Description

@mhmd-azeez

main.go:

package main

import (
	"crypto/sha1"
	"crypto/sha256"
	"encoding/hex"
	"fmt"
)

//export sha256
func Sha256() {
	sha256HelloWorld()
}

//export sha1
func Sha1() {
	sha1HelloWorld()
}

func main() {
	sha1HelloWorld()
	sha256HelloWorld()
}

func sha256HelloWorld() {
	h := sha256.New()
	h.Write([]byte("hello world"))
	hash := hex.EncodeToString(h.Sum([]byte{}))
	fmt.Printf("sha256: %s\n", hash)
}

func sha1HelloWorld() {
	h := sha1.New()
	h.Write([]byte("hello world"))
	hash := hex.EncodeToString(h.Sum([]byte{}))
	fmt.Printf("sha1: %s\n", hash)
}

build.sh:

GOOS=wasip1 GOARCH=wasm tinygo build -buildmode c-shared -o reactor.wasm main.go
GOOS=wasip1 GOARCH=wasm tinygo build -o command.wasm main.go
➜  sha256-repro git:(main) ✗ ./build.sh                            
➜  sha256-repro git:(main) ✗ extism call --wasi command.wasm _start
sha1: 2aae6c35c94fcfb415dbe95f408b9ce91ee846ed
sha256: b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9

➜  sha256-repro git:(main) ✗ extism call --wasi reactor.wasm sha1  
sha1: 2aae6c35c94fcfb415dbe95f408b9ce91ee846ed

➜  sha256-repro git:(main) ✗ extism call --wasi reactor.wasm sha256
panic: runtime error: nil pointer dereference
Error: wasm error: unreachable
wasm stack trace:
	main.runtime.runtimePanicAt(i32,i32)
		0xda08: /usr/local/lib/tinygo/src/runtime/runtime_tinygowasm.go:78:6
	main.runtime.nilPanic()
		0x2ee6: /usr/local/lib/tinygo/src/runtime/panic.go:180:16
	main.crypto/internal/fips140.RecordApproved()
		0x1d0bc: /usr/local/lib/tinygo/src/runtime/scheduler.go:37:24 (inlined)
	main.(*crypto/internal/fips140/sha256.Digest).Sum(i32,i32,i32,i32)
		0x1dcc9: /usr/local/go/src/crypto/internal/fips140/sha256/sha256.go:185:24 (inlined)
	main.interface:{BlockSize:func:{}{basic:int},Reset:func:{}{},Size:func:{}{basic:int},Sum:func:{slice:basic:uint8}{slice:basic:uint8},Write:func:{slice:basic:uint8}{basic:int,named:error}}.Sum$invoke(i32,i32,i32,i32,i32)
		0x210db: /usr/local/go/src/crypto/sha1/sha1.go:155:15 (inlined)
		         /usr/local/go/src/io/fs/fs.go:110:34 (inlined)
		         <Go interface method>:101:33 (inlined)
		         <Go interface method>:103:29 (inlined)
		         /usr/local/lib/tinygo/src/runtime/slice.go:180:33 (inlined)
		         /usr/local/lib/tinygo/src/runtime/slice.go:298:36
	main.sha256()
		0x38228: /home/mo/work/x/wasm/sha256-repro/main.go:28:34 (inlined)
		         /home/mo/work/x/wasm/sha256-repro/main.go:12:18 (inlined)
returned non-zero exit code: 1

TinyGo version:

➜  sha256-repro git:(main) ✗ tinygo version
tinygo version 0.37.0 linux/amd64 (using go version go1.24.2 and LLVM version 19.1.2)

As you can see from the results, Sha1 works in both reactor and command modules, but Sha256 only works in command modules

Full repro:

https://github.com/mhmd-azeez/tinygo-sha256-repro

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingwasmWebAssembly

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions