fix(wgpu): C-allocate device descriptor to allow DeviceLostCallback - #8
Open
dhannyell wants to merge 1 commit into
Open
fix(wgpu): C-allocate device descriptor to allow DeviceLostCallback#8dhannyell wants to merge 1 commit into
dhannyell wants to merge 1 commit into
Conversation
RequestDevice Go-allocated the descriptor with &C.WGPUDeviceDescriptor{} and then embedded the DeviceLostCallback handle (a Go pointer, &pointers[0] from newHandle) in desc.deviceLostCallbackInfo.userdata1. Passing desc (a Go pointer to Go memory now containing a Go pointer) to C violates the cgo pointer rule and panics with 'Go pointer to unpinned Go pointer'.
C-allocate desc with calloc instead, matching the pattern already used for requiredFeatures/requiredLimits/deviceExtras in the same function. The checker does not scan C memory, so the embedded Go pointer is allowed; &pointers[0] points to byte storage with no nested pointers. wgpuAdapterRequestDevice is synchronous, so freeing at function exit is safe. The nil-descriptor path is unchanged.
Verified: RequestDevice with a DeviceLostCallback now succeeds instead of panicking; RequestDevice(nil) still works; three devices created/released in sequence (nil, label+callback, label+RequiredLimits+callback) all succeed; go run -race clean; go build ./wgpu/ and go vet ./wgpu/ pass.
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.
Fixes #7
C-allocates the device descriptor in
RequestDeviceso the cgo pointer checkerdoes not scan it as Go memory, allowing the
DeviceLostCallbackhandle to beembedded without panicking. See #7 for the full root-cause analysis and the
minimal reproduction.
Diff
if descriptor != nil { - desc = &C.WGPUDeviceDescriptor{} + desc = (*C.WGPUDeviceDescriptor)(C.calloc(1, C.size_t(unsafe.Sizeof(C.WGPUDeviceDescriptor{})))) + defer C.free(unsafe.Pointer(desc))Validation
label+callback → label+RequiredLimits+callback) all succeed.
pass.