• Austin Clements's avatar
    runtime: atomically set span state and use as publication barrier · 7de15e36
    Austin Clements authored
    When everything is working correctly, any pointer the garbage
    collector encounters can only point into a fully initialized heap
    span, since the span must have been initialized before that pointer
    could escape the heap allocator and become visible to the GC.
    
    However, in various cases, we try to be defensive against bad
    pointers. In findObject, this is just a sanity check: we never expect
    to find a bad pointer, but programming errors can lead to them. In
    spanOfHeap, we don't necessarily trust the pointer and we're trying to
    check if it really does point to the heap, though it should always
    point to something. Conservative scanning takes this to a new level,
    since it can only guess that a word may be a pointer and verify this.
    
    In all of these cases, we have a problem that the span lookup and
    check can race with span initialization, since the span becomes
    visible to lookups before it's fully initialized.
    
    Furthermore, we're about to start initializing the span without the
    heap lock held, which is going to introduce races where accesses were
    previously protected by the heap lock.
    
    To address this, this CL makes accesses to mspan.state atomic, and
    ensures that the span is fully initialized before setting the state to
    mSpanInUse. All loads are now atomic, and in any case where we don't
    trust the pointer, it first atomically loads the span state and checks
    that it's mSpanInUse, after which it will have synchronized with span
    initialization and can safely check the other span fields.
    
    For #10958, #24543, but a good fix in general.
    
    Change-Id: I518b7c63555b02064b98aa5f802c92b758fef853
    Reviewed-on: https://go-review.googlesource.com/c/go/+/203286
    Run-TryBot: Austin Clements <austin@google.com>
    TryBot-Result: Gobot Gobot <gobot@golang.org>
    Reviewed-by: default avatarMichael Knyszek <mknyszek@google.com>
    7de15e36
stack.go 38.8 KB