Skip to content

Commit

Permalink
Merge branch 'rz_bv_performance' into rz_bv_copy
Browse files Browse the repository at this point in the history
  • Loading branch information
rajRishi22 committed Nov 25, 2024
2 parents 2701028 + cde2cff commit 6c2a272
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 105 deletions.
97 changes: 3 additions & 94 deletions binrz/rz-test/rz-test.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#define RIZIN_CMD_DEFAULT "rizin"
#define RZ_ASM_CMD_DEFAULT "rz-asm"
#define JSON_TEST_FILE_DEFAULT "bins/elf/crackme0x00b"
#define TEST_DIR_DEFAULT "."
#define TIMEOUT_DEFAULT 960

#define STRV(x) #x
Expand Down Expand Up @@ -78,7 +79,7 @@ static int help(bool verbose) {
"-r", "[rizin]", "Path to rizin executable (default is " RIZIN_CMD_DEFAULT ")",
"-m", "[rz-asm]", "Path to rz-asm executable (default is " RZ_ASM_CMD_DEFAULT ")",
"-f", "[file]", "File to use for JSON tests (default is " JSON_TEST_FILE_DEFAULT ")",
"-C", "[dir]", "Chdir before running rz-test (default follows executable symlink + test/new)",
"-C", "[dir]", "Chdir before running rz-test (default is '" TEST_DIR_DEFAULT "')",
"-t", "[seconds]", "Timeout per test (default is " TIMEOUT_DEFAULT_STR " seconds)",
"-o", "[file]", "Output test run information in JSON format to file",
"-e", "[dir]", "Exclude a particular directory while testing (this option can appear many times)",
Expand Down Expand Up @@ -106,95 +107,10 @@ static int help(bool verbose) {
return 1;
}

static bool rz_test_chdir(const char *argv0) {
#if __UNIX__
if (rz_file_is_directory("db")) {
return true;
}
char src_path[PATH_MAX];
char *rz_test_path = rz_file_path(argv0);
bool found = false;

ssize_t linklen = readlink(rz_test_path, src_path, sizeof(src_path) - 1);
if (linklen != -1) {
src_path[linklen] = '\0';
char *p = strstr(src_path, RZ_SYS_DIR "binrz" RZ_SYS_DIR "rz-test" RZ_SYS_DIR "rz-test");
if (p) {
*p = 0;
strcat(src_path, RZ_SYS_DIR "test" RZ_SYS_DIR);
if (rz_file_is_directory(src_path)) {
if (chdir(src_path) != -1) {
eprintf("Running from %s\n", src_path);
found = true;
} else {
eprintf("Cannot find '%s' directory\n", src_path);
}
}
}
} else {
eprintf("Cannot follow the link %s\n", src_path);
}
free(rz_test_path);
return found;
#else
return false;
#endif
}

static bool rz_test_test_run_unit(void) {
return rz_sys_system("make -C unit all run") == 0;
}

static bool rz_test_chdir_fromtest(const char *test_path) {
if (!test_path || *test_path == '@') {
test_path = "";
}
char *abs_test_path = rz_file_abspath(test_path);
if (!rz_file_is_directory(abs_test_path)) {
char *last_slash = (char *)rz_str_lchr(abs_test_path, RZ_SYS_DIR[0]);
if (last_slash) {
*last_slash = 0;
}
}
if (chdir(abs_test_path) == -1) {
free(abs_test_path);
return false;
}
free(abs_test_path);
bool found = false;
char *cwd = NULL;
char *old_cwd = NULL;
while (true) {
cwd = rz_sys_getdir();
if (old_cwd && !strcmp(old_cwd, cwd)) {
break;
}
if (rz_file_is_directory("test")) {
rz_sys_chdir("test");
if (rz_file_is_directory("db")) {
found = true;
eprintf("Running from %s\n", cwd);
break;
}
rz_sys_chdir("..");
}
if (rz_file_is_directory("db")) {
found = true;
eprintf("Running from %s\n", cwd);
break;
}
free(old_cwd);
old_cwd = cwd;
cwd = NULL;
if (chdir("..") == -1) {
break;
}
}
free(old_cwd);
free(cwd);
return found;
}

static bool log_mode = false;

int rz_test_main(int argc, const char **argv) {
Expand Down Expand Up @@ -338,14 +254,7 @@ int rz_test_main(int argc, const char **argv) {
goto beach;
}
} else {
bool dir_found = (opt.ind < argc && argv[opt.ind][0] != '.')
? rz_test_chdir_fromtest(argv[opt.ind])
: rz_test_chdir(argv[0]);
if (!dir_found) {
eprintf("Cannot find db/ directory related to the given test.\n");
ret = -1;
goto beach;
}
rz_test_dir = TEST_DIR_DEFAULT;
}

if (fuzz_dir) {
Expand Down
2 changes: 1 addition & 1 deletion librz/arch/p/analysis/analysis_ppc_cs.c
Original file line number Diff line number Diff line change
Expand Up @@ -1744,7 +1744,6 @@ static int analyze_op(RzAnalysis *a, RzAnalysisOp *op, ut64 addr, const ut8 *buf
rz_strbuf_fini(&op->esil);
}
cs_free(insn, n);
// cs_close (&handle);
}
return op->size;
}
Expand Down Expand Up @@ -1780,6 +1779,7 @@ static RzAnalysisILConfig *il_config(RzAnalysis *analysis) {

static bool ppc_fini(void *user) {
PPCContext *ctx = (PPCContext *)user;
cs_close(&ctx->handle);
if (ctx) {
RZ_FREE(ctx);
}
Expand Down
22 changes: 12 additions & 10 deletions librz/util/bitvector.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@
// SPDX-License-Identifier: LGPL-3.0-only

#include "rz_util.h"
#include <rz_types.h>
#include <stdlib.h>
#include <stdio.h>
#include <limits.h>


#define NELEM(N, ELEMPER) ((N + (ELEMPER)-1) / (ELEMPER))
#define BV_ELEM_SIZE 8U
#define SIZE_OF_UNSIGNED_LONG (sizeof(unsigned long))


// optimization for reversing 8 bits which uses 32 bits
// https://graphics.stanford.edu/~seander/bithacks.html#ReverseByteWith32Bits
Expand Down Expand Up @@ -215,22 +218,23 @@ RZ_API ut32 rz_bv_copy_nbits(RZ_NONNULL const RzBitVector *src, ut32 src_start_p
rz_return_val_if_fail(src && dst, 0);

// Determine the chunk size (word size) dynamically
const ut32 chunk_size = sizeof(unsigned long) * CHAR_BIT; // Word size in bits
const ut32 chunk_size = SIZE_OF_UNSIGNED_LONG * CHAR_BIT; // Word size in bits
ut32 max_nbit = RZ_MIN((src->len - src_start_pos), (dst->len - dst_start_pos));

if (max_nbit < nbit) {
return 0;
}

ut32 bits_copied = 0;
ut32 nbit_original = nbit;

// Handle unaligned prefix
while ((src_start_pos % chunk_size != 0 || dst_start_pos % chunk_size != 0) && nbit > 0) {
if(src_start_pos % chunk_size != 0 || dst_start_pos % chunk_size != 0){
while (nbit > 0) {
bool bit = rz_bv_get(src, src_start_pos++);
rz_bv_set(dst, dst_start_pos++, bit);
--nbit;
++bits_copied;
}
}

// Process aligned chunks
while (nbit >= chunk_size) {
Expand All @@ -239,7 +243,7 @@ RZ_API ut32 rz_bv_copy_nbits(RZ_NONNULL const RzBitVector *src, ut32 src_start_p
unsigned long dst_chunk = rz_bv_get_chunk(dst, dst_start_pos / chunk_size);

// Create a mask for the bits to copy
unsigned long mask = (1UL << chunk_size) - 1;
unsigned long mask = UINT32_MAX;
if (nbit < chunk_size) {
mask = (1UL << nbit) - 1;
}
Expand All @@ -251,18 +255,16 @@ RZ_API ut32 rz_bv_copy_nbits(RZ_NONNULL const RzBitVector *src, ut32 src_start_p
src_start_pos += chunk_size;
dst_start_pos += chunk_size;
nbit -= chunk_size;
bits_copied += chunk_size;
}

// Handle remaining unaligned suffix bits
while (nbit > 0) {
bool bit = rz_bv_get(src, src_start_pos++);
rz_bv_set(dst, dst_start_pos++, bit);
--nbit;
++bits_copied;
}

return bits_copied;
return nbit_original;
}


Expand Down Expand Up @@ -1525,7 +1527,7 @@ RZ_API bool rz_bv_set_range(RZ_NONNULL RzBitVector *bv, ut32 pos_start, ut32 pos
}

// Determine the chunk size dynamically
const ut32 chunk_size = sizeof(unsigned long) * CHAR_BIT;
const ut32 chunk_size = SIZE_OF_UNSIGNED_LONG * CHAR_BIT;

// Handle unaligned prefix bits
while (pos_start < pos_end && pos_start % chunk_size != 0) {
Expand Down

0 comments on commit 6c2a272

Please sign in to comment.