]> Cypherpunks repositories - gostls13.git/commitdiff
runtime: make sysReserve return page-aligned memory on js-wasm
authorMichael Anthony Knyszek <mknyszek@google.com>
Mon, 4 Nov 2019 20:01:18 +0000 (20:01 +0000)
committerMichael Knyszek <mknyszek@google.com>
Thu, 7 Nov 2019 17:45:27 +0000 (17:45 +0000)
This change ensures js-wasm returns page-aligned memory. While today
its lack of alignment doesn't cause problems, this is an invariant of
sysAlloc which is documented in HACKING.md but isn't upheld by js-wasm.

Any code that calls sysAlloc directly for small structures expects a
certain alignment (e.g. debuglog, tracebufs) but this is not maintained
by js-wasm's sysAlloc.

Where sysReserve comes into play is that sysAlloc is implemented in
terms of sysReserve on js-wasm. Also, the documentation of sysReserve
says that the returned memory is "OS-aligned" which on most platforms
means page-aligned, but the "OS-alignment" on js-wasm is effectively 1,
which doesn't seem right either.

The expected impact of this change is increased memory use on wasm,
since there's no way to decommit memory, and any small structures
allocated with sysAlloc won't be packed quite as tightly. However, any
memory increase should be minimal. Most calls to sysReserve and sysAlloc
already aligned their request to physPageSize before calling it; there
are only a few circumstances where this is not true, and they involve
allocating an amount of memory returned by unsafe.Sizeof where it's
actually quite important that we get the alignment right.

Updates #35112.

Change-Id: I9ca171e507ff3bd186326ccf611b35b9ebea1bfe
Reviewed-on: https://go-review.googlesource.com/c/go/+/205277
Run-TryBot: Michael Knyszek <mknyszek@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Keith Randall <khr@golang.org>
Reviewed-by: Cherry Zhang <cherryyz@google.com>
Reviewed-by: Richard Musiol <neelance@gmail.com>
src/runtime/mem_js.go

index 699a80db267bce39773b4cc64c0bc49883eee88f..092b3d4fa227f601fdc288fcc5af58150604ee37 100644 (file)
@@ -7,7 +7,6 @@
 package runtime
 
 import (
-       "runtime/internal/sys"
        "unsafe"
 )
 
@@ -52,14 +51,18 @@ func sysReserve(v unsafe.Pointer, n uintptr) unsafe.Pointer {
                return nil
        }
 
-       if reserveEnd < lastmoduledatap.end {
-               reserveEnd = lastmoduledatap.end
+       // Round up the initial reserveEnd to 64 KiB so that
+       // reservations are always aligned to the page size.
+       initReserveEnd := alignUp(lastmoduledatap.end, physPageSize)
+       if reserveEnd < initReserveEnd {
+               reserveEnd = initReserveEnd
        }
        v = unsafe.Pointer(reserveEnd)
-       reserveEnd += n
+       reserveEnd += alignUp(n, physPageSize)
 
        current := currentMemory()
-       needed := int32(reserveEnd/sys.DefaultPhysPageSize + 1)
+       // reserveEnd is always at a page boundary.
+       needed := int32(reserveEnd / physPageSize)
        if current < needed {
                if growMemory(needed-current) == -1 {
                        return nil