wasm_shared_memory.c 13 KB

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