Skip to content

Commit

Permalink
Total rewrite of rz_search
Browse files Browse the repository at this point in the history
  • Loading branch information
wargio committed Nov 25, 2024
1 parent abd0c6c commit fd68bba
Show file tree
Hide file tree
Showing 27 changed files with 1,135 additions and 1,672 deletions.
23 changes: 9 additions & 14 deletions librz/arch/analysis.c
Original file line number Diff line number Diff line change
Expand Up @@ -795,28 +795,23 @@ RZ_API void rz_analysis_bind(RzAnalysis *analysis, RzAnalysisBind *b) {
}
}

RZ_API RzList /*<RzSearchKeyword *>*/ *rz_analysis_preludes(RzAnalysis *analysis) {
RZ_API RzSearchCollection *rz_analysis_preludes(RzAnalysis *analysis) {
if (analysis->cur && analysis->cur->preludes) {
return analysis->cur->preludes(analysis);
}
return NULL;
}

RZ_API bool rz_analysis_is_prelude(RzAnalysis *analysis, const ut8 *data, int len) {
RzList *l = rz_analysis_preludes(analysis);
if (l) {
RzSearchKeyword *kw;
RzListIter *iter;
rz_list_foreach (l, iter, kw) {
int ks = kw->keyword_length;
if (len >= ks && !memcmp(data, kw->bin_keyword, ks)) {
rz_list_free(l);
return true;
}
}
rz_list_free(l);
RzSearchCollection *col = rz_analysis_preludes(analysis);
if (!col || len < 1) {
rz_search_collection_free(col);
return false;
}
return false;

bool any = rz_search_collection_match_any(col, data, len);
rz_search_collection_free(col);
return any;
}

RZ_API void rz_analysis_add_import(RzAnalysis *analysis, const char *imp) {
Expand Down
31 changes: 17 additions & 14 deletions librz/arch/p/analysis/analysis_arm_cs.c
Original file line number Diff line number Diff line change
Expand Up @@ -2701,29 +2701,32 @@ static ut8 *analysis_mask(RzAnalysis *analysis, int size, const ut8 *data, ut64
return ret;
}

static RzList /*<RzSearchKeyword *>*/ *analysis_preludes(RzAnalysis *analysis) {
#define KW(d, ds, m, ms) rz_list_append(l, rz_search_keyword_new((const ut8 *)d, ds, (const ut8 *)m, ms, NULL))
RzList *l = rz_list_newf((RzListFree)rz_search_keyword_free);
static RzSearchCollection *analysis_preludes(RzAnalysis *analysis) {
RzSearchCollection *sc = rz_search_collection_bytes();
if (!sc) {
return NULL;
}
#define ADD_PRELUDE(d, m, l) rz_search_collection_bytes_add(sc, NULL, (const ut8 *)d, (const ut8 *)m, l)
switch (analysis->bits) {
case 16:
KW("\x00\xb5", 2, "\x0f\xff", 2);
KW("\x08\xb5", 2, "\x0f\xff", 2);
ADD_PRELUDE("\x00\xb5", "\x0f\xff", 2);
ADD_PRELUDE("\x08\xb5", "\x0f\xff", 2);
break;
case 32:
KW("\x00\x00\x2d\xe9", 4, "\x0f\x0f\xff\xff", 4);
ADD_PRELUDE("\x00\x00\x2d\xe9", "\x0f\x0f\xff\xff", 4);
break;
case 64:
KW("\x7f\x23\x03\xd5", 4, "\xff\xff\xff\xff", 4); // pacibsp - Pointer auth
KW("\xf0\x0f\x00\xf8", 4, "\xf0\x0f\x00\xff", 4);
KW("\xf0\x00\x00\xd1", 4, "\xf0\x00\x00\xff", 4);
KW("\xf0\x00\x00\xa9", 4, "\xf0\x00\x00\xff", 4);
KW("\x7f\x23\x03\xd5\xff", 5, NULL, 0);
ADD_PRELUDE("\x7f\x23\x03\xd5", "\xff\xff\xff\xff", 4); // pacibsp - Pointer auth
ADD_PRELUDE("\xf0\x0f\x00\xf8", "\xf0\x0f\x00\xff", 4);
ADD_PRELUDE("\xf0\x00\x00\xd1", "\xf0\x00\x00\xff", 4);
ADD_PRELUDE("\xf0\x00\x00\xa9", "\xf0\x00\x00\xff", 4);
ADD_PRELUDE("\x7f\x23\x03\xd5\xff", NULL, 5);
break;
default:
rz_list_free(l);
l = NULL;
rz_search_collection_free(sc);
sc = NULL;
}
return l;
return sc;
}

static int address_bits(RzAnalysis *analysis, int bits) {
Expand Down
13 changes: 8 additions & 5 deletions librz/arch/p/analysis/analysis_mips_cs.c
Original file line number Diff line number Diff line change
Expand Up @@ -1234,11 +1234,14 @@ static int archinfo(RzAnalysis *a, RzAnalysisInfoType query) {
}
}

static RzList /*<RzSearchKeyword *>*/ *analysis_preludes(RzAnalysis *analysis) {
#define KW(d, ds, m, ms) rz_list_append(l, rz_search_keyword_new((const ut8 *)d, ds, (const ut8 *)m, ms, NULL))
RzList *l = rz_list_newf((RzListFree)rz_search_keyword_free);
KW("\x27\xbd\x00", 3, NULL, 0);
return l;
static RzSearchCollection *analysis_preludes(RzAnalysis *analysis) {
RzSearchCollection *sc = rz_search_collection_bytes();
if (!sc) {
return NULL;
}
#define ADD_PRELUDE(d, m, l) rz_search_collection_bytes_add(sc, NULL, (const ut8 *)d, (const ut8 *)m, l)
ADD_PRELUDE("\x27\xbd\x00", NULL, 3);
return sc;
}

static bool mips_fini(void *user) {
Expand Down
13 changes: 8 additions & 5 deletions librz/arch/p/analysis/analysis_ppc_cs.c
Original file line number Diff line number Diff line change
Expand Up @@ -1763,11 +1763,14 @@ static int archinfo(RzAnalysis *a, RzAnalysisInfoType query) {
}
}

static RzList /*<RzSearchKeyword *>*/ *analysis_preludes(RzAnalysis *analysis) {
#define KW(d, ds, m, ms) rz_list_append(l, rz_search_keyword_new((const ut8 *)d, ds, (const ut8 *)m, ms, NULL))
RzList *l = rz_list_newf((RzListFree)rz_search_keyword_free);
KW("\x7c\x08\x02\xa6", 4, NULL, 0);
return l;
static RzSearchCollection *analysis_preludes(RzAnalysis *analysis) {
RzSearchCollection *sc = rz_search_collection_bytes();
if (!sc) {
return NULL;
}
#define ADD_PRELUDE(d, m, l) rz_search_collection_bytes_add(sc, NULL, (const ut8 *)d, (const ut8 *)m, l)
ADD_PRELUDE("\x7c\x08\x02\xa6", NULL, 4);
return sc;
}

static RzAnalysisILConfig *il_config(RzAnalysis *analysis) {
Expand Down
34 changes: 19 additions & 15 deletions librz/arch/p/analysis/analysis_v850.c
Original file line number Diff line number Diff line change
Expand Up @@ -421,38 +421,42 @@ static char *get_reg_profile(RzAnalysis *analysis) {
/**
* All preludes are guessed by looking at the instruction at the beginning of the function
*/
static RzList /*<RzSearchKeyword *>*/ *analysis_preludes(RzAnalysis *analysis) {
#define KW(d, ds, m, ms) rz_list_append(l, rz_search_keyword_new((const ut8 *)d, ds, (const ut8 *)m, ms, NULL))
RzList *l = rz_list_newf((RzListFree)rz_search_keyword_free);

static RzSearchCollection *analysis_preludes(RzAnalysis *analysis) {
RzSearchCollection *sc = rz_search_collection_bytes();
if (!sc) {
return NULL;
}
#define ADD_PRELUDE(d, m, l) rz_search_collection_bytes_add(sc, NULL, (const ut8 *)d, (const ut8 *)m, l)

// movea 0xff, r0, r20
KW("\x20\xa6\xff\x00", 4, "\xff\xff\xff\xff", 4);
ADD_PRELUDE("\x20\xa6\xff\x00", "\xff\xff\xff\xff", 4);

// mov r6, r7
// ld.w ?[gp], r6
// prepare {lp}, 0
KW("\x06\x38\x24\x37\x01\x00\x80\x07\x21\x00", 10, "\xff\xff\xff\xff\x01\x00\xff\xff\xff\xff", 10);
ADD_PRELUDE("\x06\x38\x24\x37\x01\x00\x80\x07\x21\x00", "\xff\xff\xff\xff\x01\x00\xff\xff\xff\xff", 10);

// ld.w ?[gp], r6
// prepare {lp}, 0
KW("\x24\x37\x01\x00\x80\x07\x21\x00", 8, "\xff\xff\x01\x00\xff\xff\xff\xff", 8);
ADD_PRELUDE("\x24\x37\x01\x00\x80\x07\x21\x00", "\xff\xff\x01\x00\xff\xff\xff\xff", 8);

// prepare
KW("\x80\x07\x01\x00", 4, "\xc0\xff\x1f\x00", 4);
KW("\x80\x07\x03\x00", 4, "\xc0\xff\x1f\x00", 4);
KW("\x80\x07\x0b\x00\x00\x00", 6, "\xc0\xff\x1f\x00\x00\x00", 6);
KW("\x80\x07\x13\x00\x00\x00", 6, "\xc0\xff\x1f\x00\x00\x00", 6);
KW("\x80\x07\x1b\x00\x00\x00\x00\x00", 8, "\xc0\xff\x1f\x00\x00\x00\x00\x00", 8);
ADD_PRELUDE("\x80\x07\x01\x00", "\xc0\xff\x1f\x00", 4);
ADD_PRELUDE("\x80\x07\x03\x00", "\xc0\xff\x1f\x00", 4);
ADD_PRELUDE("\x80\x07\x0b\x00\x00\x00", "\xc0\xff\x1f\x00\x00\x00", 6);
ADD_PRELUDE("\x80\x07\x13\x00\x00\x00", "\xc0\xff\x1f\x00\x00\x00", 6);
ADD_PRELUDE("\x80\x07\x1b\x00\x00\x00\x00\x00", "\xc0\xff\x1f\x00\x00\x00\x00\x00", 8);

// trap
KW("\xe0\x07\x00\x01", 4, "\xe0\xff\xff\xff", 4);
ADD_PRELUDE("\xe0\x07\x00\x01", "\xe0\xff\xff\xff", 4);

// addi ?, sp, sp
KW("\x03\x1e\xd0\xff", 4, "\xff\xff\xff\xff", 4);
ADD_PRELUDE("\x03\x1e\xd0\xff", "\xff\xff\xff\xff", 4);

// add ?, sp
KW("\x50\x1a", 2, "\xf0\xff", 2);
return l;
ADD_PRELUDE("\x50\x1a", "\xf0\xff", 2);
return sc;
}

static int archinfo(RzAnalysis *a, RzAnalysisInfoType query) {
Expand Down
30 changes: 16 additions & 14 deletions librz/arch/p/analysis/analysis_x86_cs.c
Original file line number Diff line number Diff line change
Expand Up @@ -3702,27 +3702,29 @@ static int archinfo(RzAnalysis *a, RzAnalysisInfoType query) {
}
}

static RzList /*<RzSearchKeyword *>*/ *analysis_preludes(RzAnalysis *analysis) {
#define KW(d, ds, m, ms) rz_list_append(l, rz_search_keyword_new((const ut8 *)d, ds, (const ut8 *)m, ms, NULL))
RzList *l = rz_list_newf((RzListFree)rz_search_keyword_free);
static RzSearchCollection *analysis_preludes(RzAnalysis *analysis) {
RzSearchCollection *sc = rz_search_collection_bytes();
if (!sc) {
return NULL;
}
#define ADD_PRELUDE(d, m, l) rz_search_collection_bytes_add(sc, NULL, (const ut8 *)d, (const ut8 *)m, l)
switch (analysis->bits) {
case 32:
KW("\x8b\xff\x55\x8b\xec", 5, NULL, 0);
KW("\x55\x89\xe5", 3, NULL, 0);
KW("\x55\x8b\xec", 3, NULL, 0);
KW("\xf3\x0f\x1e\xfb", 4, NULL, 0); // endbr32
ADD_PRELUDE("\x8b\xff\x55\x8b\xec", NULL, 5);
ADD_PRELUDE("\x55\x89\xe5", NULL, 3);
ADD_PRELUDE("\x55\x8b\xec", NULL, 3);
ADD_PRELUDE("\xf3\x0f\x1e\xfb", NULL, 4); // endbr32
break;
case 64:
KW("\x55\x48\x89\xe5", 4, NULL, 0);
KW("\x55\x48\x8b\xec", 4, NULL, 0);
KW("\xf3\x0f\x1e\xfa", 4, NULL, 0); // endbr64
ADD_PRELUDE("\x55\x48\x89\xe5", NULL, 4);
ADD_PRELUDE("\x55\x48\x8b\xec", NULL, 4);
ADD_PRELUDE("\xf3\x0f\x1e\xfa", NULL, 4); // endbr64
break;
default:
rz_list_free(l);
l = NULL;
break;
rz_search_collection_free(sc);
sc = NULL;
}
return l;
return sc;
}

RzAnalysisPlugin rz_analysis_plugin_x86_cs = {
Expand Down
4 changes: 2 additions & 2 deletions librz/include/rz_analysis.h
Original file line number Diff line number Diff line change
Expand Up @@ -1372,7 +1372,7 @@ typedef struct rz_analysis_plugin_t {
// int (*reset_counter) (RzAnalysis *analysis, ut64 start_addr);
int (*archinfo)(RzAnalysis *analysis, RzAnalysisInfoType query);
ut8 *(*analysis_mask)(RzAnalysis *analysis, int size, const ut8 *data, ut64 at);
RzList /*<RzSearchKeyword *>*/ *(*preludes)(RzAnalysis *analysis);
RzSearchCollection *(*preludes)(RzAnalysis *analysis);

/**
* The actual bit-size of an address for given analysis.bits.
Expand Down Expand Up @@ -2275,7 +2275,7 @@ RZ_API void rz_analysis_rtti_print_at_vtable(RzAnalysis *analysis, ut64 addr, Rz
RZ_API void rz_analysis_rtti_print_all(RzAnalysis *analysis, RzOutputMode mode);
RZ_API void rz_analysis_rtti_recover_all(RzAnalysis *analysis);

RZ_API RzList /*<RzSearchKeyword *>*/ *rz_analysis_preludes(RzAnalysis *analysis);
RZ_API RzSearchCollection *rz_analysis_preludes(RzAnalysis *analysis);
RZ_API bool rz_analysis_is_prelude(RzAnalysis *analysis, const ut8 *data, int len);

/* classes */
Expand Down
2 changes: 1 addition & 1 deletion librz/include/rz_core.h
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ struct rz_core_t {
RzLang *lang;
RzDebug *dbg;
RzFlag *flags;
RzSearch *search;
RzSearchOpt *search;
RzEgg *egg;
RzCrypto *crypto;
RzAGraph *graph;
Expand Down
Loading

0 comments on commit fd68bba

Please sign in to comment.