• Ian Lance Taylor's avatar
    runtime: fix 32-bit malloc for pointers >= 0x80000000 · 8da8b376
    Ian Lance Taylor authored
    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
    8da8b376
malloc.goc 22.1 KB