Skip to content

Commit

Permalink
Fix some resource leaks.
Browse files Browse the repository at this point in the history
  • Loading branch information
Rot127 committed Jan 14, 2024
1 parent c5c141b commit 34aeac2
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 18 deletions.
23 changes: 20 additions & 3 deletions handwritten/asm_hexagon_c/initialization.c
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,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 @@ -139,8 +139,24 @@ 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
Expand All @@ -161,7 +177,7 @@ static bool hexagon_init(void **user) {
}

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 @@ -195,6 +211,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
33 changes: 19 additions & 14 deletions handwritten/hexagon_arch_c/functions.c
Original file line number Diff line number Diff line change
Expand Up @@ -232,13 +232,18 @@ 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 (state) {
return state;
if (reset) {
state = NULL;
return NULL;
}

state = calloc(1, sizeof(HexState));
Expand Down Expand Up @@ -408,7 +413,7 @@ static void hex_set_pkt_info(const RzAsm *rz_asm, RZ_INOUT HexInsnContainer *hic
rz_return_if_fail(hic && p);
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 @@ -467,7 +472,7 @@ static void hex_set_pkt_info(const RzAsm *rz_asm, RZ_INOUT HexInsnContainer *hic
}
}
if (update_text) {
hex_set_hic_text(hic);
hex_set_hic_text(hic);
}
}

Expand Down Expand Up @@ -823,7 +828,7 @@ RZ_API void hex_extend_op(HexState *state, RZ_INOUT HexOp *op, const bool set_ne
* \param addr The address of the current opcode.
*/
RZ_API void hexagon_reverse_opcode(const RzAsm *rz_asm, HexReversedOpcode *rz_reverse, const ut8 *buf, const ut64 addr) {
HexState *state = hexagon_get_state();
HexState *state = hexagon_state(false);
if (!state) {
RZ_LOG_FATAL("HexState was NULL.");
}
Expand All @@ -839,13 +844,13 @@ RZ_API void hexagon_reverse_opcode(const RzAsm *rz_asm, HexReversedOpcode *rz_re
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);
rz_reverse->asm_op->asm_toks->op_type = hic->ana_op.type;
rz_reverse->asm_op->asm_toks->op_type = hic->ana_op.type;
return;
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);
rz_reverse->asm_op->asm_toks->op_type = hic->ana_op.type;
rz_reverse->asm_op->asm_toks = rz_asm_tokenize_asm_regex(&rz_reverse->asm_op->buf_asm, state->token_patterns);
rz_reverse->asm_op->asm_toks->op_type = hic->ana_op.type;
return;
case HEXAGON_ANALYSIS:
memcpy(rz_reverse->ana_op, &(hic->ana_op), sizeof(RzAnalysisOp));
Expand All @@ -872,14 +877,14 @@ RZ_API void hexagon_reverse_opcode(const RzAsm *rz_asm, HexReversedOpcode *rz_re
memcpy(rz_reverse->asm_op, &hic->asm_op, sizeof(RzAsmOp));
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);
rz_reverse->asm_op->asm_toks->op_type = hic->ana_op.type;
rz_reverse->asm_op->asm_toks = rz_asm_tokenize_asm_regex(&rz_reverse->asm_op->buf_asm, state->token_patterns);
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);
rz_reverse->asm_op->asm_toks->op_type = hic->ana_op.type;
rz_reverse->asm_op->asm_toks = rz_asm_tokenize_asm_regex(&rz_reverse->asm_op->buf_asm, state->token_patterns);
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 Down
3 changes: 2 additions & 1 deletion handwritten/hexagon_arch_h/declarations.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ RZ_API void hex_insn_free(RZ_NULLABLE HexInsn *i);
RZ_API HexInsnContainer *hexagon_alloc_instr_container();
RZ_API void hex_insn_container_free(RZ_NULLABLE HexInsnContainer *c);
RZ_API void hex_const_ext_free(RZ_NULLABLE HexConstExt *ce);
RZ_API HexState *hexagon_get_state();
RZ_API HexState *hexagon_state(bool reset);
RZ_IPI void hexagon_state_fini(HexState *state);
RZ_API void hexagon_reverse_opcode(const RzAsm *rz_asm, HexReversedOpcode *rz_reverse, const ut8 *buf, const ut64 addr);
RZ_API ut8 hexagon_get_pkt_index_of_addr(const ut32 addr, const HexPkt *p);
RZ_API HexLoopAttr hex_get_loop_flag(const HexPkt *p);
Expand Down

0 comments on commit 34aeac2

Please sign in to comment.