From: Matt Corallo Date: Thu, 1 Feb 2024 17:31:45 +0000 (+0000) Subject: [C#] Use `Heap{Alloc,Free}` rather than `malloc`/`free` on Windows X-Git-Tag: v0.0.121.1~3 X-Git-Url: http://git.bitcoin.ninja/index.cgi?p=ldk-java;a=commitdiff_plain;h=db75028fd2cef77ce1f3c21451bb7d84227cccf0 [C#] Use `Heap{Alloc,Free}` rather than `malloc`/`free` on Windows We generally expect to allocate pointers in C which Rust will free, and vice versa. On Windows, this requires that we use the same allocation path as Rust's stdlib does, which is actually `Heap{Alloc,Free}`, not `malloc`/`free`. Here, we swap the heap allocation functions we call, fixing heap corruption issues on Windows. --- diff --git a/csharp_strings.py b/csharp_strings.py index b25549a9..1121f934 100644 --- a/csharp_strings.py +++ b/csharp_strings.py @@ -3,7 +3,9 @@ from enum import Enum import sys class Target(Enum): - CSHARP = 1, + WINDOWS = 1, + LINUX = 2, + PTHREAD = 3, def first_to_lower(string: str) -> str: first = string[0] @@ -188,7 +190,25 @@ public class CommonBase { self.c_file_pfx = self.c_file_pfx + "#include \n#define DEBUG_PRINT(...) fprintf(stderr, __VA_ARGS__)\n" - if not DEBUG or sys.platform == "darwin": + if self.target == Target.WINDOWS: + self.c_file_pfx = self.c_file_pfx + """#include +static HANDLE process_heap = NULL; +static inline void* init_heap() { + if (UNLIKELY(process_heap == NULL)) { + // Assume pointer writes wont tear, which is true where we need it. + process_heap = GetProcessHeap(); + } +} +static inline void* MALLOC(size_t a, const char* _) { + init_heap(); + return HeapAlloc(process_heap, HEAP_ZERO_MEMORY, a); +} +#define do_MALLOC(a, b, _c) MALLOC(a, b) +#define FREE(p) if ((uint64_t)(p) > 4096) { init_heap(); HeapFree(process_heap, 0, p); } +#define CHECK_ACCESS(p) +#define CHECK_INNER_FIELD_ACCESS_OR_NULL(v) +""" + elif not DEBUG or self.target != Target.LINUX: self.c_file_pfx = self.c_file_pfx + """#define do_MALLOC(a, _b, _c) malloc(a) #define MALLOC(a, _) malloc(a) #define FREE(p) if ((uint64_t)(p) > 4096) { free(p); } @@ -214,7 +234,7 @@ void __attribute__((constructor)) debug_log_version() { } """ - if sys.platform != "darwin": + if self.target == Target.LINUX: self.c_file_pfx += """ // Running a leak check across all the allocations and frees of the JDK is a mess, // so instead we implement our own naive leak checker here, relying on the -wrap diff --git a/genbindings.py b/genbindings.py index ee5c3783..3c79bb8f 100755 --- a/genbindings.py +++ b/genbindings.py @@ -28,10 +28,17 @@ elif sys.argv[6] == "typescript": target = typescript_strings.Target.NODEJS if len(sys.argv) == 8 and sys.argv[7] == 'browser': target = typescript_strings.Target.BROWSER -elif sys.argv[6] == "c_sharp": +elif sys.argv[6].startswith("c_sharp"): import csharp_strings from csharp_strings import Consts - target = csharp_strings.Target.CSHARP + if sys.argv[6] == "c_sharp-win": + target = csharp_strings.Target.WINDOWS + elif sys.argv[6] == "c_sharp-darwin": + target = csharp_strings.Target.PTHREAD + elif sys.argv[6] == "c_sharp-linux": + target = csharp_strings.Target.LINUX + else: + assert False elif sys.argv[6] == "python": import python_strings from python_strings import Consts diff --git a/genbindings.sh b/genbindings.sh index 4a37eb30..39355d86 100755 --- a/genbindings.sh +++ b/genbindings.sh @@ -106,7 +106,10 @@ if [ "$2" = "c_sharp" ]; then echo "Creating C# bindings..." mkdir -p c_sharp/src/org/ldk/{enums,structs,impl} rm -f c_sharp/src/org/ldk/{enums,structs,impl}/*.cs - ./genbindings.py "./lightning.h" c_sharp/src/org/ldk/impl c_sharp/src/org/ldk c_sharp/ $DEBUG_ARG c_sharp $4 $TARGET_STRING + GEN_PLAT="c_sharp-linux" + [ "$IS_WIN" = "true" ] && GEN_PLAT="c_sharp-win" + [ "$IS_MAC" = "true" ] && GEN_PLAT="c_sharp-darwin" + ./genbindings.py "./lightning.h" c_sharp/src/org/ldk/impl c_sharp/src/org/ldk c_sharp/ $DEBUG_ARG $GEN_PLAT $4 $TARGET_STRING rm -f c_sharp/bindings.c if [ "$3" = "true" ]; then echo "#define LDK_DEBUG_BUILD" > c_sharp/bindings.c @@ -151,6 +154,8 @@ if [ "$2" = "c_sharp" ]; then [ "$IS_MAC" = "true" -a "$IS_APPLE_CLANG" = "false" ] && LINK="$LINK -fuse-ld=lld" [ "$IS_MAC" = "true" -a "$IS_APPLE_CLANG" = "false" ] && echo "WARNING: Need at least upstream clang 13!" [ "$IS_MAC" = "false" -a "$3" != "false" ] && LINK="$LINK -Wl,-wrap,calloc -Wl,-wrap,realloc -Wl,-wrap,malloc -Wl,-wrap,free" + [ "$IS_WIN" = "true" ] && LINK="$LINK --target=x86_64-pc-windows-gnu -lbcrypt -lntdll -static-libgcc" + [ "$IS_WIN" = "true" ] && COMPILE="$COMPILE --target=x86_64-pc-windows-gnu -I/usr/x86_64-w64-mingw32/sys-root/mingw/include/ -I/usr/x86_64-w64-mingw32/include/" if [ "$3" = "true" ]; then $COMPILE $LINK -o libldkcsharp_debug$LDK_TARGET_SUFFIX.so -g -fsanitize=address -shared-libasan -rdynamic -I"$1"/lightning-c-bindings/include/ c_sharp/bindings.c "$1"/lightning-c-bindings/target/$LDK_TARGET/debug/libldk.a -lm @@ -159,8 +164,6 @@ if [ "$2" = "c_sharp" ]; then [ "$IS_APPLE_CLANG" = "false" ] && COMPILE="$COMPILE -flto" [ "$IS_MAC" = "false" ] && LINK="$LINK -Wl,--no-undefined" [ "$IS_WIN" = "false" ] && LINK="$LINK -Wl,--lto-O3" - [ "$IS_WIN" = "true" ] && LINK="$LINK --target=x86_64-pc-windows-gnu -L/usr/lib/gcc/x86_64-w64-mingw32/12-win32/ -lbcrypt -lntdll -static-libgcc" - [ "$IS_WIN" = "true" ] && COMPILE="$COMPILE --target=x86_64-pc-windows-gnu" LDK_LIB="$1"/lightning-c-bindings/target/$LDK_TARGET/release/libldk.a if [ "$IS_MAC" = "false" -a "$IS_WIN" = "false" -a "$4" = "false" ]; then LINK="$LINK -Wl,--version-script=c_sharp/libcode.version -Wl,--build-id=0x0000000000000000"