Skip to content

Commit 1242f43

Browse files
mknyszekdmitshur
authored andcommitted
[release-branch.go1.17] runtime: set iOS addr space to 40 bits with incremental pagealloc
In iOS <14, the address space is strictly limited to 8 GiB, or 33 bits. As a result, the page allocator also assumes all heap memory lives in this region. This is especially necessary because the page allocator has a PROT_NONE mapping proportional to the size of the usable address space, so this keeps that mapping very small. However starting with iOS 14, this restriction is relaxed, and mmap may start returning addresses outside of the <14 range. Today this means that in iOS 14 and later, users experience an error in the page allocator when a heap arena is mapped outside of the old range. This change increases the ios/arm64 heapAddrBits to 40 while simultaneously making ios/arm64 use the 64-bit pagealloc implementation (with reservations and incremental mapping) to accommodate both iOS versions <14 and 14+. Once iOS <14 is deprecated, we can remove these exceptions and treat ios/arm64 like any other arm64 platform. This change also makes the BaseChunkIdx expression a little bit easier to read, while we're here. For #46860. Fixes #48116. Change-Id: I13865f799777739109585f14f1cc49d6d57e096b Reviewed-on: https://go-review.googlesource.com/c/go/+/344401 Trust: Michael Knyszek <[email protected]> Run-TryBot: Michael Knyszek <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: Cherry Mui <[email protected]> Reviewed-by: Austin Clements <[email protected]> (cherry picked from commit af368da0b137116faba81ca249a8d964297e6e45) Reviewed-on: https://go-review.googlesource.com/c/go/+/369737
1 parent 59f1a26 commit 1242f43

7 files changed

+45
-24
lines changed

src/runtime/export_test.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1066,7 +1066,19 @@ func FreePageAlloc(pp *PageAlloc) {
10661066
//
10671067
// This should not be higher than 0x100*pallocChunkBytes to support
10681068
// mips and mipsle, which only have 31-bit address spaces.
1069-
var BaseChunkIdx = ChunkIdx(chunkIndex(((0xc000*pageAlloc64Bit + 0x100*pageAlloc32Bit) * pallocChunkBytes) + arenaBaseOffset*sys.GoosAix))
1069+
var BaseChunkIdx = func() ChunkIdx {
1070+
var prefix uintptr
1071+
if pageAlloc64Bit != 0 {
1072+
prefix = 0xc000
1073+
} else {
1074+
prefix = 0x100
1075+
}
1076+
baseAddr := prefix * pallocChunkBytes
1077+
if sys.GoosAix != 0 {
1078+
baseAddr += arenaBaseOffset
1079+
}
1080+
return ChunkIdx(chunkIndex(baseAddr))
1081+
}()
10701082

10711083
// PageBase returns an address given a chunk index and a page index
10721084
// relative to that chunk.

src/runtime/malloc.go

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -199,15 +199,21 @@ const (
199199
// we further limit it to 31 bits.
200200
//
201201
// On ios/arm64, although 64-bit pointers are presumably
202-
// available, pointers are truncated to 33 bits. Furthermore,
203-
// only the top 4 GiB of the address space are actually available
204-
// to the application, but we allow the whole 33 bits anyway for
205-
// simplicity.
206-
// TODO(mknyszek): Consider limiting it to 32 bits and using
207-
// arenaBaseOffset to offset into the top 4 GiB.
202+
// available, pointers are truncated to 33 bits in iOS <14.
203+
// Furthermore, only the top 4 GiB of the address space are
204+
// actually available to the application. In iOS >=14, more
205+
// of the address space is available, and the OS can now
206+
// provide addresses outside of those 33 bits. Pick 40 bits
207+
// as a reasonable balance between address space usage by the
208+
// page allocator, and flexibility for what mmap'd regions
209+
// we'll accept for the heap. We can't just move to the full
210+
// 48 bits because this uses too much address space for older
211+
// iOS versions.
212+
// TODO(mknyszek): Once iOS <14 is deprecated, promote ios/arm64
213+
// to a 48-bit address space like every other arm64 platform.
208214
//
209215
// WebAssembly currently has a limit of 4GB linear memory.
210-
heapAddrBits = (_64bit*(1-sys.GoarchWasm)*(1-sys.GoosIos*sys.GoarchArm64))*48 + (1-_64bit+sys.GoarchWasm)*(32-(sys.GoarchMips+sys.GoarchMipsle)) + 33*sys.GoosIos*sys.GoarchArm64
216+
heapAddrBits = (_64bit*(1-sys.GoarchWasm)*(1-sys.GoosIos*sys.GoarchArm64))*48 + (1-_64bit+sys.GoarchWasm)*(32-(sys.GoarchMips+sys.GoarchMipsle)) + 40*sys.GoosIos*sys.GoarchArm64
211217

212218
// maxAlloc is the maximum size of an allocation. On 64-bit,
213219
// it's theoretically possible to allocate 1<<heapAddrBits bytes. On

src/runtime/mgcscavenge_test.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"fmt"
99
"math/rand"
1010
. "runtime"
11+
"runtime/internal/sys"
1112
"testing"
1213
)
1314

@@ -408,7 +409,9 @@ func TestPageAllocScavenge(t *testing.T) {
408409
},
409410
},
410411
}
411-
if PageAlloc64Bit != 0 {
412+
// Disable these tests on iOS since we have a small address space.
413+
// See #46860.
414+
if PageAlloc64Bit != 0 && sys.GoosIos == 0 {
412415
tests["ScavAllVeryDiscontiguous"] = setup{
413416
beforeAlloc: map[ChunkIdx][]BitRange{
414417
BaseChunkIdx: {},

src/runtime/mpagealloc_32bit.go

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,14 @@
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

5-
//go:build 386 || arm || mips || mipsle || wasm || (ios && arm64)
6-
// +build 386 arm mips mipsle wasm ios,arm64
5+
//go:build 386 || arm || mips || mipsle || wasm
6+
// +build 386 arm mips mipsle wasm
77

88
// wasm is a treated as a 32-bit architecture for the purposes of the page
99
// allocator, even though it has 64-bit pointers. This is because any wasm
1010
// pointer always has its top 32 bits as zero, so the effective heap address
1111
// space is only 2^32 bytes in size (see heapAddrBits).
1212

13-
// ios/arm64 is treated as a 32-bit architecture for the purposes of the
14-
// page allocator, even though it has 64-bit pointers and a 33-bit address
15-
// space (see heapAddrBits). The 33 bit address space cannot be rounded up
16-
// to 64 bits because there are too many summary levels to fit in just 33
17-
// bits.
18-
1913
package runtime
2014

2115
import "unsafe"

src/runtime/mpagealloc_64bit.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,8 @@
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

5-
//go:build amd64 || (!ios && arm64) || mips64 || mips64le || ppc64 || ppc64le || riscv64 || s390x
6-
// +build amd64 !ios,arm64 mips64 mips64le ppc64 ppc64le riscv64 s390x
7-
8-
// See mpagealloc_32bit.go for why ios/arm64 is excluded here.
5+
//go:build amd64 || arm64 || mips64 || mips64le || ppc64 || ppc64le || riscv64 || s390x
6+
// +build amd64 arm64 mips64 mips64le ppc64 ppc64le riscv64 s390x
97

108
package runtime
119

src/runtime/mpagealloc_test.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package runtime_test
77
import (
88
"fmt"
99
. "runtime"
10+
"runtime/internal/sys"
1011
"testing"
1112
)
1213

@@ -165,7 +166,9 @@ func TestPageAllocGrow(t *testing.T) {
165166
},
166167
},
167168
}
168-
if PageAlloc64Bit != 0 {
169+
// Disable these tests on iOS since we have a small address space.
170+
// See #46860.
171+
if PageAlloc64Bit != 0 && sys.GoosIos == 0 {
169172
tests["ExtremelyDiscontiguous"] = test{
170173
chunks: []ChunkIdx{
171174
BaseChunkIdx,
@@ -571,7 +574,9 @@ func TestPageAllocAlloc(t *testing.T) {
571574
},
572575
},
573576
}
574-
if PageAlloc64Bit != 0 {
577+
// Disable these tests on iOS since we have a small address space.
578+
// See #46860.
579+
if PageAlloc64Bit != 0 && sys.GoosIos == 0 {
575580
const chunkIdxBigJump = 0x100000 // chunk index offset which translates to O(TiB)
576581

577582
// This test attempts to trigger a bug wherein we look at unmapped summary

src/runtime/mpagecache_test.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package runtime_test
77
import (
88
"math/rand"
99
. "runtime"
10+
"runtime/internal/sys"
1011
"testing"
1112
)
1213

@@ -350,7 +351,9 @@ func TestPageAllocAllocToCache(t *testing.T) {
350351
},
351352
},
352353
}
353-
if PageAlloc64Bit != 0 {
354+
// Disable these tests on iOS since we have a small address space.
355+
// See #46860.
356+
if PageAlloc64Bit != 0 && sys.GoosIos == 0 {
354357
const chunkIdxBigJump = 0x100000 // chunk index offset which translates to O(TiB)
355358

356359
// This test is similar to the one with the same name for

0 commit comments

Comments
 (0)