beauty-fixes for pancake

This commit is contained in:
condret 2017-08-14 00:55:47 +00:00
parent 492efde563
commit 116810aeb7
19 changed files with 144 additions and 112 deletions

View File

@ -14,19 +14,17 @@ static ut64 ws_find_label(int l, RIOBind iob) {
RIO *io = iob.get_io (&iob);
ut64 cur = 0, size = iob.desc_size (io->desc);
ut8 buf[128];
RAsmOp *aop = R_NEW0 (RAsmOp);
RAsmOp aop;;
iob.read_at (iob.io, cur, buf, 128);
while(cur <= size && wsdis(aop, buf, 128)) {
if( aop->buf_asm[0] == 'm' &&
aop->buf_asm[1] == 'a' &&
l == atoi(&aop->buf_asm[5])) {
r_asm_op_free(aop);
while (cur <= size && wsdis(&aop, buf, 128)) {
if( aop.buf_asm[0] == 'm' &&
aop.buf_asm[1] == 'a' &&
l == atoi(&aop.buf_asm[5])) {
return cur;
}
cur = cur + aop->size;
cur = cur + aop.size;
iob.read_at(iob.io, cur, buf, 128);
}
r_asm_op_free(aop);
return 0;
}

View File

@ -2666,7 +2666,7 @@ R_API int r_bin_file_delete(RBin *bin, ut32 bin_fd) {
RListIter *iter;
RBinFile *bf;
RBinFile *cur = r_bin_cur (bin);
if (bin) {
if (bin && cur) {
r_list_foreach (bin->binfiles, iter, bf) {
if (bf && bf->fd == bin_fd) {
if (cur->fd == bin_fd) {

View File

@ -398,10 +398,12 @@ R_API RList *r_core_asm_bwdisassemble(RCore *core, ut64 addr, int n, int len) {
}
buf = (ut8 *)malloc (len);
if (!hits || !buf) {
if (!buf) {
if (hits) {
r_list_free (hits);
}
return NULL;
} else if (!hits) {
free (buf);
return NULL;
}

View File

@ -2601,7 +2601,7 @@ R_API int r_core_config_init(RCore *core) {
SETI ("io.buffer.from", 0, "Lower address of buffered cache");
SETI ("io.buffer.to", 0, "Higher address of buffered cache");
SETCB ("io.cache", "false", &cb_iocache, "Enable cache for io changes");
SETCB("io.pcache", "false", &cb_iopcache, "io.cache for p-level");
SETCB ("io.pcache", "false", &cb_iopcache, "io.cache for p-level");
SETCB ("io.ff", "true", &cb_ioff, "Fill invalid buffers with 0xff instead of returning error");
SETICB ("io.0xff", 0xff, &cb_io_oxff, "Use this value instead of 0xff to fill unallocated areas");
SETCB ("io.aslr", "false", &cb_ioaslr, "Disable ASLR for spawn and such");

View File

@ -436,7 +436,7 @@ static bool desc_list_cb(void *user, void *data, ut32 id) {
if (desc->io && desc->io->va && desc->io->maps) {
ls_foreach_prev (desc->io->maps, iter, map) {
if (map->fd == desc->fd) {
p->cb_printf ("\t+0x%"PFMT64x" 0x%"PFMT64x
p->cb_printf (" +0x%"PFMT64x" 0x%"PFMT64x
" - 0x%"PFMT64x" : %s : %s\n", map->delta,
map->from, map->to, r_str_rwx_i (map->flags), "");
}

View File

@ -1096,8 +1096,11 @@ static int cmd_write(void *data, const char *input) {
}
} else {
if (toend) {
sz = r_io_desc_size (core->file->desc) - core->offset;
r_core_dump (core, filename, core->offset, (ut64)sz, append);
sz = r_io_desc_size (core->file->desc);
if (sz != UT64_MAX) {
sz -= core->offset;
r_core_dump (core, filename, core->offset, (ut64)sz, append);
}
} else {
if (!r_file_dump (filename, core->block, core->blocksize, append)) {
sz = 0;

View File

@ -182,6 +182,7 @@ typedef struct r_io_plugin_t {
ut64 (*lseek)(RIO *io, RIODesc *fd, ut64 offset, int whence);
int (*write)(RIO *io, RIODesc *fd, const ut8 *buf, int count);
int (*close)(RIODesc *desc);
bool (*is_blockdevice)(RIODesc *desc);
int (*getpid)(RIODesc *desc);
int (*gettid)(RIODesc *desc);
bool (*resize)(RIO *io, RIODesc *fd, ut64 size);
@ -264,7 +265,6 @@ R_API int r_io_plugin_generate(RIO *io);
R_API bool r_io_plugin_add(RIO *io, RIOPlugin *plugin);
R_API int r_io_plugin_list(RIO *io);
R_API int r_io_is_listener(RIO *io);
R_API bool r_io_is_blockdevice(RIO *io);
R_API RIOPlugin *r_io_plugin_byname(RIO *io, const char *name);
R_API RIOPlugin *r_io_plugin_resolve(RIO *io, const char *filename, bool many);
@ -298,6 +298,8 @@ R_API int r_io_write_at(RIO *io, ut64 addr, const ut8 *buf, int len);
R_API int r_io_pwrite_at(RIO *io, ut64 paddr, const ut8 *buf, int len);
R_API int r_io_vwrite_at(RIO *io, ut64 paddr, const ut8 *buf, int len);
R_API ut64 r_io_seek(RIO *io, ut64 offset, int whence);
R_API ut64 r_io_fd_size(RIO *io, int fd);
R_API bool r_io_fd_is_blockdevice(RIO *io, int fd);
R_API int r_io_system(RIO *io, const char *cmd);
R_API int r_io_plugin_close(RIO *io, RIODesc *desc);
R_API bool r_io_close(RIO *io, int fd);
@ -420,6 +422,7 @@ R_API bool r_io_desc_del (RIO *io, int fd);
R_API RIODesc *r_io_desc_get (RIO *io, int fd);
R_API ut64 r_io_desc_seek (RIODesc *desc, ut64 offset, int whence);
R_API ut64 r_io_desc_size (RIODesc *desc);
R_API bool r_io_desc_is_blockdevice (RIODesc *desc);
R_API bool r_io_desc_exchange (RIO *io, int fd, int fdx);
R_API int r_io_desc_get_pid (RIO *io, int fd);
R_API int r_io_desc_get_tid (RIO *io, int fd);

View File

@ -1,4 +1,4 @@
/* radare - LGPL - Copyright 2008-2015 - pancake */
/* radare - LGPL - Copyright 2008-2017 - pancake */
// TODO: implement a more inteligent way to store cached memory
// TODO: define limit of max mem to cache

View File

@ -117,13 +117,23 @@ R_API ut64 r_io_desc_size(RIODesc* desc) {
if (!desc || !desc->plugin || !desc->plugin->lseek) {
return 0LL;
}
if (r_io_desc_is_blockdevice (desc)) {
return UT64_MAX;
}
off = desc->plugin->lseek (desc->io, desc, 0LL, R_IO_SEEK_CUR);
ret = desc->plugin->lseek (desc->io, desc, 0LL, R_IO_SEEK_END);
//what to do if that seek fails?
desc->plugin->lseek (desc->io, desc, off, R_IO_SEEK_CUR);
desc->plugin->lseek (desc->io, desc, off, R_IO_SEEK_SET);
return ret;
}
R_API bool r_io_desc_is_blockdevice (RIODesc *desc) {
if (!desc || !desc->plugin || !desc->plugin->is_blockdevice) {
return false;
}
return desc->plugin->is_blockdevice (desc);
}
R_API bool r_io_desc_exchange(RIO* io, int fd, int fdx) {
RIODesc* desc, * descx;
SdbListIter* iter;

View File

@ -440,19 +440,19 @@ R_API int r_io_vread_at(RIO *io, ut64 vaddr, ut8 *buf, int len) {
//the API differs with SIOL in that returns a bool instead of amount read
R_API int r_io_read_at(RIO *io, ut64 addr, ut8 *buf, int len) {
int ret;
if (!io || !buf || len < 1) {
return 0;
}
if (io->va) {
ret = r_io_vread_at (io, addr, buf, len);
} else {
ret = r_io_pread_at (io, addr, buf, len);
}
if (io->cached_read) {
r_io_cache_read (io, addr, buf, len);
}
return ret;
int ret;
if (!io || !buf || len < 1) {
return 0;
}
if (io->va) {
ret = r_io_vread_at (io, addr, buf, len);
} else {
ret = r_io_pread_at (io, addr, buf, len);
}
if (io->cached_read) {
r_io_cache_read (io, addr, buf, len);
}
return ret;
}
@ -694,29 +694,29 @@ R_API int r_io_vwrite_at(RIO *io, ut64 vaddr, const ut8 *buf, int len) {
}
R_API int r_io_write_at(RIO *io, ut64 addr, const ut8 *buf, int len) {
int i, ret = 0;
ut8 *mybuf = (ut8*)buf;
if (!io || !buf || len < 1) {
return 0;
}
if (io->write_mask_buf) {
mybuf = r_mem_dup ((void*)buf, len);
for (i = 0; i < len; i++) {
//this sucks
mybuf[i] &= io->write_mask_buf[i % io->write_mask_len];
}
}
if (io->cached) {
ret = r_io_cache_write (io, addr, mybuf, len);
} else if (io->va) {
ret = r_io_vwrite_at (io, addr, mybuf, len);
} else {
ret = r_io_pwrite_at (io, addr, mybuf, len);
}
if (buf != mybuf) {
free (mybuf);
}
return ret;
int i, ret = 0;
ut8 *mybuf = (ut8*)buf;
if (!io || !buf || len < 1) {
return 0;
}
if (io->write_mask_buf) {
mybuf = r_mem_dup ((void*)buf, len);
for (i = 0; i < len; i++) {
//this sucks
mybuf[i] &= io->write_mask_buf[i % io->write_mask_len];
}
}
if (io->cached) {
ret = r_io_cache_write (io, addr, mybuf, len);
} else if (io->va) {
ret = r_io_vwrite_at (io, addr, mybuf, len);
} else {
ret = r_io_pwrite_at (io, addr, mybuf, len);
}
if (buf != mybuf) {
free (mybuf);
}
return ret;
}
R_API ut64 r_io_seek(RIO *io, ut64 offset, int whence) {
@ -773,28 +773,9 @@ R_API ut64 r_io_fd_size(RIO *io, int fd) {
return r_io_desc_size (desc);
}
R_API bool r_io_is_blockdevice(RIO *io) {
#if __UNIX__
if (io && io->desc && io->desc->fd) {
struct stat buf;
if (io->desc->obsz) {
return 1;
}
if (fstat (io->desc->fd, &buf) == -1)
return 0;
if (io->plugin == &r_io_plugin_default) {
// TODO: optimal blocksize = 2048 for disk, 4096 for files
// usually is 128K
// eprintf ("OPtimal blocksize: %d\n", buf.st_blksize);
if ((buf.st_mode & S_IFCHR) == S_IFCHR) {
io->desc->obsz = buf.st_blksize;
return true;
}
return ((buf.st_mode & S_IFBLK) == S_IFBLK);
}
}
#endif
return false;
R_API bool r_io_fd_is_blockdevice(RIO *io, int fd) {
RIODesc *desc = r_io_desc_get (io, fd);
return r_io_desc_is_blockdevice (desc);
}
R_API ut64 r_io_size(RIO *io) {
@ -996,7 +977,7 @@ R_API int r_io_pwrite_at(RIO* io, ut64 paddr, const ut8* buf, int len) {
//check pointers and permissions
int ret;
if (!io || !buf || !io->desc ||
(!io->p_cache && !(io->desc->flags & R_IO_WRITE)) || len < 1) {
(!io->p_cache && !(io->desc->flags & R_IO_WRITE)) || len < 1) {
return 0;
}
if (io->p_cache) {

View File

@ -1,4 +1,4 @@
/* radare - LGPL - Copyright 2007-2016 - pancake */
/* radare - LGPL - Copyright 2007-2017 - pancake */
#include <r_io.h>
#include <r_lib.h>
@ -106,7 +106,7 @@ static int fork_and_ptraceme(RIO *io, int bits, const char *cmd) {
}
setup_tokens ();
char *_cmd = io->args ? r_str_appendf (strdup (cmd), " %s", io->args) :
strdup (cmd);
strdup (cmd);
char **argv = r_str_argv (_cmd, NULL);
// We need to build a command line with quoted argument and escaped quotes
int cmd_len = 0;
@ -186,14 +186,22 @@ err_fork:
}
#else // windows
#if !__APPLE__
static void trace_me () {
#if __APPLE__
signal (SIGTRAP, SIG_IGN); //NEED BY STEP
#endif
#if __APPLE__ || __BSD__
/* we can probably remove this #if..as long as PT_TRACE_ME is redefined for OSX in r_debug.h */
signal (SIGABRT, inferior_abort_handler);
if (ptrace (PT_TRACE_ME, 0, 0, 0) != 0) {
#else
if (ptrace (PTRACE_TRACEME, 0, NULL, NULL) != 0) {
r_sys_perror ("ptrace-traceme");
exit (MAGIC_EXIT);
}
}
#endif
}
void handle_posix_error(int err) {
switch (err) {

View File

@ -3,6 +3,7 @@
#include <r_userconf.h>
#include <r_io.h>
#include <r_lib.h>
#include <stdio.h>
typedef struct r_io_mmo_t {
char * filename;
@ -202,7 +203,7 @@ static int r_io_def_mmap_read(RIO *io, RIODesc *fd, ut8 *buf, int count) {
a_buf = malloc (a_count+aligned);
if (a_buf) {
int i;
memset (a_buf, 0xff, a_count+aligned);
memset (a_buf, 0xff, a_count + aligned);
if (lseek (mmo->fd, a_off, SEEK_SET) < 0) {
free (a_buf);
return -1;
@ -365,6 +366,20 @@ static bool __resize(RIO *io, RIODesc *fd, ut64 size) {
return r_io_def_mmap_truncate (mmo, size);
}
#if __UNIX__
static bool __is_blockdevice (RIODesc *desc) {
RIOMMapFileObj *mmo;
struct stat buf;
if (!desc || !desc->data) {
return false;
}
mmo = desc->data;
if (fstat (mmo->fd, &buf) == -1)
return false;
return ((buf.st_mode & S_IFBLK) == S_IFBLK);
}
#endif
struct r_io_plugin_t r_io_plugin_default = {
.name = "default",
.desc = "open local files using def_mmap://",
@ -376,6 +391,9 @@ struct r_io_plugin_t r_io_plugin_default = {
.lseek = __lseek,
.write = __write,
.resize = __resize,
#if __UNIX__
.is_blockdevice = __is_blockdevice,
#endif
};
#ifndef CORELIB

View File

@ -1,4 +1,4 @@
/* radare - LGPL - Copyright 2010-2016 pancake */
/* radare - LGPL - Copyright 2010-2017 pancake */
#include <r_io.h>
#include <r_lib.h>
@ -72,7 +72,7 @@ static int debug_gdb_write_at(const ut8 *buf, int sz, ut64 addr) {
static RIODesc *__open(RIO *io, const char *file, int rw, int mode) {
RIOGdb *riog;
char host[128], *port, *pid;
char host[128], *port, *pid, *name;
int i_port = -1;
bool isdev = false;
@ -127,8 +127,7 @@ static RIODesc *__open(RIO *io, const char *file, int rw, int mode) {
i_port = atoi (port);
}
riog = R_NEW0 (RIOGdb);
if (!riog) {
if (!(riog = R_NEW0 (RIOGdb))) {
return NULL;
}
gdbr_init (&riog->desc, false);
@ -137,18 +136,23 @@ static RIODesc *__open(RIO *io, const char *file, int rw, int mode) {
desc = &riog->desc;
if (pid) { // FIXME this is here for now because RDebug's pid and libgdbr's aren't properly synced.
desc->pid = i_pid;
int ret = gdbr_attach (desc, i_pid);
if (ret < 0) {
if (gdbr_attach (desc, i_pid) < 0) {
eprintf ("gdbr: Failed to attach to PID %i\n", i_pid);
return NULL;
}
}
} else if ((i_pid = desc->pid) < 0) {
i_pid = -1;
}
riogdb = r_io_desc_new (io, &r_io_plugin_gdb, file, rw, mode, riog);
return riogdb;
}
eprintf ("gdb.io.open: Cannot connect to host.\n");
free (riog);
return NULL;
// Get name
if (riogdb) {
riogdb->name = gdbr_exec_file_read (desc, i_pid);
} else {
eprintf ("gdb.io.open: Cannot connect to host.\n");
free (riog);
}
return riogdb;
}
static int __write(RIO *io, RIODesc *fd, const ut8 *buf, int count) {
@ -178,6 +182,9 @@ static int __read(RIO *io, RIODesc *fd, ut8 *buf, int count) {
}
static int __close(RIODesc *fd) {
if (fd) {
R_FREE (fd->name);
}
gdbr_disconnect (desc);
gdbr_cleanup (desc);
free (desc);
@ -315,3 +322,11 @@ RIOPlugin r_io_plugin_gdb = {
.system = __system,
.isdbg = true
};
#ifndef CORELIB
RLibStruct radare_plugin = {
.type = R_LIB_TYPE_IO,
.data = &r_io_plugin_gdb,
.version = R2_VERSION
};
#endif

View File

@ -117,13 +117,16 @@ static bool __plugin_open(RIO *io, const char *pathname, bool many) {
static RIODesc *__open(RIO *io, const char *pathname, int rw, int mode) {
if (__plugin_open (io, pathname, 0)) {
RIOGzip *mal = R_NEW0 (RIOGzip);
if (!mal) return NULL;
if (!mal) {
return NULL;
}
int len;
ut8 *data = (ut8*)r_file_slurp (pathname+7, &len);
ut8 *data = (ut8*)r_file_slurp (pathname+7, &len); //memleak here?
mal->buf = r_inflate (data, len, NULL, &mal->size);
if (mal->buf)
if (mal->buf) {
return r_io_desc_new (io, &r_io_plugin_malloc,
pathname, rw, mode, mal);
}
free (data);
eprintf ("Cannot allocate (%s) %d bytes\n", pathname+9,
mal->size);

View File

@ -381,7 +381,7 @@ static RIODesc *__open(RIO *io, const char *pathname, int rw, int mode) {
free (str);
return NULL;
}
mal->rbuf = r_buf_new_sparse();
mal->rbuf = r_buf_new_sparse ();
if (!mal->rbuf) {
free (str);
free (mal);

View File

@ -473,10 +473,7 @@ static int __system(RIO *io, RIODesc *fd, const char *cmd) {
static int __get_pid (RIODesc *desc) {
RIOMach *mach = desc ? (RIOMach *) desc->data : NULL;
if (mach) {
return mach->pid;
}
return -1;
return mach ? mach->pid : -1;
}
// TODO: rename ptrace to io_mach .. err io.ptrace ??

View File

@ -11,7 +11,7 @@
#include "io_r2k_windows.h"
#elif defined (__linux__) && !defined (__GNU__)
#include "io_r2k_linux.h"
struct io_r2k_linux r2k_struct;
struct io_r2k_linux r2k_struct; //TODO: move this into desc->data
#endif
int r2k__write(RIO *io, RIODesc *fd, const ut8 *buf, int count) {
@ -65,7 +65,7 @@ static int r2k__close(RIODesc *fd) {
}
#elif defined (__linux__) && !defined (__GNU__)
if (fd) {
close ((int)fd->data);
close ((int)(size_t)fd->data);
}
#else
eprintf ("TODO: r2k not implemented for this plataform.\n");
@ -109,7 +109,6 @@ static RIODesc *r2k__open(RIO *io, const char *pathname, int rw, int mode) {
//return r_io_desc_new (&r_io_plugin_r2k, -1, pathname, rw, mode, w32);
return r_io_desc_new (io, &r_io_plugin_r2k, pathname, rw, mode, w32);
#elif defined (__linux__) && !defined (__GNU__)
RIODesc *iodesc = NULL;
int fd = open ("/dev/r2k", O_RDONLY);
if (fd == -1) {
io->cb_printf ("r2k__open: Error in opening /dev/r2k.");
@ -119,10 +118,7 @@ static RIODesc *r2k__open(RIO *io, const char *pathname, int rw, int mode) {
r2k_struct.beid = 0;
r2k_struct.pid = 0;
r2k_struct.wp = 1;
//return r_io_desc_new (&r_io_plugin_r2k, fd, pathname, rw, mode, NULL);
iodesc = r_io_desc_new (io, &r_io_plugin_r2k, pathname, rw, mode, NULL);
iodesc->data = (void *)fd;
return iodesc;
return r_io_desc_new (io, &r_io_plugin_r2k, pathname, rw, mode, (void *)fd);
#else
io->cb_printf ("Not supported on this platform\n");
#endif

View File

@ -87,9 +87,10 @@ static RIODesc *__open(RIO *io, const char *pathname, int rw, int mode) {
free (data);
}
}
if (mal->buf)
if (mal->buf) {
return r_io_desc_new (io, &r_io_plugin_sparse,
pathname, rw, mode, mal);
}
r_buf_free (mal->buf);
free (mal);
}

View File

@ -54,10 +54,7 @@ R_API RIOPlugin *r_io_plugin_resolve(RIO *io, const char *filename, bool many) {
SdbListIter *iter;
RIOPlugin *ret;
ls_foreach (io->plugins, iter, ret) {
if (!ret) {
continue;
}
if (!ret->check) {
if (!ret || !ret->check) {
continue;
}
if (ret->check (io, filename, many))