bitmap_race.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. // Copyright 2024 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. //go:build race
  5. package impl
  6. // When running under race detector, we add a presence map of bytes, that we can access
  7. // in the hook functions so that we trigger the race detection whenever we have concurrent
  8. // Read-Writes or Write-Writes. The race detector does not otherwise detect invalid concurrent
  9. // access to lazy fields as all updates of bitmaps and pointers are done using atomic operations.
  10. type RaceDetectHookData struct {
  11. shadowPresence *[]byte
  12. }
  13. // Hooks for presence bitmap operations that allocate, read and write the shadowPresence
  14. // using non-atomic operations.
  15. func (data *RaceDetectHookData) raceDetectHookAlloc(size presenceSize) {
  16. sp := make([]byte, size)
  17. atomicStoreShadowPresence(&data.shadowPresence, &sp)
  18. }
  19. func (p presence) raceDetectHookPresent(num uint32) {
  20. data := p.toRaceDetectData()
  21. if data == nil {
  22. return
  23. }
  24. sp := atomicLoadShadowPresence(&data.shadowPresence)
  25. if sp != nil {
  26. _ = (*sp)[num]
  27. }
  28. }
  29. func (p presence) raceDetectHookSetPresent(num uint32, size presenceSize) {
  30. data := p.toRaceDetectData()
  31. if data == nil {
  32. return
  33. }
  34. sp := atomicLoadShadowPresence(&data.shadowPresence)
  35. if sp == nil {
  36. data.raceDetectHookAlloc(size)
  37. sp = atomicLoadShadowPresence(&data.shadowPresence)
  38. }
  39. (*sp)[num] = 1
  40. }
  41. func (p presence) raceDetectHookClearPresent(num uint32) {
  42. data := p.toRaceDetectData()
  43. if data == nil {
  44. return
  45. }
  46. sp := atomicLoadShadowPresence(&data.shadowPresence)
  47. if sp != nil {
  48. (*sp)[num] = 0
  49. }
  50. }
  51. // raceDetectHookAllocAndCopy allocates a new shadowPresence slice at lazy and copies
  52. // shadowPresence bytes from src to lazy.
  53. func (p presence) raceDetectHookAllocAndCopy(q presence) {
  54. sData := q.toRaceDetectData()
  55. dData := p.toRaceDetectData()
  56. if sData == nil {
  57. return
  58. }
  59. srcSp := atomicLoadShadowPresence(&sData.shadowPresence)
  60. if srcSp == nil {
  61. atomicStoreShadowPresence(&dData.shadowPresence, nil)
  62. return
  63. }
  64. n := len(*srcSp)
  65. dSlice := make([]byte, n)
  66. atomicStoreShadowPresence(&dData.shadowPresence, &dSlice)
  67. for i := 0; i < n; i++ {
  68. dSlice[i] = (*srcSp)[i]
  69. }
  70. }
  71. // raceDetectHookPresent is called by the generated file interface
  72. // (*proto.internalFuncs) Present to optionally read an unprotected
  73. // shadow bitmap when race detection is enabled. In regular code it is
  74. // a noop.
  75. func raceDetectHookPresent(field *uint32, num uint32) {
  76. data := findPointerToRaceDetectData(field, num)
  77. if data == nil {
  78. return
  79. }
  80. sp := atomicLoadShadowPresence(&data.shadowPresence)
  81. if sp != nil {
  82. _ = (*sp)[num]
  83. }
  84. }
  85. // raceDetectHookSetPresent is called by the generated file interface
  86. // (*proto.internalFuncs) SetPresent to optionally write an unprotected
  87. // shadow bitmap when race detection is enabled. In regular code it is
  88. // a noop.
  89. func raceDetectHookSetPresent(field *uint32, num uint32, size presenceSize) {
  90. data := findPointerToRaceDetectData(field, num)
  91. if data == nil {
  92. return
  93. }
  94. sp := atomicLoadShadowPresence(&data.shadowPresence)
  95. if sp == nil {
  96. data.raceDetectHookAlloc(size)
  97. sp = atomicLoadShadowPresence(&data.shadowPresence)
  98. }
  99. (*sp)[num] = 1
  100. }
  101. // raceDetectHookClearPresent is called by the generated file interface
  102. // (*proto.internalFuncs) ClearPresent to optionally write an unprotected
  103. // shadow bitmap when race detection is enabled. In regular code it is
  104. // a noop.
  105. func raceDetectHookClearPresent(field *uint32, num uint32) {
  106. data := findPointerToRaceDetectData(field, num)
  107. if data == nil {
  108. return
  109. }
  110. sp := atomicLoadShadowPresence(&data.shadowPresence)
  111. if sp != nil {
  112. (*sp)[num] = 0
  113. }
  114. }