Skip to content

Commit

Permalink
review fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
borzacchiello committed Jun 15, 2023
1 parent f8f69e5 commit 793909d
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 52 deletions.
14 changes: 4 additions & 10 deletions librz/analysis/p/analysis_pyc.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

#include "../../asm/arch/pyc/pyc_dis.h"

#define JMP_OFFSET(ops, v) ((ops)->jump_use_instruction_offset ? (v)*2 : (v))

static int archinfo(RzAnalysis *analysis, RzAnalysisInfoType query) {
if (!strcmp(analysis->cpu, "x86")) {
return -1;
Expand Down Expand Up @@ -103,11 +105,7 @@ static int pyc_op(RzAnalysis *a, RzAnalysisOp *op, ut64 addr, const ut8 *data, i

if (op_obj->type & HASJABS) {
op->type = RZ_ANALYSIS_OP_TYPE_JMP;
op->jump = func_base;
if (ops->jump_use_instruction_offset)
op->jump += oparg * 2;
else
op->jump += oparg;
op->jump = func_base + JMP_OFFSET(ops, oparg);

if (op_obj->type & HASCONDITION) {
op->type = RZ_ANALYSIS_OP_TYPE_CJMP;
Expand All @@ -117,11 +115,7 @@ static int pyc_op(RzAnalysis *a, RzAnalysisOp *op, ut64 addr, const ut8 *data, i
}
if (op_obj->type & HASJREL) {
op->type = RZ_ANALYSIS_OP_TYPE_JMP;
op->jump = addr + ((is_python36) ? 2 : 3);
if (ops->jump_use_instruction_offset)
op->jump += oparg * 2;
else
op->jump += oparg;
op->jump = addr + ((is_python36) ? 2 : 3) + JMP_OFFSET(ops, oparg);
op->fail = addr + ((is_python36) ? 2 : 3);

if (op_obj->type & HASCONDITION) {
Expand Down
2 changes: 1 addition & 1 deletion librz/asm/arch/pyc/opcode.c
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ pyc_opcodes *new_pyc_opcodes() {
if (!ret) {
return NULL;
}
ret->jump_use_instruction_offset = 0;
ret->jump_use_instruction_offset = false;
ret->have_argument = 90;
ret->opcodes = malloc(sizeof(pyc_opcode_object) * 256);
if (!ret->opcodes) {
Expand Down
2 changes: 1 addition & 1 deletion librz/asm/arch/pyc/opcode.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ typedef struct {
ut8 extended_arg;
ut8 have_argument;
ut8 bits;
ut8 jump_use_instruction_offset;
bool jump_use_instruction_offset;
void *(*version_sig)();
RzList /*<pyc_arg_fmt *>*/ *opcode_arg_fmt;
pyc_opcode_object *opcodes;
Expand Down
2 changes: 1 addition & 1 deletion librz/asm/arch/pyc/opcode_310.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pyc_opcodes *opcode_310(void) {
}

ret->version_sig = (void *(*)())opcode_310;
ret->jump_use_instruction_offset = 1;
ret->jump_use_instruction_offset = true;

return ret;
}
62 changes: 27 additions & 35 deletions librz/bin/format/pyc/marshal.c
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,27 @@ static pyc_object *get_string_object(RzBuffer *buffer) {
return ret;
}

static bool add_string_to_cache(RzBinPycObj *pyc, ut64 addr, const char *data, ut32 size, ut32 length, RzStrEnc type) {
if (size == 0) {
return true;
}

RzBinString *string = RZ_NEW0(RzBinString);
if (!string) {
return false;
}
string->paddr = string->vaddr = addr;
string->size = size;
string->length = length;
string->ordinal = 0;
string->type = type;
string->string = rz_str_new(data);
if (!rz_list_append(pyc->strings_cache, string)) {
return false;
}
return true;
}

static pyc_object *get_unicode_object(RzBinPycObj *pyc, RzBuffer *buffer) {
pyc_object *ret = NULL;
bool error = false;
Expand All @@ -446,24 +467,9 @@ static pyc_object *get_unicode_object(RzBinPycObj *pyc, RzBuffer *buffer) {
return NULL;
}

if (n > 0) {
RzBinString *string = NULL;
string = RZ_NEW0(RzBinString);
if (!string) {
RZ_FREE(ret);
return NULL;
}
string->paddr = string->vaddr = addr;
string->size = n;
string->length = rz_utf8_strlen(ret->data);
string->ordinal = 0;
string->type = RZ_STRING_ENC_UTF8;
string->string = rz_str_new(ret->data);
RzListIter *ref_idx = rz_list_append(pyc->strings_cache, string);
if (!ref_idx) {
RZ_FREE(ret);
return NULL;
}
if (!add_string_to_cache(pyc, addr, ret->data, n, rz_utf8_strlen(ret->data), RZ_STRING_ENC_UTF8)) {
RZ_FREE(ret);
return NULL;
}
return ret;
}
Expand Down Expand Up @@ -663,23 +669,9 @@ static pyc_object *get_ascii_object_generic(RzBinPycObj *pyc, RzBuffer *buffer,
RZ_FREE(ret);
}

if (size > 0) {
RzBinString *string = NULL;
string = RZ_NEW0(RzBinString);
if (!string) {
RZ_FREE(ret);
return NULL;
}
string->paddr = string->vaddr = addr;
string->length = string->size = size;
string->ordinal = 0;
string->type = RZ_STRING_ENC_8BIT;
string->string = rz_str_new(ret->data);
RzListIter *ref_idx = rz_list_append(pyc->strings_cache, string);
if (!ref_idx) {
RZ_FREE(ret);
return NULL;
}
if (!add_string_to_cache(pyc, addr, ret->data, size, size, RZ_STRING_ENC_8BIT)) {
RZ_FREE(ret);
return NULL;
}
return ret;
}
Expand Down
4 changes: 2 additions & 2 deletions librz/bin/format/pyc/marshal.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ typedef struct pyc_context {
/* used from marshall.c */
RzList /*<char *>*/ *interned_table;
RzList /*<RzBinSection *>*/ *sections_cache;
RzList /*<RzBinString>*/ *strings_cache;
RzList /*<RzBinSymbol>*/ *symbols_cache;
RzList /*<RzBinString *>*/ *strings_cache;
RzList /*<RzBinSymbol *>*/ *symbols_cache;
RzList /*<RzList<void *> *>*/ *shared;
RzList /*<pyc_object *>*/ *refs; // If you don't have a good reason, do not change this. And also checkout !refs in get_code_object()
ut32 magic_int;
Expand Down
2 changes: 1 addition & 1 deletion librz/bin/format/pyc/pyc_magic.c
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ bool magic_int_within(ut32 target_magic, ut32 lower, ut32 upper, bool *error) {
return (li <= ti) && (ti <= ui);
}

void parse_version_major_minor(const char *version, unsigned *o_major, unsigned *o_minor) {
void parse_version_major_minor(const char *version, ut32 *o_major, ut32 *o_minor) {
unsigned idx = 0, buf_idx = 0;
char buf[20] = { 0 };

Expand Down
2 changes: 1 addition & 1 deletion librz/bin/format/pyc/pyc_magic.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ struct pyc_version {

struct pyc_version get_pyc_version(ut32 magic);

void parse_version_major_minor(const char *version, unsigned *o_major, unsigned *o_minor);
void parse_version_major_minor(const char *version, ut32 *o_major, ut32 *o_minor);

bool magic_int_within(ut32 target_magic, ut32 lower, ut32 uppper, bool *error);

Expand Down

0 comments on commit 793909d

Please sign in to comment.