test_sort_func_ptrs.cc 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 = (struct func_info *)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. }
  55. TEST(TestSortFuncPtrs, qsort)
  56. {
  57. ASSERT_NE(sort_func_ptrs, nullptr);
  58. void *funcs[5] = {
  59. (void *)0x1024, (void *)0x10, (void *)0x24, (void *)0x102, (void *)0x4,
  60. };
  61. AOTModule module = {};
  62. module.func_count = 5;
  63. module.func_ptrs = &funcs[0];
  64. char buf[64] = { 0 };
  65. struct func_info *sorted_funcs = sort_func_ptrs(&module, buf, 64);
  66. // sorted
  67. ASSERT_EQ((uintptr_t)(sorted_funcs[0].ptr), 0x4);
  68. ASSERT_EQ((uintptr_t)(sorted_funcs[1].ptr), 0x10);
  69. ASSERT_EQ((uintptr_t)(sorted_funcs[2].ptr), 0x24);
  70. ASSERT_EQ((uintptr_t)(sorted_funcs[3].ptr), 0x102);
  71. ASSERT_EQ((uintptr_t)(sorted_funcs[4].ptr), 0x1024);
  72. ASSERT_EQ(sorted_funcs[0].idx, 4);
  73. ASSERT_EQ(sorted_funcs[1].idx, 1);
  74. ASSERT_EQ(sorted_funcs[2].idx, 2);
  75. ASSERT_EQ(sorted_funcs[3].idx, 3);
  76. ASSERT_EQ(sorted_funcs[4].idx, 0);
  77. // don't change input
  78. ASSERT_EQ((uintptr_t)(funcs[0]), 0x1024);
  79. ASSERT_EQ((uintptr_t)(funcs[1]), 0x10);
  80. ASSERT_EQ((uintptr_t)(funcs[2]), 0x24);
  81. ASSERT_EQ((uintptr_t)(funcs[3]), 0x102);
  82. ASSERT_EQ((uintptr_t)(funcs[4]), 0x4);
  83. wasm_runtime_free(sorted_funcs);
  84. }