Go

Golang Test

Posted on 2018-10-14

Golang race test

refer: Introducing the Go Race Detector
refer: ThreadSanitizerAlgorithm

Command

在运行go test的时候,

1
2
3
4
$ go test -race mypkg    // test the package
$ go run -race mysrc.go // compile and run the program
$ go build -race mycmd // build the command
$ go install -race mypkg // install the package

-race的原理在于,watch 是否unsynchronized accesses to shared variables.

具体的算法是:ThreadSanitizerAlgorithm

Thread Sanitizer Algorithm

监视每个memory access, unless it can be proven to be race-free or redundant. 使用的函数是,__tsan_read4(addr).

  • Race-free access

    • Read from constant globals (including vtables)
    • Accesses to memory that does not escape the current function (and therefore the current thread)
  • Redundant accesses

    • Read that happens before a write to the same location.

race的底层实现基于ThreadSanitizerAlgorithm算法,该算法的大体为,随着线程的时钟递增,创建与当前内存访问相对应的新的Shadow Word。然后状态机迭代存储在Shadow State中的所有Shadow Word。如果其中一个老的Shadow Word与新Shadow Word组成race,则data race被检测出,