]> Cypherpunks repositories - gostls13.git/commit
runtime: fix 32-bit malloc for pointers >= 0x80000000
authorIan Lance Taylor <iant@golang.org>
Thu, 9 Jan 2014 23:00:00 +0000 (15:00 -0800)
committerIan Lance Taylor <iant@golang.org>
Thu, 9 Jan 2014 23:00:00 +0000 (15:00 -0800)
commit8da8b37674732ca4532dabcabe7f495b3d6455e9
treef38661abe5508679f70ab8ddabe316e353465913
parent8a089c07ec13c2c9d3f721f2236624f6284b7877
runtime: fix 32-bit malloc for pointers >= 0x80000000

The spans array is allocated in runtime·mallocinit.  On a
32-bit system the number of entries in the spans array is
MaxArena32 / PageSize, which (2U << 30) / (1 << 12) == (1 << 19).
So we are allocating an array that can hold 19 bits for an
index that can hold 20 bits.  According to the comment in the
function, this is intentional: we only allocate enough spans
(and bitmaps) for a 2G arena, because allocating more would
probably be wasteful.

But since the span index is simply the upper 20 bits of the
memory address, this scheme only works if memory addresses are
limited to the low 2G of memory.  That would be OK if we were
careful to enforce it, but we're not.  What we are careful to
enforce, in functions like runtime·MHeap_SysAlloc, is that we
always return addresses between the heap's arena_start and
arena_start + MaxArena32.

We generally get away with it because we start allocating just
after the program end, so we only run into trouble with
programs that allocate a lot of memory, enough to get past
address 0x80000000.

This changes the code that computes a span index to subtract
arena_start on 32-bit systems just as we currently do on
64-bit systems.

R=golang-codereviews, rsc
CC=golang-codereviews
https://golang.org/cl/49460043
src/pkg/runtime/malloc.goc
src/pkg/runtime/mgc0.c
src/pkg/runtime/mheap.c