forked from capstone-engine/capstone
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
1,832 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
// Copyright © 2024 Rot127 <unisono@quyllur.org> | ||
// SPDX-License-Identifier: BSD-3 | ||
|
||
#ifndef TEST_DETAIL_RISCV_H | ||
#define TEST_DETAIL_RISCV_H | ||
|
||
#include "test_compare.h" | ||
#include <cyaml/cyaml.h> | ||
#include <capstone/capstone.h> | ||
|
||
typedef struct { | ||
char *type; | ||
char *access; | ||
|
||
char *reg; | ||
uint64_t imm; | ||
char *mem_base; | ||
int64_t mem_disp; | ||
} TestDetailRISCVOp; | ||
|
||
static const cyaml_schema_field_t test_detail_riscv_op_mapping_schema[] = { | ||
CYAML_FIELD_STRING_PTR("type", CYAML_FLAG_POINTER | CYAML_FLAG_OPTIONAL, | ||
TestDetailRISCVOp, type, 0, CYAML_UNLIMITED), | ||
CYAML_FIELD_STRING_PTR("access", CYAML_FLAG_POINTER | CYAML_FLAG_OPTIONAL, | ||
TestDetailRISCVOp, access, 0, CYAML_UNLIMITED), | ||
CYAML_FIELD_STRING_PTR("reg", CYAML_FLAG_POINTER | CYAML_FLAG_OPTIONAL, | ||
TestDetailRISCVOp, reg, 0, CYAML_UNLIMITED), | ||
CYAML_FIELD_INT("imm", CYAML_FLAG_OPTIONAL, TestDetailRISCVOp, imm), | ||
CYAML_FIELD_STRING_PTR("mem_base", | ||
CYAML_FLAG_POINTER | CYAML_FLAG_OPTIONAL, | ||
TestDetailRISCVOp, mem_base, 0, CYAML_UNLIMITED), | ||
CYAML_FIELD_INT("mem_disp", CYAML_FLAG_OPTIONAL, TestDetailRISCVOp, | ||
mem_disp), | ||
CYAML_FIELD_END | ||
}; | ||
|
||
static const cyaml_schema_value_t test_detail_riscv_op_schema = { | ||
CYAML_VALUE_MAPPING(CYAML_FLAG_POINTER, TestDetailRISCVOp, | ||
test_detail_riscv_op_mapping_schema), | ||
}; | ||
|
||
typedef struct { | ||
TestDetailRISCVOp **operands; | ||
uint32_t operands_count; | ||
} TestDetailRISCV; | ||
|
||
static const cyaml_schema_field_t test_detail_riscv_mapping_schema[] = { | ||
CYAML_FIELD_SEQUENCE( | ||
"operands", CYAML_FLAG_POINTER | CYAML_FLAG_OPTIONAL, | ||
TestDetailRISCV, operands, &test_detail_riscv_op_schema, 0, | ||
CYAML_UNLIMITED), // 0-MAX options | ||
CYAML_FIELD_END | ||
}; | ||
|
||
TestDetailRISCV *test_detail_riscv_new(); | ||
TestDetailRISCV *test_detail_riscv_clone(const TestDetailRISCV *detail); | ||
void test_detail_riscv_free(TestDetailRISCV *detail); | ||
|
||
TestDetailRISCVOp *test_detail_riscv_op_new(); | ||
TestDetailRISCVOp *test_detail_riscv_op_clone(const TestDetailRISCVOp *detail); | ||
void test_detail_riscv_op_free(TestDetailRISCVOp *detail); | ||
|
||
bool test_expected_riscv(csh *handle, const cs_riscv *actual, | ||
const TestDetailRISCV *expected); | ||
|
||
#endif // TEST_DETAIL_RISCV_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
// Copyright © 2024 Rot127 <unisono@quyllur.org> | ||
// SPDX-License-Identifier: BSD-3 | ||
|
||
#include "test_compare.h" | ||
#include "test_detail_riscv.h" | ||
#include <capstone/capstone.h> | ||
#include <stdio.h> | ||
#include <string.h> | ||
|
||
TestDetailRISCV *test_detail_riscv_new() | ||
{ | ||
return cs_mem_calloc(sizeof(TestDetailRISCV), 1); | ||
} | ||
|
||
void test_detail_riscv_free(TestDetailRISCV *detail) | ||
{ | ||
if (!detail) { | ||
return; | ||
} | ||
for (size_t i = 0; i < detail->operands_count; ++i) { | ||
test_detail_riscv_op_free(detail->operands[i]); | ||
} | ||
cs_mem_free(detail->operands); | ||
cs_mem_free(detail); | ||
} | ||
|
||
TestDetailRISCV *test_detail_riscv_clone(const TestDetailRISCV *detail) | ||
{ | ||
TestDetailRISCV *clone = test_detail_riscv_new(); | ||
|
||
clone->operands_count = detail->operands_count; | ||
if (detail->operands_count > 0) { | ||
clone->operands = cs_mem_calloc(sizeof(TestDetailRISCVOp), | ||
detail->operands_count); | ||
} | ||
for (size_t i = 0; i < detail->operands_count; ++i) { | ||
clone->operands[i] = | ||
test_detail_riscv_op_clone(detail->operands[i]); | ||
} | ||
|
||
return clone; | ||
} | ||
|
||
TestDetailRISCVOp *test_detail_riscv_op_new() | ||
{ | ||
return cs_mem_calloc(sizeof(TestDetailRISCVOp), 1); | ||
} | ||
|
||
TestDetailRISCVOp *test_detail_riscv_op_clone(const TestDetailRISCVOp *op) | ||
{ | ||
TestDetailRISCVOp *clone = test_detail_riscv_op_new(); | ||
|
||
clone->type = op->type ? strdup(op->type) : NULL; | ||
clone->access = op->access ? strdup(op->access) : NULL; | ||
clone->reg = op->reg ? strdup(op->reg) : NULL; | ||
clone->imm = op->imm; | ||
clone->mem_base = op->mem_base ? strdup(op->mem_base) : NULL; | ||
clone->mem_disp = op->mem_disp; | ||
|
||
return clone; | ||
} | ||
|
||
void test_detail_riscv_op_free(TestDetailRISCVOp *op) | ||
{ | ||
if (!op) { | ||
return; | ||
} | ||
cs_mem_free(op->type); | ||
cs_mem_free(op->access); | ||
cs_mem_free(op->reg); | ||
cs_mem_free(op->mem_base); | ||
cs_mem_free(op); | ||
} | ||
|
||
bool test_expected_riscv(csh *handle, const cs_riscv *actual, | ||
const TestDetailRISCV *expected) | ||
{ | ||
assert(handle && actual && expected); | ||
|
||
compare_uint8_ret(actual->op_count, expected->operands_count, false); | ||
for (size_t i = 0; i < actual->op_count; ++i) { | ||
const cs_riscv_op *op = &actual->operands[i]; | ||
TestDetailRISCVOp *eop = expected->operands[i]; | ||
compare_enum_ret(op->type, eop->type, false); | ||
compare_enum_ret(op->access, eop->access, false); | ||
switch (op->type) { | ||
default: | ||
fprintf(stderr, | ||
"sh op type %" PRId32 " not handled.\n", | ||
op->type); | ||
return false; | ||
case RISCV_OP_REG: | ||
compare_reg_ret(*handle, op->reg, eop->reg, false); | ||
break; | ||
case RISCV_OP_IMM: | ||
compare_uint64_ret(op->imm, eop->imm, false); | ||
break; | ||
case RISCV_OP_MEM: | ||
compare_reg_ret(*handle, op->mem.base, eop->mem_base, | ||
false); | ||
compare_int64_ret(op->mem.disp, eop->mem_disp, false); | ||
break; | ||
} | ||
} | ||
|
||
return true; | ||
} |
Oops, something went wrong.