Skip to content

Commit

Permalink
Test
Browse files Browse the repository at this point in the history
  • Loading branch information
imbillow committed Nov 25, 2023
1 parent d432c35 commit 209f76f
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 1 deletion.
11 changes: 10 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# For MSVC_RUNTIME_LIBRARY
cmake_minimum_required(VERSION 3.15)
cmake_minimum_required(VERSION 3.25)

if(CMAKE_SOURCE_DIR STREQUAL CMAKE_BINARY_DIR)
message(FATAL_ERROR "In-tree builds are not supported. Run CMake from a separate directory: cmake -B build")
Expand Down Expand Up @@ -785,3 +785,12 @@ if(CAPSTONE_BUILD_CSTEST)
install(TARGETS cstest EXPORT capstone-targets DESTINATION ${CMAKE_INSTALL_BINDIR})
endif()
endif()

find_package(folly REQUIRED)

add_executable(cs_tricore_test tests/test_tricore.cpp)
set_property(TARGET cs_tricore_test PROPERTY CXX_STANDARD 26)
target_link_libraries(cs_tricore_test PRIVATE capstone ${FOLLY_LIBRARIES})
target_include_directories(cs_tricore_test PRIVATE
<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
${FOLLY_INCLUDE_DIRS})
99 changes: 99 additions & 0 deletions tests/test_tricore.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
//
// Created by hoshinoaya on 11/24/23.
//

#include "test_tricore.h"
#include <capstone/capstone.h>
#include <capstone/tricore.h>
#include <folly/Executor.h>
#include <folly/executors/CPUThreadPoolExecutor.h>
#include <folly/ThreadCachedInt.h>
#include <folly/synchronization/Latch.h>

static void print_insn_detail(csh handle, cs_insn *ins)
{
cs_tricore *tricore;
int i;

// detail can be NULL on "data" instruction if SKIPDATA option is turned ON
if (ins->detail == NULL)
return;

tricore = &(ins->detail->tricore);
if (tricore->op_count)
printf("\top_count: %u\n", tricore->op_count);

for (i = 0; i < tricore->op_count; i++) {
cs_tricore_op *op = &(tricore->operands[i]);
switch ((int)op->type) {
default:
break;
case TRICORE_OP_REG:
printf("\t\toperands[%u].type: REG = %s\n", i,
cs_reg_name(handle, op->reg));
break;
case TRICORE_OP_IMM:
printf("\t\toperands[%u].type: IMM = 0x%x\n", i,
op->imm);
break;
case TRICORE_OP_MEM:
printf("\t\toperands[%u].type: MEM\n", i);
if (op->mem.base != TRICORE_REG_INVALID)
printf("\t\t\toperands[%u].mem.base: REG = %s\n",
i, cs_reg_name(handle, op->mem.base));
if (op->mem.disp != 0)
printf("\t\t\toperands[%u].mem.disp: 0x%x\n", i,
op->mem.disp);

break;
}
}

printf("\n");
}

int main()
{
auto q = folly::CPUThreadPoolExecutor(60);
folly::ThreadCachedInt<uint32_t> counter;
for (; counter.readFull() < std::numeric_limits<uint32_t>::max();
counter += 1) {
auto f = [&]() -> void {
csh handle;
uint64_t address = 0x1000;
cs_insn *insn;
size_t count;
cs_err err = cs_open(CS_ARCH_TRICORE,
CS_MODE_TRICORE_162, &handle);
if (err) {
printf("Failed on cs_open() with error returned: %u\n",
err);
}
cs_option(handle, CS_OPT_DETAIL, CS_OPT_ON);

uint32_t x = counter.readFull();
count = cs_disasm(handle,
reinterpret_cast<const uint8_t *>(&x),
sizeof(x), address, 0, &insn);
if (count) {
size_t j;
for (j = 0; j < count; j++) {
printf("0x%" PRIx64 ":\t%s\t%s\n",
insn[j].address,
insn[j].mnemonic,
insn[j].op_str);
print_insn_detail(handle, &insn[j]);
}
printf("0x%" PRIx64 ":\n",
insn[j - 1].address + insn[j - 1].size);

// free memory allocated by cs_disasm()
cs_free(insn, count);
}
cs_close(&handle);
};
q.add(f);
}
q.join();
return 0;
}
8 changes: 8 additions & 0 deletions tests/test_tricore.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
//
// Created by hoshinoaya on 11/24/23.
//

#ifndef CAPSTONE_TEST_TRICORE_H
#define CAPSTONE_TEST_TRICORE_H

#endif //CAPSTONE_TEST_TRICORE_H

0 comments on commit 209f76f

Please sign in to comment.