Wip Fix r2 -d oo (workaround, because it's a race condition in fork+pid2path)

This commit is contained in:
pancake 2014-11-03 11:47:51 +01:00
parent 3c6ae9ed33
commit dcfb90ae94
11 changed files with 38 additions and 15 deletions

View File

@ -245,8 +245,9 @@ R_API char *r_anal_strmask (RAnal *anal, const char *data) {
R_API void r_anal_trace_bb(RAnal *anal, ut64 addr) {
RAnalBlock *bbi;
RAnalFunction *fcni;
RListIter *iter, *iter2;
RListIter *iter2;
#define OLD 0
RListIter *iter;
#if OLD
r_list_foreach (anal->fcns, iter, fcni) {
r_list_foreach (fcni->bbs, iter2, bbi) {

View File

@ -43,7 +43,8 @@ static void stream_file_read_pages(R_STREAM_FILE *stream_file, int start_indx,
// tmp = stream_file->pages[i];
page_offset = stream_file->pages[i] * stream_file->page_size;
stream_file->buf->cur = page_offset;
r_buf_read_at(stream_file->buf, page_offset, res, stream_file->page_size);
r_buf_read_at(stream_file->buf, page_offset,
(ut8*)res, stream_file->page_size);
// fseek(stream_file->fp, page_offset, SEEK_SET);
// curr_pos = ftell(stream_file->fp);
// fread(res, stream_file->page_size, 1, stream_file->fp);

View File

@ -43,6 +43,8 @@ static void r_core_file_info (RCore *core, int mode) {
} else fn = (cf && cf->desc) ? cf->desc->name : NULL;
if (cf && mode == R_CORE_BIN_JSON) {
r_cons_printf ("\"file\":\"%s\"", fn);
if (cf->desc->referer)
r_cons_printf ("\"referer\":\"%s\"", cf->desc->referer);
if (dbg) dbg = R_IO_WRITE | R_IO_EXEC;
if (cf->desc) {
r_cons_printf (",\"fd\":%d", cf->desc->fd);
@ -65,6 +67,8 @@ static void r_core_file_info (RCore *core, int mode) {
} else if (cf) {
//r_cons_printf ("# Core file info\n");
r_cons_printf ("file\t%s\n", fn);
if (cf->desc->referer)
r_cons_printf ("referer\t%s\n", cf->desc->referer);
if (dbg) dbg = R_IO_WRITE | R_IO_EXEC;
if (cf->desc) {
r_cons_printf ("fd\t%d\n", cf->desc->fd);

View File

@ -30,16 +30,19 @@ R_API int r_core_file_reopen(RCore *core, const char *args, int perm, int loadbi
ofilepath = odesc->uri;
}
}
if (r_sandbox_enable (0)) {
eprintf ("Cannot reopen in sandbox\n");
return R_FALSE;
}
#if 0
if (isdebug) {
// if its in debugger mode we have to respawn a new process
// instead of reattaching
free (ofilepath);
ofilepath = r_str_newf ("dbg://%s", odesc->name);
}
#endif
if (!core->file) {
eprintf ("No file opened to reopen\n");
free (ofilepath);
@ -57,9 +60,7 @@ R_API int r_core_file_reopen(RCore *core, const char *args, int perm, int loadbi
perm = 4; //R_IO_READ;
}
}
if (ofilepath) {
path = strdup (ofilepath);
} else {
if (!ofilepath) {
eprintf ("Unknown file path");
return R_FALSE;
}
@ -72,6 +73,9 @@ R_API int r_core_file_reopen(RCore *core, const char *args, int perm, int loadbi
}
// closing the file to make sure there are no collisions
// when the new memory maps are created.
path = strdup (ofilepath);
obinfilepath = strdup(ofilepath);
file = r_core_file_open (core, path, perm, baddr);
if (file) {
int had_rbin_info = 0;
@ -141,7 +145,7 @@ R_API int r_core_file_reopen(RCore *core, const char *args, int perm, int loadbi
// This is done to ensure that the file is correctly
// loaded into the view
free (obinfilepath);
free (ofilepath);
//free (ofilepath);
free (path);
return ret;
}
@ -689,6 +693,7 @@ R_API void r_core_file_free(RCoreFile *cf) {
}
R_API int r_core_file_close(RCore *r, RCoreFile *fh) {
int ret;
RIODesc *desc = fh && fh->desc? fh->desc : NULL;
RCoreFile *prev_cf = r && r->file != fh ? r->file : NULL;
@ -713,7 +718,10 @@ R_API int r_core_file_close(RCore *r, RCoreFile *fh) {
/* delete filedescriptor from io descs here */
r_io_desc_del (r->io, fh->desc->fd);
int ret = r_list_delete_data (r->files, fh);
// AVOID DOUBLE FREE HERE
r->files->free = NULL;
ret = r_list_delete_data (r->files, fh);
if (ret) {
if (!prev_cf && r_list_length (r->files) > 0)
prev_cf = (RCoreFile *) r_list_get_n (r->files, 0);

View File

@ -457,7 +457,6 @@ R_API int r_debug_step_over(RDebug *dbg, int steps) {
R_API int r_debug_continue_kill(RDebug *dbg, int sig) {
ut64 pc;
char *out = NULL;
int ret = R_FALSE;
if (!dbg)
return R_FALSE;

View File

@ -103,12 +103,13 @@ static inline RIODesc *__getioplugin(RIO *io, const char *_uri, int flags, int m
RIOPlugin *plugin;
RIODesc *desc = NULL;
char *uri = strdup (_uri);
char *redir = NULL;
for (;;) {
plugin = r_io_plugin_resolve (io, uri, 0);
if (plugin && plugin->open) {
desc = plugin->open (io, uri, flags, mode);
if (io->redirect) {
desc->referer = uri;
redir = uri;
uri = strdup (io->redirect);
r_io_redirect (io, NULL);
continue;
@ -117,6 +118,8 @@ static inline RIODesc *__getioplugin(RIO *io, const char *_uri, int flags, int m
if (desc->fd != -1)
r_io_plugin_open (io, desc->fd, plugin);
desc->uri = uri;
//desc->name = strdup (uri);
desc->referer = redir;
}
}
break;

View File

@ -169,10 +169,12 @@ R_API int r_io_map_del_all(RIO *io, int fd) {
RIOMap *map;
RListIter *iter, *tmp;
ut8 deleted = R_FALSE;
r_list_foreach_safe (io->maps, iter, tmp, map) {
if (fd==-1 || map->fd==fd) {
r_list_delete (io->maps, iter);
deleted = R_TRUE;
if (io && io->maps) {
r_list_foreach_safe (io->maps, iter, tmp, map) {
if (fd==-1 || map->fd==fd) {
r_list_delete (io->maps, iter);
deleted = R_TRUE;
}
}
}
return deleted;

View File

@ -240,9 +240,11 @@ static RIODesc *__open(RIO *io, const char *file, int rw, int mode) {
}
return NULL;
}
riom = R_NEW (RIOMach);
riom = R_NEW0 (RIOMach);
riom->pid = pid;
riom->task = task;
// sleep 1s to get proper path (program name instead of ls) (racy)
r_sys_sleep (1);
pidpath = r_sys_pid_to_path (pid);
ret = r_io_desc_new (&r_io_plugin_mach, riom->pid,
pidpath, rw | R_IO_EXEC, mode, riom);

View File

@ -189,6 +189,8 @@ static RIODesc *__open(RIO *io, const char *file, int rw, int mode) {
riop->pid = riop->tid = pid;
open_pidmem (riop);
pidpath = r_sys_pid_to_path (pid);
// sleep 1s to get proper path (racy)
r_sys_sleep (1);
desc = r_io_desc_new (&r_io_plugin_ptrace, pid,
pidpath, rw | R_IO_EXEC, mode, riop);
free (pidpath);

View File

@ -172,7 +172,7 @@ static int r_buf_cpy(RBuffer *b, ut64 addr, ut8 *dst, const ut8 *src, int len, i
int end;
if (!b || b->empty) return 0;
addr = (addr==R_BUF_CUR)? b->cur: addr-b->base;
if (len<1 || dst == NULL || addr <0 || addr > b->length)
if (len<1 || dst == NULL || addr > b->length)
return -1;
end = (int)(addr+len);
if (end > b->length)

View File

@ -611,6 +611,7 @@ R_API char *r_sys_pid_to_path(int pid) {
return NULL;
#elif __APPLE__
char pathbuf[PROC_PIDPATHINFO_MAXSIZE];
pathbuf[0] = 0;
int ret = proc_pidpath (pid, pathbuf, sizeof (pathbuf));
if (ret <= 0)
return NULL;