[TS] Fix Some array leaks, mark others, treat < 10 leaks as "okay"
[ldk-java] / typescript_strings.py
index 2979481d1e9ab4648d4d532246e090284923e3ea..d669821301b53fdde1bff004c51e86f8f56ab9dd 100644 (file)
@@ -73,21 +73,15 @@ imports.wasi_snapshot_preview1 = {
                // This is called before fd_write to format + print panic messages
                console.log("wasi_snapshot_preview1:environ_sizes_get");
                const out_count_view = new Uint32Array(wasm.memory.buffer, environ_var_count_ptr, 1);
-               out_count_view[0] = 1;
+               out_count_view[0] = 0;
                const out_len_view = new Uint32Array(wasm.memory.buffer, environ_len_ptr, 1);
-               out_len_view[0] = "RUST_BACKTRACE=1".length + 1; // Note that string must be NULL-terminated
+               out_len_view[0] = 0;
                return 0;
        },
        "environ_get": (environ_ptr: number, environ_buf_ptr: number) => {
                // This is called before fd_write to format + print panic messages
                console.log("wasi_snapshot_preview1:environ_get");
-               const out_ptrs = new Uint32Array(wasm.memory.buffer, environ_ptr, 2);
-               out_ptrs[0] = environ_buf_ptr;
-               out_ptrs[1] = "RUST_BACKTRACE=1".length;
-               const out_environ = new Uint8Array(wasm.memory.buffer, environ_buf_ptr, out_ptrs[1]);
-               for (var i = 0; i < out_ptrs[1]; i++) { out_environ[i] = "RUST_BACKTRACE=1".codePointAt(i); }
-               out_environ[out_ptrs[1]] = 0;
-               return 0;
+               return 58; // Note supported - we said there were 0 environment entries!
        },
        "proc_exit" : () => {
                console.log("wasi_snapshot_preview1:proc_exit");
@@ -123,12 +117,14 @@ export async function initializeWasm(uri: string) {
        if (decodeString(wasm.TS_get_lib_version_string()) !== version.get_ldk_java_bindings_version())
                throw new Error(\"Compiled LDK library and LDK class failes do not match\");
        // Fetching the LDK versions from C also checks that the header and binaries match
-       if (wasm.TS_get_ldk_c_bindings_version() == 0)
+       const c_bindings_ver: number = wasm.TS_get_ldk_c_bindings_version();
+       const ldk_ver: number = wasm.TS_get_ldk_version();
+       if (c_bindings_ver == 0)
                throw new Error(\"LDK version did not match the header we built against\");
-       if (wasm.TS_get_ldk_version() == 0)
+       if (ldk_ver == 0)
                throw new Error(\"LDK C bindings version did not match the header we built against\");
-       const c_bindings_version: string = decodeString(wasm.TS_get_ldk_c_bindings_version());
-       const ldk_version: string = decodeString(wasm.TS_get_ldk_version());
+       const c_bindings_version: string = decodeString(c_bindings_ver)
+       const ldk_version: string = decodeString(ldk_ver);
        console.log(\"Loaded LDK-Java Bindings with LDK \" + ldk_version + \" and LDK-C-Bindings \" + c_bindings_version);
 
        isWasmInitialized = true;
@@ -207,6 +203,9 @@ const decodeUint32Array = (arrayPointer: number, free = true) => {
        return actualArray;
 }
 
+
+export function freeWasmMemory(pointer: number) { wasm.TS_free(pointer); }
+
 /* @internal */
 export function getU32ArrayElem(arrayPointer: number, idx: number): number {
        const actualArrayViewer = new Uint32Array(wasm.memory.buffer, arrayPointer + 4, idx + 1);
@@ -376,8 +375,9 @@ typedef struct allocation {
 static allocation* allocation_ll = NULL;
 static allocation* freed_ll = NULL;
 
-void* __real_malloc(size_t len);
-void* __real_calloc(size_t nmemb, size_t len);
+extern void* __real_malloc(size_t len);
+extern void* __real_calloc(size_t nmemb, size_t len);
+extern void* __real_aligned_alloc(size_t alignment, size_t size);
 static void new_allocation(void* res, const char* struct_name, int lineno) {
        allocation* new_alloc = __real_malloc(sizeof(allocation));
        new_alloc->ptr = res;
@@ -450,6 +450,11 @@ void* __wrap_calloc(size_t nmemb, size_t len) {
        new_allocation(res, "calloc call", 0);
        return res;
 }
+void* __wrap_aligned_alloc(size_t alignment, size_t size) {
+       void* res = __real_aligned_alloc(alignment, size);
+       new_allocation(res, "aligned_alloc call", 0);
+       return res;
+}
 void __wrap_free(void* ptr) {
        if (ptr == NULL) return;
        alloc_freed(ptr, 0);
@@ -581,12 +586,12 @@ import * as bindings from '../bindings.mjs'
     def get_native_arr_contents(self, arr_name, dest_name, arr_len, ty_info, copy):
         if ty_info.c_ty == "int8_tArray":
             if copy:
-                return "memcpy(" + dest_name + ", " + arr_name + "->elems, " + arr_len + ")"
+                return "memcpy(" + dest_name + ", " + arr_name + "->elems, " + arr_len + "); FREE(" + arr_name + ")"
         if ty_info.c_ty == "ptrArray":
-            return "(void*) " + arr_name + "->elems"
+            return "(void*) " + arr_name + "->elems /* XXX " + arr_name + " leaks */"
         else:
             assert not copy
-            return arr_name + "->elems"
+            return arr_name + "->elems /* XXX " + arr_name + " leaks */"
     def get_native_arr_elem(self, arr_name, idxc, ty_info):
         assert False # Only called if above is None
     def get_native_arr_ptr_call(self, ty_info):
@@ -629,6 +634,8 @@ import * as bindings from '../bindings.mjs'
             assert False
     def constr_hu_array(self, ty_info, arr_len):
         return "new Array(" + arr_len + ").fill(null)"
+    def cleanup_converted_native_array(self, ty_info, arr_name):
+        return "bindings.freeWasmMemory(" + arr_name + ")"
 
     def primitive_arr_from_hu(self, mapped_ty, fixed_len, arr_name):
         inner = arr_name