Black Lives Matter. Support the Equal Justice Initiative.

Source file src/runtime/race/race_unix_test.go

Documentation: runtime/race

     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  //go:build race && (darwin || freebsd || linux)
     6  // +build race
     7  // +build darwin freebsd linux
     8  
     9  package race_test
    10  
    11  import (
    12  	"sync/atomic"
    13  	"syscall"
    14  	"testing"
    15  	"unsafe"
    16  )
    17  
    18  // Test that race detector does not crash when accessing non-Go allocated memory (issue 9136).
    19  func TestNonGoMemory(t *testing.T) {
    20  	data, err := syscall.Mmap(-1, 0, 4096, syscall.PROT_READ|syscall.PROT_WRITE, syscall.MAP_ANON|syscall.MAP_PRIVATE)
    21  	if err != nil {
    22  		t.Fatalf("failed to mmap memory: %v", err)
    23  	}
    24  	p := (*uint32)(unsafe.Pointer(&data[0]))
    25  	atomic.AddUint32(p, 1)
    26  	(*p)++
    27  	if *p != 2 {
    28  		t.Fatalf("data[0] = %v, expect 2", *p)
    29  	}
    30  	syscall.Munmap(data)
    31  }
    32  

View as plain text