[C#] Use `Heap{Alloc,Free}` rather than `malloc`/`free` on Windows
authorMatt Corallo <git@bluematt.me>
Thu, 1 Feb 2024 17:31:45 +0000 (17:31 +0000)
committerMatt Corallo <git@bluematt.me>
Thu, 1 Feb 2024 19:01:06 +0000 (19:01 +0000)
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.

csharp_strings.py
genbindings.py
genbindings.sh

index b25549a9aa80b5862f41f3649b879082e340ee1d..1121f93413fc2975e1a398b9235d6f41d4dbb7f3 100644 (file)
@@ -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 <stdio.h>\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 <heapapi.h>
+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
index ee5c3783670e18da56ca7336e9b94945568c2eb3..3c79bb8fa94789fd245dd4e6f6891442ac0d62d7 100755 (executable)
@@ -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
index 4a37eb3068f9fc6b4f9299fe2a4c38017455a47e..39355d86c43edf1649b250cf5e7f718fa747678a 100755 (executable)
@@ -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"