test_sort_func_ptrs.cc 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include "aot_runtime.h"
  6. #include <cstdint>
  7. #include <gtest/gtest.h>
  8. #include <dlfcn.h>
  9. extern "C" {
  10. // TODO: won't work, for non static function create_perf_map have goto statement jump to label ‘quit’
  11. // #include "aot_perf_map.c"
  12. // simply copy the function
  13. struct func_info {
  14. uint32 idx;
  15. void *ptr;
  16. };
  17. static int
  18. compare_func_ptrs(const void *f1, const void *f2)
  19. {
  20. return (intptr_t)((struct func_info *)f1)->ptr
  21. - (intptr_t)((struct func_info *)f2)->ptr;
  22. }
  23. static struct func_info *
  24. sort_func_ptrs(const AOTModule *module, char *error_buf, uint32 error_buf_size)
  25. {
  26. uint64 content_len;
  27. struct func_info *sorted_func_ptrs;
  28. unsigned i;
  29. content_len = (uint64)sizeof(struct func_info) * module->func_count;
  30. sorted_func_ptrs = wasm_runtime_malloc(content_len);
  31. if (!sorted_func_ptrs) {
  32. snprintf(error_buf, error_buf_size,
  33. "allocate memory failed when creating perf map");
  34. return NULL;
  35. }
  36. for (i = 0; i < module->func_count; i++) {
  37. sorted_func_ptrs[i].idx = i;
  38. sorted_func_ptrs[i].ptr = module->func_ptrs[i];
  39. }
  40. qsort(sorted_func_ptrs, module->func_count, sizeof(struct func_info),
  41. compare_func_ptrs);
  42. return sorted_func_ptrs;
  43. }
  44. void *
  45. wasm_runtime_malloc(unsigned int size)
  46. {
  47. return malloc(size);
  48. }
  49. void
  50. wasm_runtime_free(void* ptr)
  51. {
  52. return free(ptr);
  53. }
  54. int
  55. b_memcpy_s(void *s1, unsigned int s1max, const void *s2, unsigned int n)
  56. {
  57. return memcpy(s1, s2, n);
  58. }
  59. }
  60. TEST(TestSortFuncPtrs, qsort)
  61. {
  62. void *p = sort_func_ptrs;
  63. ASSERT_NE(p, nullptr);
  64. void *funcs[5] = {
  65. (void *)0x1024, (void *)0x10, (void *)0x24, (void *)0x102, (void *)0x4,
  66. };
  67. AOTModule module = { 0 };
  68. module.func_count = 5;
  69. module.func_ptrs = &funcs[0];
  70. char buf[64] = { 0 };
  71. struct func_info *sorted_funcs = sort_func_ptrs(&module, buf, 64);
  72. // sorted
  73. ASSERT_EQ((uintptr_t)(sorted_funcs[0].ptr), 0x4);
  74. ASSERT_EQ((uintptr_t)(sorted_funcs[1].ptr), 0x10);
  75. ASSERT_EQ((uintptr_t)(sorted_funcs[2].ptr), 0x24);
  76. ASSERT_EQ((uintptr_t)(sorted_funcs[3].ptr), 0x102);
  77. ASSERT_EQ((uintptr_t)(sorted_funcs[4].ptr), 0x1024);
  78. ASSERT_EQ(sorted_funcs[0].idx, 4);
  79. ASSERT_EQ(sorted_funcs[1].idx, 1);
  80. ASSERT_EQ(sorted_funcs[2].idx, 2);
  81. ASSERT_EQ(sorted_funcs[3].idx, 3);
  82. ASSERT_EQ(sorted_funcs[4].idx, 0);
  83. // don't change input
  84. ASSERT_EQ((uintptr_t)(funcs[0]), 0x1024);
  85. ASSERT_EQ((uintptr_t)(funcs[1]), 0x10);
  86. ASSERT_EQ((uintptr_t)(funcs[2]), 0x24);
  87. ASSERT_EQ((uintptr_t)(funcs[3]), 0x102);
  88. ASSERT_EQ((uintptr_t)(funcs[4]), 0x4);
  89. wasm_runtime_free(sorted_funcs);
  90. }