github.com/likebike/go--@v0.0.0-20190911215757-0bd925d16e96/go/src/runtime/race/race_unix_test.go (about)

     1  // Copyright 2014 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // +build race
     6  // +build darwin freebsd linux
     7  
     8  package race_test
     9  
    10  import (
    11  	"sync/atomic"
    12  	"syscall"
    13  	"testing"
    14  	"unsafe"
    15  )
    16  
    17  // Test that race detector does not crash when accessing non-Go allocated memory (issue 9136).
    18  func TestNonGoMemory(t *testing.T) {
    19  	data, err := syscall.Mmap(-1, 0, 4096, syscall.PROT_READ|syscall.PROT_WRITE, syscall.MAP_ANON|syscall.MAP_PRIVATE)
    20  	if err != nil {
    21  		t.Fatalf("failed to mmap memory: %v", err)
    22  	}
    23  	p := (*uint32)(unsafe.Pointer(&data[0]))
    24  	atomic.AddUint32(p, 1)
    25  	(*p)++
    26  	if *p != 2 {
    27  		t.Fatalf("data[0] = %v, expect 2", *p)
    28  	}
    29  	syscall.Munmap(data)
    30  }