22 * Copyright (C) 2019 Intel Corporation. All rights reserved.
33 * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
44 */
5+
56#include "wasm_c_api_internal.h"
67
78#include "bh_assert.h"
1819#endif /*WASM_ENABLE_JIT != 0 && WASM_ENABLE_LAZY_JIT == 0*/
1920#endif /*WASM_ENABLE_AOT != 0*/
2021
22+ #if WASM_ENABLE_WASM_CACHE != 0
23+ #include <openssl/sha.h>
24+ #endif
25+
2126/*
2227 * Thread Model:
2328 * - Only one wasm_engine_t in one process
@@ -35,6 +40,9 @@ typedef struct wasm_module_ex_t {
3540 wasm_byte_vec_t * binary ;
3641 korp_mutex lock ;
3742 uint32 ref_count ;
43+ #if WASM_ENABLE_WASM_CACHE != 0
44+ char hash [SHA256_DIGEST_LENGTH ];
45+ #endif
3846} wasm_module_ex_t ;
3947
4048#ifndef os_thread_local_attribute
@@ -2087,16 +2095,68 @@ module_to_module_ext(wasm_module_t *module)
20872095#define MODULE_AOT (module_comm ) ((AOTModule *)(*module_comm))
20882096#endif
20892097
2098+ #if WASM_ENABLE_WASM_CACHE != 0
2099+ static wasm_module_ex_t *
2100+ check_loaded_module (Vector * modules , char * binary_hash )
2101+ {
2102+ unsigned i ;
2103+ wasm_module_ex_t * module = NULL ;
2104+
2105+ for (i = 0 ; i < modules -> num_elems ; i ++ ) {
2106+ bh_vector_get (modules , i , & module );
2107+ if (!module ) {
2108+ LOG_ERROR ("Unexpected failure at %d\n" , __LINE__ );
2109+ return NULL ;
2110+ }
2111+
2112+ if (!module -> ref_count )
2113+ /* deleted */
2114+ continue ;
2115+
2116+ if (memcmp (module -> hash , binary_hash , SHA256_DIGEST_LENGTH ) == 0 )
2117+ return module ;
2118+ }
2119+ return NULL ;
2120+ }
2121+
2122+ static wasm_module_ex_t *
2123+ try_reuse_loaded_module (wasm_store_t * store , char * binary_hash )
2124+ {
2125+ wasm_module_ex_t * cached = NULL ;
2126+ wasm_module_ex_t * ret = NULL ;
2127+
2128+ cached = check_loaded_module (& singleton_engine -> modules , binary_hash );
2129+ if (!cached )
2130+ goto quit ;
2131+
2132+ os_mutex_lock (& cached -> lock );
2133+ if (!cached -> ref_count )
2134+ goto unlock ;
2135+
2136+ if (!bh_vector_append ((Vector * )store -> modules , & cached ))
2137+ goto unlock ;
2138+
2139+ cached -> ref_count += 1 ;
2140+ ret = cached ;
2141+
2142+ unlock :
2143+ os_mutex_unlock (& cached -> lock );
2144+ quit :
2145+ return ret ;
2146+ }
2147+ #endif /* WASM_ENABLE_WASM_CACHE != 0 */
2148+
20902149wasm_module_t *
20912150wasm_module_new (wasm_store_t * store , const wasm_byte_vec_t * binary )
20922151{
20932152 char error_buf [128 ] = { 0 };
20942153 wasm_module_ex_t * module_ex = NULL ;
2154+ #if WASM_ENABLE_WASM_CACHE != 0
2155+ char binary_hash [SHA256_DIGEST_LENGTH ] = { 0 };
2156+ #endif
20952157
20962158 bh_assert (singleton_engine );
20972159
2098- WASM_C_DUMP_PROC_MEM ();
2099-
21002160 if (!store || !binary || binary -> size == 0 || binary -> size > UINT32_MAX )
21012161 goto quit ;
21022162
@@ -2121,6 +2181,16 @@ wasm_module_new(wasm_store_t *store, const wasm_byte_vec_t *binary)
21212181 }
21222182 }
21232183
2184+ #if WASM_ENABLE_WASM_CACHE != 0
2185+ /* if cached */
2186+ SHA256 ((void * )binary -> data , binary -> num_elems , binary_hash );
2187+ module_ex = try_reuse_loaded_module (store , binary_hash );
2188+ if (module_ex )
2189+ return module_ext_to_module (module_ex );
2190+ #endif
2191+
2192+ WASM_C_DUMP_PROC_MEM ();
2193+
21242194 module_ex = malloc_internal (sizeof (wasm_module_ex_t ));
21252195 if (!module_ex )
21262196 goto quit ;
@@ -2151,6 +2221,11 @@ wasm_module_new(wasm_store_t *store, const wasm_byte_vec_t *binary)
21512221 if (!bh_vector_append (& singleton_engine -> modules , & module_ex ))
21522222 goto destroy_lock ;
21532223
2224+ #if WASM_ENABLE_WASM_CACHE != 0
2225+ bh_memcpy_s (module_ex -> hash , sizeof (module_ex -> hash ), binary_hash ,
2226+ sizeof (binary_hash ));
2227+ #endif
2228+
21542229 module_ex -> ref_count = 1 ;
21552230
21562231 WASM_C_DUMP_PROC_MEM ();
@@ -2226,6 +2301,10 @@ wasm_module_delete_internal(wasm_module_t *module)
22262301 module_ex -> module_comm_rt = NULL ;
22272302 }
22282303
2304+ #if WASM_ENABLE_WASM_CACHE != 0
2305+ memset (module_ex -> hash , 0 , sizeof (module_ex -> hash ));
2306+ #endif
2307+
22292308 os_mutex_unlock (& module_ex -> lock );
22302309}
22312310
0 commit comments