Skip to content

Commit

Permalink
Add unaligned integer types and fix unaligned load and bound issue in…
Browse files Browse the repository at this point in the history
… /v (#10934)
  • Loading branch information
MaskRay authored and radare committed Aug 5, 2018
1 parent 95094a3 commit 015ee0b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 8 deletions.
20 changes: 12 additions & 8 deletions libr/core/core.c
Expand Up @@ -3016,7 +3016,7 @@ R_API int r_core_search_value_in_range(RCore *core, RInterval search_itv, ut64 v
bool vinfun = r_config_get_i (core->config, "anal.vinfun");
bool vinfunr = r_config_get_i (core->config, "anal.vinfunrange");
ut8 buf[4096];
ut64 v64, value = 0;
ut64 v64, value = 0, size;
ut64 from = search_itv.addr, to = r_itv_end (search_itv);
ut32 v32;
ut16 v16;
Expand All @@ -3042,11 +3042,12 @@ R_API int r_core_search_value_in_range(RCore *core, RInterval search_itv, ut64 v
r_cons_break_push (NULL, NULL);

while (from < to) {
size = R_MIN (to - from, sizeof (buf));
memset (buf, 0xff, sizeof (buf)); // probably unnecessary
if (r_cons_is_breaked ()) {
goto beach;
}
bool res = r_io_read_at (core->io, from, buf, sizeof (buf));
bool res = r_io_read_at_mapped (core->io, from, buf, size);

This comment has been minimized.

Copy link
@condret

condret Aug 7, 2018

Member

stop this

if (!res || !memcmp (buf, "\xff\xff\xff\xff", 4) || !memcmp (buf, "\x00\x00\x00\x00", 4)) {
if (!isValidAddress (core, from)) {
ut64 next = r_io_map_next_address (core->io, from);
Expand All @@ -3058,7 +3059,7 @@ R_API int r_core_search_value_in_range(RCore *core, RInterval search_itv, ut64 v
continue;
}
}
for (i = 0; i < sizeof (buf) - vsize; i++) {
for (i = 0; i <= size - vsize; i++) {
void *v = (buf + i);
ut64 addr = from + i;
if (r_cons_is_breaked ()) {
Expand All @@ -3069,10 +3070,10 @@ R_API int r_core_search_value_in_range(RCore *core, RInterval search_itv, ut64 v
}
match = false;
switch (vsize) {
case 1: value = *(ut8 *) (v); match = (buf[i] >= vmin && buf[i] <= vmax); break;
case 2: v16 = *((ut16 *) (v)); match = (v16 >= vmin && v16 <= vmax); value = v16; break;
case 4: v32 = *((ut32 *) (v)); match = (v32 >= vmin && v32 <= vmax); value = v32; break;
case 8: v64 = *((ut64 *) (v)); match = (v64 >= vmin && v64 <= vmax); value = v64; break;
case 1: value = *(ut8 *)v; match = (buf[i] >= vmin && buf[i] <= vmax); break;
case 2: v16 = *(uut16 *)v; match = (v16 >= vmin && v16 <= vmax); value = v16; break;
case 4: v32 = *(uut32 *)v; match = (v32 >= vmin && v32 <= vmax); value = v32; break;
case 8: v64 = *(uut64 *)v; match = (v64 >= vmin && v64 <= vmax); value = v64; break;
default: eprintf ("Unknown vsize %d\n", vsize); return -1;
}
if (match && !vinfun) {
Expand Down Expand Up @@ -3101,7 +3102,10 @@ R_API int r_core_search_value_in_range(RCore *core, RInterval search_itv, ut64 v
}
}
}
from += sizeof (buf);
if (size == to-from) {
break;
}
from += size-vsize+1;
}
beach:
r_cons_break_pop ();
Expand Down
13 changes: 13 additions & 0 deletions libr/include/r_types_base.h
Expand Up @@ -14,6 +14,19 @@
#define st8 signed char
#define boolt int

#if defined(_MSC_VER)
# define R_ALIGNED(x) __declspec(align(x))
#else
# define R_ALIGNED(x) __attribute__((aligned(x)))
#endif

typedef R_ALIGNED(1) ut16 uut16;
typedef R_ALIGNED(1) ut32 uut32;
typedef R_ALIGNED(1) ut64 uut64;
typedef R_ALIGNED(1) st16 ust16;
typedef R_ALIGNED(1) st32 ust32;
typedef R_ALIGNED(1) st64 ust64;

typedef union {
ut8 v8;
ut16 v16;
Expand Down

0 comments on commit 015ee0b

Please sign in to comment.