Repository navigation
Conversation
…pointer to SDL LogSetOutputFunction passed the address of a Go struct holding the callback and its data to SDL_LogSetOutputFunction as userdata. Nothing on the Go side kept that struct alive, so it could be collected while SDL still used it, and cgo panics when the struct contains Go pointers (capturing closures, method values, pointer data). Store the callback and data in the existing package variables, guarded by a mutex, and let logOutputFunction read them from there. SDL now gets NULL as userdata. Fixes veandco#624 Fixes veandco#626
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Changes
LogSetOutputFunctiongave SDL the address of a Go struct (logOutputFunctionCtx) as userdata. That caused two problems:dataholds a Go pointer, the struct contains Go pointers. The default cgocheck then panics at the call withargument of cgo function has Go pointer to unpinned Go pointer(Possible cgo pointer rule violation: LogSetOutputFunction panics for closures and non-constant data under the default cgocheck #626).SDL has only one log output function at a time. So this change stores the callback and data in the existing
logOutputFunctionCache/logOutputDataCachevariables, guarded by async.RWMutex, and passesNULLas userdata.logOutputFunctionreads the current values throughLogGetOutputFunction. The unusedlogOutputFunctionCtxtype is removed.log.cand the public API do not change.The caches are set before
SDL_LogSetOutputFunctionis called, so the first message after registration already sees the new function. The lock is released before the user callback runs, so a callback can callLogSetOutputFunctionorLogGetOutputFunctionitself.Testing
In Docker (
golang:1.27, Debianlibsdl2-dev2.32.4):go build ./sdlsucceeds before and after.go vet ./sdlreports nothing inlog.go. The existing vet findings in other files are unchanged.go test -run 'TestLog' -v ./sdl:TestLogandTestLogSetOutputFunctionpass before and after. The same tests also pass with-raceafter the change.&struct{...}as data, runsruntime.GC()a few times, and then callsLog. Before:panic: runtime error: argument of cgo function has Go pointer to unpinned Go pointerat theLogSetOutputFunctioncall. After: the callback runs and receives the data.Fixes #624
Fixes #626