Commit 763d3ac7 authored by Michael Anthony Knyszek's avatar Michael Anthony Knyszek Committed by Michael Knyszek

runtime: make sysReserve return page-aligned memory on js-wasm

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: default avatarKeith Randall <khr@golang.org>
Reviewed-by: default avatarCherry Zhang <cherryyz@google.com>
Reviewed-by: default avatarRichard Musiol <neelance@gmail.com>
parent cec01395
...@@ -7,7 +7,6 @@ ...@@ -7,7 +7,6 @@
package runtime package runtime
import ( import (
"runtime/internal/sys"
"unsafe" "unsafe"
) )
...@@ -52,14 +51,18 @@ func sysReserve(v unsafe.Pointer, n uintptr) unsafe.Pointer { ...@@ -52,14 +51,18 @@ func sysReserve(v unsafe.Pointer, n uintptr) unsafe.Pointer {
return nil return nil
} }
if reserveEnd < lastmoduledatap.end { // Round up the initial reserveEnd to 64 KiB so that
reserveEnd = lastmoduledatap.end // reservations are always aligned to the page size.
initReserveEnd := alignUp(lastmoduledatap.end, physPageSize)
if reserveEnd < initReserveEnd {
reserveEnd = initReserveEnd
} }
v = unsafe.Pointer(reserveEnd) v = unsafe.Pointer(reserveEnd)
reserveEnd += n reserveEnd += alignUp(n, physPageSize)
current := currentMemory() current := currentMemory()
needed := int32(reserveEnd/sys.DefaultPhysPageSize + 1) // reserveEnd is always at a page boundary.
needed := int32(reserveEnd / physPageSize)
if current < needed { if current < needed {
if growMemory(needed-current) == -1 { if growMemory(needed-current) == -1 {
return nil return nil
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment