Skip to content

Commit

Permalink
Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
imbillow committed Nov 18, 2023
1 parent ef8f513 commit 4b0f0a0
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 48 deletions.
32 changes: 28 additions & 4 deletions librz/analysis/dwarf_process.c
Original file line number Diff line number Diff line change
Expand Up @@ -1379,7 +1379,8 @@ static bool function_var_parse(
RzAnalysisDwarfFunction *f,
const RzBinDwarfDie *fn_die,
RzAnalysisDwarfVariable *v,
const RzBinDwarfDie *var_die) {
const RzBinDwarfDie *var_die,
bool *has_unspecified_parameters) {
v->offset = var_die->offset;
switch (var_die->tag) {
case DW_TAG_formal_parameter:
Expand All @@ -1392,6 +1393,9 @@ static bool function_var_parse(
if (f) {
f->has_unspecified_parameters = true;
}
if (has_unspecified_parameters) {
*has_unspecified_parameters = true;
}
return true;
default:
return false;
Expand Down Expand Up @@ -1462,10 +1466,11 @@ static bool function_children_parse(
continue;
}
RzAnalysisDwarfVariable v = { 0 };
if (!function_var_parse(ctx, fn, die, &v, child_die)) {
bool has_unspecified_parameters = false;
if (!function_var_parse(ctx, fn, die, &v, child_die, &has_unspecified_parameters)) {
goto loop_end;
}
if (fn->has_unspecified_parameters) {
if (has_unspecified_parameters) {
callable->has_unspecified_parameters = true;
goto loop_end;
}
Expand Down Expand Up @@ -1623,16 +1628,32 @@ static bool function_from_die(
return false;
}

static bool variable_exist_global(RzAnalysis *a, RzAnalysisDwarfVariable *v) {
RzAnalysisVarGlobal *existing_glob = NULL;
if ((existing_glob = rz_analysis_var_global_get_byaddr_in(a, v->location->address))) {
return true;
}
if ((existing_glob = rz_analysis_var_global_get_byname(a, v->prefer_name))) {
return true;
}
return false;
}

static bool variable_from_die(
RZ_BORROW RZ_IN RZ_NONNULL Context *ctx,
RZ_BORROW RZ_IN RZ_NONNULL const RzBinDwarfDie *die) {
RzAnalysisDwarfVariable v = { 0 };
if (!function_var_parse(ctx, NULL, NULL, &v, die)) {
if (!function_var_parse(ctx, NULL, NULL, &v, die, NULL)) {
return false;
}
if (!(v.type && v.location->kind == RzBinDwarfLocationKind_ADDRESS)) {
return false;
}

if (variable_exist_global(ctx->analysis, &v)) {
return false;
}

bool result = rz_analysis_var_global_create(
ctx->analysis, v.prefer_name, v.type, v.location->address);

Expand Down Expand Up @@ -1892,6 +1913,9 @@ static bool RzBinDwarfLocation_as_RzAnalysisVarStorage(
break;
}
case RzBinDwarfLocationKind_ADDRESS: {
if (variable_exist_global(a, dw_var)) {
return false;
}
rz_analysis_var_global_create(a, dw_var->prefer_name,
rz_type_clone(dw_var->type), loc->address);
rz_analysis_var_fini(var);
Expand Down
1 change: 1 addition & 0 deletions librz/core/cdwarf.c
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@ static bool htup_loclists_cb(void *u, ut64 k, const void *v) {
RzBinDWARFDumpOption dump_opt = {
.loclist_sep = ",\t",
.loclist_indent = "",
.expr_sep = ", "
};
rz_bin_dwarf_expression_dump(
&ctx->cu->hdr.encoding, entry->expression, ctx->sb, &dump_opt);
Expand Down
73 changes: 29 additions & 44 deletions test/integration/test_dwarf_integration.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// SPDX-FileCopyrightText: 2020 HoundThe <[email protected]>
// SPDX-License-Identifier: LGPL-3.0-only

#include <rz_core.h>
#include <rz_analysis.h>
#include <rz_bin.h>
#include <rz_type.h>
Expand All @@ -19,14 +20,12 @@
}

static bool test_parse_dwarf_types(void) {
RzBin *bin = rz_bin_new();
mu_assert_notnull(bin, "Couldn't create new RzBin");
RzIO *io = rz_io_new();
mu_assert_notnull(io, "Couldn't create new RzIO");
RzAnalysis *analysis = rz_analysis_new();
mu_assert_notnull(analysis, "Couldn't create new RzAnalysis");
rz_io_bind(io, &bin->iob);
analysis->binb.demangle = rz_bin_demangle;
RzCore *core = rz_core_new();
mu_assert_notnull(core->bin, "Couldn't create new RzBin");
mu_assert_notnull(core->io, "Couldn't create new RzIO");
mu_assert_notnull(core->analysis, "Couldn't create new RzAnalysis");
RzAnalysis *analysis = core->analysis;
RzBin *bin = core->bin;

// TODO fix, how to correctly promote binary info to the RzAnalysis in unit tests?
rz_analysis_set_cpu(analysis, "x86");
Expand Down Expand Up @@ -122,22 +121,18 @@ static bool test_parse_dwarf_types(void) {
// check_kv("union.unaligned.s8", "long long int,0,0");

rz_bin_dwarf_free(dw);
rz_analysis_free(analysis);
rz_bin_free(bin);
rz_io_free(io);
rz_core_free(core);
mu_end;
}

static bool test_dwarf_function_parsing_cpp(void) {
#if WITH_GPL
RzBin *bin = rz_bin_new();
mu_assert_notnull(bin, "Couldn't create new RzBin");
RzIO *io = rz_io_new();
mu_assert_notnull(io, "Couldn't create new RzIO");
RzAnalysis *analysis = rz_analysis_new();
mu_assert_notnull(analysis, "Couldn't create new RzAnalysis");
rz_io_bind(io, &bin->iob);
analysis->binb.demangle = rz_bin_demangle;
RzCore *core = rz_core_new();
mu_assert_notnull(core->bin, "Couldn't create new RzBin");
mu_assert_notnull(core->io, "Couldn't create new RzIO");
mu_assert_notnull(core->analysis, "Couldn't create new RzAnalysis");
RzAnalysis *analysis = core->analysis;
RzBin *bin = core->bin;

// TODO fix, how to correctly promote binary info to the RzAnalysis in unit tests?
rz_analysis_set_cpu(analysis, "x86");
Expand Down Expand Up @@ -165,22 +160,18 @@ static bool test_dwarf_function_parsing_cpp(void) {
check_fn(0x401160, "main", "int main()");

rz_bin_dwarf_free(dw);
rz_analysis_free(analysis);
rz_bin_free(bin);
rz_io_free(io);
rz_core_free(core);
#endif
mu_end;
}

static bool test_dwarf_function_parsing_go(void) {
RzBin *bin = rz_bin_new();
mu_assert_notnull(bin, "Couldn't create new RzBin");
RzIO *io = rz_io_new();
mu_assert_notnull(io, "Couldn't create new RzIO");
RzAnalysis *analysis = rz_analysis_new();
mu_assert_notnull(analysis, "Couldn't create new RzAnalysis");
rz_io_bind(io, &bin->iob);
analysis->binb.demangle = rz_bin_demangle;
RzCore *core = rz_core_new();
mu_assert_notnull(core->bin, "Couldn't create new RzBin");
mu_assert_notnull(core->io, "Couldn't create new RzIO");
mu_assert_notnull(core->analysis, "Couldn't create new RzAnalysis");
RzAnalysis *analysis = core->analysis;
RzBin *bin = core->bin;

// TODO fix, how to correctly promote binary info to the RzAnalysis in unit tests?
rz_analysis_set_cpu(analysis, "x86");
Expand Down Expand Up @@ -208,21 +199,17 @@ static bool test_dwarf_function_parsing_go(void) {
don't check variable information and add it in the future */

rz_bin_dwarf_free(dw);
rz_analysis_free(analysis);
rz_bin_free(bin);
rz_io_free(io);
rz_core_free(core);
mu_end;
}

static bool test_dwarf_function_parsing_rust(void) {
RzBin *bin = rz_bin_new();
mu_assert_notnull(bin, "Couldn't create new RzBin");
RzIO *io = rz_io_new();
mu_assert_notnull(io, "Couldn't create new RzIO");
RzAnalysis *analysis = rz_analysis_new();
mu_assert_notnull(analysis, "Couldn't create new RzAnalysis");
rz_io_bind(io, &bin->iob);
analysis->binb.demangle = rz_bin_demangle;
RzCore *core = rz_core_new();
mu_assert_notnull(core->bin, "Couldn't create new RzBin");
mu_assert_notnull(core->io, "Couldn't create new RzIO");
mu_assert_notnull(core->analysis, "Couldn't create new RzAnalysis");
RzAnalysis *analysis = core->analysis;
RzBin *bin = core->bin;

// TODO fix, how to correctly promote binary info to the RzAnalysis in unit tests?
rz_analysis_set_cpu(analysis, "x86");
Expand Down Expand Up @@ -252,9 +239,7 @@ static bool test_dwarf_function_parsing_rust(void) {
check_fn(0x8730, "lang_start_internal", "isize lang_start_internal(struct &Fn<()> main, isize argc, u8 **argv)");

rz_bin_dwarf_free(dw);
rz_analysis_free(analysis);
rz_bin_free(bin);
rz_io_free(io);
rz_core_free(core);
mu_end;
}

Expand Down

0 comments on commit 4b0f0a0

Please sign in to comment.