Skip to content

Commit

Permalink
Add compatibility header test to CMake and CI
Browse files Browse the repository at this point in the history
  • Loading branch information
Rot127 committed Jul 24, 2024
1 parent 1b548b3 commit b4d57d9
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 25 deletions.
14 changes: 3 additions & 11 deletions .github/workflows/CITest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -97,18 +97,10 @@ jobs:
# Work-around ASAN bug https://github.com/google/sanitizers/issues/1716
sudo sysctl vm.mmap_rnd_bits=28
- name: "Compatibility header test build"
if: matrix.config.diet-build == 'OFF'
env:
asan: ${{ matrix.config.enable-asan }}
- name: "Compatibility header test"
if: startsWith(matrix.config.build-system, 'cmake') && matrix.config.diet-build == 'OFF'
run: |
cd "$(git rev-parse --show-toplevel)/suite/auto-sync/c_tests/"
if [ "$asan" = "ON" ]; then
clang -lcapstone -fsanitize=address src/test_arm64_compatibility_header.c -o test_arm64_compatibility_header
else
clang -lcapstone src/test_arm64_compatibility_header.c -o test_arm64_compatibility_header
fi
./test_arm64_compatibility_header
ctest --output-on-failure -R ASCompatibilityHeaderTest
- name: cstool - reaches disassembler engine
run: |
Expand Down
7 changes: 7 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -939,8 +939,15 @@ if(CAPSTONE_BUILD_CSTEST)
${LIBCYAML_INCLUDE_DIR}
)

# Unit tests for cstest
set(CSTEST_TEST_DIR ${PROJECT_SOURCE_DIR}/suite/cstest/test/)
add_subdirectory(${CSTEST_TEST_DIR})

# Unit tests for auto-sync
set(AUTO_SYNC_C_TEST_DIR ${PROJECT_SOURCE_DIR}/suite/auto-sync/c_tests/)
add_subdirectory(${AUTO_SYNC_C_TEST_DIR})

# Test targets
add_test(MCTests
cstest ${PROJECT_SOURCE_DIR}/tests/MC
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
Expand Down
16 changes: 16 additions & 0 deletions suite/auto-sync/c_tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
cmake_minimum_required(VERSION 3.15)

set(AUTO_SYNC_C_TEST_SRC_DIR ${AUTO_SYNC_C_TEST_DIR}/src)
set(AUTO_SYNC_C_TEST_INC_DIR ${AUTO_SYNC_C_TEST_DIR}/include)

include_directories(${AUTO_SYNC_C_TEST_INC_DIR} ${PROJECT_SOURCE_DIR}/include)

file(GLOB AUTO_SYNC_C_SRC ${AUTO_SYNC_C_TEST_SRC_DIR}/*.c)
add_executable(compat_header_build_test ${AUTO_SYNC_C_SRC})
add_dependencies(compat_header_build_test libcstest)
target_link_libraries(compat_header_build_test PUBLIC libcstest)

add_test(NAME ASCompatibilityHeaderTest
COMMAND compat_header_build_test
WORKING_DIRECTORY ${AUTO_SYNC_C_TEST_DIR}
)
55 changes: 41 additions & 14 deletions suite/auto-sync/c_tests/src/test_arm64_compatibility_header.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,29 +12,56 @@ int main(void)
csh handle;

if (cs_open(CS_ARCH_ARM64, CS_MODE_BIG_ENDIAN, &handle) != CS_ERR_OK) {
printf("cs_open failed\n");
fprintf(stderr, "cs_open failed\n");
return -1;
}

cs_option(handle, CS_OPT_DETAIL, CS_OPT_ON);

cs_insn *insn;
uint8_t bytes[] = "0x1a,0x48,0xa0,0xf8";
size_t count = cs_disasm(handle, bytes, sizeof(bytes), 0x1000, 1, &insn);
if (count > 0) {
printf("0x%" PRIx64 ":\t%s\t\t%s\n", insn[0].address,
insn[0].mnemonic, insn[0].op_str);
printf("A register = %s\n", cs_reg_name(handle, insn[0].detail->arm64.operands[0].reg));
printf("An imm = 0x%" PRIx64 "\n", insn[0].detail->arm64.operands[1].imm);

cs_free(insn, count);
} else {
printf("ERROR: Failed to disassemble given code!\n");
cs_close(&handle);
return -1;
size_t count =
cs_disasm(handle, bytes, sizeof(bytes), 0x1000, 1, &insn);
if (count != 1) {
fprintf(stderr, "Failed to disassemble code.\n");
goto err;
}
printf("0x%" PRIx64 ":\t%s\t\t%s\n", insn[0].address, insn[0].mnemonic,
insn[0].op_str);
printf("A register = %s\n",
cs_reg_name(handle, insn[0].detail->arm64.operands[0].reg));
printf("An imm = 0x%" PRIx64 "\n",
insn[0].detail->arm64.operands[1].imm);

cs_close(&handle);
if (insn[0].address != 0x1000) {
fprintf(stderr, "Address wrong.\n");
goto err;
}
if (strcmp(insn[0].mnemonic, "adr") != 0) {
fprintf(stderr, "Mnemonic wrong.\n");
goto err;
}
if (strcmp(insn[0].op_str, "x1, 0xf162d") != 0) {
fprintf(stderr, "op_str wrong.\n");
goto err;
}
if (strcmp(cs_reg_name(handle, insn[0].detail->arm64.operands[0].reg),
"x1") != 0) {
fprintf(stderr, "register wrong.\n");
goto err;
}
if (insn[0].detail->arm64.operands[1].imm != 0xf162d) {
fprintf(stderr, "Immediate wrong.\n");
goto err;
}

cs_free(insn, count);
cs_close(&handle);
return 0;

err:
printf("ERROR: Failed to disassemble given code corrcetly!\n");
cs_free(insn, count);
cs_close(&handle);
return -1;
}

0 comments on commit b4d57d9

Please sign in to comment.