Source file src/runtime/race/race_unix_test.go

     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  
     7  package race_test
     8  
     9  import (
    10  	"sync/atomic"
    11  	"syscall"
    12  	"testing"
    13  	"unsafe"
    14  )
    15  
    16  // Test that race detector does not crash when accessing non-Go allocated memory (issue 9136).
    17  func TestNonGoMemory(t *testing.T) {
    18  	data, err := syscall.Mmap(-1, 0, 4096, syscall.PROT_READ|syscall.PROT_WRITE, syscall.MAP_ANON|syscall.MAP_PRIVATE)
    19  	if err != nil {
    20  		t.Fatalf("failed to mmap memory: %v", err)
    21  	}
    22  	defer syscall.Munmap(data)
    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  }
    30  

View as plain text