From 9e4e182615cd80303c564f95020e0c3bd10af64a Mon Sep 17 00:00:00 2001 From: Ayke van Laethem Date: Thu, 15 Sep 2022 16:18:15 +0200 Subject: [PATCH] interp: fix reading from external global This fixes https://github.com/tinygo-org/tinygo/issues/3020. --- interp/interpreter.go | 5 +++++ testdata/embed/embed.go | 3 +++ testdata/embed/out.txt | 1 + 3 files changed, 9 insertions(+) diff --git a/interp/interpreter.go b/interp/interpreter.go index 95c99c84..7cee14bf 100644 --- a/interp/interpreter.go +++ b/interp/interpreter.go @@ -369,6 +369,11 @@ func (r *runner) run(fn *function, params []value, parentMem *memoryView, indent nBytes := uint32(operands[3].Uint()) dstObj := mem.getWritable(dst.index()) dstBuf := dstObj.buffer.asRawValue(r) + if mem.get(src.index()).buffer == nil { + // Looks like the source buffer is not defined. + // This can happen with //extern or //go:embed. + return nil, mem, r.errorAt(inst, errUnsupportedRuntimeInst) + } srcBuf := mem.get(src.index()).buffer.asRawValue(r) copy(dstBuf.buf[dst.offset():dst.offset()+nBytes], srcBuf.buf[src.offset():]) dstObj.buffer = dstBuf diff --git a/testdata/embed/embed.go b/testdata/embed/embed.go index da658718..4b2a052c 100644 --- a/testdata/embed/embed.go +++ b/testdata/embed/embed.go @@ -20,9 +20,12 @@ var ( //go:embed a/b/.hidden var hidden string +var helloStringBytes = []byte(helloString) + func main() { println("string:", strings.TrimSpace(helloString)) println("bytes:", strings.TrimSpace(string(helloBytes))) + println("[]byte(string):", strings.TrimSpace(string(helloStringBytes))) println("files:") readFiles(".") } diff --git a/testdata/embed/out.txt b/testdata/embed/out.txt index 3b216da7..b14b1501 100644 --- a/testdata/embed/out.txt +++ b/testdata/embed/out.txt @@ -1,5 +1,6 @@ string: hello world! bytes: hello world! +[]byte(string): hello world! files: - a - a/b