test_hello2.c 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. /*
  6. * This example basically does the same thing as test_hello.c,
  7. * using wasm_export.h API.
  8. */
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include "wasm_export.h"
  12. static int
  13. test_hello2_wrapper(wasm_exec_env_t exec_env, uint32_t nameaddr,
  14. uint32_t resultaddr, uint32_t resultlen)
  15. {
  16. /*
  17. * Perform wasm_runtime_malloc to check if the runtime has been
  18. * initialized as expected.
  19. * This would fail with "memory hasn't been initialize" error
  20. * unless we are not sharing a runtime with the loader app. (iwasm)
  21. */
  22. void *p = wasm_runtime_malloc(1);
  23. if (p == NULL) {
  24. return -1;
  25. }
  26. wasm_runtime_free(p);
  27. wasm_module_inst_t inst = wasm_runtime_get_module_inst(exec_env);
  28. if (!wasm_runtime_validate_app_str_addr(inst, (uint64_t)nameaddr)
  29. || !wasm_runtime_validate_app_addr(inst, (uint64_t)resultaddr,
  30. (uint64_t)resultlen)) {
  31. return -1;
  32. }
  33. const char *name =
  34. wasm_runtime_addr_app_to_native(inst, (uint64_t)nameaddr);
  35. char *result = wasm_runtime_addr_app_to_native(inst, (uint64_t)resultaddr);
  36. return snprintf(result, resultlen,
  37. "Hello, %s. This is %s! Your wasm_module_inst_t is %p.\n",
  38. name, __func__, inst);
  39. }
  40. /* clang-format off */
  41. #define REG_NATIVE_FUNC(func_name, signature) \
  42. { #func_name, func_name##_wrapper, signature, NULL }
  43. static NativeSymbol native_symbols[] = {
  44. REG_NATIVE_FUNC(test_hello2, "(iii)i")
  45. };
  46. /* clang-format on */
  47. uint32_t
  48. get_native_lib(char **p_module_name, NativeSymbol **p_native_symbols)
  49. {
  50. *p_module_name = "env";
  51. *p_native_symbols = native_symbols;
  52. return sizeof(native_symbols) / sizeof(NativeSymbol);
  53. }
  54. int
  55. init_native_lib()
  56. {
  57. printf("%s in test_hello2.c called\n", __func__);
  58. return 0;
  59. }
  60. void
  61. deinit_native_lib()
  62. {
  63. printf("%s in test_hello2.c called\n", __func__);
  64. }