From 26da3735a3810d793056a7b46a12417a3922d20a Mon Sep 17 00:00:00 2001 From: Ebrahim Date: Sun, 22 Nov 2026 14:56:35 +0330 Subject: [PATCH 1/2] windows: add opt-in CRT-free backend Add HIDAPI_NO_CRT to replace CRT allocation, string, memory and formatting helpers with Win32 equivalents while leaving the default CRT build unchanged. Descriptor reconstruction uses the same helpers; stack probes and security cookies are disabled only for this library mode. Add MSVC, clang-cl and MinGW no-CRT CI that links a custom-entry probe without the CRT and checks its DLL imports. Assisted-by: deepseek-harness:deepseek-flash --- .github/workflows/builds.yml | 75 +++++++++++ CMakeLists.txt | 1 + windows/CMakeLists.txt | 19 +++ windows/hid.c | 16 +++ windows/hidapi_descriptor_reconstruct.c | 4 + windows/hidapi_winapi_crt.c | 70 +++++++++++ windows/hidapi_winapi_crt.h | 160 ++++++++++++++++++++++++ 7 files changed, 345 insertions(+) create mode 100644 windows/hidapi_winapi_crt.c create mode 100644 windows/hidapi_winapi_crt.h diff --git a/.github/workflows/builds.yml b/.github/workflows/builds.yml index a7a3d2cec..4e75df94e 100644 --- a/.github/workflows/builds.yml +++ b/.github/workflows/builds.yml @@ -344,6 +344,81 @@ jobs: ninja + windows-cmake-no-crt: + + name: windows no-CRT + runs-on: windows-latest + + steps: + - uses: actions/checkout@v7 + with: + path: hidapisrc + - name: Set up MSVC developer environment + uses: TheMrMilchmann/setup-msvc-dev@v4 + with: + arch: x64 + - name: Install Ninja + shell: cmd + run: choco install ninja -y --no-progress + - name: Build and check all no-CRT compilers + shell: pwsh + run: | + @' + #include + #include + + void __cdecl mainCRTStartup(void) + { + if (hid_init() == 0) + hid_exit(); + ExitProcess(0); + } + '@ | Set-Content -Encoding ascii no_crt_probe.c + + $ok = $true + foreach ($compiler in @('cl', 'clang-cl', 'gcc')) { + Write-Host "`n===== $compiler =====" + $build = "build\no-crt-$compiler" + + cmake -G Ninja -B $build -S hidapisrc ` + "-DCMAKE_C_COMPILER=$compiler" -DCMAKE_BUILD_TYPE=Release ` + -DBUILD_SHARED_LIBS=OFF -DHIDAPI_NO_CRT=ON ` + -DHIDAPI_WITH_TESTS=OFF -DHIDAPI_BUILD_HIDTEST=OFF + if ($LASTEXITCODE -ne 0) { $ok = $false; continue } + + cmake --build $build + if ($LASTEXITCODE -ne 0) { $ok = $false; continue } + + $lib = Get-ChildItem "$build\src\windows" -File | + Where-Object { $_.Name -eq 'hidapi.lib' -or $_.Name -eq 'libhidapi.a' } | + Select-Object -First 1 + if (-not $lib) { + Write-Host "::error title=$compiler::no-CRT static library not found" + $ok = $false + continue + } + + if ($compiler -eq 'gcc') { + gcc -c -DHID_API_NO_EXPORT_DEFINE -Ihidapisrc\hidapi no_crt_probe.c -o no_crt_probe.o + if ($LASTEXITCODE -ne 0) { $ok = $false; continue } + gcc -nostdlib "-Wl,--entry=mainCRTStartup" "-Wl,--subsystem,console" ` + -o no_crt_probe.exe no_crt_probe.o "$($lib.FullName)" -lkernel32 -luser32 -lshlwapi -lgcc + } else { + & $compiler /nologo /c /GS- /Gs9999999 /DHID_API_NO_EXPORT_DEFINE /Ihidapisrc\hidapi no_crt_probe.c + if ($LASTEXITCODE -ne 0) { $ok = $false; continue } + link /nologo /NODEFAULTLIB /ENTRY:mainCRTStartup /SUBSYSTEM:CONSOLE ` + /OUT:no_crt_probe.exe no_crt_probe.obj "$($lib.FullName)" kernel32.lib user32.lib shlwapi.lib + } + if ($LASTEXITCODE -ne 0) { $ok = $false; continue } + + $deps = dumpbin /nologo /dependents no_crt_probe.exe | Out-String + if ($deps -match '(?i)(vcruntime|ucrtbase|msvcrt|api-ms-win-crt|libgcc|libwinpthread)') { + Write-Host "::error title=$compiler::CRT dependency found in the no-CRT probe" + $ok = $false + } + } + if (-not $ok) { exit 1 } + windows-cmake-mingw: runs-on: windows-latest diff --git a/CMakeLists.txt b/CMakeLists.txt index 207e27971..8cd256ae8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -82,6 +82,7 @@ endif() if(WIN32) option(HIDAPI_BUILD_PP_DATA_DUMP "Build small Windows console application pp_data_dump.exe" ${IS_DEBUG_BUILD}) + option(HIDAPI_NO_CRT "Build the Windows backend without the C runtime" OFF) endif() add_subdirectory(src) diff --git a/windows/CMakeLists.txt b/windows/CMakeLists.txt index eca7aa394..4b00d4930 100644 --- a/windows/CMakeLists.txt +++ b/windows/CMakeLists.txt @@ -8,8 +8,13 @@ set(SOURCES hidapi_hidclass.h hidapi_hidpi.h hidapi_hidsdi.h + hidapi_winapi_crt.h ) +if(HIDAPI_NO_CRT) + list(APPEND SOURCES hidapi_winapi_crt.c) +endif() + if(BUILD_SHARED_LIBS) list(APPEND SOURCES hidapi.rc) endif() @@ -32,6 +37,20 @@ target_compile_definitions(hidapi_winapi PRIVATE HIDAPI_CMAKE_V0_BUILD ) +if(HIDAPI_NO_CRT) + target_compile_definitions(hidapi_winapi PRIVATE HIDAPI_NO_CRT) + target_link_libraries(hidapi_winapi PUBLIC shlwapi) + if(MSVC) + # The descriptor reconstructor has a large stack frame. Disable stack + # probes and the security cookie so the static library does not need + # __chkstk or __security_cookie from the CRT. + target_compile_options(hidapi_winapi PRIVATE /GS- /Gs9999999) + elseif(MINGW) + # GCC's automatic stack probe would need ___chkstk_ms from the CRT. + target_compile_options(hidapi_winapi PRIVATE -mno-stack-arg-probe) + endif() +endif() + if(NOT BUILD_SHARED_LIBS) target_compile_definitions(hidapi_winapi # prevent marking functions as __declspec(dllexport) for static library build diff --git a/windows/hid.c b/windows/hid.c index 8456c9b32..f08e3c04b 100644 --- a/windows/hid.c +++ b/windows/hid.c @@ -56,10 +56,17 @@ typedef LONG NTSTATUS; #include "hidapi_hidclass.h" #include "hidapi_hidsdi.h" +#if !defined(HIDAPI_NO_CRT) #include +#endif #include #include +#if defined(HIDAPI_NO_CRT) +#include "hidapi_winapi_crt.h" +#define HIDAPI_SWPRINTF(dest, count, ...) wnsprintfW((dest), (int)(count), __VA_ARGS__) +#define HIDAPI_WCSNCPY(dest, dest_count, src) lstrcpynW((dest), (src), (int)(dest_count)) +#else /* MSVC secure CRT (VS2005+) provides swprintf_s/wcsncpy_s. Older MSVC and GCC/MinGW/Cygwin use the classic variants. */ #if defined(_MSC_VER) && (_MSC_VER >= 1400) @@ -69,6 +76,7 @@ typedef LONG NTSTATUS; #define HIDAPI_SWPRINTF swprintf #define HIDAPI_WCSNCPY(dest, dest_count, src) wcsncpy((dest), (src), (dest_count)) #endif +#endif #ifdef MIN #undef MIN @@ -464,7 +472,11 @@ static void* hid_internal_get_device_interface_property(const wchar_t* interface static void hid_internal_towupper(wchar_t* string) { +#if defined(HIDAPI_NO_CRT) + CharUpperW(string); +#else for (wchar_t* p = string; *p; ++p) *p = towupper(*p); +#endif } static int hid_internal_extract_int_token_value(wchar_t* string, const wchar_t* token) @@ -477,7 +489,11 @@ static int hid_internal_extract_int_token_value(wchar_t* string, const wchar_t* return -1; startptr += wcslen(token); +#if defined(HIDAPI_NO_CRT) + token_value = hid_internal_wcstol_hex(startptr, &endptr); +#else token_value = wcstol(startptr, &endptr, 16); +#endif if (endptr == startptr) return -1; diff --git a/windows/hidapi_descriptor_reconstruct.c b/windows/hidapi_descriptor_reconstruct.c index 4465c3613..3c78a793e 100644 --- a/windows/hidapi_descriptor_reconstruct.c +++ b/windows/hidapi_descriptor_reconstruct.c @@ -18,6 +18,10 @@ ********************************************************/ #include "hidapi_descriptor_reconstruct.h" +#if defined(HIDAPI_NO_CRT) +#include "hidapi_winapi_crt.h" +#endif + /** * @brief References to report descriptor buffer. * diff --git a/windows/hidapi_winapi_crt.c b/windows/hidapi_winapi_crt.c new file mode 100644 index 000000000..64b2826d2 --- /dev/null +++ b/windows/hidapi_winapi_crt.c @@ -0,0 +1,70 @@ +/******************************************************* + HIDAPI - Multi-Platform library for + communication with HID devices. + + libusb/hidapi Team + + Copyright 2026, All Rights Reserved. + + At the discretion of the user of this library, + this software may be licensed under the terms of the + GNU General Public License v3, a BSD-Style license, or the + original HIDAPI license as outlined in the LICENSE.txt, + LICENSE-gpl3.txt, LICENSE-bsd.txt, and LICENSE-orig.txt + files located at the root of the source distribution. + These files may also be found in the public source + code repository located at: + https://github.com/libusb/hidapi . +********************************************************/ + +#include +#include + +#if defined(HIDAPI_NO_CRT) + +#if (defined(_M_IX86) || defined(_M_X64)) && (defined(_MSC_VER) || defined(__clang__)) +#include +#endif +#if defined(_MSC_VER) && !defined(__clang__) +#pragma function(memset, memcpy, memcmp) +#endif + +void *__cdecl memset(void *dest, int value, size_t count) +{ +#if (defined(_M_IX86) || defined(_M_X64)) && (defined(_MSC_VER) || defined(__clang__)) + __stosb((unsigned char *)dest, (unsigned char)value, count); +#else + volatile unsigned char *d = (volatile unsigned char *)dest; + while (count--) + *d++ = (unsigned char)value; +#endif + return dest; +} + +void *__cdecl memcpy(void *dest, const void *src, size_t count) +{ +#if (defined(_M_IX86) || defined(_M_X64)) && (defined(_MSC_VER) || defined(__clang__)) + __movsb((unsigned char *)dest, (const unsigned char *)src, count); +#else + volatile unsigned char *d = (volatile unsigned char *)dest; + const unsigned char *s = (const unsigned char *)src; + while (count--) + *d++ = *s++; +#endif + return dest; +} + +int __cdecl memcmp(const void *lhs, const void *rhs, size_t count) +{ + const volatile unsigned char *a = (const volatile unsigned char *)lhs; + const volatile unsigned char *b = (const volatile unsigned char *)rhs; + while (count--) { + if (*a != *b) + return *a < *b ? -1 : 1; + ++a; + ++b; + } + return 0; +} + +#endif /* HIDAPI_NO_CRT */ diff --git a/windows/hidapi_winapi_crt.h b/windows/hidapi_winapi_crt.h new file mode 100644 index 000000000..ed965b7b2 --- /dev/null +++ b/windows/hidapi_winapi_crt.h @@ -0,0 +1,160 @@ +/******************************************************* + HIDAPI - Multi-Platform library for + communication with HID devices. + + libusb/hidapi Team + + Copyright 2026, All Rights Reserved. + + At the discretion of the user of this library, + this software may be licensed under the terms of the + GNU General Public License v3, a BSD-Style license, or the + original HIDAPI license as outlined in the LICENSE.txt, + LICENSE-gpl3.txt, LICENSE-bsd.txt, and LICENSE-orig.txt + files located at the root of the source distribution. + These files may also be found in the public source + code repository located at: + https://github.com/libusb/hidapi . +********************************************************/ + +#ifndef HIDAPI_WINAPI_CRT_H__ +#define HIDAPI_WINAPI_CRT_H__ + +#if defined(HIDAPI_NO_CRT) + +#include +#include +#include + +#if (defined(_M_IX86) || defined(_M_X64)) && (defined(_MSC_VER) || defined(__clang__)) +#include +#define HIDAPI_WINAPI_HAVE_MOVSB +#endif + +static void *hid_internal_alloc(size_t bytes) +{ + return HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, bytes ? bytes : 1); +} + +static void hid_internal_free(void *ptr) +{ + if (ptr) + HeapFree(GetProcessHeap(), 0, ptr); +} + +static void *hid_internal_calloc(size_t count, size_t size) +{ + if (count != 0 && size > SIZE_MAX / count) + return NULL; + return hid_internal_alloc(count * size); +} + +static void *hid_internal_copy(void *dest, const void *src, size_t bytes) +{ +#ifdef HIDAPI_WINAPI_HAVE_MOVSB + __movsb((unsigned char *)dest, (const unsigned char *)src, bytes); +#else + volatile unsigned char *d = (volatile unsigned char *)dest; + const unsigned char *s = (const unsigned char *)src; + while (bytes--) + *d++ = *s++; +#endif + return dest; +} + +static void *hid_internal_fill(void *dest, int value, size_t bytes) +{ +#ifdef HIDAPI_WINAPI_HAVE_MOVSB + __stosb((unsigned char *)dest, (unsigned char)value, bytes); +#else + volatile unsigned char *d = (volatile unsigned char *)dest; + while (bytes--) + *d++ = (unsigned char)value; +#endif + return dest; +} + +static int hid_internal_compare(const void *lhs, const void *rhs, size_t bytes) +{ + const volatile unsigned char *a = (const volatile unsigned char *)lhs; + const volatile unsigned char *b = (const volatile unsigned char *)rhs; + while (bytes--) { + if (*a != *b) + return *a < *b ? -1 : 1; + ++a; + ++b; + } + return 0; +} + +static void *hid_internal_dup(const void *src, size_t bytes) +{ + void *dest = hid_internal_alloc(bytes); + if (dest) + hid_internal_copy(dest, src, bytes); + return dest; +} + +static char *hid_internal_strdup(const char *src) +{ + return (char *)hid_internal_dup(src, (size_t)lstrlenA(src) + 1); +} + +static wchar_t *hid_internal_wcsdup(const wchar_t *src) +{ + return (wchar_t *)hid_internal_dup(src, ((size_t)lstrlenW(src) + 1) * sizeof(wchar_t)); +} + +static int hid_internal_wcscmp(const wchar_t *lhs, const wchar_t *rhs) +{ + int result = CompareStringOrdinal(lhs, -1, rhs, -1, FALSE); + if (result == CSTR_EQUAL) + return 0; + return result == CSTR_LESS_THAN ? -1 : 1; +} + +static inline long hid_internal_wcstol_hex(const wchar_t *string, wchar_t **endptr) +{ + long value = 0; + const wchar_t *p = string; + + for (;; ++p) { + wchar_t c = *p; + int digit; + if (c >= L'0' && c <= L'9') digit = c - L'0'; + else if (c >= L'A' && c <= L'F') digit = c - L'A' + 10; + else if (c >= L'a' && c <= L'f') digit = c - L'a' + 10; + else break; + value = (value << 4) | digit; + } + *endptr = (wchar_t *)p; + return value; +} + +#undef malloc +#undef calloc +#undef free +#undef memcpy +#undef memset +#undef memcmp +#undef wcslen +#undef wcscmp +#undef wcsstr +#undef _wcsdup +#undef _strdup + +#define malloc(n) hid_internal_alloc(n) +#define calloc(n, size) hid_internal_calloc((n), (size)) +#define free(p) hid_internal_free(p) +#define memcpy(dest, src, bytes) hid_internal_copy((dest), (src), (bytes)) +#define memset(dest, value, bytes) hid_internal_fill((dest), (value), (bytes)) +#define memcmp(lhs, rhs, bytes) hid_internal_compare((lhs), (rhs), (bytes)) +#define wcslen(s) ((size_t)lstrlenW(s)) +#define wcscmp(lhs, rhs) hid_internal_wcscmp((lhs), (rhs)) +#define wcsstr(haystack, needle) StrStrW((haystack), (needle)) +#define _wcsdup(s) hid_internal_wcsdup(s) +#define _strdup(s) hid_internal_strdup(s) + +#endif /* HIDAPI_NO_CRT */ + +#endif /* HIDAPI_WINAPI_CRT_H__ */ From 04754748f2805c57f2a2467fc5fafc3390c604a3 Mon Sep 17 00:00:00 2001 From: Ebrahim Date: Sun, 22 Nov 2026 14:56:36 +0330 Subject: [PATCH 2/2] tests: scope MSVC C4815 suppression to reconstructor test MSVC 14.52, rolled out on the windows-2025-vs2026 image after 2026-11-20, diagnoses the intentional flexible-array base object under /W4 /WX. This is unrelated to the CRT-free backend; scope the suppression to the upstream reconstructor helper so the new compiler does not fail CI. Assisted-by: deepseek-harness:deepseek-flash --- windows/test/hid_report_reconstructor_test.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/windows/test/hid_report_reconstructor_test.c b/windows/test/hid_report_reconstructor_test.c index 3a910bf26..abe15bbf2 100644 --- a/windows/test/hid_report_reconstructor_test.c +++ b/windows/test/hid_report_reconstructor_test.c @@ -35,6 +35,10 @@ static const char* hidapi_strerror_compat(int errnum, char* buf, size_t buf_size #endif } +#if defined(_MSC_VER) +#pragma warning(push) +#pragma warning(disable: 4815) +#endif static hidp_preparsed_data * alloc_preparsed_data_from_file(char* filename) { FILE* file; @@ -481,6 +485,9 @@ static hidp_preparsed_data * alloc_preparsed_data_from_file(char* filename) return pp_data; } +#if defined(_MSC_VER) +#pragma warning(pop) +#endif static BOOLEAN read_hex_data_from_text_file(const char *filename, unsigned char *data_out, size_t data_size, size_t *actual_read) {