Skip to content

Commit ec02889

Browse files
authored
RzIL: Hash Pure variable names to make them more suitable as hash table keys. (#5971)
* Pre-compute DJB2 hash of VAR name for internal hash map key usage. * IL VARS hash: Hash variable name. * Add tests
1 parent a65be1e commit ec02889

File tree

3 files changed

+11
-0
lines changed

3 files changed

+11
-0
lines changed

librz/il/il_opcodes.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ RZ_API RZ_OWN RzILOpPure *rz_il_op_new_var(RZ_NONNULL const char *v, RzILVarKind
6666
rz_return_val_if_fail(v, NULL);
6767
RzILOpPure *ret;
6868
rz_il_op_new_2(Pure, RZ_IL_OP_VAR, RzILOpArgsVar, var, v, kind);
69+
ret->op.var.hash = rz_str_djb2_hash(v);
6970
return ret;
7071
}
7172

@@ -76,6 +77,7 @@ RZ_API RZ_OWN RzILOpPure *rz_il_op_new_let(RZ_NONNULL const char *name, RZ_NONNU
7677
rz_return_val_if_fail(name && exp && body, NULL);
7778
RzILOpPure *ret;
7879
rz_il_op_new_3(Pure, RZ_IL_OP_LET, RzILOpArgsLet, let, name, exp, body);
80+
ret->op.let.hash = rz_str_djb2_hash(name);
7981
return ret;
8082
}
8183

@@ -578,6 +580,7 @@ RZ_API RZ_OWN RzILOpEffect *rz_il_op_new_set(RZ_NONNULL const char *v, bool is_l
578580
rz_return_val_if_fail(v && x, NULL);
579581
RzILOpEffect *ret;
580582
rz_il_op_new_3(Effect, RZ_IL_OP_SET, RzILOpArgsSet, set, v, is_local, x);
583+
ret->op.set.hash = rz_str_djb2_hash(v);
581584
return ret;
582585
}
583586

@@ -1090,13 +1093,15 @@ RZ_API RzILOpPure *rz_il_op_pure_dup(RZ_NONNULL RzILOpPure *op) {
10901093
switch (op->code) {
10911094
case RZ_IL_OP_VAR:
10921095
r->op.var.v = op->op.var.v;
1096+
r->op.var.hash = op->op.var.hash;
10931097
r->op.var.kind = op->op.var.kind;
10941098
break;
10951099
case RZ_IL_OP_ITE:
10961100
DUP_OP3(ite, condition, x, y);
10971101
break;
10981102
case RZ_IL_OP_LET:
10991103
r->op.let.name = op->op.let.name;
1104+
r->op.let.hash = op->op.let.hash;
11001105
DUP_OP2(let, exp, body);
11011106
break;
11021107
case RZ_IL_OP_B0:

librz/include/rz_il/rz_il_opcodes.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ typedef struct rz_il_op_args_shift_t RzILOpArgsShiftRight;
167167
*/
168168
typedef struct rz_il_op_args_set_t {
169169
const char *v; ///< name of variable, const one
170+
ut64 hash; ///< DJB2 hash of variable name
170171
bool is_local; ///< whether a global variable should be set or a local optionally created and set
171172
RzILOpPure *x; ///< value to set the variable to
172173
} RzILOpArgsSet;
@@ -178,6 +179,7 @@ typedef struct rz_il_op_args_set_t {
178179
*/
179180
typedef struct rz_il_op_args_let_t {
180181
const char *name; ///< name of variable
182+
ut64 hash; ///< DJB2 hash of variable name
181183
RzILOpPure *exp; ///< value/expression to bind the variable to
182184
RzILOpPure *body; ///< body in which the variable will be bound and that produces the result
183185
} RzILOpArgsLet;
@@ -260,6 +262,7 @@ typedef struct rz_il_op_args_ite_t {
260262
*/
261263
typedef struct rz_il_op_args_var_t {
262264
const char *v; ///< name of variable, const one
265+
ut64 hash; ///< The DJB2 hash of the name.
263266
RzILVarKind kind; ///< set of variables to pick from
264267
} RzILOpArgsVar;
265268

test/unit/test_il_validate.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ static bool test_il_validate_pure_let() {
103103
RzILSortPure sort;
104104
RzILValidateReport report;
105105
bool val = rz_il_validate_pure(op, ctx, &sort, &report);
106+
mu_assert_eq(op->op.let.hash, rz_str_djb2_hash("x"), "Hash mismatch");
106107
mu_assert_true(val, "valid");
107108
mu_assert_true(rz_il_sort_pure_eq(sort, rz_il_sort_pure_bv(64)), "sort");
108109
mu_assert_null(report, "no report");
@@ -179,6 +180,7 @@ static bool test_il_validate_pure_var() {
179180
mu_assert_true(val, "valid");
180181
mu_assert_true(rz_il_sort_pure_eq(sort, rz_il_sort_pure_bv(42)), "sort");
181182
mu_assert_null(report, "no report");
183+
mu_assert_eq(op->op.var.hash, rz_str_djb2_hash("y"), "Hash mismatch");
182184
rz_il_op_pure_free(op);
183185

184186
RzILOpEffect *eop = rz_il_op_new_seq(
@@ -195,6 +197,7 @@ static bool test_il_validate_pure_var() {
195197
val = rz_il_validate_pure(op, ctx, &sort, &report);
196198
mu_assert_false(val, "invalid");
197199
mu_assert_streq_free(report, "Global variable \"x\" referenced by var op does not exist.", "report");
200+
mu_assert_eq(op->op.var.hash, rz_str_djb2_hash("x"), "Hash mismatch");
198201
rz_il_op_pure_free(op);
199202

200203
op = rz_il_op_new_var("x", RZ_IL_VAR_KIND_LOCAL);

0 commit comments

Comments
 (0)