forked from capstone-engine/capstone
-
Notifications
You must be signed in to change notification settings - Fork 0
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
9 changed files
with
137 additions
and
62 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
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
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 |
---|---|---|
@@ -1,7 +1,6 @@ | ||
/* Capstone testing regression */ | ||
/* By Do Minh Tuan <[email protected]>, 02-2019 */ | ||
|
||
|
||
#include "../../../cs_priv.h" | ||
#include "cstool.h" | ||
#include "capstone_test.h" | ||
|
@@ -39,28 +38,35 @@ void test_single_MC(csh *handle, int mc_mode, char *line) | |
code[i] = (unsigned char)strtol(list_byte[i], NULL, 16); | ||
} | ||
|
||
((struct cs_struct *)(uintptr_t)*handle)->PrintBranchImmNotAsAddress = true; | ||
((struct cs_struct *)(uintptr_t)*handle)->PrintBranchImmNotAsAddress = | ||
true; | ||
count = cs_disasm(*handle, code, size_byte, offset, 0, &insn); | ||
if (count == 0) { | ||
fprintf(stderr, "[ ERROR ] --- %s --- Failed to disassemble given code!\n", list_part[0]); | ||
fprintf(stderr, | ||
"[ ERROR ] --- %s --- Failed to disassemble given code!\n", | ||
list_part[0]); | ||
free_strs(list_part, size_part); | ||
free_strs(offset_opcode, size_offset_opcode); | ||
free_strs(list_byte, size_byte); | ||
free(code); | ||
_fail(__FILE__, __LINE__); | ||
} | ||
if (count > 1) { | ||
fprintf(stderr, "[ ERROR ] --- %s --- Multiple instructions(%d) disassembling doesn't support!\n", list_part[0], count); | ||
fprintf(stderr, | ||
"[ ERROR ] --- %s --- Multiple instructions(%d) disassembling doesn't support!\n", | ||
list_part[0], count); | ||
free_strs(list_part, size_part); | ||
free_strs(offset_opcode, size_offset_opcode); | ||
free_strs(list_byte, size_byte); | ||
free(code); | ||
_fail(__FILE__, __LINE__); | ||
} | ||
|
||
for (p = list_part[1]; *p; ++p) *p = tolower(*p); | ||
for (p = list_part[1]; *p; ++p) | ||
if (*p == '\t') *p = ' '; | ||
*p = tolower(*p); | ||
for (p = list_part[1]; *p; ++p) | ||
if (*p == '\t') | ||
*p = ' '; | ||
trim_str(list_part[1]); | ||
strcpy(tmp_mc, list_part[1]); | ||
replace_hex(tmp_mc); | ||
|
@@ -78,7 +84,8 @@ void test_single_MC(csh *handle, int mc_mode, char *line) | |
replace_hex(tmp); | ||
replace_negative(tmp, mc_mode); | ||
replace_tabs(tmp); | ||
for (p = tmp; *p; ++p) *p = tolower(*p); | ||
for (p = tmp; *p; ++p) | ||
*p = tolower(*p); | ||
|
||
// Skip ARM because the duplicate disassembly messes with the IT/VPT states | ||
// and laeds to wrong results. | ||
|
@@ -89,15 +96,19 @@ void test_single_MC(csh *handle, int mc_mode, char *line) | |
strcpy(tmp_noreg, insn[0].mnemonic); | ||
if (strlen(insn[0].op_str) > 0) { | ||
tmp_noreg[strlen(insn[0].mnemonic)] = ' '; | ||
strcpy(tmp_noreg + strlen(insn[0].mnemonic) + 1, insn[0].op_str); | ||
strcpy(tmp_noreg + strlen(insn[0].mnemonic) + 1, | ||
insn[0].op_str); | ||
} | ||
|
||
trim_str(tmp_noreg); | ||
replace_hex(tmp_noreg); | ||
replace_negative(tmp_noreg, mc_mode); | ||
|
||
if (strcmp(tmp, tmp_mc) && strcmp(tmp_noreg, tmp_mc)) { | ||
fprintf(stderr, "[ ERROR ] --- %s --- \"%s\" != \"%s\" ( \"%s\" != \"%s\" and \"%s\" != \"%s\" )\n", list_part[0], origin, list_part[1], tmp, tmp_mc, tmp_noreg, tmp_mc); | ||
fprintf(stderr, | ||
"[ ERROR ] --- %s --- \"%s\" != \"%s\" ( \"%s\" != \"%s\" and \"%s\" != \"%s\" )\n", | ||
list_part[0], origin, list_part[1], tmp, tmp_mc, | ||
tmp_noreg, tmp_mc); | ||
free_strs(list_part, size_part); | ||
free_strs(offset_opcode, size_offset_opcode); | ||
free_strs(list_byte, size_byte); | ||
|
@@ -106,7 +117,9 @@ void test_single_MC(csh *handle, int mc_mode, char *line) | |
_fail(__FILE__, __LINE__); | ||
} | ||
} else if (strcmp(tmp, tmp_mc)) { | ||
fprintf(stderr, "[ ERROR ] --- %s --- \"%s\" != \"%s\" ( \"%s\" != \"%s\" )\n", list_part[0], origin, list_part[1], tmp, tmp_mc); | ||
fprintf(stderr, | ||
"[ ERROR ] --- %s --- \"%s\" != \"%s\" ( \"%s\" != \"%s\" )\n", | ||
list_part[0], origin, list_part[1], tmp, tmp_mc); | ||
free_strs(list_part, size_part); | ||
free_strs(offset_opcode, size_offset_opcode); | ||
free_strs(list_byte, size_byte); | ||
|
@@ -155,8 +168,8 @@ void test_single_issue(csh *handle, cs_mode mode, char *line, int detail) | |
int size_offset_opcode; | ||
unsigned long offset; | ||
|
||
Stream steam = {0}; | ||
stream_init_sstream(&steam); | ||
Stream steam = { 0 }; | ||
stream_init_stream(&steam); | ||
list_part = split(line, " == ", &size_part); | ||
|
||
offset_opcode = split(list_part[0], ": ", &size_offset_opcode); | ||
|
@@ -177,39 +190,51 @@ void test_single_issue(csh *handle, cs_mode mode, char *line, int detail) | |
for (i = 0; i < count; ++i) { | ||
stream_print(&steam, insn[i].mnemonic); | ||
if (strlen(insn[i].op_str) > 0) { | ||
stream_print(&steam, " "); | ||
stream_print(&steam, insn[i].op_str); | ||
stream_printf(&steam, " %s", insn[i].op_str); | ||
} | ||
if (detail && insn->detail) { | ||
stream_print(&steam, "\n"); | ||
print_insn_detail(*handle, ((cs_struct *)*handle)->arch, | ||
((cs_struct *)*handle)->mode, | ||
insn + i, &steam); | ||
} | ||
} | ||
|
||
if (detail == 1) { | ||
print_insn_detail(*handle, ((cs_struct*)handle)->arch, ((cs_struct*)handle)->mode, insn, &steam); | ||
|
||
if (detail == 1 && insn->detail) { | ||
if (insn->detail->groups_count) { | ||
stream_print(&steam, " ; Groups: "); | ||
for (j = 0; j < insn->detail->groups_count; j++) { | ||
stream_printf(&steam, "%s ", cs_group_name(*handle, insn->detail->groups[j])); | ||
stream_printf( | ||
&steam, "%s ", | ||
cs_group_name(*handle, | ||
insn->detail->groups[j])); | ||
} | ||
} | ||
} | ||
|
||
stream_rtrim(&steam); | ||
// list_part_cs_result = split(cs_result, " ; ", &size_part_cs_result); | ||
for (p = list_part[1]; *p; ++p) if (*p == '\t') *p = ' '; | ||
list_part_issue_result = split(list_part[1], " ; ", &size_part_issue_result); | ||
for (p = list_part[1]; *p; ++p) | ||
if (*p == '\t') | ||
*p = ' '; | ||
list_part_issue_result = | ||
split(list_part[1], " ; ", &size_part_issue_result); | ||
|
||
for (i = 0; i < size_part_issue_result; ++i) { | ||
trim_str(list_part_issue_result[i]); | ||
|
||
if ((strstr(stream_as_str(&steam), list_part_issue_result[i])) == NULL) { | ||
fprintf(stderr, "[ ERROR ] --- %s --- \"%s\" not in \"%s\"\n", | ||
list_part[0], list_part_issue_result[i], stream_as_str(&steam)); | ||
if ((strstr(stream_as_str(&steam), | ||
list_part_issue_result[i])) == NULL) { | ||
fprintf(stderr, | ||
"[ ERROR ] --- %s --- \"%s\" not in \"%s\"\n", | ||
list_part[0], list_part_issue_result[i], | ||
stream_as_str(&steam)); | ||
cs_free(insn, count); | ||
free_strs(list_part, size_part); | ||
free_strs(list_byte, size_byte); | ||
stream_close(&steam); | ||
// free_strs(list_part_cs_result, size_part_cs_result); | ||
free_strs(list_part_issue_result, size_part_issue_result); | ||
free_strs(list_part_issue_result, | ||
size_part_issue_result); | ||
_fail(__FILE__, __LINE__); | ||
} | ||
} | ||
|
Oops, something went wrong.