Skip to content

Commit c068675

Browse files
committed
Test verifies that ROP search results are cached and reused on subsequent searches
1 parent 7980552 commit c068675

File tree

1 file changed

+27
-15
lines changed

1 file changed

+27
-15
lines changed

test/unit/test_rop.c

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -141,35 +141,47 @@ bool test_rz_direct_solver() {
141141
mu_end;
142142
}
143143

144-
bool test_rop_cache() {
145-
RzCore *core = setup_rz_core("x86", 64);
146-
mu_assert_notnull(core, "setup_rz_core failed");
144+
145+
bool test_rop_cache(void) {
146+
RzCore *core = rz_core_new();
147+
mu_assert_notnull(core, "core");
148+
149+
RzCoreFile *cf = rz_core_file_open(core, "bins/elf/analysis/hello-linux-x86_64", RZ_PERM_RX, 0);
150+
mu_assert_notnull(cf, "open file");
151+
rz_core_bin_load(core, NULL, 0);
147152
rz_config_set_b(core->config, "rop.cache", true);
148153

149-
const char *greparg = "mov";
150-
const char *expected = "0x00000000: mov rbx, 1; ret;\n";
151-
ut64 cache_key = rz_str_djb2_hash(greparg);
154+
const char *filter = "ret";
155+
ut64 key = rz_str_djb2_hash(filter);
152156

153-
core->analysis->ht_rop = ht_up_new(NULL, free);
154-
ht_up_insert(core->analysis->ht_rop, cache_key, strdup(expected));
155-
char *cached = ht_up_find(core->analysis->ht_rop, cache_key, NULL);
156-
mu_assert_streq(cached, expected, "cache store failed");
157+
RzCmdStateOutput state;
158+
rz_cmd_state_output_init(&state, RZ_OUTPUT_MODE_QUIET);
157159

158-
rz_cons_new();
159160
RzRopSearchContext *ctx = rz_core_rop_search_context_new(
160-
core, greparg, false, RZ_ROP_GADGET_PRINT, RZ_ROP_DETAIL_SEARCH_NON, NULL);
161+
core, filter, false, RZ_ROP_GADGET_PRINT, RZ_ROP_DETAIL_SEARCH_NON, &state);
162+
rz_core_rop_search(core, ctx);
163+
rz_core_rop_search_context_free(ctx);
164+
165+
mu_assert_notnull(core->analysis->ht_rop, "ht_rop");
166+
char *result = ht_up_find(core->analysis->ht_rop, key, NULL);
167+
mu_assert_notnull(result, "cached result");
168+
169+
ctx = rz_core_rop_search_context_new(
170+
core, filter, false, RZ_ROP_GADGET_PRINT, RZ_ROP_DETAIL_SEARCH_NON, &state);
161171
rz_core_rop_search(core, ctx);
162172
rz_core_rop_search_context_free(ctx);
163-
mu_assert_streq(rz_cons_get_buffer(), expected, "cache retrieval failed");
164-
rz_cons_free();
165173

174+
char *result2 = ht_up_find(core->analysis->ht_rop, key, NULL);
175+
mu_assert_ptreq(result2, result, "cache hit");
176+
177+
rz_cmd_state_output_fini(&state);
166178
rz_core_free(core);
167179
mu_end;
168180
}
169181

170182
bool all_tests() {
171-
mu_run_test(test_rz_direct_solver);
172183
mu_run_test(test_rop_cache);
184+
mu_run_test(test_rz_direct_solver);
173185
return tests_passed != tests_run;
174186
}
175187

0 commit comments

Comments
 (0)