tsan5.go 927 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. // Copyright 2016 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. package main
  5. // Check that calls to C.malloc/C.free do not collide with the calls
  6. // made by the os/user package.
  7. // #cgo CFLAGS: -fsanitize=thread
  8. // #cgo LDFLAGS: -fsanitize=thread
  9. // #include <stdlib.h>
  10. import "C"
  11. import (
  12. "fmt"
  13. "os"
  14. "os/user"
  15. "runtime"
  16. "sync"
  17. )
  18. func main() {
  19. u, err := user.Current()
  20. if err != nil {
  21. fmt.Fprintln(os.Stderr, err)
  22. // Let the test pass.
  23. os.Exit(0)
  24. }
  25. var wg sync.WaitGroup
  26. for i := 0; i < 20; i++ {
  27. wg.Add(2)
  28. go func() {
  29. defer wg.Done()
  30. for i := 0; i < 1000; i++ {
  31. user.Lookup(u.Username)
  32. runtime.Gosched()
  33. }
  34. }()
  35. go func() {
  36. defer wg.Done()
  37. for i := 0; i < 1000; i++ {
  38. p := C.malloc(C.size_t(len(u.Username) + 1))
  39. runtime.Gosched()
  40. C.free(p)
  41. }
  42. }()
  43. }
  44. wg.Wait()
  45. }