opaque_arg_conv = ty_info.rust_obj + " " + ty_info.var_name + "_conv;\n"
opaque_arg_conv = opaque_arg_conv + ty_info.var_name + "_conv.inner = (void*)(" + ty_info.var_name + " & (~1));\n"
if ty_info.is_ptr and holds_ref:
- opaque_arg_conv = opaque_arg_conv + ty_info.var_name + "_conv.is_owned = false;"
+ opaque_arg_conv += ty_info.var_name + "_conv.is_owned = false;\n"
else:
- opaque_arg_conv = opaque_arg_conv + ty_info.var_name + "_conv.is_owned = (" + ty_info.var_name + " & 1) || (" + ty_info.var_name + " == 0);"
+ opaque_arg_conv += ty_info.var_name + "_conv.is_owned = (" + ty_info.var_name + " & 1) || (" + ty_info.var_name + " == 0);\n"
+ opaque_arg_conv += "CHECK_INNER_FIELD_ACCESS_OR_NULL(" + ty_info.var_name + "_conv);"
if not is_free and (not ty_info.is_ptr or not holds_ref or ty_info.requires_clone == True) and ty_info.requires_clone != False:
if (ty_info.rust_obj.replace("LDK", "") + "_clone") in self.clone_fns:
# TODO: This is a bit too naive, even with the checks above, we really need to know if rust wants a ref or not, not just if its pass as a ptr.
opaque_ret_conv_suf += indent + "CHECK((((uint64_t)" + ty_info.var_name + "_var.inner) & 1) == 0); // We rely on a free low bit, malloc guarantees this.\n"
opaque_ret_conv_suf += indent + "CHECK((((uint64_t)&" + ty_info.var_name + "_var) & 1) == 0); // We rely on a free low bit, pointer alignment guarantees this.\n"
+ opaque_ret_conv_suf += "CHECK_INNER_FIELD_ACCESS_OR_NULL(" + ty_info.var_name + "_var);\n"
if holds_ref:
opaque_ret_conv_suf += indent + ty_info.var_name + "_ref = (uint64_t)" + ty_info.var_name + "_var.inner & ~1;"
else:
method_comma_separated_arguments = re_match.group(3)
method_arguments = method_comma_separated_arguments.split(',')
+ if method_name.startswith("__"):
+ return
+
is_free = method_name.endswith("_free")
if method_name.startswith("COption") or method_name.startswith("CResult"):
struct_meth = method_name.rsplit("Z", 1)[0][1:] + "Z"
else
./genbindings.py "./lightning.h" src/main/java/org/ldk/impl src/main/java/org/ldk src/main/jni/ $DEBUG_ARG java $4
fi
-echo "#define LDKCVec_C2Tuple_TxidCVec_C2Tuple_u32TxOutZZZZ LDKCVec_TransactionOutputsZ" > src/main/jni/bindings.c
+rm -f src/main/jni/bindings.c
+if [ "$3" = "true" ]; then
+ echo "#define LDK_DEBUG_BUILD" > src/main/jni/bindings.c
+elif [ "$3" = "leaks" ]; then
+ # For leak checking we use release libldk which doesn't expose
+ # __unmangle_inner_ptr, but the C code expects to be able to call it.
+ echo "#define __unmangle_inner_ptr(a) (a)" > src/main/jni/bindings.c
+fi
+echo "#define LDKCVec_C2Tuple_TxidCVec_C2Tuple_u32TxOutZZZZ LDKCVec_TransactionOutputsZ" >> src/main/jni/bindings.c
echo "#define CVec_C2Tuple_TxidCVec_C2Tuple_u32TxOutZZZZ_free CVec_TransactionOutputsZ_free" >> src/main/jni/bindings.c
cat src/main/jni/bindings.c.body >> src/main/jni/bindings.c
javac -h src/main/jni src/main/java/org/ldk/enums/*.java src/main/java/org/ldk/impl/*.java
self.c_file_pfx = self.c_file_pfx + """#define MALLOC(a, _) malloc(a)
#define FREE(p) if ((uint64_t)(p) > 4096) { free(p); }
#define CHECK_ACCESS(p)
+#define CHECK_INNER_FIELD_ACCESS_OR_NULL(v)
"""
if not DEBUG:
self.c_file_pfx += """#define DO_ASSERT(a) (void)(a)
__real_free(ptr);
}
-static void CHECK_ACCESS(void* ptr) {
+static void CHECK_ACCESS(const void* ptr) {
DO_ASSERT(!pthread_mutex_lock(&allocation_mtx));
allocation* it = allocation_ll;
while (it->ptr != ptr) {
}
DO_ASSERT(!pthread_mutex_unlock(&allocation_mtx));
}
+#define CHECK_INNER_FIELD_ACCESS_OR_NULL(v) \\
+ if (v.is_owned && v.inner != NULL) { \\
+ const void *p = __unmangle_inner_ptr(v.inner); \\
+ if (p != NULL) { \\
+ CHECK_ACCESS(p); \\
+ } \\
+ }
void* __real_realloc(void* ptr, size_t newlen);
void* __wrap_realloc(void* ptr, size_t len) {
#define DO_ASSERT(a) (void)(a)
#define CHECK(a)
#define CHECK_ACCESS(p)
+#define CHECK_INNER_FIELD_ACCESS_OR_NULL(v)
"""
else:
self.c_file_pfx = self.c_file_pfx + """
}
}
}
+#define CHECK_INNER_FIELD_ACCESS_OR_NULL(v) \\
+ if (v.is_owned && v.inner != NULL) { \\
+ const void *p = __unmangle_inner_ptr(v.inner); \\
+ if (p != NULL) { \\
+ CHECK_ACCESS(p); \\
+ } \\
+ }
void* __wrap_malloc(size_t len) {
void* res = __real_malloc(len);