Skip to content

Commit db75028

Browse files
committed
[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.
1 parent 13de6f1 commit db75028

File tree

3 files changed

+38
-8
lines changed

3 files changed

+38
-8
lines changed

csharp_strings.py

+23-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
import sys
44

55
class Target(Enum):
6-
CSHARP = 1,
6+
WINDOWS = 1,
7+
LINUX = 2,
8+
PTHREAD = 3,
79

810
def first_to_lower(string: str) -> str:
911
first = string[0]
@@ -188,7 +190,25 @@ def __init__(self, DEBUG: bool, target: Target, outdir: str, **kwargs):
188190

189191
self.c_file_pfx = self.c_file_pfx + "#include <stdio.h>\n#define DEBUG_PRINT(...) fprintf(stderr, __VA_ARGS__)\n"
190192

191-
if not DEBUG or sys.platform == "darwin":
193+
if self.target == Target.WINDOWS:
194+
self.c_file_pfx = self.c_file_pfx + """#include <heapapi.h>
195+
static HANDLE process_heap = NULL;
196+
static inline void* init_heap() {
197+
if (UNLIKELY(process_heap == NULL)) {
198+
// Assume pointer writes wont tear, which is true where we need it.
199+
process_heap = GetProcessHeap();
200+
}
201+
}
202+
static inline void* MALLOC(size_t a, const char* _) {
203+
init_heap();
204+
return HeapAlloc(process_heap, HEAP_ZERO_MEMORY, a);
205+
}
206+
#define do_MALLOC(a, b, _c) MALLOC(a, b)
207+
#define FREE(p) if ((uint64_t)(p) > 4096) { init_heap(); HeapFree(process_heap, 0, p); }
208+
#define CHECK_ACCESS(p)
209+
#define CHECK_INNER_FIELD_ACCESS_OR_NULL(v)
210+
"""
211+
elif not DEBUG or self.target != Target.LINUX:
192212
self.c_file_pfx = self.c_file_pfx + """#define do_MALLOC(a, _b, _c) malloc(a)
193213
#define MALLOC(a, _) malloc(a)
194214
#define FREE(p) if ((uint64_t)(p) > 4096) { free(p); }
@@ -214,7 +234,7 @@ def __init__(self, DEBUG: bool, target: Target, outdir: str, **kwargs):
214234
}
215235
"""
216236

217-
if sys.platform != "darwin":
237+
if self.target == Target.LINUX:
218238
self.c_file_pfx += """
219239
// Running a leak check across all the allocations and frees of the JDK is a mess,
220240
// so instead we implement our own naive leak checker here, relying on the -wrap

genbindings.py

+9-2
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,17 @@
2828
target = typescript_strings.Target.NODEJS
2929
if len(sys.argv) == 8 and sys.argv[7] == 'browser':
3030
target = typescript_strings.Target.BROWSER
31-
elif sys.argv[6] == "c_sharp":
31+
elif sys.argv[6].startswith("c_sharp"):
3232
import csharp_strings
3333
from csharp_strings import Consts
34-
target = csharp_strings.Target.CSHARP
34+
if sys.argv[6] == "c_sharp-win":
35+
target = csharp_strings.Target.WINDOWS
36+
elif sys.argv[6] == "c_sharp-darwin":
37+
target = csharp_strings.Target.PTHREAD
38+
elif sys.argv[6] == "c_sharp-linux":
39+
target = csharp_strings.Target.LINUX
40+
else:
41+
assert False
3542
elif sys.argv[6] == "python":
3643
import python_strings
3744
from python_strings import Consts

genbindings.sh

+6-3
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,10 @@ if [ "$2" = "c_sharp" ]; then
106106
echo "Creating C# bindings..."
107107
mkdir -p c_sharp/src/org/ldk/{enums,structs,impl}
108108
rm -f c_sharp/src/org/ldk/{enums,structs,impl}/*.cs
109-
./genbindings.py "./lightning.h" c_sharp/src/org/ldk/impl c_sharp/src/org/ldk c_sharp/ $DEBUG_ARG c_sharp $4 $TARGET_STRING
109+
GEN_PLAT="c_sharp-linux"
110+
[ "$IS_WIN" = "true" ] && GEN_PLAT="c_sharp-win"
111+
[ "$IS_MAC" = "true" ] && GEN_PLAT="c_sharp-darwin"
112+
./genbindings.py "./lightning.h" c_sharp/src/org/ldk/impl c_sharp/src/org/ldk c_sharp/ $DEBUG_ARG $GEN_PLAT $4 $TARGET_STRING
110113
rm -f c_sharp/bindings.c
111114
if [ "$3" = "true" ]; then
112115
echo "#define LDK_DEBUG_BUILD" > c_sharp/bindings.c
@@ -151,6 +154,8 @@ if [ "$2" = "c_sharp" ]; then
151154
[ "$IS_MAC" = "true" -a "$IS_APPLE_CLANG" = "false" ] && LINK="$LINK -fuse-ld=lld"
152155
[ "$IS_MAC" = "true" -a "$IS_APPLE_CLANG" = "false" ] && echo "WARNING: Need at least upstream clang 13!"
153156
[ "$IS_MAC" = "false" -a "$3" != "false" ] && LINK="$LINK -Wl,-wrap,calloc -Wl,-wrap,realloc -Wl,-wrap,malloc -Wl,-wrap,free"
157+
[ "$IS_WIN" = "true" ] && LINK="$LINK --target=x86_64-pc-windows-gnu -lbcrypt -lntdll -static-libgcc"
158+
[ "$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/"
154159

155160
if [ "$3" = "true" ]; then
156161
$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
159164
[ "$IS_APPLE_CLANG" = "false" ] && COMPILE="$COMPILE -flto"
160165
[ "$IS_MAC" = "false" ] && LINK="$LINK -Wl,--no-undefined"
161166
[ "$IS_WIN" = "false" ] && LINK="$LINK -Wl,--lto-O3"
162-
[ "$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"
163-
[ "$IS_WIN" = "true" ] && COMPILE="$COMPILE --target=x86_64-pc-windows-gnu"
164167
LDK_LIB="$1"/lightning-c-bindings/target/$LDK_TARGET/release/libldk.a
165168
if [ "$IS_MAC" = "false" -a "$IS_WIN" = "false" -a "$4" = "false" ]; then
166169
LINK="$LINK -Wl,--version-script=c_sharp/libcode.version -Wl,--build-id=0x0000000000000000"

0 commit comments

Comments
 (0)