|
| 1 | +// SPDX-FileCopyrightText: 2024 z3phyr <giridh1337@gmail.com> |
| 2 | +// SPDX-License-Identifier: LGPL-3.0-only |
| 3 | + |
| 4 | +#include <rz_core.h> |
| 5 | +#include <rz_rop.h> |
| 6 | +#include "../unit/minunit.h" |
| 7 | + |
| 8 | +bool test_rop_cache(void) { |
| 9 | + RzCore *core = rz_core_new(); |
| 10 | + mu_assert_notnull(core, "core"); |
| 11 | + |
| 12 | +#if __WINDOWS__ |
| 13 | + const char *test_bin = "bins/pe/standard.exe"; |
| 14 | +#else |
| 15 | + const char *test_bin = "bins/elf/analysis/hello-linux-x86_64"; |
| 16 | +#endif |
| 17 | + |
| 18 | + RzCoreFile *cf = rz_core_file_open(core, test_bin, RZ_PERM_RX, 0); |
| 19 | + mu_assert_notnull(cf, "open file"); |
| 20 | + rz_core_bin_load(core, NULL, 0); |
| 21 | + rz_config_set_b(core->config, "rop.cache", true); |
| 22 | + |
| 23 | + RzCmdStateOutput state; |
| 24 | + rz_cmd_state_output_init(&state, RZ_OUTPUT_MODE_QUIET, core); |
| 25 | + |
| 26 | + RzRopSearchContext *ctx = rz_core_rop_search_context_new( |
| 27 | + core, "", false, RZ_ROP_GADGET_PRINT, RZ_ROP_DETAIL_SEARCH_NON, &state); |
| 28 | + rz_core_rop_search(core, ctx); |
| 29 | + rz_core_rop_search_context_free(ctx); |
| 30 | + |
| 31 | + if (core->analysis->ht_rop) { |
| 32 | + mu_assert_eq(core->analysis->ht_rop->count, 0, "empty filter not cached"); |
| 33 | + } |
| 34 | + |
| 35 | + const char *filter = "ret"; |
| 36 | + ut64 key = rz_str_djb2_hash(filter); |
| 37 | + |
| 38 | + ctx = rz_core_rop_search_context_new( |
| 39 | + core, filter, false, RZ_ROP_GADGET_PRINT, RZ_ROP_DETAIL_SEARCH_NON, &state); |
| 40 | + rz_core_rop_search(core, ctx); |
| 41 | + rz_core_rop_search_context_free(ctx); |
| 42 | + |
| 43 | + mu_assert_notnull(core->analysis->ht_rop, "ht_rop"); |
| 44 | + char *result = ht_up_find(core->analysis->ht_rop, key, NULL); |
| 45 | + mu_assert_notnull(result, "cached result"); |
| 46 | + |
| 47 | + ctx = rz_core_rop_search_context_new( |
| 48 | + core, filter, false, RZ_ROP_GADGET_PRINT, RZ_ROP_DETAIL_SEARCH_NON, &state); |
| 49 | + rz_core_rop_search(core, ctx); |
| 50 | + rz_core_rop_search_context_free(ctx); |
| 51 | + |
| 52 | + char *result2 = ht_up_find(core->analysis->ht_rop, key, NULL); |
| 53 | + mu_assert_ptreq(result2, result, "cache hit"); |
| 54 | + |
| 55 | + const char *filter2 = "pop"; |
| 56 | + ut64 key2 = rz_str_djb2_hash(filter2); |
| 57 | + |
| 58 | + ctx = rz_core_rop_search_context_new( |
| 59 | + core, filter2, false, RZ_ROP_GADGET_PRINT, RZ_ROP_DETAIL_SEARCH_NON, &state); |
| 60 | + rz_core_rop_search(core, ctx); |
| 61 | + rz_core_rop_search_context_free(ctx); |
| 62 | + |
| 63 | + char *result3 = ht_up_find(core->analysis->ht_rop, key2, NULL); |
| 64 | + mu_assert_notnull(result3, "pop cached"); |
| 65 | + mu_assert_ptrneq(result3, result, "different cache entries"); |
| 66 | + mu_assert_eq(core->analysis->ht_rop->count, 2, "cache count"); |
| 67 | + |
| 68 | + rz_cmd_state_output_fini(&state); |
| 69 | + rz_core_free(core); |
| 70 | + mu_end; |
| 71 | +} |
| 72 | + |
| 73 | +bool all_tests() { |
| 74 | + mu_run_test(test_rop_cache); |
| 75 | + return tests_passed != tests_run; |
| 76 | +} |
| 77 | + |
| 78 | +mu_main(all_tests) |
0 commit comments