run.sh 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. #!/bin/bash
  2. #
  3. # Copyright (C) 2019 Intel Corporation. All rights reserved.
  4. # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  5. #
  6. # Test for x18 register reservation on macOS ARM64 (aarch64).
  7. #
  8. # On macOS ARM64, x18 is reserved by Apple for TLS (Thread Local Storage).
  9. # Without the +reserve-x18 LLVM flag, the AOT compiler may generate code
  10. # that uses x18, causing random SIGSEGV crashes when run on macOS.
  11. #
  12. # This test compiles a WASM module that stresses register allocation
  13. # (forcing x18 usage without the fix) and runs it 1000 times to verify
  14. # no crashes occur.
  15. #
  16. SCRIPT_DIR=$(cd "$(dirname "$0")" && pwd)
  17. WAMR_DIR="${SCRIPT_DIR}/../../.."
  18. # Detect platform
  19. UNAME_S=$(uname -s)
  20. UNAME_M=$(uname -m)
  21. # Only run this test on macOS ARM64
  22. if [[ "${UNAME_S}" != "Darwin" ]] || [[ "${UNAME_M}" != "arm64" ]]; then
  23. echo "Skipping x18 reserve test: only applicable on macOS ARM64"
  24. echo "Current platform: ${UNAME_S} ${UNAME_M}"
  25. exit 0
  26. fi
  27. # Determine iwasm path based on platform
  28. if [[ "${UNAME_S}" == "Darwin" ]]; then
  29. IWASM_CMD="${WAMR_DIR}/product-mini/platforms/darwin/build/iwasm"
  30. else
  31. IWASM_CMD="${WAMR_DIR}/product-mini/platforms/linux/build/iwasm"
  32. fi
  33. WAMRC_CMD="${WAMR_DIR}/wamr-compiler/build/wamrc"
  34. # Check if required binaries exist
  35. if [[ ! -x "${IWASM_CMD}" ]]; then
  36. echo "Error: iwasm not found at ${IWASM_CMD}"
  37. echo "Please build iwasm first"
  38. exit 1
  39. fi
  40. if [[ ! -x "${WAMRC_CMD}" ]]; then
  41. echo "Error: wamrc not found at ${WAMRC_CMD}"
  42. echo "Please build wamrc first"
  43. exit 1
  44. fi
  45. cd "${SCRIPT_DIR}"
  46. # Find wat2wasm (check CI path first, then system PATH)
  47. if [[ -x "/opt/wabt/bin/wat2wasm" ]]; then
  48. WAT2WASM="/opt/wabt/bin/wat2wasm"
  49. elif command -v wat2wasm &> /dev/null; then
  50. WAT2WASM="wat2wasm"
  51. else
  52. echo "Error: wat2wasm not found"
  53. echo "Please install wabt tools"
  54. exit 1
  55. fi
  56. # Compile WAT to WASM if needed
  57. if [[ ! -f stress_registers.wasm ]] || [[ stress_registers.wat -nt stress_registers.wasm ]]; then
  58. echo "Compiling stress_registers.wat to WASM..."
  59. if ! ${WAT2WASM} stress_registers.wat -o stress_registers.wasm; then
  60. echo "Error: Failed to compile WAT to WASM"
  61. exit 1
  62. fi
  63. fi
  64. if [[ $1 != "--aot" ]]; then
  65. echo "============> run stress_registers.wasm (interpreter mode)"
  66. echo "Running 1000 iterations in interpreter mode..."
  67. for i in $(seq 1 1000); do
  68. if ! ${IWASM_CMD} stress_registers.wasm 2>&1; then
  69. echo "FAILED: Crash at iteration $i"
  70. exit 1
  71. fi
  72. done
  73. echo "PASSED: 1000 iterations completed without crash"
  74. else
  75. echo "============> compile stress_registers.wasm to AOT"
  76. # Compile to AOT - the fix should add +reserve-x18 automatically on macOS ARM64
  77. if ! ${WAMRC_CMD} --opt-level=3 -o stress_registers.aot stress_registers.wasm; then
  78. echo "Error: Failed to compile WASM to AOT"
  79. exit 1
  80. fi
  81. echo "============> run stress_registers.aot"
  82. echo "Running 1000 iterations to verify x18 is properly reserved..."
  83. echo "(Without the fix, this would crash ~80% of the time)"
  84. failed=0
  85. for i in $(seq 1 1000); do
  86. if ! ${IWASM_CMD} stress_registers.aot 2>&1 > /dev/null; then
  87. echo "FAILED: Crash at iteration $i"
  88. failed=1
  89. break
  90. fi
  91. # Progress indicator every 100 iterations
  92. if [[ $((i % 100)) -eq 0 ]]; then
  93. echo " Progress: $i/1000 iterations completed"
  94. fi
  95. done
  96. if [[ ${failed} -eq 0 ]]; then
  97. echo "PASSED: 1000 iterations completed without crash"
  98. echo "The +reserve-x18 fix is working correctly"
  99. exit 0
  100. else
  101. echo "FAILED: x18 register corruption detected"
  102. echo "The +reserve-x18 fix may not be applied correctly"
  103. exit 1
  104. fi
  105. fi