Skip to content

Commit

Permalink
Sync from rizin branch
Browse files Browse the repository at this point in the history
  • Loading branch information
Rot127 committed Jan 17, 2024
1 parent 1aca4d4 commit 47ce8d6
Show file tree
Hide file tree
Showing 5 changed files with 154 additions and 140 deletions.
80 changes: 38 additions & 42 deletions handwritten/asm_hexagon_c/initialization.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,28 @@
// SPDX-License-Identifier: LGPL-3.0-only

static RZ_OWN RzPVector /*<RzAsmTokenPattern *>*/ *get_token_patterns() {
static RzPVector *pvec = NULL;
if (pvec) {
return pvec;
}

pvec = rz_pvector_new(rz_asm_token_pattern_free);
RzPVector *pvec = rz_pvector_new(rz_asm_token_pattern_free);

RzAsmTokenPattern *pat = RZ_NEW0(RzAsmTokenPattern);
pat->type = RZ_ASM_TOKEN_META;
pat->pattern = strdup(
"^[\\[\\?\\/\\|\\\\\\{┌│└]|" // Packet prefix
"(|[<\\}])[\\s:]endloop[01]{1,2}" // Endloop markers
"(^[\\[\\?\\/\\|\\\\\\{])|(┌)|(│)|(└)|" // Packet prefix
"((∎)|[<\\}])([ :])(endloop[01]{1,2})" // Endloop markers
);
rz_pvector_push(pvec, pat);

pat = RZ_NEW0(RzAsmTokenPattern);
pat->type = RZ_ASM_TOKEN_META;
pat->pattern = strdup(
"\\#{1,2}|\\}$|" // Immediate prefix, Closing packet bracket
"(#{1,2})|(\\}$)|" // Immediate prefix, Closing packet bracket
"\\.new|:n?t|:raw|<err>" // .new and jump hints
);
rz_pvector_push(pvec, pat);

pat = RZ_NEW0(RzAsmTokenPattern);
pat->type = RZ_ASM_TOKEN_REGISTER;
pat->pattern = strdup(
"[CNPRMQVO]\\d{1,2}(:\\d{1,2})?(in)?" // Registers and double registers
"([CNPRMQVO][[:digit:]]{1,2}(:[[:digit:]]{1,2})?(in)?)" // Registers and double registers
);
rz_pvector_push(pvec, pat);

Expand All @@ -42,74 +37,57 @@ static RZ_OWN RzPVector /*<RzAsmTokenPattern *>*/ *get_token_patterns() {
pat = RZ_NEW0(RzAsmTokenPattern);
pat->type = RZ_ASM_TOKEN_NUMBER;
pat->pattern = strdup(
"0x(\\d|[abcdef])+" // Hexadecimal numbers
"(0x[[:digit:]abcdef]+)" // Hexadecimal numbers
);
rz_pvector_push(pvec, pat);

pat = RZ_NEW0(RzAsmTokenPattern);
pat->type = RZ_ASM_TOKEN_MNEMONIC;
pat->pattern = strdup(
"[a-zA-Z]+\\d+[a-zA-Z]*" // Mnemonics with a decimal number in the name.
"([[:alpha:]]+[[:digit:]]+[[:alpha:]]*)" // Mnemonics with a decimal number in the name.
);
rz_pvector_push(pvec, pat);

pat = RZ_NEW0(RzAsmTokenPattern);
pat->type = RZ_ASM_TOKEN_NUMBER;
pat->pattern = strdup(
"\\d+" // Decimal numbers
"([[:digit:]]+)" // Decimal numbers
);
rz_pvector_push(pvec, pat);

pat = RZ_NEW0(RzAsmTokenPattern);
pat->type = RZ_ASM_TOKEN_SEPARATOR;
pat->pattern = strdup(
"\\s+|" // Spaces and tabs
"[,;\\.\\(\\)\\{\\}:]" // Brackets and others
"([[:blank:]]+)|" // Spaces and tabs
"([,;\\.\\(\\)\\{\\}:])" // Brackets and others
);
rz_pvector_push(pvec, pat);

pat = RZ_NEW0(RzAsmTokenPattern);
pat->type = RZ_ASM_TOKEN_OPERATOR;
pat->pattern = strdup(
"[\\+=!-]" // +,-,=,],[, ! (not the packet prefix)
"(\\+)|([*&+-\\&^/]?=)|(!)|(-)" // +,-,=,],[, ! (not the packet prefix)
);
rz_pvector_push(pvec, pat);

pat = RZ_NEW0(RzAsmTokenPattern);
pat->type = RZ_ASM_TOKEN_OPERATOR;
pat->pattern = strdup(
"\\]|\\[|<{1,2}|>{1,2}" // +,-,=,],[, ! (not the packet prefix)
"(\\])|(\\[|<{1,2}|>{1,2})" // +,-,=,],[, ! (not the packet prefix)
);
rz_pvector_push(pvec, pat);

pat = RZ_NEW0(RzAsmTokenPattern);
pat->type = RZ_ASM_TOKEN_MNEMONIC;
pat->pattern = strdup(
"\\w+_\\w+|" // Mnemonics with "_" e.g dealloc_return
"\\w+" // Alphanumeric mnemonics
"([[:alnum:]]+)|" // Alphanumeric mnemonics
"([[:alnum:]]+_[[:alnum:]]+)" // Menmonics with "_" e.g dealloc_return
);
rz_pvector_push(pvec, pat);

return pvec;
}

static void compile_token_patterns(RZ_INOUT RzPVector /*<RzAsmTokenPattern *>*/ *patterns) {
rz_return_if_fail(patterns);

void **it;
rz_pvector_foreach (patterns, it) {
RzAsmTokenPattern *pat = *it;
if (pat->regex) {
continue;
}
pat->regex = rz_regex_new(pat->pattern, "e");
if (!pat->regex) {
RZ_LOG_WARN("Did not compile regex pattern %s.\n", pat->pattern);
rz_warn_if_reached();
}
}
}

/**
* \brief Setter for the plugins RzConfig nodes.
*
Expand All @@ -121,7 +99,7 @@ static void compile_token_patterns(RZ_INOUT RzPVector /*<RzAsmTokenPattern *>*/
*/
static bool hex_cfg_set(void *user, void *data) {
rz_return_val_if_fail(user && data, false);
HexState *state = hexagon_get_state();
HexState *state = hexagon_state(false);
if (!state) {
return false;
}
Expand All @@ -140,11 +118,26 @@ static bool hex_cfg_set(void *user, void *data) {
return false;
}

RZ_IPI void hexagon_state_fini(HexState *state) {
if (!state) {
return;
}
rz_config_free(state->cfg);
rz_pvector_free(state->token_patterns);
rz_list_free(state->const_ext_l);
return;
}

static bool hexagon_fini(void *user) {
hexagon_state_fini(hexagon_state(false));
hexagon_state(true);
return true;
}

static bool hexagon_init(void **user) {
HexState *state = hexagon_get_state();
HexState *state = hexagon_state(false);
rz_return_val_if_fail(state, false);

*user = state; // user = RzAsm.plugin_data
state->cfg = rz_config_new(state);
rz_return_val_if_fail(state->cfg, false);

Expand All @@ -155,14 +148,16 @@ static bool hexagon_init(void **user) {
SETCB("plugins.hexagon.sdk", "false", &hex_cfg_set, "Print packet syntax in objdump style.");
SETCB("plugins.hexagon.reg.alias", "true", &hex_cfg_set, "Print the alias of registers (Alias from C0 = SA0).");

state->token_patterns = get_token_patterns();
compile_token_patterns(state->token_patterns);
if (!state->token_patterns) {
state->token_patterns = get_token_patterns();
}
rz_asm_compile_token_patterns(state->token_patterns);

return true;
}

RZ_API RZ_BORROW RzConfig *hexagon_get_config() {
HexState *state = hexagon_get_state();
HexState *state = hexagon_state(false);
rz_return_val_if_fail(state, NULL);
return state->cfg;
}
Expand Down Expand Up @@ -209,6 +204,7 @@ RzAsmPlugin rz_asm_plugin_hexagon = {
.bits = 32,
.desc = "Qualcomm Hexagon (QDSP6) V6",
.init = &hexagon_init,
.fini = &hexagon_fini,
.disassemble = &disassemble,
.get_config = &hexagon_get_config,
};
Expand Down
31 changes: 18 additions & 13 deletions handwritten/hexagon_arch_c/functions.c
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ static void hex_clear_pkt(RZ_NONNULL HexPkt *p) {
p->is_valid = false;
p->last_access = 0;
rz_list_purge(p->bin);
rz_vector_clear(p->il_ops);
rz_pvector_clear(p->il_ops);
}

/**
Expand Down Expand Up @@ -259,7 +259,7 @@ RZ_API void hex_insn_free(RZ_NULLABLE HexInsn *i) {
*/
RZ_API void hex_insn_container_free(RZ_NULLABLE HexInsnContainer *c) {
if (c) {
// bin is a union. Just free all of them.
// bin is a uninion. Just free all of them.
hex_insn_free(c->bin.sub[0]);
hex_insn_free(c->bin.sub[1]);
}
Expand Down Expand Up @@ -306,11 +306,19 @@ static ut8 get_state_pkt_index(HexState *state, const HexPkt *p) {

/**
* \brief Initializes each packet of the state once.
* Note that this state is not thread safe.
* It requires RzArch for this.
*
* \return The initialized state of the plugins.
* \param reset Reset the state to NULL. Assumes it was freed before.
*
* \return The initialized state of the plugins or NULL if \p reset = true.
*/
RZ_API HexState *hexagon_get_state() {
RZ_API HexState *hexagon_state(bool reset) {
static HexState *state = NULL;
if (reset) {
state = NULL;
return NULL;
}
if (state) {
return state;
}
Expand All @@ -321,13 +329,14 @@ RZ_API HexState *hexagon_get_state() {
}
for (int i = 0; i < HEXAGON_STATE_PKTS; ++i) {
state->pkts[i].bin = rz_list_newf((RzListFree)hex_insn_container_free);
state->pkts[i].il_ops = rz_vector_new(sizeof(HexILOp), NULL, NULL);
state->pkts[i].il_ops = rz_pvector_new(NULL);
if (!state->pkts[i].bin) {
RZ_LOG_FATAL("Could not initialize instruction list!");
}
hex_clear_pkt(&(state->pkts[i]));
}
state->const_ext_l = rz_list_newf((RzListFree)hex_const_ext_free);
state->token_patterns = NULL;
return state;
}

Expand Down Expand Up @@ -483,7 +492,7 @@ static void hex_set_pkt_info(const RzAsm *rz_asm, RZ_INOUT HexInsnContainer *hic
rz_return_if_fail(hic && pkt);
bool is_first = (k == 0);
HexPktInfo *hi_pi = &hic->pkt_info;
HexState *state = hexagon_get_state();
HexState *state = hexagon_state(false);
bool sdk_form = rz_config_get_b(state->cfg, "plugins.hexagon.sdk");

strncpy(hi_pi->text_postfix, "", 16);
Expand Down Expand Up @@ -1018,17 +1027,13 @@ static void copy_asm_ana_ops(const HexState *state, RZ_BORROW HexReversedOpcode
memcpy(rz_reverse->ana_op, &hic->ana_op, sizeof(RzAnalysisOp));
rz_strbuf_set(&rz_reverse->asm_op->buf_asm, hic->text);
rz_reverse->asm_op->asm_toks = rz_asm_tokenize_asm_regex(&rz_reverse->asm_op->buf_asm, state->token_patterns);
if (rz_reverse->asm_op->asm_toks) {
rz_reverse->asm_op->asm_toks->op_type = hic->ana_op.type;
}
rz_reverse->asm_op->asm_toks->op_type = hic->ana_op.type;
break;
case HEXAGON_DISAS:
memcpy(rz_reverse->asm_op, &hic->asm_op, sizeof(RzAsmOp));
rz_strbuf_set(&rz_reverse->asm_op->buf_asm, hic->text);
rz_reverse->asm_op->asm_toks = rz_asm_tokenize_asm_regex(&rz_reverse->asm_op->buf_asm, state->token_patterns);
if (rz_reverse->asm_op->asm_toks) {
rz_reverse->asm_op->asm_toks->op_type = hic->ana_op.type;
}
rz_reverse->asm_op->asm_toks->op_type = hic->ana_op.type;
break;
case HEXAGON_ANALYSIS:
memcpy(rz_reverse->ana_op, &hic->ana_op, sizeof(RzAnalysisOp));
Expand All @@ -1046,7 +1051,7 @@ static void copy_asm_ana_ops(const HexState *state, RZ_BORROW HexReversedOpcode
* \param copy_result If set, it copies the result. Otherwise it only buffers it in the internal state.
*/
RZ_API void hexagon_reverse_opcode(const RzAsm *rz_asm, HexReversedOpcode *rz_reverse, const ut8 *buf, const ut64 addr, const bool copy_result) {
HexState *state = hexagon_get_state();
HexState *state = hexagon_state(false);
if (!state) {
RZ_LOG_FATAL("HexState was NULL.");
}
Expand Down
2 changes: 1 addition & 1 deletion handwritten/hexagon_h/typedefs.h
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ typedef struct {
ut32 pkt_addr; ///< Address of the packet. Equals the address of the first instruction.
ut64 last_access; ///< Last time accessed in milliseconds
RzList /*<HexInsnContainer>*/ *bin; ///< Descending by address sorted list of instruction containers.
RzVector /*<HexILOp>*/ *il_ops; ///< Pointer to RZIL ops of the packet. If empty the il ops were not shuffled into order yet.
RzPVector /*<HexILOp *>*/ *il_ops; ///< Pointer to RZIL ops of the packet. If empty the il ops were not shuffled into order yet.
HexILExecData il_op_stats; ///< Meta information about the IL operations executed (register read/written etc.)
} HexPkt;

Expand Down
32 changes: 17 additions & 15 deletions handwritten/hexagon_il_X_ops_c/non_insn_ops.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,22 +46,22 @@ RZ_IPI RZ_OWN RzILOpEffect *hex_get_npc(const HexPkt *pkt) {
}

static void reset_pkt_stats(HexILExecData *stats) {
stats->ctr_written = 0;
stats->gpr_written = 0;
stats->pred_written = 0;
stats->ctr_read = 0;
stats->gpr_read = 0;
stats->pred_read = 0;
stats->ctr_tmp_read = 0;
stats->gpr_tmp_read = 0;
stats->pred_tmp_read = 0;
rz_bits_map_init_64(&stats->ctr_written);
rz_bits_map_init_64(&stats->gpr_written);
rz_bits_map_init_32(&stats->pred_written);
rz_bits_map_init_64(&stats->ctr_read);
rz_bits_map_init_64(&stats->gpr_read);
rz_bits_map_init_32(&stats->pred_read);
rz_bits_map_init_64(&stats->ctr_tmp_read);
rz_bits_map_init_64(&stats->gpr_tmp_read);
rz_bits_map_init_32(&stats->pred_tmp_read);
}

RZ_IPI RZ_OWN RzILOpEffect *hex_commit_packet(HexInsnPktBundle *bundle) {
HexILExecData *stats = &bundle->pkt->il_op_stats;
RzILOpEffect *commit_seq = EMPTY();
for (ut8 i = 0; i <= HEX_REG_CTR_REGS_C31; ++i) {
if (!(stats->ctr_written & (1 << i))) {
if (!(rz_bits_map_get_64(&stats->ctr_written, i))) {
continue;
}
const char *dest_reg = hex_get_reg_in_class(HEX_REG_CLASS_CTR_REGS, i, false, false, false);
Expand All @@ -70,7 +70,7 @@ RZ_IPI RZ_OWN RzILOpEffect *hex_commit_packet(HexInsnPktBundle *bundle) {
}

for (ut8 i = 0; i <= HEX_REG_INT_REGS_R31; ++i) {
if (!(stats->gpr_written & (1 << i))) {
if (!(rz_bits_map_get_64(&stats->gpr_written, i))) {
continue;
}
const char *dest_reg = hex_get_reg_in_class(HEX_REG_CLASS_INT_REGS, i, false, false, false);
Expand All @@ -79,7 +79,7 @@ RZ_IPI RZ_OWN RzILOpEffect *hex_commit_packet(HexInsnPktBundle *bundle) {
}

for (ut8 i = 0; i <= HEX_REG_PRED_REGS_P3; ++i) {
if (!(stats->pred_written & (1 << i))) {
if (!(rz_bits_map_get_32(&stats->pred_written, i))) {
continue;
}
const char *dest_reg = hex_get_reg_in_class(HEX_REG_CLASS_PRED_REGS, i, false, false, false);
Expand All @@ -97,9 +97,10 @@ RZ_IPI RZ_OWN RzILOpEffect *hex_il_op_jump_flag_init(HexInsnPktBundle *bundle) {

RZ_IPI RZ_OWN RzILOpEffect *hex_il_op_next_pkt_jmp(HexInsnPktBundle *bundle) {
bool has_direct_jump = false;
HexILOp *it;
rz_vector_foreach(bundle->pkt->il_ops, it) {
if (it->attr & HEX_IL_INSN_ATTR_BRANCH && !(it->attr & HEX_IL_INSN_ATTR_COND)) {
void **it;
rz_pvector_foreach (bundle->pkt->il_ops, it) {
HexILOp *op = *it;
if (op->attr & HEX_IL_INSN_ATTR_BRANCH && !(op->attr & HEX_IL_INSN_ATTR_COND)) {
has_direct_jump = true;
}
}
Expand All @@ -109,3 +110,4 @@ RZ_IPI RZ_OWN RzILOpEffect *hex_il_op_next_pkt_jmp(HexInsnPktBundle *bundle) {
}
return EMPTY();
}

Loading

0 comments on commit 47ce8d6

Please sign in to comment.