* Remove some use of alloca()

* Fix lock when using 'r2 -d'
* Invalid program name results in error, not warning
This commit is contained in:
pancake 2010-10-12 13:22:19 +02:00
parent 17542ef3de
commit 8f9dbbd0d1
8 changed files with 68 additions and 71 deletions

3
TODO
View File

@ -23,9 +23,7 @@ Questions
* Add support for float/double in r_num :?
* in Visual Debugger mode cursor mode also scrolls O_o
* distribute 'spp' with 'rarc2' ? imho no
* r2 -dls HANGS!!1
* rarc2 allows to compile invalid code like calling puts() out of context
* Add rabin2 .! calls into r_core api? r_core_rabin_import(core, "rIs") ?
0.6 RELEASE
===========
@ -57,6 +55,7 @@ TODO nibble
TODO edu
--------
* remove all uses of alloca()
* Implement more get_main()
* typedef all function pointers, like in r_bp
* Implement /A : search AES

View File

@ -9,55 +9,57 @@
static int disassemble(struct r_asm_t *a, struct r_asm_aop_t *aop, ut8 *buf, ut64 len) {
int i;
char *buf_cp, *b;
int i;
if ((b = buf_cp = alloca(len+1)) == NULL)
if ((b = buf_cp = malloc (len+1)) == NULL)
return 0;
memcpy(buf_cp, buf, len+1);
memcpy (buf_cp, buf, len+1);
for(i=0;b[0] == b[1] && i<len; b=b+1,i++); b[1] = '\0';
for (i=0; b[0] == b[1] && i<len; b++, i++);
b[1] = '\0';
switch(buf[0]) {
case '[':
strcpy(aop->buf_asm, "[ loop {");
strcpy (aop->buf_asm, "[ loop {");
break;
case ']':
strcpy(aop->buf_asm, "] }"); // TODO: detect clause and put label name
strcpy (aop->buf_asm, "] }"); // TODO: detect clause and put label name
break;
case '>':
if (i>1) strcpy(aop->buf_asm, "> add [ptr]");
else strcpy(aop->buf_asm, "> inc [ptr]");
if (i>1) strcpy (aop->buf_asm, "> add [ptr]");
else strcpy (aop->buf_asm, "> inc [ptr]");
break;
case '<':
if (i>1) strcpy(aop->buf_asm, "< sub [ptr]");
else strcpy(aop->buf_asm, "< dec [ptr]");
if (i>1) strcpy (aop->buf_asm, "< sub [ptr]");
else strcpy (aop->buf_asm, "< dec [ptr]");
break;
case '+':
if (i>1) strcpy(aop->buf_asm, "+ add [ptr]");
else strcpy(aop->buf_asm, "+ inc [ptr]");
if (i>1) strcpy (aop->buf_asm, "+ add [ptr]");
else strcpy (aop->buf_asm, "+ inc [ptr]");
break;
case '-':
if (i>1) strcpy(aop->buf_asm, "- sub [ptr]");
else strcpy(aop->buf_asm, "- dec [ptr]");
if (i>1) strcpy (aop->buf_asm, "- sub [ptr]");
else strcpy (aop->buf_asm, "- dec [ptr]");
break;
case ',':
strcpy(aop->buf_asm, ", [ptr] = getch()");
strcpy (aop->buf_asm, ", [ptr] = getch()");
break;
case '.':
strcpy(aop->buf_asm, ". print( [ptr] )");
strcpy (aop->buf_asm, ". print( [ptr] )");
break;
case '\x00':
strcpy(aop->buf_asm, " trap");
strcpy (aop->buf_asm, " trap");
break;
default:
strcpy(aop->buf_asm, " nop");
strcpy (aop->buf_asm, " nop");
break;
}
if (i>0) sprintf(aop->buf_asm, "%s, %d", aop->buf_asm, i+1);
if (i>0) sprintf (aop->buf_asm, "%s, %d", aop->buf_asm, i+1);
if (i<1) i=1; else i++;
free (buf_cp);
return i;
}

View File

@ -756,12 +756,17 @@ static void *gdbwrap_writememory(gdbwrap_t *desc, la32 linaddr,
static void *gdbwrap_writememory2(gdbwrap_t *desc, la32 linaddr,
void *value, unsigned bytes)
{
char *rec;
char *packet = alloca(2 * bytes + MSG_BUF);
char *rec *packet;
u_char *val = value;
u_short i;
u_int len;
packet = malloc (2*bytes+MSG_BUF);
if (packet == NULL) {
eprintf ("Cannot allocate %d bytes\n", 2*bytes+MSG_BUF);
return;
}
snprintf(packet, MSG_BUF, "%s%x%s%x%s", GDBWRAP_MEMWRITE2,
linaddr, GDBWRAP_SEP_COMMA, bytes, GDBWRAP_SEP_COLON);
@ -773,6 +778,8 @@ static void *gdbwrap_writememory2(gdbwrap_t *desc, la32 linaddr,
}
rec = gdbwrap_send_data(desc, packet);
free (packet);
return rec;
}

View File

@ -79,7 +79,6 @@ R_API int r_io_open(struct r_io_t *io, const char *file, int flags, int mode) {
for (;;) {
plugin = r_io_plugin_resolve (io, uri);
if (plugin) {
printf("plugin %s\n", plugin->name);
fd = plugin->open (io, uri, flags, mode);
if (io->redirect) {
free ((void *)uri);

View File

@ -6,7 +6,7 @@
#if __linux__ || __NetBSD__ || __FreeBSD__ || __OpenBSD__ || __APPLE__ || __WINDOWS__
#define MAGIC_EXIT 31337
#define MAGIC_EXIT 123
#include <signal.h>
#if __UNIX__
@ -81,7 +81,8 @@ static int fork_and_ptraceme(const char *cmd) {
DEBUG_EVENT de;
int pid, tid;
HANDLE th = INVALID_HANDLE_VALUE;
if (!*cmd)
return -1;
setup_tokens ();
/* TODO: with args */
if (!CreateProcess (cmd, NULL,
@ -154,6 +155,7 @@ err_fork:
}
#else
#if 0
static int __waitpid(int pid) {
int st = 0;
if (waitpid (pid, &st, 0) == -1)
@ -166,6 +168,7 @@ static int __waitpid(int pid) {
}
return R_TRUE;
}
#endif
static int fork_and_ptraceme(const char *cmd) {
char **argv;
@ -202,17 +205,18 @@ static int fork_and_ptraceme(const char *cmd) {
wait (&status);
if (WIFSTOPPED (status))
eprintf ("Process with PID %d started...\n", (int)pid);
// XXX
//kill (pid, SIGSTOP);
if (WEXITSTATUS (status))
pid = -1;
// XXX kill (pid, SIGSTOP);
break;
}
printf ("PID = %d\n", pid);
eprintf ("PID = %d\n", pid);
return pid;
}
#endif
static int __plugin_open(struct r_io_t *io, const char *file) {
if (!memcmp (file, "dbg://", 6))
if (!memcmp (file, "dbg://", 6) && file[6])
return R_TRUE;
return R_FALSE;
}

View File

@ -20,8 +20,8 @@ R_API int r_vm_op_eval(struct r_vm_t *vm, const char *str) {
p = alloca(len);
s = alloca(len);
memcpy(p, str, len);
memcpy(s, str, len);
memcpy (p, str, len);
memcpy (s, str, len);
r_str_subchr (s, ',', 0);
r_str_subchr (s, '\t', 0);
r_str_subchr (s, '#', 0);

View File

@ -47,7 +47,7 @@ R_API int r_vm_reg_type_i(const char *str) {
return -1;
}
R_API int r_vm_reg_del(struct r_vm_t *vm, const char *name) {
R_API int r_vm_reg_del(RVm *vm, const char *name) {
struct list_head *pos;
list_for_each(pos, &vm->regs) {
@ -60,7 +60,7 @@ R_API int r_vm_reg_del(struct r_vm_t *vm, const char *name) {
return R_TRUE;
}
R_API int r_vm_reg_set(struct r_vm_t *vm, const char *name, ut64 value) {
R_API int r_vm_reg_set(RVm *vm, const char *name, ut64 value) {
struct list_head *pos;
if (name)
list_for_each(pos, &vm->regs) {
@ -78,7 +78,7 @@ R_API int r_vm_reg_set(struct r_vm_t *vm, const char *name, ut64 value) {
return R_FALSE;
}
R_API int r_vm_reg_alias_list(struct r_vm_t *vm) {
R_API int r_vm_reg_alias_list(RVm *vm) {
struct r_vm_reg_t *reg;
struct list_head *pos;
int len,space;
@ -100,7 +100,7 @@ R_API int r_vm_reg_alias_list(struct r_vm_t *vm) {
return 0;
}
R_API int r_vm_reg_alias(struct r_vm_t *vm, const char *name, const char *get, const char *set) {
R_API int r_vm_reg_alias(RVm *vm, const char *name, const char *get, const char *set) {
struct r_vm_reg_t *reg;
struct list_head *pos;
@ -129,20 +129,17 @@ R_API int r_vm_cmd_eval(RVm *vm, const char *cmd) {
*next=0;
next++;
}
if (strlen(cmd)>2 && !memcmp (cmd, "av", 2))
if (strlen (cmd)>2 && !memcmp (cmd, "av", 2))
r_vm_cmd_reg (vm, cmd+2);
cmd = next;
} while (next);
return R_TRUE;
}
R_API int r_vm_cmd_reg(struct r_vm_t *vm, const char *_str) {
char *str, *ptr;
int len;
len = strlen (_str)+1;
str = alloca (len);
memcpy (str, _str, len); // XXX: suboptimal
R_API int r_vm_cmd_reg(RVm *vm, const char *_str) {
char *str, ostr[128], *ptr;
str = ostr;
strncpy (str, _str, sizeof (ostr)-1);
switch(*str) {
case '*':
@ -175,31 +172,31 @@ R_API int r_vm_cmd_reg(struct r_vm_t *vm, const char *_str) {
case 'a':
if (str[1]==' ') {
char *get,*set;
get = strchr(str+2, ' ');
get = strchr (str+2, ' ');
if (get) {
get[0]='\0';
get = get+1;
set = strchr(get, ' ');
set = strchr (get, ' ');
if (set) {
set[0]='\0';
set = set +1;
r_vm_reg_alias(vm, str+2, get, set);
*set = '\0';
set++;
r_vm_reg_alias (vm, str+2, get, set);
}
}
} else r_vm_reg_alias_list(vm);
} else r_vm_reg_alias_list (vm);
break;
case 't':
r_vm_reg_type_list(vm);
r_vm_reg_type_list (vm);
break;
case '+':
// add register
// avr+ eax int32
for(str=str+1;str&&*str==' ';str=str+1);
for (str=str+1;str&&*str==' ';str=str+1);
ptr = strchr(str, ' ');
if (ptr) {
ptr[0]='\0';
r_vm_reg_add(vm, str, r_vm_reg_type_i(ptr+1), 0);
} else r_vm_reg_add(vm, str, R_VMREG_INT32, 0);
r_vm_reg_add (vm, str, r_vm_reg_type_i(ptr+1), 0);
} else r_vm_reg_add (vm, str, R_VMREG_INT32, 0);
break;
case '-':
// rm register
@ -214,11 +211,11 @@ R_API int r_vm_cmd_reg(struct r_vm_t *vm, const char *_str) {
r_vm_setup_flags (vm, str+2);
break;
default:
for(;str&&*str==' ';str=str+1);
ptr = strchr(str, '=');
for (;str && *str==' '; str++);
ptr = strchr (str, '=');
if (ptr) {
//vm_eval(str);
r_vm_op_eval(vm, str);
r_vm_op_eval (vm, str);
#if 0
/* set register value */
ptr[0]='\0';
@ -226,18 +223,14 @@ R_API int r_vm_cmd_reg(struct r_vm_t *vm, const char *_str) {
ptr[0]='=';
#endif
} else {
if (*str=='.') {
r_vm_print(vm, r_vm_reg_type_i(str+1));
} else {
/* show single registers */
eprintf("%s = 0x%08"PFMT64x"\n", str, r_vm_reg_get(vm, str));
}
if (*str=='.') r_vm_print (vm, r_vm_reg_type_i(str+1));
else eprintf ("%s = 0x%08"PFMT64x"\n", str, r_vm_reg_get(vm, str));
}
}
return 0;
}
R_API ut64 r_vm_reg_get(struct r_vm_t *vm, const char *name) {
R_API ut64 r_vm_reg_get(RVm *vm, const char *name) {
struct list_head *pos;
int len;
if (!name)

View File

@ -3,9 +3,6 @@
#include "r_vm.h"
#include "p/plugins.h"
/* TODO: move into r_vm_t */
int vm_arch = -1;
static ut64 r_vm_get_value(RVm *vm, const char *str) {
ut64 ret = 0LL;
for (;*str&&*str==' ';str=str+1);
@ -193,10 +190,6 @@ R_API int r_vm_set_arch(RVm *vm, const char *name, int bits) {
// This is conceptually rotten
R_API int r_vm_init(RVm *vm, int init) {
#if 0
if (config.arch != vm_arch)
init = 1;
#endif
if (init) {
vm->log = 0;
vm->use_mmu_cache = 0;