Skip to content

Commit

Permalink
core/rop: refactor and grep ROP info (#4569)
Browse files Browse the repository at this point in the history
  • Loading branch information
giridharprasath authored Jul 31, 2024
1 parent 6d45cbd commit c02c0b7
Show file tree
Hide file tree
Showing 12 changed files with 448 additions and 167 deletions.
5 changes: 3 additions & 2 deletions librz/arch/analysis.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
#include <rz_util/rz_path.h>
#include <rz_arch.h>
#include <rz_lib.h>
#include <rz_rop.h>

/**
* \brief Returns the default size byte width of memory access operations.
Expand Down Expand Up @@ -130,7 +129,9 @@ RZ_API RzAnalysis *rz_analysis_new(void) {
}
}
analysis->ht_global_var = ht_sp_new(HT_STR_DUP, NULL, (HtSPFreeValue)rz_analysis_var_global_free);
analysis->ht_rop_semantics = NULL;
analysis->ht_rop = NULL;
analysis->is_rop_analysis = false;
analysis->global_var_tree = NULL;
analysis->il_vm = NULL;
analysis->hash = rz_hash_new();
Expand Down Expand Up @@ -187,7 +188,7 @@ RZ_API RzAnalysis *rz_analysis_free(RzAnalysis *a) {
rz_list_free(a->imports);
rz_str_constpool_fini(&a->constpool);
ht_sp_free(a->ht_global_var);
ht_up_free(a->ht_rop);
ht_up_free(a->ht_rop_semantics);
rz_list_free(a->plugins);
rz_analysis_debug_info_free(a->debug_info);
free(a);
Expand Down
12 changes: 5 additions & 7 deletions librz/core/cmd/cmd_search.c
Original file line number Diff line number Diff line change
Expand Up @@ -230,18 +230,18 @@ RZ_IPI RzCmdStatus rz_cmd_info_gadget_handler(RzCore *core, int argc, const char
}

RZ_IPI RzCmdStatus rz_cmd_query_gadget_handler(RzCore *core, int argc, const char **argv, RzCmdStateOutput *state) {
RzList /*<RzILOpPureCode *>*/ *constraints = rop_constraint_list_parse(core, argc, argv);
RzPVector /*<RzRopConstraint *>*/ *constraints = rop_constraint_map_parse(core, argc, argv);
if (!constraints) {
return RZ_CMD_STATUS_ERROR;
}
if (rz_list_empty(constraints)) {
rz_list_free(constraints);
if (rz_pvector_empty(constraints)) {
rz_pvector_fini(constraints);
return RZ_CMD_STATUS_INVALID;
}

RzRopSearchContext *context = rz_core_rop_search_context_new(core, argv[1], false, RZ_ROP_GADGET_PRINT, state);
const RzCmdStatus cmd_status = rz_core_rop_search(core, context);
rz_list_free(constraints);
rz_pvector_fini(constraints);
return cmd_status;
}

Expand All @@ -256,10 +256,8 @@ RZ_IPI RzCmdStatus rz_cmd_search_gadget_handler(RzCore *core, int argc, const ch

RZ_IPI RzCmdStatus rz_cmd_detail_gadget_handler(RzCore *core, int argc, const char **argv, RzCmdStateOutput *state) {
const char *input = argc > 1 ? argv[1] : "";

RzRopSearchContext *context = rz_core_rop_search_context_new(core, input, true, RZ_ROP_GADGET_PRINT_DETAIL | RZ_ROP_GADGET_ANALYZE, state);
RzRopSearchContext *context = rz_core_rop_search_context_new(core, input, false, RZ_ROP_GADGET_PRINT_DETAIL | RZ_ROP_GADGET_ANALYZE, state);
return rz_core_rop_search(core, context);
;
}

static void cmd_search_bin(RzCore *core, RzInterval itv) {
Expand Down
29 changes: 22 additions & 7 deletions librz/core/cmd/cmd_search_rop.c
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,8 @@ RZ_OWN RZ_API RzRopSearchContext *rz_core_rop_search_context_new(RZ_NONNULL cons
}

context->greparg = greparg ? strdup(greparg) : NULL;
context->mode_str = rz_config_get(core->config, "search.in");
context->arch = rz_config_get(core->config, "asm.arch");
context->regexp = regexp;
context->mask = mask;
context->state = state;
Expand All @@ -310,6 +312,7 @@ RZ_OWN RZ_API RzRopSearchContext *rz_core_rop_search_context_new(RZ_NONNULL cons
context->unique_hitlists = NULL;
context->crop = rz_config_get_i(core->config, "rop.conditional");
context->subchain = rz_config_get_i(core->config, "rop.subchain");
context->cache = rz_config_get_i(core->config, "rop.cache");

return context;
}
Expand Down Expand Up @@ -401,7 +404,7 @@ static bool parse_reg_op_reg(const RzCore *core, const char *str, RzRopConstrain
*
* The function returns true if any of these parsing methods succeed.
*/
RZ_API bool rz_core_rop_analyze_constraint(RzCore *core, const char *str, RzRopConstraint *rop_constraint) {
RZ_API bool rz_core_rop_analyze_constraint(RZ_NONNULL RzCore *core, const char *str, RzRopConstraint *rop_constraint) {
rz_return_val_if_fail(core, false);
return parse_reg_to_const(core, str, rop_constraint) ||
parse_reg_to_reg(core, str, rop_constraint) ||
Expand Down Expand Up @@ -433,16 +436,28 @@ static RzRopConstraint *rop_constraint_parse_args(RzCore *core, char *token) {
return rop_constraint;
}

static RzList /*<RzILOpPureCode *>*/ *rop_constraint_list_parse(RzCore *core, const int argc, const char **argv) {
RzList *constr_list = rz_rop_constraint_list_new();
/**
* \brief Parse rop constraint map
* \param core Pointer to the RzCore object.
* \param argc Number of arguments.
* \param argv Array of arguments.
* \return RzPVector of RzRopConstraint objects.
*
* This function parses a list of arguments into a RzPVector of RzRopConstraint objects.
*/
RZ_API RzPVector /*<RzRopConstraint *>*/ *rop_constraint_map_parse(RZ_NONNULL RzCore *core, const int argc, const char **argv) {
RzPVector *constr_map = rz_core_rop_constraint_map_new();
if (!constr_map) {
return NULL;
}
for (int i = 1; i < argc; i++) {
RzList *l = rz_str_split_duplist_n(argv[i], ",", 1, false);
if (!l) {
return constr_list;
return constr_map;
}
size_t llen = rz_list_length(l);
if (!llen) {
return constr_list;
return constr_map;
}
RzListIter *it;
char *token;
Expand All @@ -451,9 +466,9 @@ static RzList /*<RzILOpPureCode *>*/ *rop_constraint_list_parse(RzCore *core, co
if (!rop_constraint) {
continue;
}
rz_list_append(constr_list, rop_constraint);
rz_pvector_push(constr_map, rop_constraint);
}
rz_list_free(l);
}
return constr_list;
return constr_map;
}
2 changes: 1 addition & 1 deletion librz/core/cmd_descs/cmd_descs.c
Original file line number Diff line number Diff line change
Expand Up @@ -19247,7 +19247,7 @@ RZ_IPI void rzshell_cmddescs_init(RzCore *core) {
rz_warn_if_fail(cmd_query_gadget_cd);
rz_cmd_desc_set_default_mode(cmd_query_gadget_cd, RZ_OUTPUT_MODE_STANDARD);

RzCmdDesc *cmd_detail_gadget_cd = rz_cmd_desc_argv_state_new(core->rcmd, slash_R_cd, "/Rg", RZ_OUTPUT_MODE_STANDARD | RZ_OUTPUT_MODE_JSON | RZ_OUTPUT_MODE_QUIET | RZ_OUTPUT_MODE_TABLE, rz_cmd_detail_gadget_handler, &cmd_detail_gadget_help);
RzCmdDesc *cmd_detail_gadget_cd = rz_cmd_desc_argv_state_new(core->rcmd, slash_R_cd, "/Rg", RZ_OUTPUT_MODE_STANDARD | RZ_OUTPUT_MODE_JSON, rz_cmd_detail_gadget_handler, &cmd_detail_gadget_help);
rz_warn_if_fail(cmd_detail_gadget_cd);
rz_cmd_desc_set_default_mode(cmd_detail_gadget_cd, RZ_OUTPUT_MODE_STANDARD);

Expand Down
2 changes: 0 additions & 2 deletions librz/core/cmd_descs/cmd_search.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,6 @@ commands:
modes:
- RZ_OUTPUT_MODE_STANDARD
- RZ_OUTPUT_MODE_JSON
- RZ_OUTPUT_MODE_QUIET
- RZ_OUTPUT_MODE_TABLE
args:
- name: Gadget address
type: RZ_CMD_ARG_TYPE_STRING
Expand Down
Loading

0 comments on commit c02c0b7

Please sign in to comment.