Skip to content

fix: use signer's own Free() instead of system libc free on Windows#135

Open
lavrric wants to merge 2 commits intomainfrom
devin/1773939337-fix-windows-signer-free
Open

fix: use signer's own Free() instead of system libc free on Windows#135
lavrric wants to merge 2 commits intomainfrom
devin/1773939337-fix-windows-signer-free

Conversation

@lavrric
Copy link
Contributor

@lavrric lavrric commented Mar 19, 2026

Summary

Fixes a Windows crash/heap corruption caused by cross-CRT memory freeing. The previous code used ctypes.util.find_library("c") (via libc.py) to find the system C library's free(), which returns msvcrt.dll on Windows. However, the Go signer DLL is cross-compiled with MinGW and may use a different C runtime (or statically link its own). Calling one CRT's free() on memory allocated by another CRT's malloc() is undefined behavior on Windows.

The fix replaces libc.free() with the signer's own exported Free() function, which is guaranteed to use the same allocator that C.CString() used to allocate the memory.

Requires the companion PR in lighter-go (elliottech/lighter-go#56) which exports the Free function, and the signer binaries in lighter/signers/ must be rebuilt from that updated lighter-go before this change will work.

Review & Testing Checklist for Human

  • The signer binaries (.dll, .so, .dylib) in lighter/signers/ must be rebuilt from the updated lighter-go that exports Free before merging this PR. Without that, __populate_shared_library_functions will fail at the signer.Free line because the symbol won't exist.
  • Verify decode_and_free is never called before get_signer() — the code references the module-level __signer directly, which is None until get_signer() runs. Current call flow looks safe but worth confirming.
  • Test on Windows: install the rebuilt DLL + this Python code, and verify basic operations (e.g., GenerateAPIKey, create_order) work without crashes or heap corruption.
  • lighter/libc.py is now unused dead code — consider removing it in a follow-up.

Notes

  • On Linux/macOS, there's a single system libc, so the old free() happened to work. The new approach is correct on all platforms.
  • The libc.py module is no longer imported anywhere but was not deleted in this PR to keep the diff minimal.

Requested by: @lavrric

On Windows, ctypes.util.find_library('c') returns msvcrt.dll, but the Go
signer DLL (cross-compiled with MinGW) may use a different C runtime or
statically link its own CRT. Calling msvcrt's free() on memory allocated
by a different CRT's malloc() is undefined behavior (crashes, heap
corruption). Fix by using the signer's own exported Free() function which
is guaranteed to use the matching allocator.

Requires the corresponding lighter-go change that exports the Free function.

Co-Authored-By: Mihail  <mlavric64@gmail.com>
@devin-ai-integration
Copy link
Contributor

🤖 Devin AI Engineer

I'll be helping with this pull request! Here's what you should know:

✅ I will automatically:

  • Address comments on this PR that start with 'DevinAI' or '@devin'.
  • Look at CI failures and help fix them

Note: I can only respond to comments from users who have write access to this repository.

⚙️ Control Options:

  • Disable automatic comment and CI monitoring

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant