From: Matt Corallo Date: Mon, 1 Feb 2021 21:56:18 +0000 (-0500) Subject: Bindings updates for latest upstream, check in .so X-Git-Tag: v0.0.1~8 X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=commitdiff_plain;h=173b29468956dcaa3424b0319227d492d3188052;p=ldk-java Bindings updates for latest upstream, check in .so --- diff --git a/liblightningjni.so b/liblightningjni.so new file mode 100755 index 00000000..a1c44c61 Binary files /dev/null and b/liblightningjni.so differ diff --git a/src/main/java/org/ldk/impl/bindings.java b/src/main/java/org/ldk/impl/bindings.java index 52da72db..6f9bd8d5 100644 --- a/src/main/java/org/ldk/impl/bindings.java +++ b/src/main/java/org/ldk/impl/bindings.java @@ -2409,6 +2409,8 @@ public class bindings { public static native void PeerManager_process_events(long this_arg); // void PeerManager_socket_disconnected(const struct LDKPeerManager *NONNULL_PTR this_arg, const struct LDKSocketDescriptor *NONNULL_PTR descriptor); public static native void PeerManager_socket_disconnected(long this_arg, long descriptor); + // void PeerManager_disconnect_by_node_id(const struct LDKPeerManager *NONNULL_PTR this_arg, struct LDKPublicKey node_id, bool no_connection_possible); + public static native void PeerManager_disconnect_by_node_id(long this_arg, byte[] node_id, boolean no_connection_possible); // void PeerManager_timer_tick_occured(const struct LDKPeerManager *NONNULL_PTR this_arg); public static native void PeerManager_timer_tick_occured(long this_arg); // struct LDKThirtyTwoBytes build_commitment_secret(const uint8_t (*commitment_seed)[32], uint64_t idx); diff --git a/src/main/java/org/ldk/structs/PeerManager.java b/src/main/java/org/ldk/structs/PeerManager.java index 1daf97ae..3c8a441a 100644 --- a/src/main/java/org/ldk/structs/PeerManager.java +++ b/src/main/java/org/ldk/structs/PeerManager.java @@ -66,6 +66,10 @@ public class PeerManager extends CommonBase { this.ptrs_to.add(descriptor); } + public void disconnect_by_node_id(byte[] node_id, boolean no_connection_possible) { + bindings.PeerManager_disconnect_by_node_id(this.ptr, node_id, no_connection_possible); + } + public void timer_tick_occured() { bindings.PeerManager_timer_tick_occured(this.ptr); } diff --git a/src/main/jni/bindings.c b/src/main/jni/bindings.c index ad260dc2..3aa76d3c 100644 --- a/src/main/jni/bindings.c +++ b/src/main/jni/bindings.c @@ -5,123 +5,10 @@ #include #include -#include -// Always run a, then assert it is true: -#define DO_ASSERT(a) do { bool _assert_val = (a); assert(_assert_val); } while(0) -// Assert a is true or do nothing -#define CHECK(a) DO_ASSERT(a) - -// 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 -// linker option to wrap malloc/calloc/realloc/free, tracking everyhing allocated -// and free'd in Rust or C across the generated bindings shared library. -#include -#include -#include -static mtx_t allocation_mtx; - -void __attribute__((constructor)) init_mtx() { - DO_ASSERT(mtx_init(&allocation_mtx, mtx_plain) == thrd_success); -} - -#define BT_MAX 128 -typedef struct allocation { - struct allocation* next; - void* ptr; - const char* struct_name; - void* bt[BT_MAX]; - int bt_len; - size_t alloc_len; -} allocation; -static allocation* allocation_ll = NULL; - -void* __real_malloc(size_t len); -void* __real_calloc(size_t nmemb, size_t len); -static void new_allocation(void* res, const char* struct_name, size_t len) { - allocation* new_alloc = __real_malloc(sizeof(allocation)); - new_alloc->ptr = res; - new_alloc->struct_name = struct_name; - new_alloc->bt_len = backtrace(new_alloc->bt, BT_MAX); - new_alloc->alloc_len = len; - DO_ASSERT(mtx_lock(&allocation_mtx) == thrd_success); - new_alloc->next = allocation_ll; - allocation_ll = new_alloc; - DO_ASSERT(mtx_unlock(&allocation_mtx) == thrd_success); -} -static void* MALLOC(size_t len, const char* struct_name) { - void* res = __real_malloc(len); - new_allocation(res, struct_name, len); - return res; -} -void __real_free(void* ptr); -static void alloc_freed(void* ptr) { - allocation* p = NULL; - DO_ASSERT(mtx_lock(&allocation_mtx) == thrd_success); - allocation* it = allocation_ll; - while (it->ptr != ptr) { - p = it; it = it->next; - if (it == NULL) { - fprintf(stderr, "Tried to free unknown pointer %p at:\n", ptr); - void* bt[BT_MAX]; - int bt_len = backtrace(bt, BT_MAX); - backtrace_symbols_fd(bt, bt_len, STDERR_FILENO); - fprintf(stderr, "\n\n"); - DO_ASSERT(mtx_unlock(&allocation_mtx) == thrd_success); - return; // addrsan should catch malloc-unknown and print more info than we have - } - } - if (p) { p->next = it->next; } else { allocation_ll = it->next; } - DO_ASSERT(mtx_unlock(&allocation_mtx) == thrd_success); - DO_ASSERT(it->ptr == ptr); - __real_free(it); -} -static void FREE(void* ptr) { - if ((long)ptr < 1024) return; // Rust loves to create pointers to the NULL page for dummys - alloc_freed(ptr); - __real_free(ptr); -} - -void* __wrap_malloc(size_t len) { - void* res = __real_malloc(len); - new_allocation(res, "malloc call", len); - return res; -} -void* __wrap_calloc(size_t nmemb, size_t len) { - void* res = __real_calloc(nmemb, len); - new_allocation(res, "calloc call", len); - return res; -} -void __wrap_free(void* ptr) { - if (ptr == NULL) return; - alloc_freed(ptr); - __real_free(ptr); -} - -void* __real_realloc(void* ptr, size_t newlen); -void* __wrap_realloc(void* ptr, size_t len) { - if (ptr != NULL) alloc_freed(ptr); - void* res = __real_realloc(ptr, len); - new_allocation(res, "realloc call", len); - return res; -} -void __wrap_reallocarray(void* ptr, size_t new_sz) { - // Rust doesn't seem to use reallocarray currently - DO_ASSERT(false); -} - -void __attribute__((destructor)) check_leaks() { - size_t alloc_count = 0; - size_t alloc_size = 0; - for (allocation* a = allocation_ll; a != NULL; a = a->next) { - fprintf(stderr, "%s %p remains:\n", a->struct_name, a->ptr); - backtrace_symbols_fd(a->bt, a->bt_len, STDERR_FILENO); - fprintf(stderr, "\n\n"); - alloc_count++; - alloc_size += a->alloc_len; - } - fprintf(stderr, "%lu allocations remained for %lu bytes.\n", alloc_count, alloc_size); - DO_ASSERT(allocation_ll == NULL); -} +#define MALLOC(a, _) malloc(a) +#define FREE(p) if ((long)(p) > 1024) { free(p); } +#define DO_ASSERT(a) (void)(a) +#define CHECK(a) static jmethodID ordinal_meth = NULL; static jmethodID slicedef_meth = NULL; @@ -225,15 +112,15 @@ static inline jstring str_ref_to_java(JNIEnv *env, const char* chars, size_t len FREE(err_buf); return err_conv; } -static jclass arr_of_B_clz = NULL; static jclass arr_of_J_clz = NULL; +static jclass arr_of_B_clz = NULL; JNIEXPORT void Java_org_ldk_impl_bindings_init_1class_1cache(JNIEnv * env, jclass clz) { - arr_of_B_clz = (*env)->FindClass(env, "[B"); - CHECK(arr_of_B_clz != NULL); - arr_of_B_clz = (*env)->NewGlobalRef(env, arr_of_B_clz); arr_of_J_clz = (*env)->FindClass(env, "[J"); CHECK(arr_of_J_clz != NULL); arr_of_J_clz = (*env)->NewGlobalRef(env, arr_of_J_clz); + arr_of_B_clz = (*env)->FindClass(env, "[B"); + CHECK(arr_of_B_clz != NULL); + arr_of_B_clz = (*env)->NewGlobalRef(env, arr_of_B_clz); } static inline struct LDKThirtyTwoBytes ThirtyTwoBytes_clone(const struct LDKThirtyTwoBytes *orig) { struct LDKThirtyTwoBytes ret; memcpy(ret.data, orig->data, 32); return ret; } static inline LDKAccessError LDKAccessError_from_java(JNIEnv *env, jclass clz) { @@ -14286,6 +14173,16 @@ JNIEXPORT void JNICALL Java_org_ldk_impl_bindings_PeerManager_1socket_1disconnec PeerManager_socket_disconnected(&this_arg_conv, descriptor_conv); } +JNIEXPORT void JNICALL Java_org_ldk_impl_bindings_PeerManager_1disconnect_1by_1node_1id(JNIEnv *env, jclass clz, int64_t this_arg, int8_tArray node_id, jboolean no_connection_possible) { + LDKPeerManager this_arg_conv; + this_arg_conv.inner = (void*)(this_arg & (~1)); + this_arg_conv.is_owned = false; + LDKPublicKey node_id_ref; + CHECK((*env)->GetArrayLength(env, node_id) == 33); + (*env)->GetByteArrayRegion(env, node_id, 0, 33, node_id_ref.compressed_form); + PeerManager_disconnect_by_node_id(&this_arg_conv, node_id_ref, no_connection_possible); +} + JNIEXPORT void JNICALL Java_org_ldk_impl_bindings_PeerManager_1timer_1tick_1occured(JNIEnv *env, jclass clz, int64_t this_arg) { LDKPeerManager this_arg_conv; this_arg_conv.inner = (void*)(this_arg & (~1)); diff --git a/src/main/jni/org_ldk_impl_bindings.h b/src/main/jni/org_ldk_impl_bindings.h index ad8358be..d8aa468f 100644 --- a/src/main/jni/org_ldk_impl_bindings.h +++ b/src/main/jni/org_ldk_impl_bindings.h @@ -9063,6 +9063,14 @@ JNIEXPORT void JNICALL Java_org_ldk_impl_bindings_PeerManager_1process_1events JNIEXPORT void JNICALL Java_org_ldk_impl_bindings_PeerManager_1socket_1disconnected (JNIEnv *, jclass, jlong, jlong); +/* + * Class: org_ldk_impl_bindings + * Method: PeerManager_disconnect_by_node_id + * Signature: (J[BZ)V + */ +JNIEXPORT void JNICALL Java_org_ldk_impl_bindings_PeerManager_1disconnect_1by_1node_1id + (JNIEnv *, jclass, jlong, jbyteArray, jboolean); + /* * Class: org_ldk_impl_bindings * Method: PeerManager_timer_tick_occured diff --git a/ts/bindings.c b/ts/bindings.c index a383cb9d..f8423583 100644 --- a/ts/bindings.c +++ b/ts/bindings.c @@ -13,93 +13,13 @@ static inline void assert(bool expression) { if (!expression) { abort(); } } -// Always run a, then assert it is true: -#define DO_ASSERT(a) do { bool _assert_val = (a); assert(_assert_val); } while(0) -// Assert a is true or do nothing -#define CHECK(a) DO_ASSERT(a) - -// 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 -// linker option to wrap malloc/calloc/realloc/free, tracking everyhing allocated -// and free'd in Rust or C across the generated bindings shared library. - -#define BT_MAX 128 -typedef struct allocation { - struct allocation* next; - void* ptr; - const char* struct_name; -} allocation; -static allocation* allocation_ll = NULL; - -void* __real_malloc(size_t len); -void* __real_calloc(size_t nmemb, size_t len); -static void new_allocation(void* res, const char* struct_name) { - allocation* new_alloc = __real_malloc(sizeof(allocation)); - new_alloc->ptr = res; - new_alloc->struct_name = struct_name; - new_alloc->next = allocation_ll; - allocation_ll = new_alloc; -} -static void* MALLOC(size_t len, const char* struct_name) { - void* res = __real_malloc(len); - new_allocation(res, struct_name); - return res; -} -void __real_free(void* ptr); -static void alloc_freed(void* ptr) { - allocation* p = NULL; - allocation* it = allocation_ll; - while (it->ptr != ptr) { - p = it; it = it->next; - if (it == NULL) { - //XXX: fprintf(stderr, "Tried to free unknown pointer %p\n", ptr); - return; // addrsan should catch malloc-unknown and print more info than we have - } - } - if (p) { p->next = it->next; } else { allocation_ll = it->next; } - DO_ASSERT(it->ptr == ptr); - __real_free(it); -} -static void FREE(void* ptr) { - if ((long)ptr < 1024) return; // Rust loves to create pointers to the NULL page for dummys - alloc_freed(ptr); - __real_free(ptr); -} - -void* __wrap_malloc(size_t len) { - void* res = __real_malloc(len); - new_allocation(res, "malloc call"); - return res; -} -void* __wrap_calloc(size_t nmemb, size_t len) { - void* res = __real_calloc(nmemb, len); - new_allocation(res, "calloc call"); - return res; -} -void __wrap_free(void* ptr) { - if (ptr == NULL) return; - alloc_freed(ptr); - __real_free(ptr); -} - -void* __real_realloc(void* ptr, size_t newlen); -void* __wrap_realloc(void* ptr, size_t len) { - if (ptr != NULL) alloc_freed(ptr); - void* res = __real_realloc(ptr, len); - new_allocation(res, "realloc call"); - return res; -} -void __wrap_reallocarray(void* ptr, size_t new_sz) { - // Rust doesn't seem to use reallocarray currently - DO_ASSERT(false); -} +void *malloc(size_t size); +void free(void *ptr); -void __attribute__((destructor)) check_leaks() { - for (allocation* a = allocation_ll; a != NULL; a = a->next) { - //XXX: fprintf(stderr, "%s %p remains\n", a->struct_name, a->ptr); - } - DO_ASSERT(allocation_ll == NULL); -} +#define MALLOC(a, _) malloc(a) +#define FREE(p) if ((long)(p) > 1024) { free(p); } +#define DO_ASSERT(a) (void)(a) +#define CHECK(a) // We assume that CVec_u8Z and u8slice are the same size and layout (and thus pointers to the two can be mixed) _Static_assert(sizeof(LDKCVec_u8Z) == sizeof(LDKu8slice), "Vec and [u8] need to have been mapped identically"); @@ -13058,6 +12978,16 @@ void __attribute__((visibility("default"))) TS_PeerManager_socket_disconnected( PeerManager_socket_disconnected(&this_arg_conv, descriptor_conv); } +void __attribute__((visibility("default"))) TS_PeerManager_disconnect_by_node_id(uint32_t this_arg, int8_tArray node_id, jboolean no_connection_possible) { + LDKPeerManager this_arg_conv; + this_arg_conv.inner = (void*)(this_arg & (~1)); + this_arg_conv.is_owned = false; + LDKPublicKey node_id_ref; + CHECK(*((uint32_t*)node_id) == 33); + memcpy(node_id_ref.compressed_form, (uint8_t*)(node_id + 4), 33); + PeerManager_disconnect_by_node_id(&this_arg_conv, node_id_ref, no_connection_possible); +} + void __attribute__((visibility("default"))) TS_PeerManager_timer_tick_occured(uint32_t this_arg) { LDKPeerManager this_arg_conv; this_arg_conv.inner = (void*)(this_arg & (~1)); diff --git a/ts/bindings.ts b/ts/bindings.ts index 942649eb..d5c6475b 100644 --- a/ts/bindings.ts +++ b/ts/bindings.ts @@ -8200,6 +8200,14 @@ public static native long new_empty_slice_vec(); const nativeResponseValue = wasm.PeerManager_socket_disconnected(this_arg, descriptor); // debug statements here } + // void PeerManager_disconnect_by_node_id(const struct LDKPeerManager *NONNULL_PTR this_arg, struct LDKPublicKey node_id, bool no_connection_possible); + export function PeerManager_disconnect_by_node_id(this_arg: number, node_id: Uint8Array, no_connection_possible: boolean): void { + if(!isWasmInitialized) { + throw new Error("initializeWasm() must be awaited first!"); + } + const nativeResponseValue = wasm.PeerManager_disconnect_by_node_id(this_arg, encodeArray(node_id), no_connection_possible); + // debug statements here + } // void PeerManager_timer_tick_occured(const struct LDKPeerManager *NONNULL_PTR this_arg); export function PeerManager_timer_tick_occured(this_arg: number): void { if(!isWasmInitialized) { diff --git a/ts/structs/PeerManager.ts b/ts/structs/PeerManager.ts index 165440cf..cc01f8bd 100644 --- a/ts/structs/PeerManager.ts +++ b/ts/structs/PeerManager.ts @@ -70,6 +70,10 @@ import * as bindings from '../bindings' // TODO: figure out location this.ptrs_to.add(descriptor); } + public void disconnect_by_node_id(Uint8Array node_id, boolean no_connection_possible) { + bindings.PeerManager_disconnect_by_node_id(this.ptr, node_id, no_connection_possible); + } + public void timer_tick_occured() { bindings.PeerManager_timer_tick_occured(this.ptr); }