fauxgl icon indicating copy to clipboard operation
fauxgl copied to clipboard

Data race in rasterizer

Open amlinux opened this issue 2 years ago • 0 comments

Rasterizer is doing an unsafe data access operation that results in a race:

==================
WARNING: DATA RACE
Write at 0x00c0003012f0 by goroutine 8:
  github.com/fogleman/fauxgl.(*Context).rasterize()
      external/com_github_fogleman_fauxgl/context.go:252 +0x109c
  github.com/fogleman/fauxgl.(*Context).drawClippedTriangle()
      external/com_github_fogleman_fauxgl/context.go:347 +0xa49
  github.com/fogleman/fauxgl.(*Context).DrawTriangle()
      external/com_github_fogleman_fauxgl/context.go:387 +0x924
  github.com/fogleman/fauxgl.(*Context).DrawTriangles.func1()
      external/com_github_fogleman_fauxgl/context.go:421 +0xda
  github.com/fogleman/fauxgl.(*Context).DrawTriangles·dwrap·3()
      external/com_github_fogleman_fauxgl/context.go:426 +0x47

Previous read at 0x00c0003012f0 by goroutine 15:
  github.com/fogleman/fauxgl.(*Context).rasterize()
      external/com_github_fogleman_fauxgl/context.go:232 +0x99c
  github.com/fogleman/fauxgl.(*Context).drawClippedTriangle()
      external/com_github_fogleman_fauxgl/context.go:347 +0xa49
  github.com/fogleman/fauxgl.(*Context).DrawTriangle()
      external/com_github_fogleman_fauxgl/context.go:387 +0x924
  github.com/fogleman/fauxgl.(*Context).DrawTriangles.func1()
      external/com_github_fogleman_fauxgl/context.go:421 +0xda
  github.com/fogleman/fauxgl.(*Context).DrawTriangles·dwrap·3()
      external/com_github_fogleman_fauxgl/context.go:426 +0x47

Goroutine 8 (running) created at:
  github.com/fogleman/fauxgl.(*Context).DrawTriangles()
      external/com_github_fogleman_fauxgl/context.go:417 +0x7b
  github.com/fogleman/fauxgl.(*Context).DrawMesh()
      external/com_github_fogleman_fauxgl/context.go:436 +0x54
<skipped>

The code in question even has a comment:

            if dc.ReadDepth && bz > dc.DepthBuffer[i] { // safe w/out lock?
                continue
            }

I think, a sync.RWMutex around accesses to DepthBuffer would fix it.

amlinux avatar Apr 10 '22 06:04 amlinux