wasm_shared_memory.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include "bh_log.h"
  6. #include "wasm_shared_memory.h"
  7. #if WASM_ENABLE_THREAD_MGR != 0
  8. #include "../libraries/thread-mgr/thread_manager.h"
  9. #endif
  10. static bh_list shared_memory_list_head;
  11. static bh_list *const shared_memory_list = &shared_memory_list_head;
  12. static korp_mutex shared_memory_list_lock;
  13. /* clang-format off */
  14. enum {
  15. S_WAITING,
  16. S_NOTIFIED
  17. };
  18. /* clang-format on */
  19. typedef struct AtomicWaitInfo {
  20. bh_list wait_list_head;
  21. bh_list *wait_list;
  22. /* WARNING: insert to the list allowed only in acquire_wait_info
  23. otherwise there will be data race as described in PR #2016 */
  24. } AtomicWaitInfo;
  25. typedef struct AtomicWaitNode {
  26. bh_list_link l;
  27. uint8 status;
  28. korp_cond wait_cond;
  29. } AtomicWaitNode;
  30. /* Atomic wait map */
  31. static HashMap *wait_map;
  32. static uint32
  33. wait_address_hash(void *address);
  34. static bool
  35. wait_address_equal(void *h1, void *h2);
  36. static void
  37. destroy_wait_info(void *wait_info);
  38. bool
  39. wasm_shared_memory_init()
  40. {
  41. if (os_mutex_init(&shared_memory_list_lock) != 0)
  42. return false;
  43. /* wait map not exists, create new map */
  44. if (!(wait_map = bh_hash_map_create(32, true, (HashFunc)wait_address_hash,
  45. (KeyEqualFunc)wait_address_equal, NULL,
  46. destroy_wait_info))) {
  47. os_mutex_destroy(&shared_memory_list_lock);
  48. return false;
  49. }
  50. return true;
  51. }
  52. void
  53. wasm_shared_memory_destroy()
  54. {
  55. bh_hash_map_destroy(wait_map);
  56. os_mutex_destroy(&shared_memory_list_lock);
  57. }
  58. static WASMSharedMemNode *
  59. search_module(WASMModuleCommon *module)
  60. {
  61. WASMSharedMemNode *node;
  62. os_mutex_lock(&shared_memory_list_lock);
  63. node = bh_list_first_elem(shared_memory_list);
  64. while (node) {
  65. if (module == node->module) {
  66. os_mutex_unlock(&shared_memory_list_lock);
  67. return node;
  68. }
  69. node = bh_list_elem_next(node);
  70. }
  71. os_mutex_unlock(&shared_memory_list_lock);
  72. return NULL;
  73. }
  74. WASMSharedMemNode *
  75. wasm_module_get_shared_memory(WASMModuleCommon *module)
  76. {
  77. return search_module(module);
  78. }
  79. int32
  80. shared_memory_inc_reference(WASMModuleCommon *module)
  81. {
  82. WASMSharedMemNode *node = search_module(module);
  83. uint32 ref_count = -1;
  84. if (node) {
  85. os_mutex_lock(&node->lock);
  86. ref_count = ++node->ref_count;
  87. os_mutex_unlock(&node->lock);
  88. }
  89. return ref_count;
  90. }
  91. int32
  92. shared_memory_dec_reference(WASMModuleCommon *module)
  93. {
  94. WASMSharedMemNode *node = search_module(module);
  95. uint32 ref_count = 0;
  96. if (node) {
  97. os_mutex_lock(&node->lock);
  98. ref_count = --node->ref_count;
  99. os_mutex_unlock(&node->lock);
  100. if (ref_count == 0) {
  101. os_mutex_lock(&shared_memory_list_lock);
  102. bh_list_remove(shared_memory_list, node);
  103. os_mutex_unlock(&shared_memory_list_lock);
  104. os_mutex_destroy(&node->shared_mem_lock);
  105. os_mutex_destroy(&node->lock);
  106. wasm_runtime_free(node);
  107. }
  108. return ref_count;
  109. }
  110. return -1;
  111. }
  112. WASMMemoryInstanceCommon *
  113. shared_memory_get_memory_inst(WASMSharedMemNode *node)
  114. {
  115. return node->memory_inst;
  116. }
  117. WASMSharedMemNode *
  118. shared_memory_set_memory_inst(WASMModuleCommon *module,
  119. WASMMemoryInstanceCommon *memory)
  120. {
  121. WASMSharedMemNode *node;
  122. bh_list_status ret;
  123. if (!(node = wasm_runtime_malloc(sizeof(WASMSharedMemNode))))
  124. return NULL;
  125. node->module = module;
  126. node->memory_inst = memory;
  127. node->ref_count = 1;
  128. if (os_mutex_init(&node->shared_mem_lock) != 0) {
  129. wasm_runtime_free(node);
  130. return NULL;
  131. }
  132. if (os_mutex_init(&node->lock) != 0) {
  133. os_mutex_destroy(&node->shared_mem_lock);
  134. wasm_runtime_free(node);
  135. return NULL;
  136. }
  137. os_mutex_lock(&shared_memory_list_lock);
  138. ret = bh_list_insert(shared_memory_list, node);
  139. bh_assert(ret == BH_LIST_SUCCESS);
  140. os_mutex_unlock(&shared_memory_list_lock);
  141. (void)ret;
  142. return node;
  143. }
  144. /* Atomics wait && notify APIs */
  145. static uint32
  146. wait_address_hash(void *address)
  147. {
  148. return (uint32)(uintptr_t)address;
  149. }
  150. static bool
  151. wait_address_equal(void *h1, void *h2)
  152. {
  153. return h1 == h2 ? true : false;
  154. }
  155. static bool
  156. is_wait_node_exists(bh_list *wait_list, AtomicWaitNode *node)
  157. {
  158. AtomicWaitNode *curr;
  159. curr = bh_list_first_elem(wait_list);
  160. while (curr) {
  161. if (curr == node) {
  162. return true;
  163. }
  164. curr = bh_list_elem_next(curr);
  165. }
  166. return false;
  167. }
  168. static uint32
  169. notify_wait_list(bh_list *wait_list, uint32 count)
  170. {
  171. AtomicWaitNode *node, *next;
  172. uint32 i, notify_count = count;
  173. if (count > wait_list->len)
  174. notify_count = wait_list->len;
  175. node = bh_list_first_elem(wait_list);
  176. if (!node)
  177. return 0;
  178. for (i = 0; i < notify_count; i++) {
  179. bh_assert(node);
  180. next = bh_list_elem_next(node);
  181. node->status = S_NOTIFIED;
  182. /* wakeup */
  183. os_cond_signal(&node->wait_cond);
  184. node = next;
  185. }
  186. return notify_count;
  187. }
  188. static AtomicWaitInfo *
  189. acquire_wait_info(void *address, AtomicWaitNode *wait_node)
  190. {
  191. AtomicWaitInfo *wait_info = NULL;
  192. bh_list_status ret;
  193. if (address)
  194. wait_info = (AtomicWaitInfo *)bh_hash_map_find(wait_map, address);
  195. if (!wait_node) {
  196. return wait_info;
  197. }
  198. /* No wait info on this address, create new info */
  199. if (!wait_info) {
  200. if (!(wait_info = (AtomicWaitInfo *)wasm_runtime_malloc(
  201. sizeof(AtomicWaitInfo)))) {
  202. return NULL;
  203. }
  204. memset(wait_info, 0, sizeof(AtomicWaitInfo));
  205. /* init wait list */
  206. wait_info->wait_list = &wait_info->wait_list_head;
  207. ret = bh_list_init(wait_info->wait_list);
  208. bh_assert(ret == BH_LIST_SUCCESS);
  209. (void)ret;
  210. if (!bh_hash_map_insert(wait_map, address, (void *)wait_info)) {
  211. wasm_runtime_free(wait_info);
  212. return NULL;
  213. }
  214. }
  215. ret = bh_list_insert(wait_info->wait_list, wait_node);
  216. bh_assert(ret == BH_LIST_SUCCESS);
  217. (void)ret;
  218. return wait_info;
  219. }
  220. static void
  221. destroy_wait_info(void *wait_info)
  222. {
  223. AtomicWaitNode *node, *next;
  224. if (wait_info) {
  225. node = bh_list_first_elem(((AtomicWaitInfo *)wait_info)->wait_list);
  226. while (node) {
  227. next = bh_list_elem_next(node);
  228. os_cond_destroy(&node->wait_cond);
  229. wasm_runtime_free(node);
  230. node = next;
  231. }
  232. wasm_runtime_free(wait_info);
  233. }
  234. }
  235. static void
  236. map_try_release_wait_info(HashMap *wait_map_, AtomicWaitInfo *wait_info,
  237. void *address)
  238. {
  239. if (wait_info->wait_list->len > 0) {
  240. return;
  241. }
  242. bh_hash_map_remove(wait_map_, address, NULL, NULL);
  243. destroy_wait_info(wait_info);
  244. }
  245. uint32
  246. wasm_runtime_atomic_wait(WASMModuleInstanceCommon *module, void *address,
  247. uint64 expect, int64 timeout, bool wait64)
  248. {
  249. WASMModuleInstance *module_inst = (WASMModuleInstance *)module;
  250. AtomicWaitInfo *wait_info;
  251. AtomicWaitNode *wait_node;
  252. WASMSharedMemNode *node;
  253. #if WASM_ENABLE_THREAD_MGR != 0
  254. WASMExecEnv *exec_env;
  255. #endif
  256. uint64 timeout_left, timeout_wait, timeout_1sec;
  257. bool check_ret, is_timeout, no_wait;
  258. bh_assert(module->module_type == Wasm_Module_Bytecode
  259. || module->module_type == Wasm_Module_AoT);
  260. if (wasm_copy_exception(module_inst, NULL)) {
  261. return -1;
  262. }
  263. /* Currently we have only one memory instance */
  264. if (!module_inst->memories[0]->is_shared) {
  265. wasm_runtime_set_exception(module, "expected shared memory");
  266. return -1;
  267. }
  268. if ((uint8 *)address < module_inst->memories[0]->memory_data
  269. || (uint8 *)address + (wait64 ? 8 : 4)
  270. > module_inst->memories[0]->memory_data_end) {
  271. wasm_runtime_set_exception(module, "out of bounds memory access");
  272. return -1;
  273. }
  274. #if WASM_ENABLE_THREAD_MGR != 0
  275. exec_env =
  276. wasm_clusters_search_exec_env((WASMModuleInstanceCommon *)module_inst);
  277. bh_assert(exec_env);
  278. #endif
  279. node = search_module((WASMModuleCommon *)module_inst->module);
  280. bh_assert(node);
  281. /* Lock the shared_mem_lock for the whole atomic wait process,
  282. and use it to os_cond_reltimedwait */
  283. os_mutex_lock(&node->shared_mem_lock);
  284. no_wait = (!wait64 && *(uint32 *)address != (uint32)expect)
  285. || (wait64 && *(uint64 *)address != expect);
  286. if (no_wait) {
  287. os_mutex_unlock(&node->shared_mem_lock);
  288. return 1;
  289. }
  290. if (!(wait_node = wasm_runtime_malloc(sizeof(AtomicWaitNode)))) {
  291. os_mutex_unlock(&node->shared_mem_lock);
  292. wasm_runtime_set_exception(module, "failed to create wait node");
  293. return -1;
  294. }
  295. memset(wait_node, 0, sizeof(AtomicWaitNode));
  296. if (0 != os_cond_init(&wait_node->wait_cond)) {
  297. os_mutex_unlock(&node->shared_mem_lock);
  298. wasm_runtime_free(wait_node);
  299. wasm_runtime_set_exception(module, "failed to init wait cond");
  300. return -1;
  301. }
  302. wait_node->status = S_WAITING;
  303. /* Acquire the wait info, create new one if not exists */
  304. wait_info = acquire_wait_info(address, wait_node);
  305. if (!wait_info) {
  306. os_mutex_unlock(&node->shared_mem_lock);
  307. os_cond_destroy(&wait_node->wait_cond);
  308. wasm_runtime_free(wait_node);
  309. wasm_runtime_set_exception(module, "failed to acquire wait_info");
  310. return -1;
  311. }
  312. /* unit of timeout is nsec, convert it to usec */
  313. timeout_left = (uint64)timeout / 1000;
  314. timeout_1sec = 1e6;
  315. while (1) {
  316. if (timeout < 0) {
  317. /* wait forever until it is notified or terminatied
  318. here we keep waiting and checking every second */
  319. os_cond_reltimedwait(&wait_node->wait_cond, &node->shared_mem_lock,
  320. (uint64)timeout_1sec);
  321. if (wait_node->status == S_NOTIFIED /* notified by atomic.notify */
  322. #if WASM_ENABLE_THREAD_MGR != 0
  323. /* terminated by other thread */
  324. || wasm_cluster_is_thread_terminated(exec_env)
  325. #endif
  326. ) {
  327. break;
  328. }
  329. }
  330. else {
  331. timeout_wait =
  332. timeout_left < timeout_1sec ? timeout_left : timeout_1sec;
  333. os_cond_reltimedwait(&wait_node->wait_cond, &node->shared_mem_lock,
  334. timeout_wait);
  335. if (wait_node->status == S_NOTIFIED /* notified by atomic.notify */
  336. || timeout_left <= timeout_wait /* time out */
  337. #if WASM_ENABLE_THREAD_MGR != 0
  338. /* terminated by other thread */
  339. || wasm_cluster_is_thread_terminated(exec_env)
  340. #endif
  341. ) {
  342. break;
  343. }
  344. timeout_left -= timeout_wait;
  345. }
  346. }
  347. is_timeout = wait_node->status == S_WAITING ? true : false;
  348. check_ret = is_wait_node_exists(wait_info->wait_list, wait_node);
  349. bh_assert(check_ret);
  350. (void)check_ret;
  351. /* Remove wait node from wait list */
  352. bh_list_remove(wait_info->wait_list, wait_node);
  353. os_cond_destroy(&wait_node->wait_cond);
  354. wasm_runtime_free(wait_node);
  355. /* Release wait info if no wait nodes are attached */
  356. map_try_release_wait_info(wait_map, wait_info, address);
  357. os_mutex_unlock(&node->shared_mem_lock);
  358. return is_timeout ? 2 : 0;
  359. }
  360. uint32
  361. wasm_runtime_atomic_notify(WASMModuleInstanceCommon *module, void *address,
  362. uint32 count)
  363. {
  364. WASMModuleInstance *module_inst = (WASMModuleInstance *)module;
  365. uint32 notify_result;
  366. AtomicWaitInfo *wait_info;
  367. WASMSharedMemNode *node;
  368. bool out_of_bounds;
  369. bh_assert(module->module_type == Wasm_Module_Bytecode
  370. || module->module_type == Wasm_Module_AoT);
  371. out_of_bounds =
  372. ((uint8 *)address < module_inst->memories[0]->memory_data
  373. || (uint8 *)address + 4 > module_inst->memories[0]->memory_data_end);
  374. if (out_of_bounds) {
  375. wasm_runtime_set_exception(module, "out of bounds memory access");
  376. return -1;
  377. }
  378. /* Currently we have only one memory instance */
  379. if (!module_inst->memories[0]->is_shared) {
  380. /* Always return 0 for ushared linear memory since there is
  381. no way to create a waiter on it */
  382. return 0;
  383. }
  384. node = search_module((WASMModuleCommon *)module_inst->module);
  385. bh_assert(node);
  386. /* Lock the shared_mem_lock for the whole atomic notify process,
  387. and use it to os_cond_signal */
  388. os_mutex_lock(&node->shared_mem_lock);
  389. wait_info = acquire_wait_info(address, NULL);
  390. /* Nobody wait on this address */
  391. if (!wait_info) {
  392. os_mutex_unlock(&node->shared_mem_lock);
  393. return 0;
  394. }
  395. /* Notify each wait node in the wait list */
  396. notify_result = notify_wait_list(wait_info->wait_list, count);
  397. os_mutex_unlock(&node->shared_mem_lock);
  398. return notify_result;
  399. }