From 03d60b7b9bb754978337f9a27f03c23c71e7fd97 Mon Sep 17 00:00:00 2001 From: BryanDClark Date: Tue, 21 Jul 2026 14:20:39 -0700 Subject: [PATCH 1/5] Fix memory leaks in fakelibusb_test.go --- fakelibusb_test.go | 76 +++++++++++++++++++++++----------------------- 1 file changed, 38 insertions(+), 38 deletions(-) diff --git a/fakelibusb_test.go b/fakelibusb_test.go index b9ca735..5396321 100644 --- a/fakelibusb_test.go +++ b/fakelibusb_test.go @@ -85,10 +85,8 @@ func (t *fakeTransfer) setStatus(st TransferStatus) { // that allows the test to explicitly control individual transfer behavior. type fakeLibusb struct { mu sync.Mutex - // devices has a map of devices and their descriptors. - devices map[*libusbDevice]*fakeDevice - // sysDevices keeps the order of devices to be accessd by wrapSysDevice - sysDevices map[uintptr]*libusbDevice + // fakeDevices has a map of devices and their descriptors. + fakeDevices map[*libusbDevice]*fakeDevice // ts has a map of all allocated transfers, indexed by the pointer of // underlying libusbTransfer. ts map[*libusbTransfer]*fakeTransfer @@ -104,32 +102,33 @@ func (f *fakeLibusb) init() (*libusbContext, error) { retu func (f *fakeLibusb) handleEvents(c *libusbContext, done <-chan struct{}) { <-done } func (f *fakeLibusb) getDevices(*libusbContext) ([]*libusbDevice, error) { ret := make([]*libusbDevice, 0, len(fakeDevices)) - for d := range f.devices { + for d := range f.fakeDevices { ret = append(ret, d) } return ret, nil } -func (f *fakeLibusb) wrapSysDevice(ctx *libusbContext, systemDeviceHandle uintptr) (*libusbDevHandle, error) { - dev, ok := f.sysDevices[systemDeviceHandle] - if !ok { - return nil, fmt.Errorf("the passed file descriptor %d does not point to a valid device", systemDeviceHandle) - } - h := newDevHandlePointer() +func (f *fakeLibusb) exit(ctx *libusbContext) error { + close(f.submitted) f.mu.Lock() - defer f.mu.Unlock() - f.handles[h] = dev - return h, nil -} + for d := range f.fakeDevices { + freeDevicePointer(d) + delete(f.fakeDevices, d) + } + for h := range f.handles { + freeDevHandlePointer(h) + delete(f.handles, h) + } + var remaining []*libusbTransfer + for t := range f.ts { + remaining = append(remaining, t) + } + f.mu.Unlock() -func (f *fakeLibusb) getDevice(handle *libusbDevHandle) *libusbDevice { - return f.handles[handle] -} + freeContextPointer(ctx) -func (f *fakeLibusb) exit(*libusbContext) error { - close(f.submitted) - if got := len(f.ts); got > 0 { - for t := range f.ts { + if got := len(remaining); got > 0 { + for _, t := range remaining { f.free(t) } return fmt.Errorf("fakeLibusb has %d remaining transfers that should have been freed", got) @@ -140,7 +139,7 @@ func (f *fakeLibusb) exit(*libusbContext) error { func (f *fakeLibusb) setDebug(*libusbContext, int) {} func (f *fakeLibusb) dereference(d *libusbDevice) {} func (f *fakeLibusb) getDeviceDesc(d *libusbDevice) (*DeviceDesc, error) { - if dev, ok := f.devices[d]; ok { + if dev, ok := f.fakeDevices[d]; ok { return dev.devDesc, nil } return nil, fmt.Errorf("invalid USB device %p", d) @@ -156,7 +155,10 @@ func (f *fakeLibusb) open(d *libusbDevice) (*libusbDevHandle, error) { func (f *fakeLibusb) close(h *libusbDevHandle) { f.mu.Lock() defer f.mu.Unlock() - delete(f.handles, h) + if _, ok := f.handles[h]; ok { + delete(f.handles, h) + freeDevHandlePointer(h) + } } func (f *fakeLibusb) reset(*libusbDevHandle) error { return nil } func (f *fakeLibusb) control(*libusbDevHandle, time.Duration, uint8, uint8, uint16, uint16, []byte) (int, error) { @@ -176,7 +178,7 @@ func (f *fakeLibusb) setConfig(d *libusbDevHandle, cfg uint8) error { return nil } func (f *fakeLibusb) getStringDesc(d *libusbDevHandle, index int) (string, error) { - dev, ok := f.devices[f.handles[d]] + dev, ok := f.fakeDevices[f.handles[d]] if !ok { return "", fmt.Errorf("invalid USB device %p", d) } @@ -219,7 +221,7 @@ func (f *fakeLibusb) setAlt(d *libusbDevHandle, intf, alt uint8) error { if !f.claims[f.handles[d]][intf] { return fmt.Errorf("interface %d must be claimed before alt setup can be set", intf) } - f.devices[f.handles[d]].alt = alt + f.fakeDevices[f.handles[d]].alt = alt return nil } @@ -274,7 +276,10 @@ func (f *fakeLibusb) data(t *libusbTransfer) (int, TransferStatus) { func (f *fakeLibusb) free(t *libusbTransfer) { f.mu.Lock() defer f.mu.Unlock() - delete(f.ts, t) + if _, ok := f.ts[t]; ok { + delete(f.ts, t) + freeFakeTransferPointer(t) + } } func (f *fakeLibusb) setIsoPacketLengths(t *libusbTransfer, length uint32) { f.mu.Lock() @@ -306,12 +311,11 @@ func (f *fakeLibusb) empty() bool { func newFakeLibusb() *fakeLibusb { fl := &fakeLibusb{ - devices: make(map[*libusbDevice]*fakeDevice), - sysDevices: make(map[uintptr]*libusbDevice), - ts: make(map[*libusbTransfer]*fakeTransfer), - submitted: make(chan *fakeTransfer, 10), - handles: make(map[*libusbDevHandle]*libusbDevice), - claims: make(map[*libusbDevice]map[uint8]bool), + fakeDevices: make(map[*libusbDevice]*fakeDevice), + ts: make(map[*libusbTransfer]*fakeTransfer), + submitted: make(chan *fakeTransfer, 10), + handles: make(map[*libusbDevHandle]*libusbDevice), + claims: make(map[*libusbDevice]map[uint8]bool), } for _, d := range fakeDevices { // libusb does not export a way to allocate a new libusb_device struct @@ -320,11 +324,7 @@ func newFakeLibusb() *fakeLibusb { // The contents of these pointers is never accessed. fd := new(fakeDevice) *fd = d - devPointer := newDevicePointer() - fl.devices[devPointer] = fd - if fd.sysDevPtr != 0 { // the sysDevPtr not being set in the fakeDevices list - fl.sysDevices[fd.sysDevPtr] = devPointer - } + fl.fakeDevices[newDevicePointer()] = fd } return fl } From 8d7e5826469b7dcc0de2e7889e5f50d20d331202 Mon Sep 17 00:00:00 2001 From: BryanDClark Date: Tue, 21 Jul 2026 14:22:08 -0700 Subject: [PATCH 2/5] Fix memory leaks in fakelibusb_test.go --- libusb.go | 54 ++++++++++++++++++++++-------------------------------- 1 file changed, 22 insertions(+), 32 deletions(-) diff --git a/libusb.go b/libusb.go index 0997dc8..1d86294 100644 --- a/libusb.go +++ b/libusb.go @@ -24,8 +24,9 @@ import ( ) /* -#cgo pkg-config: libusb-1.0 -#include +// Disabled for blaze #cgo pkg-config: libusb-1.0 +#include +#include "third_party/libusb/libusb1/libusb.h" int gousb_compact_iso_data(struct libusb_transfer *xfer, unsigned char *status); struct libusb_transfer *gousb_alloc_transfer_and_buffer(int bufLen, int numIsoPackets); @@ -143,7 +144,6 @@ type libusbIntf interface { dereference(*libusbDevice) getDeviceDesc(*libusbDevice) (*DeviceDesc, error) open(*libusbDevice) (*libusbDevHandle, error) - wrapSysDevice(*libusbContext, uintptr) (*libusbDevHandle, error) close(*libusbDevHandle) reset(*libusbDevHandle) error @@ -153,7 +153,6 @@ type libusbIntf interface { getStringDesc(*libusbDevHandle, int) (string, error) setAutoDetach(*libusbDevHandle, int) error detachKernelDriver(*libusbDevHandle, uint8) error - getDevice(*libusbDevHandle) *libusbDevice // interface claim(*libusbDevHandle, uint8) error @@ -171,24 +170,13 @@ type libusbIntf interface { } // libusbImpl is an implementation of libusbIntf using real CGo-wrapped libusb. -type libusbImpl struct { - discovery DeviceDiscovery -} +type libusbImpl struct{} -func (impl libusbImpl) init() (*libusbContext, error) { +func (libusbImpl) init() (*libusbContext, error) { var ctx *C.libusb_context - - var libusbOpts [4]C.struct_libusb_init_option // fixed to 4 - there are maximum 4 options - nOpts := 0 - if impl.discovery == DisableDeviceDiscovery { - libusbOpts[nOpts].option = C.LIBUSB_OPTION_NO_DEVICE_DISCOVERY - nOpts++ - } - - if err := fromErrNo(C.libusb_init_context(&ctx, &(libusbOpts[0]), C.int(nOpts))); err != nil { + if err := fromErrNo(C.libusb_init(&ctx)); err != nil { return nil, err } - return (*libusbContext)(ctx), nil } @@ -230,20 +218,6 @@ func (libusbImpl) getDevices(ctx *libusbContext) ([]*libusbDevice, error) { return ret, nil } -func (libusbImpl) wrapSysDevice(ctx *libusbContext, fd uintptr) (*libusbDevHandle, error) { - var handle *C.libusb_device_handle - if ret := C.libusb_wrap_sys_device((*C.libusb_context)(ctx), C.intptr_t(fd), &handle); ret < 0 { - return nil, fromErrNo(C.int(ret)) - } - - return (*libusbDevHandle)(handle), nil -} - -func (libusbImpl) getDevice(d *libusbDevHandle) *libusbDevice { - device := C.libusb_get_device((*C.libusb_device_handle)(d)) - return (*libusbDevice)(device) -} - func (libusbImpl) exit(c *libusbContext) error { C.libusb_exit((*C.libusb_context)(c)) return nil @@ -566,3 +540,19 @@ func newContextPointer() *libusbContext { func newDevHandlePointer() *libusbDevHandle { return (*libusbDevHandle)(unsafe.Pointer(C.malloc(1))) } + +func freeDevicePointer(d *libusbDevice) { + C.free(unsafe.Pointer(d)) +} + +func freeFakeTransferPointer(t *libusbTransfer) { + C.free(unsafe.Pointer(t)) +} + +func freeContextPointer(c *libusbContext) { + C.free(unsafe.Pointer(c)) +} + +func freeDevHandlePointer(h *libusbDevHandle) { + C.free(unsafe.Pointer(h)) +} From f78bc9550c094fc0e94485f123454c7e0083668a Mon Sep 17 00:00:00 2001 From: BryanDClark Date: Tue, 21 Jul 2026 14:24:17 -0700 Subject: [PATCH 3/5] Update libusb.go --- libusb.go | 38 ++++++++++++++++++++++++++++++++------ 1 file changed, 32 insertions(+), 6 deletions(-) diff --git a/libusb.go b/libusb.go index 1d86294..824c6a1 100644 --- a/libusb.go +++ b/libusb.go @@ -24,9 +24,8 @@ import ( ) /* -// Disabled for blaze #cgo pkg-config: libusb-1.0 -#include -#include "third_party/libusb/libusb1/libusb.h" +#cgo pkg-config: libusb-1.0 +#include int gousb_compact_iso_data(struct libusb_transfer *xfer, unsigned char *status); struct libusb_transfer *gousb_alloc_transfer_and_buffer(int bufLen, int numIsoPackets); @@ -144,6 +143,7 @@ type libusbIntf interface { dereference(*libusbDevice) getDeviceDesc(*libusbDevice) (*DeviceDesc, error) open(*libusbDevice) (*libusbDevHandle, error) + wrapSysDevice(*libusbContext, uintptr) (*libusbDevHandle, error) close(*libusbDevHandle) reset(*libusbDevHandle) error @@ -153,6 +153,7 @@ type libusbIntf interface { getStringDesc(*libusbDevHandle, int) (string, error) setAutoDetach(*libusbDevHandle, int) error detachKernelDriver(*libusbDevHandle, uint8) error + getDevice(*libusbDevHandle) *libusbDevice // interface claim(*libusbDevHandle, uint8) error @@ -170,13 +171,24 @@ type libusbIntf interface { } // libusbImpl is an implementation of libusbIntf using real CGo-wrapped libusb. -type libusbImpl struct{} +type libusbImpl struct { + discovery DeviceDiscovery +} -func (libusbImpl) init() (*libusbContext, error) { +func (impl libusbImpl) init() (*libusbContext, error) { var ctx *C.libusb_context - if err := fromErrNo(C.libusb_init(&ctx)); err != nil { + + var libusbOpts [4]C.struct_libusb_init_option // fixed to 4 - there are maximum 4 options + nOpts := 0 + if impl.discovery == DisableDeviceDiscovery { + libusbOpts[nOpts].option = C.LIBUSB_OPTION_NO_DEVICE_DISCOVERY + nOpts++ + } + + if err := fromErrNo(C.libusb_init_context(&ctx, &(libusbOpts[0]), C.int(nOpts))); err != nil { return nil, err } + return (*libusbContext)(ctx), nil } @@ -218,6 +230,20 @@ func (libusbImpl) getDevices(ctx *libusbContext) ([]*libusbDevice, error) { return ret, nil } +func (libusbImpl) wrapSysDevice(ctx *libusbContext, fd uintptr) (*libusbDevHandle, error) { + var handle *C.libusb_device_handle + if ret := C.libusb_wrap_sys_device((*C.libusb_context)(ctx), C.intptr_t(fd), &handle); ret < 0 { + return nil, fromErrNo(C.int(ret)) + } + + return (*libusbDevHandle)(handle), nil +} + +func (libusbImpl) getDevice(d *libusbDevHandle) *libusbDevice { + device := C.libusb_get_device((*C.libusb_device_handle)(d)) + return (*libusbDevice)(device) +} + func (libusbImpl) exit(c *libusbContext) error { C.libusb_exit((*C.libusb_context)(c)) return nil From 2540e38ec54c5ce9ec91d0e5413cdd1ce3baa5ef Mon Sep 17 00:00:00 2001 From: BryanDClark Date: Tue, 21 Jul 2026 16:38:56 -0700 Subject: [PATCH 4/5] Update fakelibusb_test.go --- fakelibusb_test.go | 51 +++++++++++++++++++++++++++++++++------------- 1 file changed, 37 insertions(+), 14 deletions(-) diff --git a/fakelibusb_test.go b/fakelibusb_test.go index 5396321..73c4ddd 100644 --- a/fakelibusb_test.go +++ b/fakelibusb_test.go @@ -85,8 +85,10 @@ func (t *fakeTransfer) setStatus(st TransferStatus) { // that allows the test to explicitly control individual transfer behavior. type fakeLibusb struct { mu sync.Mutex - // fakeDevices has a map of devices and their descriptors. - fakeDevices map[*libusbDevice]*fakeDevice + // devices has a map of devices and their descriptors. + devices map[*libusbDevice]*fakeDevice + // sysDevices keeps the order of devices to be accessd by wrapSysDevice + sysDevices map[uintptr]*libusbDevice // ts has a map of all allocated transfers, indexed by the pointer of // underlying libusbTransfer. ts map[*libusbTransfer]*fakeTransfer @@ -102,18 +104,34 @@ func (f *fakeLibusb) init() (*libusbContext, error) { retu func (f *fakeLibusb) handleEvents(c *libusbContext, done <-chan struct{}) { <-done } func (f *fakeLibusb) getDevices(*libusbContext) ([]*libusbDevice, error) { ret := make([]*libusbDevice, 0, len(fakeDevices)) - for d := range f.fakeDevices { + for d := range f.devices { ret = append(ret, d) } return ret, nil } +func (f *fakeLibusb) wrapSysDevice(ctx *libusbContext, systemDeviceHandle uintptr) (*libusbDevHandle, error) { + dev, ok := f.sysDevices[systemDeviceHandle] + if !ok { + return nil, fmt.Errorf("the passed file descriptor %d does not point to a valid device", systemDeviceHandle) + } + h := newDevHandlePointer() + f.mu.Lock() + defer f.mu.Unlock() + f.handles[h] = dev + return h, nil +} + +func (f *fakeLibusb) getDevice(handle *libusbDevHandle) *libusbDevice { + return f.handles[handle] +} + func (f *fakeLibusb) exit(ctx *libusbContext) error { close(f.submitted) f.mu.Lock() - for d := range f.fakeDevices { + for d := range f.devices { freeDevicePointer(d) - delete(f.fakeDevices, d) + delete(f.devices, d) } for h := range f.handles { freeDevHandlePointer(h) @@ -139,7 +157,7 @@ func (f *fakeLibusb) exit(ctx *libusbContext) error { func (f *fakeLibusb) setDebug(*libusbContext, int) {} func (f *fakeLibusb) dereference(d *libusbDevice) {} func (f *fakeLibusb) getDeviceDesc(d *libusbDevice) (*DeviceDesc, error) { - if dev, ok := f.fakeDevices[d]; ok { + if dev, ok := f.devices[d]; ok { return dev.devDesc, nil } return nil, fmt.Errorf("invalid USB device %p", d) @@ -178,7 +196,7 @@ func (f *fakeLibusb) setConfig(d *libusbDevHandle, cfg uint8) error { return nil } func (f *fakeLibusb) getStringDesc(d *libusbDevHandle, index int) (string, error) { - dev, ok := f.fakeDevices[f.handles[d]] + dev, ok := f.devices[f.handles[d]] if !ok { return "", fmt.Errorf("invalid USB device %p", d) } @@ -221,7 +239,7 @@ func (f *fakeLibusb) setAlt(d *libusbDevHandle, intf, alt uint8) error { if !f.claims[f.handles[d]][intf] { return fmt.Errorf("interface %d must be claimed before alt setup can be set", intf) } - f.fakeDevices[f.handles[d]].alt = alt + f.devices[f.handles[d]].alt = alt return nil } @@ -311,11 +329,12 @@ func (f *fakeLibusb) empty() bool { func newFakeLibusb() *fakeLibusb { fl := &fakeLibusb{ - fakeDevices: make(map[*libusbDevice]*fakeDevice), - ts: make(map[*libusbTransfer]*fakeTransfer), - submitted: make(chan *fakeTransfer, 10), - handles: make(map[*libusbDevHandle]*libusbDevice), - claims: make(map[*libusbDevice]map[uint8]bool), + devices: make(map[*libusbDevice]*fakeDevice), + sysDevices: make(map[uintptr]*libusbDevice), + ts: make(map[*libusbTransfer]*fakeTransfer), + submitted: make(chan *fakeTransfer, 10), + handles: make(map[*libusbDevHandle]*libusbDevice), + claims: make(map[*libusbDevice]map[uint8]bool), } for _, d := range fakeDevices { // libusb does not export a way to allocate a new libusb_device struct @@ -324,7 +343,11 @@ func newFakeLibusb() *fakeLibusb { // The contents of these pointers is never accessed. fd := new(fakeDevice) *fd = d - fl.fakeDevices[newDevicePointer()] = fd + devPointer := newDevicePointer() + fl.devices[devPointer] = fd + if fd.sysDevPtr != 0 { // the sysDevPtr not being set in the fakeDevices list + fl.sysDevices[fd.sysDevPtr] = devPointer + } } return fl } From ac585695a72a7e12e9cf679132cd247f7700396f Mon Sep 17 00:00:00 2001 From: BryanDClark Date: Thu, 29 Oct 2026 22:11:53 +0000 Subject: [PATCH 5/5] Address PR review comments and restore stdlib.h include --- fakelibusb_test.go | 22 ++++++++++++++-------- libusb.go | 8 +++++++- 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/fakelibusb_test.go b/fakelibusb_test.go index 73c4ddd..cc645bb 100644 --- a/fakelibusb_test.go +++ b/fakelibusb_test.go @@ -133,22 +133,28 @@ func (f *fakeLibusb) exit(ctx *libusbContext) error { freeDevicePointer(d) delete(f.devices, d) } + var remainingHandles []*libusbDevHandle for h := range f.handles { - freeDevHandlePointer(h) - delete(f.handles, h) + remainingHandles = append(remainingHandles, h) } - var remaining []*libusbTransfer + var remainingTransfers []*libusbTransfer for t := range f.ts { - remaining = append(remaining, t) + remainingTransfers = append(remainingTransfers, t) } f.mu.Unlock() freeContextPointer(ctx) - if got := len(remaining); got > 0 { - for _, t := range remaining { - f.free(t) - } + for _, h := range remainingHandles { + f.close(h) + } + for _, t := range remainingTransfers { + f.free(t) + } + if got := len(remainingHandles); got > 0 { + return fmt.Errorf("fakeLibusb has %d remaining handles that should have been closed", got) + } + if got := len(remainingTransfers); got > 0 { return fmt.Errorf("fakeLibusb has %d remaining transfers that should have been freed", got) } return nil diff --git a/libusb.go b/libusb.go index 824c6a1..b36eb8b 100644 --- a/libusb.go +++ b/libusb.go @@ -25,6 +25,7 @@ import ( /* #cgo pkg-config: libusb-1.0 +#include #include int gousb_compact_iso_data(struct libusb_transfer *xfer, unsigned char *status); @@ -550,7 +551,10 @@ func libusbSetDebug(c *libusbContext, lvl int) { C.gousb_set_debug((*C.libusb_context)(c), C.int(lvl)) } -// for obtaining unique CGo pointers. +// In tests, the code allocates some C pointers directly to obtain unique CGo +// pointers, and it needs to be able to free them directly too. These helpers +// are defined here because Go does not support cgo (import "C") in *_test.go +// files. func newDevicePointer() *libusbDevice { return (*libusbDevice)(unsafe.Pointer(C.malloc(1))) } @@ -567,6 +571,8 @@ func newDevHandlePointer() *libusbDevHandle { return (*libusbDevHandle)(unsafe.Pointer(C.malloc(1))) } +// In tests, the code allocates some C pointers directly, so it needs to be +// able to free them directly too. func freeDevicePointer(d *libusbDevice) { C.free(unsafe.Pointer(d)) }