Skip to content

Commit

Permalink
Xtensa: fix
Browse files Browse the repository at this point in the history
  • Loading branch information
imbillow committed Sep 25, 2024
1 parent a754f59 commit b0f26a9
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 11 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/auto-sync.yml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ jobs:
./src/autosync/ASUpdater.py -d -a LoongArch -s IncGen
./src/autosync/ASUpdater.py -d -a Mips -s IncGen
./src/autosync/ASUpdater.py -d -a SystemZ -s IncGen
./src/autosync/ASUpdater.py -d -a Xtensa -s IncGen
- name: CppTranslator - Patch tests
run: |
Expand All @@ -96,3 +97,4 @@ jobs:
./src/autosync/ASUpdater.py --ci -d -a LoongArch -s Translate
./src/autosync/ASUpdater.py --ci -d -a Mips -s Translate
./src/autosync/ASUpdater.py --ci -d -a SystemZ -s Translate
./src/autosync/ASUpdater.py --ci -d -a Xtensa -s Translate
2 changes: 1 addition & 1 deletion arch/Xtensa/XtensaModule.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ cs_err Xtensa_option(cs_struct *handle, cs_opt_type type, size_t value)
}

if (type == CS_OPT_LITBASE) {
handle->LITBASE = value;
handle->LITBASE = (uint32_t)value;
}

return CS_ERR_OK;
Expand Down
2 changes: 1 addition & 1 deletion cs_priv.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ struct cs_struct {
const uint8_t *regsize_map; // map to register size (x86-only for now)
GetRegisterAccess_t reg_access;
struct insn_mnem *mnem_list; // linked list of customized instruction mnemonic
size_t LITBASE;
uint32_t LITBASE;
};

#define MAX_ARCH CS_ARCH_MAX
Expand Down
1 change: 1 addition & 0 deletions suite/auto-sync/src/autosync/ASUpdater.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ def __init__(
USteps.INC_GEN,
USteps.TRANS,
USteps.DIFF,
USteps.MC,
USteps.PATCH_HEADER,
]
else:
Expand Down
1 change: 1 addition & 0 deletions suite/auto-sync/src/autosync/MCUpdater.py
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,7 @@ def gen_all(self):
log.info("Check prerequisites")
disas_tests = self.mc_dir.joinpath(f"Disassembler/{self.arch}")
test_paths = [disas_tests]
# Xtensa only defines assembly tests.
if self.arch == "Xtensa":
test_paths.append(self.mc_dir.joinpath(self.arch))
self.check_prerequisites(test_paths)
Expand Down
10 changes: 9 additions & 1 deletion tests/integration/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ cmake_minimum_required(VERSION 3.15)
# Old integration tests.
if (CAPSTONE_BUILD_LEGACY_TESTS)
enable_testing()
set(TEST_SOURCES test_skipdata.c test_iter.c test_litbase.c)
set(TEST_SOURCES test_skipdata.c test_iter.c)
if(CAPSTONE_X86_SUPPORT)
set(TEST_SOURCES ${TEST_SOURCES} test_customized_mnem.c)
endif()
Expand Down Expand Up @@ -32,3 +32,11 @@ add_test(NAME integration_compat_headers
COMMAND compat_header_build_test
WORKING_DIRECTORY ${COMPAT_C_TEST_DIR}
)

set(INTEGRATION_TEST_SRC test_litbase.c)
foreach(TSRC ${INTEGRATION_TEST_SRC})
string(REGEX REPLACE ".c$" "" TBIN ${TSRC})
add_executable(${TBIN} "${TESTS_INTEGRATION_DIR}/${TSRC}")
target_link_libraries(${TBIN} PRIVATE capstone)
add_test(NAME "legintegration_${TBIN}" COMMAND ${TBIN})
endforeach()
29 changes: 21 additions & 8 deletions tests/integration/test_litbase.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,8 @@ static void print_string_hex(unsigned char *str, size_t len)
printf("\t");
}

static void print_insn(csh handle)
static void print_insn(cs_insn *insn, size_t count)
{
cs_insn *insn;
size_t count;

count = cs_disasm(handle, (const uint8_t *)DATA, sizeof(DATA) - 1,
0x10000, 2, &insn);
if (count) {
for (int i = 0; i < count; ++i) {
print_string_hex((unsigned char *)DATA,
Expand All @@ -39,6 +34,12 @@ static void print_insn(csh handle)
}
}

static void check_insn(cs_insn *insn, const char *mnemonic, const char *op_str)
{
assert(strcmp(insn[0].mnemonic, mnemonic) == 0);
assert(strcmp(insn[0].op_str, op_str) == 0);
}

static void test()
{
csh handle;
Expand All @@ -54,16 +55,28 @@ static void test()
return;
}

cs_insn *insn = NULL;
size_t count = 0;

count = cs_disasm(handle, (const uint8_t *)DATA, sizeof(DATA) - 1,
0x10000, 2, &insn);

// 1. Print out the instruction in default setup.
printf("Disassemble xtensa code with PC=0x10000\n");
print_insn(handle);
check_insn(insn, "l32r", "a1, . 0xc000");
check_insn(insn + 1, "l32r", "a1, . 0x10000");
print_insn(insn, count);

// Customized mnemonic JNE to JNZ using CS_OPT_LITBASE option
printf("\nNow customize engine to change LITBASA to 0xff001\n");
cs_option(handle, CS_OPT_LITBASE, (size_t)0xff001);
count = cs_disasm(handle, (const uint8_t *)DATA, sizeof(DATA) - 1,
0x10000, 2, &insn);

// 2. Now print out the instruction in newly customized setup.
print_insn(handle);
check_insn(insn, "l32r", "a1, . -0x3fff");
check_insn(insn + 1, "l32r", "a1, . -3");
print_insn(insn, count);

// Done
cs_close(&handle);
Expand Down

0 comments on commit b0f26a9

Please sign in to comment.