Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: RIOMaps should have a 'kind/klass' attribute #23863

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion libr/core/cmd_open.inc.c
Original file line number Diff line number Diff line change
Expand Up @@ -628,6 +628,13 @@ static void cmd_omf(RCore *core, int argc, char *argv[]) {
const int id = r_num_math (core->num, argv[0]);
RIOMap *map = r_io_map_get (core->io, id);
if (map) {
if (argc > 2) {
bool res = r_io_map_setattr_fromstring (map, argv[2]);
if (!res) {
R_LOG_ERROR ("Invalid meta type string");
break;
}
}
int nperm = r_str_rwx (argv[1]);
if (nperm < 0) {
R_LOG_ERROR ("Invalid permission string");
Expand Down Expand Up @@ -1264,7 +1271,17 @@ static void cmd_open_map(RCore *core, const char *input) {
}
break;
case '?':
r_core_cmd_help (core, help_msg_om);
r_core_cmd_help_match (core, help_msg_om, "omf");
break;
case 0:
{
RIOMap *map = r_io_map_get_at (core->io, core->offset);
if (map) {
char *s = r_io_map_getattr (map);
r_cons_println (s);
free (s);
}
}
break;
default:
r_core_return_invalid_command (core, "omf", input[2]);
Expand Down
41 changes: 41 additions & 0 deletions libr/include/r_io.h
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,43 @@ typedef struct r_io_plugin_t {
#define R_IO_MAP_TIE_FLG_BACK 1 //ties a map so that it resizes when the desc resizes
#define R_IO_MAP_TIE_FLG_FORTH (1 << 1) //ties a map so that the desc resizes when the map resizes

// not sure if this shoul db enum, bitfield or just char*
typedef enum {
R_IO_MAP_META_TYPE_HEAP = 0,
R_IO_MAP_META_TYPE_STACK, // program stack
R_IO_MAP_META_TYPE_MMAP, // mapped memory
R_IO_MAP_META_TYPE_MMIO, // mapped devices
R_IO_MAP_META_TYPE_DMA, // high speed hw data transfer
R_IO_MAP_META_TYPE_JIT, // just in time code
R_IO_MAP_META_TYPE_BSS, // block started symbol (zero paged memory)
R_IO_MAP_META_TYPE_SHARED, // ipc, etc
R_IO_MAP_META_TYPE_KERNEL, // VDSO, etc, text|data|buffers
R_IO_MAP_META_TYPE_GUARD, // surrounding stack for protections
R_IO_MAP_META_TYPE_NULL, // to catch null derefs
R_IO_MAP_META_TYPE_GPU, // graphics memory
R_IO_MAP_META_TYPE_TLS, // thread-local storage
R_IO_MAP_META_TYPE_BUFFER, // temporal
R_IO_MAP_META_TYPE_COW, // copy on write
R_IO_MAP_META_TYPE_PAGETABLES, // mmu settings
R_IO_MAP_META_TYPE_LAST
} RIOMapMetaType;

#define R_IO_MAP_META_FLAG_LAST 16
typedef enum {
R_IO_MAP_META_FLAG_PAGED, // anything can be non-paged.. must be bitfield
R_IO_MAP_META_FLAG_PRIVATE, // private memory
R_IO_MAP_META_FLAG_PERSISTENT, // non volatile
R_IO_MAP_META_FLAG_ASLR, // randomizable
R_IO_MAP_META_FLAG_SWAP, // swappage to disk
R_IO_MAP_META_FLAG_DEP, // same as W^X
R_IO_MAP_META_FLAG_ENCLAVE, // protected by a secure enclave
R_IO_MAP_META_FLAG_COMPRESSED, // compressed memory
R_IO_MAP_META_FLAG_ENCRYPTED, // cryptographically secure
R_IO_MAP_META_FLAG_LARGE, // different alignment for big data
R_IO_MAP_META_FLAG_SYSTEM, // frameworks, dyldcache, ..
R_IO_MAP_META_FLAG_LIBRARY, // maybe the same of system?
} RIOMapMetaFlags;

typedef struct r_io_map_t {
int fd;
int perm;
Expand All @@ -233,6 +270,7 @@ typedef struct r_io_map_t {
RRBTree *overlay;
char *name;
ut32 tie_flags;
ut32 meta; // metadata attributes
} RIOMap;

typedef struct r_io_map_ref_t {
Expand Down Expand Up @@ -583,6 +621,9 @@ R_API int r_io_fd_get_prev(RIO *io, int fd);
R_API int r_io_fd_get_highest(RIO *io);
R_API int r_io_fd_get_lowest(RIO *io);

R_API bool r_io_map_setattr_fromstring(RIOMap *map, const char *s);
R_API bool r_io_map_setattr(RIOMap *map, ut32 type, ut32 flags);
R_API char *r_io_map_getattr(RIOMap *map);

#define r_io_range_new() R_NEW0(RIORange)
#define r_io_range_free(x) free(x)
Expand Down
89 changes: 85 additions & 4 deletions libr/io/io_map.c
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
/* radare2 - LGPL - Copyright 2017-2023 - condret, MaskRay */
/* radare2 - LGPL - Copyright 2017-2024 - condret, MaskRay */

#include <r_io.h>
#include <stdlib.h>
#include <r_util.h>
#include <sdb/sdb.h>

#define END_OF_MAP_IDS UT32_MAX
R_IPI bool io_bank_has_map(RIO *io, const ut32 bankid, const ut32 mapid);
Expand Down Expand Up @@ -757,3 +754,87 @@ R_API void r_io_map_overlay_foreach(RIOMap *map, RIOMapOverlayForeach cb, void *
cb (itv, moc->buf, user);
} while ((node = r_rbnode_next (node)), node);
}

static const char* metatypename[R_IO_MAP_META_TYPE_LAST] = {
[R_IO_MAP_META_TYPE_HEAP] = "heap",
[R_IO_MAP_META_TYPE_STACK] = "stack",
[R_IO_MAP_META_TYPE_MMAP] = "mmap",
[R_IO_MAP_META_TYPE_MMIO] = "mmio",
[R_IO_MAP_META_TYPE_DMA] = "dma",
[R_IO_MAP_META_TYPE_JIT] = "jit",
[R_IO_MAP_META_TYPE_BSS] = "bss",
[R_IO_MAP_META_TYPE_SHARED] = "shared",
[R_IO_MAP_META_TYPE_KERNEL] = "kernel",
[R_IO_MAP_META_TYPE_GUARD] = "guard",
[R_IO_MAP_META_TYPE_NULL] = "null",
[R_IO_MAP_META_TYPE_GPU] = "gpu",
[R_IO_MAP_META_TYPE_TLS] = "tls",
[R_IO_MAP_META_TYPE_BUFFER] = "buffer",
[R_IO_MAP_META_TYPE_COW] = "cow",
[R_IO_MAP_META_TYPE_PAGETABLES] = "pagetables"
};

static const char *metaflagname[16] = {
"paged",
"private",
"persistent",
"aslr",
"swap",
"dep",
"enclave",
"compressed",
"encrypted",
"large",
0
};

R_API bool r_io_map_setattr_fromstring(RIOMap *map, const char *s) {
int i, maptype;
for (maptype = 0; maptype < R_IO_MAP_META_TYPE_LAST; maptype++) {
if (strstr (s, metatypename[maptype])) {
ut32 mapflag = 0;
for (i = 0; i < R_IO_MAP_META_FLAG_LAST; i++) {
if (strstr (s, metaflagname[i])) {
mapflag |= (1 << i);
}
}
return r_io_map_setattr (map, maptype, mapflag);
}
}
R_LOG_DEBUG ("invalid map type string");
return false;
}

R_API bool r_io_map_setattr(RIOMap *map, ut32 type, ut32 flags) {
if (type >= R_IO_MAP_META_TYPE_LAST) {
R_LOG_DEBUG ("invalid map type");
return false;
}
if (flags >= R_IO_MAP_META_FLAG_LAST) {
R_LOG_DEBUG ("invalid map flags");
return false;
}
map->meta = type | (flags << 16);
return true;
}

R_API char *r_io_map_getattr(RIOMap *map) {
RStrBuf *sb = r_strbuf_new ("");
ut32 maptype = map->meta & 0xffff;
ut32 mapflag = (map->meta > 16) & 0xffff;
if (maptype >= R_IO_MAP_META_TYPE_LAST) {
return false;
}
if (mapflag >= R_IO_MAP_META_FLAG_LAST) {
return false;
}
r_strbuf_append (sb, metatypename[maptype]);
int i = 0;
for (i = 0; i < 16; i++) {
if (mapflag & i) {
r_strbuf_append (sb, "+");
r_strbuf_append (sb, metaflagname[i]);
}
}
return r_strbuf_drain (sb);
}
Loading