1 from bindingstypes import *
4 def __init__(self, DEBUG):
5 self.c_file_pfx = """#include \"org_ldk_impl_bindings.h\"
6 #include <rust_types.h>
14 self.c_file_pfx = self.c_file_pfx + """#define MALLOC(a, _) malloc(a)
15 #define FREE(p) if ((long)(p) > 1024) { free(p); }
16 #define DO_ASSERT(a) (void)(a)
20 self.c_file_pfx = self.c_file_pfx + """#include <assert.h>
21 // Always run a, then assert it is true:
22 #define DO_ASSERT(a) do { bool _assert_val = (a); assert(_assert_val); } while(0)
23 // Assert a is true or do nothing
24 #define CHECK(a) DO_ASSERT(a)
26 // Running a leak check across all the allocations and frees of the JDK is a mess,
27 // so instead we implement our own naive leak checker here, relying on the -wrap
28 // linker option to wrap malloc/calloc/realloc/free, tracking everyhing allocated
29 // and free'd in Rust or C across the generated bindings shared library.
33 static mtx_t allocation_mtx;
35 void __attribute__((constructor)) init_mtx() {
36 DO_ASSERT(mtx_init(&allocation_mtx, mtx_plain) == thrd_success);
40 typedef struct allocation {
41 struct allocation* next;
43 const char* struct_name;
47 static allocation* allocation_ll = NULL;
49 void* __real_malloc(size_t len);
50 void* __real_calloc(size_t nmemb, size_t len);
51 static void new_allocation(void* res, const char* struct_name) {
52 allocation* new_alloc = __real_malloc(sizeof(allocation));
54 new_alloc->struct_name = struct_name;
55 new_alloc->bt_len = backtrace(new_alloc->bt, BT_MAX);
56 DO_ASSERT(mtx_lock(&allocation_mtx) == thrd_success);
57 new_alloc->next = allocation_ll;
58 allocation_ll = new_alloc;
59 DO_ASSERT(mtx_unlock(&allocation_mtx) == thrd_success);
61 static void* MALLOC(size_t len, const char* struct_name) {
62 void* res = __real_malloc(len);
63 new_allocation(res, struct_name);
66 void __real_free(void* ptr);
67 static void alloc_freed(void* ptr) {
69 DO_ASSERT(mtx_lock(&allocation_mtx) == thrd_success);
70 allocation* it = allocation_ll;
71 while (it->ptr != ptr) {
72 p = it; it = it->next;
74 fprintf(stderr, "Tried to free unknown pointer %p at:\\n", ptr);
76 int bt_len = backtrace(bt, BT_MAX);
77 backtrace_symbols_fd(bt, bt_len, STDERR_FILENO);
78 fprintf(stderr, "\\n\\n");
79 DO_ASSERT(mtx_unlock(&allocation_mtx) == thrd_success);
80 return; // addrsan should catch malloc-unknown and print more info than we have
83 if (p) { p->next = it->next; } else { allocation_ll = it->next; }
84 DO_ASSERT(mtx_unlock(&allocation_mtx) == thrd_success);
85 DO_ASSERT(it->ptr == ptr);
88 static void FREE(void* ptr) {
89 if ((long)ptr < 1024) return; // Rust loves to create pointers to the NULL page for dummys
94 void* __wrap_malloc(size_t len) {
95 void* res = __real_malloc(len);
96 new_allocation(res, "malloc call");
99 void* __wrap_calloc(size_t nmemb, size_t len) {
100 void* res = __real_calloc(nmemb, len);
101 new_allocation(res, "calloc call");
104 void __wrap_free(void* ptr) {
105 if (ptr == NULL) return;
110 void* __real_realloc(void* ptr, size_t newlen);
111 void* __wrap_realloc(void* ptr, size_t len) {
112 if (ptr != NULL) alloc_freed(ptr);
113 void* res = __real_realloc(ptr, len);
114 new_allocation(res, "realloc call");
117 void __wrap_reallocarray(void* ptr, size_t new_sz) {
118 // Rust doesn't seem to use reallocarray currently
122 void __attribute__((destructor)) check_leaks() {
123 for (allocation* a = allocation_ll; a != NULL; a = a->next) {
124 fprintf(stderr, "%s %p remains:\\n", a->struct_name, a->ptr);
125 backtrace_symbols_fd(a->bt, a->bt_len, STDERR_FILENO);
126 fprintf(stderr, "\\n\\n");
128 DO_ASSERT(allocation_ll == NULL);
131 self.c_file_pfx = self.c_file_pfx + """
132 static jmethodID ordinal_meth = NULL;
133 static jmethodID slicedef_meth = NULL;
134 static jclass slicedef_cls = NULL;
135 JNIEXPORT void Java_org_ldk_impl_bindings_init(JNIEnv * env, jclass _b, jclass enum_class, jclass slicedef_class) {
136 ordinal_meth = (*env)->GetMethodID(env, enum_class, "ordinal", "()I");
137 CHECK(ordinal_meth != NULL);
138 slicedef_meth = (*env)->GetMethodID(env, slicedef_class, "<init>", "(JJJ)V");
139 CHECK(slicedef_meth != NULL);
140 slicedef_cls = (*env)->NewGlobalRef(env, slicedef_class);
141 CHECK(slicedef_cls != NULL);
144 JNIEXPORT jboolean JNICALL Java_org_ldk_impl_bindings_deref_1bool (JNIEnv * env, jclass _a, jlong ptr) {
145 return *((bool*)ptr);
147 JNIEXPORT jlong JNICALL Java_org_ldk_impl_bindings_deref_1long (JNIEnv * env, jclass _a, jlong ptr) {
148 return *((long*)ptr);
150 JNIEXPORT void JNICALL Java_org_ldk_impl_bindings_free_1heap_1ptr (JNIEnv * env, jclass _a, jlong ptr) {
153 JNIEXPORT jbyteArray JNICALL Java_org_ldk_impl_bindings_read_1bytes (JNIEnv * env, jclass _b, jlong ptr, jlong len) {
154 jbyteArray ret_arr = (*env)->NewByteArray(env, len);
155 (*env)->SetByteArrayRegion(env, ret_arr, 0, len, (unsigned char*)ptr);
158 JNIEXPORT jbyteArray JNICALL Java_org_ldk_impl_bindings_get_1u8_1slice_1bytes (JNIEnv * env, jclass _b, jlong slice_ptr) {
159 LDKu8slice *slice = (LDKu8slice*)slice_ptr;
160 jbyteArray ret_arr = (*env)->NewByteArray(env, slice->datalen);
161 (*env)->SetByteArrayRegion(env, ret_arr, 0, slice->datalen, slice->data);
164 JNIEXPORT long JNICALL Java_org_ldk_impl_bindings_bytes_1to_1u8_1vec (JNIEnv * env, jclass _b, jbyteArray bytes) {
165 LDKCVec_u8Z *vec = (LDKCVec_u8Z*)MALLOC(sizeof(LDKCVec_u8Z), "LDKCVec_u8");
166 vec->datalen = (*env)->GetArrayLength(env, bytes);
167 vec->data = (uint8_t*)MALLOC(vec->datalen, "LDKCVec_u8Z Bytes");
168 (*env)->GetByteArrayRegion (env, bytes, 0, vec->datalen, vec->data);
171 JNIEXPORT jbyteArray JNICALL Java_org_ldk_impl_bindings_txpointer_1get_1buffer (JNIEnv * env, jclass _b, jlong ptr) {
172 LDKTransaction *txdata = (LDKTransaction*)ptr;
174 slice.data = txdata->data;
175 slice.datalen = txdata->datalen;
176 return Java_org_ldk_impl_bindings_get_1u8_1slice_1bytes(env, _b, (long)&slice);
178 JNIEXPORT long JNICALL Java_org_ldk_impl_bindings_new_1txpointer_1copy_1data (JNIEnv * env, jclass _b, jbyteArray bytes) {
179 LDKTransaction *txdata = (LDKTransaction*)MALLOC(sizeof(LDKTransaction), "LDKTransaction");
180 txdata->datalen = (*env)->GetArrayLength(env, bytes);
181 txdata->data = (uint8_t*)MALLOC(txdata->datalen, "Tx Data Bytes");
182 txdata->data_is_owned = false;
183 (*env)->GetByteArrayRegion (env, bytes, 0, txdata->datalen, txdata->data);
186 JNIEXPORT void JNICALL Java_org_ldk_impl_bindings_txpointer_1free (JNIEnv * env, jclass _b, jlong ptr) {
187 LDKTransaction *tx = (LDKTransaction*)ptr;
188 tx->data_is_owned = true;
189 Transaction_free(*tx);
192 JNIEXPORT jlong JNICALL Java_org_ldk_impl_bindings_vec_1slice_1len (JNIEnv * env, jclass _a, jlong ptr) {
193 // Check offsets of a few Vec types are all consistent as we're meant to be generic across types
194 _Static_assert(offsetof(LDKCVec_u8Z, datalen) == offsetof(LDKCVec_SignatureZ, datalen), "Vec<*> needs to be mapped identically");
195 _Static_assert(offsetof(LDKCVec_u8Z, datalen) == offsetof(LDKCVec_MessageSendEventZ, datalen), "Vec<*> needs to be mapped identically");
196 _Static_assert(offsetof(LDKCVec_u8Z, datalen) == offsetof(LDKCVec_EventZ, datalen), "Vec<*> needs to be mapped identically");
197 _Static_assert(offsetof(LDKCVec_u8Z, datalen) == offsetof(LDKCVec_C2Tuple_usizeTransactionZZ, datalen), "Vec<*> needs to be mapped identically");
198 LDKCVec_u8Z *vec = (LDKCVec_u8Z*)ptr;
199 return (long)vec->datalen;
201 JNIEXPORT long JNICALL Java_org_ldk_impl_bindings_new_1empty_1slice_1vec (JNIEnv * env, jclass _b) {
202 // Check sizes of a few Vec types are all consistent as we're meant to be generic across types
203 _Static_assert(sizeof(LDKCVec_u8Z) == sizeof(LDKCVec_SignatureZ), "Vec<*> needs to be mapped identically");
204 _Static_assert(sizeof(LDKCVec_u8Z) == sizeof(LDKCVec_MessageSendEventZ), "Vec<*> needs to be mapped identically");
205 _Static_assert(sizeof(LDKCVec_u8Z) == sizeof(LDKCVec_EventZ), "Vec<*> needs to be mapped identically");
206 _Static_assert(sizeof(LDKCVec_u8Z) == sizeof(LDKCVec_C2Tuple_usizeTransactionZZ), "Vec<*> needs to be mapped identically");
207 LDKCVec_u8Z *vec = (LDKCVec_u8Z*)MALLOC(sizeof(LDKCVec_u8Z), "Empty LDKCVec");
213 // We assume that CVec_u8Z and u8slice are the same size and layout (and thus pointers to the two can be mixed)
214 _Static_assert(sizeof(LDKCVec_u8Z) == sizeof(LDKu8slice), "Vec<u8> and [u8] need to have been mapped identically");
215 _Static_assert(offsetof(LDKCVec_u8Z, data) == offsetof(LDKu8slice, data), "Vec<u8> and [u8] need to have been mapped identically");
216 _Static_assert(offsetof(LDKCVec_u8Z, datalen) == offsetof(LDKu8slice, datalen), "Vec<u8> and [u8] need to have been mapped identically");
218 _Static_assert(sizeof(jlong) == sizeof(int64_t), "We assume that j-types are the same as C types");
219 _Static_assert(sizeof(jbyte) == sizeof(char), "We assume that j-types are the same as C types");
220 _Static_assert(sizeof(void*) <= 8, "Pointers must fit into 64 bits");
222 typedef jlongArray int64_tArray;
223 typedef jbyteArray int8_tArray;
227 self.hu_struct_file_prefix = """package org.ldk.structs;
229 import org.ldk.impl.bindings;
230 import org.ldk.enums.*;
231 import org.ldk.util.*;
232 import java.util.Arrays;
234 @SuppressWarnings("unchecked") // We correctly assign various generic arrays
236 self.c_fn_ty_pfx = "JNIEXPORT "
237 self.c_fn_name_pfx = "JNICALL Java_org_ldk_impl_bindings_"
238 self.c_fn_args_pfx = "JNIEnv *env, jclass clz"
239 self.file_ext = ".java"
240 self.ptr_c_ty = "int64_t"
241 self.ptr_native_ty = "long"
242 self.result_c_ty = "jclass"
243 self.ptr_arr = "jobjectArray"
244 self.get_native_arr_len_call = ("(*env)->GetArrayLength(env, ", ")")
245 self.get_native_arr_ptr_call = ("(*env)->GetPrimitiveArrayCritical(env, ", ", NULL)")
247 def release_native_arr_ptr_call(self, arr_var, arr_ptr_var):
248 return "(*env)->ReleasePrimitiveArrayCritical(env, " + arr_var + ", " + arr_ptr_var + ", 0)"
249 def create_native_arr_call(self, arr_len, ty_info):
250 if ty_info.c_ty == "int8_tArray":
251 return "(*env)->NewByteArray(env, " + arr_len + ")"
253 return "(*env)->New" + ty_info.java_ty.strip("[]").title() + "Array(env, " + arr_len + ")"
254 def set_native_arr_contents(self, arr_name, arr_len, ty_info):
255 if ty_info.c_ty == "int8_tArray":
256 return ("(*env)->SetByteArrayRegion(env, " + arr_name + ", 0, " + arr_len + ", ", ")")
259 def get_native_arr_contents(self, arr_name, dest_name, arr_len, ty_info, copy):
260 if ty_info.c_ty == "int8_tArray":
262 return "(*env)->GetByteArrayRegion(env, " + arr_name + ", 0, " + arr_len + ", " + dest_name + ")"
264 return "(*env)->GetByteArrayElements (env, " + arr_name + ", NULL)"
265 elif not ty_info.java_ty[:len(ty_info.java_ty) - 2].endswith("[]"):
266 return "(*env)->Get" + ty_info.subty.java_ty.title() + "ArrayElements (env, " + arr_name + ", NULL)"
269 def get_native_arr_elem(self, arr_name, idxc, ty_info):
270 if self.get_native_arr_contents(arr_name, "", "", ty_info, False) is None:
271 return "(*env)->GetObjectArrayElement(env, " + arr_name + ", " + idxc + ")"
273 assert False # Only called if above is None
274 def cleanup_native_arr_ref_contents(self, arr_name, dest_name, arr_len, ty_info):
275 if ty_info.c_ty == "int8_tArray":
276 return "(*env)->ReleaseByteArrayElements(env, " + arr_name + ", (int8_t*)" + dest_name + ", 0);"
278 return "(*env)->Release" + ty_info.java_ty.strip("[]").title() + "ArrayElements(env, " + arr_name + ", " + dest_name + ", 0)"
280 def init_str(self, c_array_class_caches):
282 for ty in c_array_class_caches:
283 res = res + "static jclass " + ty + "_clz = NULL;\n"
284 res = res + "JNIEXPORT void Java_org_ldk_impl_bindings_init_1class_1cache(JNIEnv * env, jclass clz) {\n"
285 for ty in c_array_class_caches:
286 res = res + "\t" + ty + "_clz = (*env)->FindClass(env, \"" + ty.replace("arr_of_", "[") + "\");\n"
287 res = res + "\tCHECK(" + ty + "_clz != NULL);\n"
288 res = res + "\t" + ty + "_clz = (*env)->NewGlobalRef(env, " + ty + "_clz);\n"
292 def native_c_unitary_enum_map(self, struct_name, variants):
293 out_java_enum = "package org.ldk.enums;\n\n"
296 out_c = out_c + "static inline " + struct_name + " " + struct_name + "_from_java(" + self.c_fn_args_pfx + ") {\n"
297 out_c = out_c + "\tswitch ((*env)->CallIntMethod(env, clz, ordinal_meth)) {\n"
299 out_java_enum = out_java_enum + "public enum " + struct_name + " {\n"
302 out_java_enum = out_java_enum + "\t" + var + ",\n"
303 out_c = out_c + "\t\tcase %d: return %s;\n" % (ord_v, var)
305 out_java_enum = out_java_enum + "\t; static native void init();\n"
306 out_java_enum = out_java_enum + "\tstatic { init(); }\n"
307 out_java_enum = out_java_enum + "}"
308 out_java = out_java + "\tstatic { " + struct_name + ".values(); /* Force enum statics to run */ }\n"
309 out_c = out_c + "\t}\n"
310 out_c = out_c + "\tabort();\n"
311 out_c = out_c + "}\n"
313 out_c = out_c + "static jclass " + struct_name + "_class = NULL;\n"
315 out_c = out_c + "static jfieldID " + struct_name + "_" + var + " = NULL;\n"
316 out_c = out_c + self.c_fn_ty_pfx + "void JNICALL Java_org_ldk_enums_" + struct_name.replace("_", "_1") + "_init (" + self.c_fn_args_pfx + ") {\n"
317 out_c = out_c + "\t" + struct_name + "_class = (*env)->NewGlobalRef(env, clz);\n"
318 out_c = out_c + "\tCHECK(" + struct_name + "_class != NULL);\n"
320 out_c = out_c + "\t" + struct_name + "_" + var + " = (*env)->GetStaticFieldID(env, " + struct_name + "_class, \"" + var + "\", \"Lorg/ldk/enums/" + struct_name + ";\");\n"
321 out_c = out_c + "\tCHECK(" + struct_name + "_" + var + " != NULL);\n"
322 out_c = out_c + "}\n"
323 out_c = out_c + "static inline jclass " + struct_name + "_to_java(JNIEnv *env, " + struct_name + " val) {\n"
324 out_c = out_c + "\tswitch (val) {\n"
327 out_c = out_c + "\t\tcase " + var + ":\n"
328 out_c = out_c + "\t\t\treturn (*env)->GetStaticObjectField(env, " + struct_name + "_class, " + struct_name + "_" + var + ");\n"
330 out_c = out_c + "\t\tdefault: abort();\n"
331 out_c = out_c + "\t}\n"
332 out_c = out_c + "}\n\n"
334 return (out_c, out_java_enum, out_java)
336 def c_unitary_enum_to_native_call(self, ty_info):
337 return (ty_info.rust_obj + "_to_java(env, ", ")")
338 def native_unitary_enum_to_c_call(self, ty_info):
339 return (ty_info.rust_obj + "_from_java(env, ", ")")
341 def c_complex_enum_pfx(self, struct_name, variants, init_meth_jty_strs):
344 out_c = out_c + "static jclass " + struct_name + "_" + var + "_class = NULL;\n"
345 out_c = out_c + "static jmethodID " + struct_name + "_" + var + "_meth = NULL;\n"
346 out_c = out_c + self.c_fn_ty_pfx + "void JNICALL Java_org_ldk_impl_bindings_00024" + struct_name.replace("_", "_1") + "_init (" + self.c_fn_args_pfx + ") {\n"
347 for var_name in variants:
348 out_c = out_c + "\t" + struct_name + "_" + var_name + "_class =\n"
349 out_c = out_c + "\t\t(*env)->NewGlobalRef(env, (*env)->FindClass(env, \"Lorg/ldk/impl/bindings$" + struct_name + "$" + var_name + ";\"));\n"
350 out_c = out_c + "\tCHECK(" + struct_name + "_" + var_name + "_class != NULL);\n"
351 out_c = out_c + "\t" + struct_name + "_" + var_name + "_meth = (*env)->GetMethodID(env, " + struct_name + "_" + var_name + "_class, \"<init>\", \"(" + init_meth_jty_strs[var_name] + ")V\");\n"
352 out_c = out_c + "\tCHECK(" + struct_name + "_" + var_name + "_meth != NULL);\n"
353 out_c = out_c + "}\n"
356 def c_complex_enum_pass_ty(self, struct_name):
359 def c_constr_native_complex_enum(self, struct_name, variant, c_params):
360 ret = "(*env)->NewObject(env, " + struct_name + "_" + variant + "_class, " + struct_name + "_" + variant + "_meth"
361 for param in c_params:
362 ret = ret + ", " + param
365 def native_c_map_trait(self, struct_name, field_vars, field_fns):
366 out_c = "typedef struct " + struct_name + "_JCalls {\n"
367 out_c = out_c + "\tatomic_size_t refcnt;\n"
368 out_c = out_c + "\tJavaVM *vm;\n"
369 out_c = out_c + "\tjweak o;\n"
370 for var in field_vars:
371 if isinstance(var, ConvInfo):
372 # We're a regular ol' field
376 out_c = out_c + "\t" + var[0] + "_JCalls* " + var[1] + ";\n"
378 if fn.fn_name != "free" and fn.fn_name != "clone":
379 out_c = out_c + "\tjmethodID " + fn.fn_name + "_meth;\n"
380 out_c = out_c + "} " + struct_name + "_JCalls;\n"
382 for fn_line in field_fns:
383 if fn_line.fn_name == "free":
384 out_c = out_c + "static void " + struct_name + "_JCalls_free(void* this_arg) {\n"
385 out_c = out_c + "\t" + struct_name + "_JCalls *j_calls = (" + struct_name + "_JCalls*) this_arg;\n"
386 out_c = out_c + "\tif (atomic_fetch_sub_explicit(&j_calls->refcnt, 1, memory_order_acquire) == 1) {\n"
387 out_c = out_c + "\t\tJNIEnv *env;\n"
388 out_c = out_c + "\t\tDO_ASSERT((*j_calls->vm)->GetEnv(j_calls->vm, (void**)&env, JNI_VERSION_1_8) == JNI_OK);\n"
389 out_c = out_c + "\t\t(*env)->DeleteWeakGlobalRef(env, j_calls->o);\n"
390 out_c = out_c + "\t\tFREE(j_calls);\n"
391 out_c = out_c + "\t}\n}\n"
393 for fn_line in field_fns:
394 if fn_line.fn_name != "free" and fn_line.fn_name != "clone":
395 assert fn_line.ret_ty_info.ty_info.get_full_rust_ty()[1] == ""
396 out_c = out_c + fn_line.ret_ty_info.ty_info.get_full_rust_ty()[0] + " " + fn_line.fn_name + "_jcall("
397 if fn_line.self_is_const:
398 out_c = out_c + "const void* this_arg"
400 out_c = out_c + "void* this_arg"
402 for idx, arg in enumerate(fn_line.args_ty):
403 out_c = out_c + ", " + arg.ty_info.get_full_rust_ty()[0] + " " + arg.arg_name + arg.ty_info.get_full_rust_ty()[1]
405 out_c = out_c + ") {\n"
406 out_c = out_c + "\t" + struct_name + "_JCalls *j_calls = (" + struct_name + "_JCalls*) this_arg;\n"
407 out_c = out_c + "\tJNIEnv *env;\n"
408 out_c = out_c + "\tDO_ASSERT((*j_calls->vm)->GetEnv(j_calls->vm, (void**)&env, JNI_VERSION_1_8) == JNI_OK);\n"
410 for arg_info in fn_line.args_ty:
411 if arg_info.ret_conv is not None:
412 out_c = out_c + "\t" + arg_info.ret_conv[0].replace('\n', '\n\t')
413 out_c = out_c + arg_info.arg_name
414 out_c = out_c + arg_info.ret_conv[1].replace('\n', '\n\t') + "\n"
416 out_c = out_c + "\tjobject obj = (*env)->NewLocalRef(env, j_calls->o);\n\tCHECK(obj != NULL);\n"
417 if fn_line.ret_ty_info.c_ty.endswith("Array"):
418 out_c = out_c + "\t" + fn_line.ret_ty_info.c_ty + " arg = (*env)->CallObjectMethod(env, obj, j_calls->" + fn_line.fn_name + "_meth"
419 elif not fn_line.ret_ty_info.passed_as_ptr:
420 out_c = out_c + "\treturn (*env)->Call" + fn_line.ret_ty_info.java_ty.title() + "Method(env, obj, j_calls->" + fn_line.fn_name + "_meth"
422 out_c = out_c + "\t" + fn_line.ret_ty_info.rust_obj + "* ret = (" + fn_line.ret_ty_info.rust_obj + "*)(*env)->CallLongMethod(env, obj, j_calls->" + fn_line.fn_name + "_meth"
424 for idx, arg_info in enumerate(fn_line.args_ty):
425 if arg_info.ret_conv is not None:
426 out_c = out_c + ", " + arg_info.ret_conv_name
428 out_c = out_c + ", " + arg_info.arg_name
429 out_c = out_c + ");\n"
430 if fn_line.ret_ty_info.arg_conv is not None:
431 out_c = out_c + "\t" + fn_line.ret_ty_info.arg_conv.replace("\n", "\n\t") + "\n\treturn " + fn_line.ret_ty_info.arg_conv_name + ";\n"
433 out_c = out_c + "}\n"