testapp.c 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /*
  2. * Copyright (C) 2024 Midokura Japan KK. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include <stdio.h>
  6. #include <stdint.h>
  7. uint32_t
  8. host_consume_stack_and_call_indirect(int (*)(int), uint32_t, uint32_t);
  9. uint32_t host_consume_stack(uint32_t);
  10. int
  11. cb(int x)
  12. {
  13. return x * x;
  14. }
  15. int
  16. consume_stack_cb(int x) __attribute__((disable_tail_calls))
  17. {
  18. /*
  19. * intentions:
  20. *
  21. * - consume native stack by making recursive calls
  22. *
  23. * - avoid tail-call optimization (either by the C compiler or
  24. * aot-compiler)
  25. */
  26. if (x == 0) {
  27. return 0;
  28. }
  29. return consume_stack_cb(x - 1) + 1;
  30. }
  31. int
  32. host_consume_stack_cb(int x)
  33. {
  34. return host_consume_stack(x);
  35. }
  36. __attribute__((export_name("test1"))) uint32_t
  37. test1(uint32_t native_stack, uint32_t recurse_count)
  38. {
  39. /*
  40. * ------ os_thread_get_stack_boundary
  41. * ^
  42. * |
  43. * | "native_stack" bytes of stack left by
  44. * | host_consume_stack_and_call_indirect
  45. * |
  46. * | ^
  47. * | |
  48. * | | consume_stack_cb (interpreter or aot)
  49. * v |
  50. * ^
  51. * |
  52. * | host_consume_stack_and_call_indirect
  53. * |
  54. *
  55. *
  56. */
  57. uint32_t ret = host_consume_stack_and_call_indirect(
  58. consume_stack_cb, recurse_count, native_stack);
  59. return 42;
  60. }
  61. __attribute__((export_name("test2"))) uint32_t
  62. test2(uint32_t native_stack, uint32_t recurse_count)
  63. {
  64. uint32_t ret = host_consume_stack_and_call_indirect(host_consume_stack_cb,
  65. 6000, native_stack);
  66. return 42;
  67. }