]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/link: produce valid binaries with large data section on wasm
authorRichard Musiol <mail@richard-musiol.de>
Sat, 5 Oct 2019 22:49:52 +0000 (00:49 +0200)
committerRichard Musiol <neelance@gmail.com>
Mon, 7 Oct 2019 18:09:29 +0000 (18:09 +0000)
CL 170950 had a regression that makes the compiler produce
an invalid wasm binary if the data section is too large.
Loading such a binary gives the following error:
"LinkError: WebAssembly.instantiate(): data segment is out of bounds"

This change fixes the issue by ensuring that the minimum size of the
linear memory is larger than the end of the data section.

Fixes #34395.

Change-Id: I0c8629de7ffd0d85895ad31bf8c9d45fef197a57
Reviewed-on: https://go-review.googlesource.com/c/go/+/199358
Reviewed-by: Cherry Zhang <cherryyz@google.com>
Run-TryBot: Cherry Zhang <cherryyz@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>

src/cmd/link/internal/wasm/asm.go
test/fixedbugs/issue34395.go [new file with mode: 0644]

index ee0a5176acf3b8e0c23aa9e3f45ed4f807e82cc9..bf22c28311dc252105a7a33db17c73d80ad0f687 100644 (file)
@@ -296,10 +296,11 @@ func writeTableSec(ctxt *ld.Link, fns []*wasmFunc) {
 func writeMemorySec(ctxt *ld.Link) {
        sizeOffset := writeSecHeader(ctxt, sectionMemory)
 
-       const (
-               initialSize  = 16 << 20 // 16MB, enough for runtime init without growing
-               wasmPageSize = 64 << 10 // 64KB
-       )
+       dataSection := ctxt.Syms.Lookup("runtime.data", 0).Sect
+       dataEnd := dataSection.Vaddr + dataSection.Length
+       var initialSize = dataEnd + 16<<20 // 16MB, enough for runtime init without growing
+
+       const wasmPageSize = 64 << 10 // 64KB
 
        writeUleb128(ctxt.Out, 1)                        // number of memories
        ctxt.Out.WriteByte(0x00)                         // no maximum memory size
diff --git a/test/fixedbugs/issue34395.go b/test/fixedbugs/issue34395.go
new file mode 100644 (file)
index 0000000..eb5a855
--- /dev/null
@@ -0,0 +1,17 @@
+// run
+
+// Copyright 2019 The Go Authors.  All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+// Test that a binary with a large data section can load. This failed on wasm.
+
+package main
+
+var test = [100 * 1024 * 1024]byte{42}
+
+func main() {
+       if test[0] != 42 {
+               panic("bad")
+       }
+}