Initial import of the WIP support for WebAssembly (bin + disasm)

This commit is contained in:
pancake 2017-03-02 14:42:05 +01:00
parent 700e7734ba
commit a41a8252ae
9 changed files with 387 additions and 2 deletions

97
libr/asm/p/asm_wasm.c Normal file
View File

@ -0,0 +1,97 @@
/* radare - LGPL - Copyright 2017 - pancake */
// http://webassembly.org/docs/binary-encoding/#module-structure
#include <stdio.h>
#include <string.h>
#include <r_types.h>
#include <r_lib.h>
#include <r_asm.h>
static int disassemble(RAsm *a, RAsmOp *op, const ut8 *buf, int len) {
int rep = 1;
switch (buf[0]) {
case 0x00:
sprintf (op->buf_asm, "trap");
break;
case 0x01:
sprintf (op->buf_asm, "nop");
break;
case 0x05:
sprintf (op->buf_asm, "else");
break;
case 0x0b:
sprintf (op->buf_asm, "end");
break;
case 0x0f:
sprintf (op->buf_asm, "return");
break;
case 0x10:
{
ut64 res = 0;
const ut8 *afterBuf = r_uleb128 (buf, 8, &res);
sprintf (op->buf_asm, "call %d", (int)res);
rep = afterBuf - buf;
}
break;
case 0x11:
{
ut64 res = 0;
const ut8 *afterBuf = r_uleb128 (buf, 8, &res);
sprintf (op->buf_asm, "call_indirect %d", (int)res);
rep = afterBuf - buf;
}
break;
case 0x20:
sprintf (op->buf_asm, "get_local %d", buf[1]);
rep = 2;
break;
case 0x21:
sprintf (op->buf_asm, "set_local %d", buf[1]);
rep = 2;
break;
case 0x22:
sprintf (op->buf_asm, "tee_local %d", buf[1]);
rep = 2;
break;
case 0x6a:
sprintf (op->buf_asm, "i32.add");
break;
case 0x1a:
sprintf (op->buf_asm, "drop");
break;
case 0x1b:
sprintf (op->buf_asm, "select");
break;
case 0x45:
sprintf (op->buf_asm, "i32.eqz");
break;
case 0x46:
sprintf (op->buf_asm, "i32.eq");
break;
}
op->size = rep;
return rep;
}
RAsmPlugin r_asm_plugin_wasm = {
.name = "wasm",
.author = "pancake",
.version = "0.1.0",
.arch = "wasm",
.license = "MIT",
.bits = 32,
.endian = R_SYS_ENDIAN_LITTLE,
.desc = "WebAssemlby",
.disassemble = &disassemble
};
#ifndef CORELIB
RLibStruct radare_plugin = {
.type = R_LIB_TYPE_ASM,
.data = &r_asm_plugin_wasm,
.version = R2_VERSION
};
#endif

10
libr/asm/p/wasm.mk Normal file
View File

@ -0,0 +1,10 @@
OBJ_WASM=asm_wasm.o
TARGET_WASM=asm_wasm.${EXT_SO}
STATIC_OBJ+=${OBJ_WASM}
ifeq ($(WITHPIC),1)
ALL_TARGETS+=${TARGET_WASM}
${TARGET_WASM}: ${OBJ_WASM}
${CC} $(call libname,asm_wasm) ${LDFLAGS} ${CFLAGS} -o ${TARGET_WASM} ${OBJ_WASM}
endif

View File

@ -386,7 +386,7 @@ RBinPlugin r_bin_plugin_coff = {
};
#ifndef CORELIB
struct r_lib_struct_t radare_plugin = {
RLibStruct radare_plugin = {
.type = R_LIB_TYPE_BIN,
.data = &r_bin_plugin_coff,
.version = R2_VERSION

264
libr/bin/p/bin_wasm.c Normal file
View File

@ -0,0 +1,264 @@
/* radare2 - LGPL - Copyright 2017 - pancake */
// http://webassembly.org/docs/binary-encoding/#module-structure
#include <r_types.h>
#include <r_util.h>
#include <r_lib.h>
#include <r_bin.h>
static int check_bytes(const ut8 *buf, ut64 length) {
return (buf && length >= 4 && !memcmp (buf, "\x00" "asm", 4));
}
ut64 entrypoint = UT64_MAX;
static int check(RBinFile *arch) {
const ut8 *bytes = arch ? r_buf_buffer (arch->buf) : NULL;
ut64 sz = arch ? r_buf_size (arch->buf): 0;
return check_bytes (bytes, sz);
}
static void * load_bytes(RBinFile *arch, const ut8 *buf, ut64 sz, ut64 loadaddr, Sdb *sdb){
return (void*)(size_t)check_bytes (buf, sz);
}
static int load(RBinFile *arch) {
return check(arch);
}
static int destroy (RBinFile *arch) {
return true;
}
static ut64 baddr(RBinFile *arch) {
return 0;
}
static RBinAddr* binsym(RBinFile *arch, int type) {
return NULL; // TODO
}
static RList* entries(RBinFile *arch) {
RList* ret;
RBinAddr *ptr = NULL;
if (!(ret = r_list_new ())) {
return NULL;
}
ret->free = free;
if ((ptr = R_NEW0 (RBinAddr))) {
ptr->paddr = 8*4;
ptr->vaddr = 8*4;// + baddr (arch);
r_list_append (ret, ptr);
}
return ret;
}
static RList* sections(RBinFile *arch) {
RList *ret = NULL;
RBinSection *ptr = NULL;
ut64 textsize, datasize, symssize, spszsize, pcszsize;
if (!arch->o->info) {
return NULL;
}
if (!(ret = r_list_new ())) {
return NULL;
}
ret->free = free;
int next, i = 0;
ut8 *buf = arch->buf->buf; // skip magic + version
for (i = 8; i< arch->buf->length; ) {
int id = buf[i];
#if 0
1 Type Function signature declarations
2 Import Import declarations
3 Function Function declarations
4 Table Indirect function table and other tables
5 Memory Memory attributes
6 Global Global declarations
7 Export Exports
8 Start Start function declaration
9 Element Elements section
10 Code Function bodies (code)
11 Data Data segments
#endif
ut64 res = 0;
ut8 *p = buf + i + 1;
ut8 *afterBuf = r_uleb128 (p, 8, &res);
int payloadLen = res;
int payloadSize = (int)(size_t)(afterBuf - p);
p += payloadSize;
afterBuf = r_uleb128 (p, 8, &res);
int nameLen = res;
int nameSize = (int)(size_t)(afterBuf - p);
eprintf (" 0x%x len = %d (%d) %d (%d): ", i, payloadLen, payloadSize, nameLen, nameSize);
next = i + payloadSize + nameSize + payloadLen; //payloadLen - payloadSize - nameSize; //nameSize - nameLen + 1; //payloadLen + nameLen + 1;
switch (id) {
case 1: // "type"
eprintf ("type: function signature declarations\n");
break;
case 2:
eprintf ("import:\n");
break;
case 3:
eprintf ("function:\n");
break;
case 4:
eprintf ("table:\n");
break;
case 5:
eprintf ("memory:\n");
break;
case 6:
eprintf ("global:\n");
break;
case 7:
eprintf ("export:\n");
break;
case 8:
eprintf ("start:\n");
break;
case 9:
eprintf ("element:\n");
break;
case 10: //
eprintf ("code:\n");
if (entrypoint == UT64_MAX) {
entrypoint = i + payloadSize + nameSize;
}
if (!(ptr = R_NEW0 (RBinSection))) {
return ret;
}
strncpy (ptr->name, "code", R_BIN_SIZEOF_STRINGS);
ptr->size = payloadLen;
ptr->vsize = payloadLen;
ptr->paddr = i + nameLen + payloadSize + nameSize + 1 + payloadSize;
ptr->vaddr = ptr->paddr;
ptr->srwx = R_BIN_SCN_READABLE | R_BIN_SCN_EXECUTABLE | R_BIN_SCN_MAP; // r-x
ptr->add = true;
r_list_append (ret, ptr);
break;
case 11: //
eprintf ("data:\n");
break;
default:
eprintf ("unknown type id: %d\n", id);
break;
}
if (next <= i) {
eprintf ("Error: prevent infinite loop\n");
break;
}
i = next;
}
// add text segment
#if 0
textsize = r_mem_get_num (arch->buf->buf + 4, 4);
if (!(ptr = R_NEW0 (RBinSection))) {
return ret;
}
strncpy (ptr->name, "text", R_BIN_SIZEOF_STRINGS);
ptr->size = textsize;
ptr->vsize = textsize + (textsize%4096);
ptr->paddr = 8*4;
ptr->vaddr = ptr->paddr;
ptr->srwx = R_BIN_SCN_READABLE | R_BIN_SCN_EXECUTABLE | R_BIN_SCN_MAP; // r-x
ptr->add = true;
r_list_append (ret, ptr);
#endif
return ret;
}
static RList* symbols(RBinFile *arch) {
// TODO: parse symbol table
return NULL;
}
static RList* imports(RBinFile *arch) {
return NULL;
}
static RList* libs(RBinFile *arch) {
return NULL;
}
static RBinInfo* info(RBinFile *arch) {
RBinInfo *ret = NULL;
if (!(ret = R_NEW0 (RBinInfo))) {
return NULL;
}
ret->file = strdup (arch->file);
ret->bclass = strdup ("module");
ret->rclass = strdup ("wasm");
ret->os = strdup ("Wasm");
ret->arch = strdup ("wasm");
ret->machine = strdup (ret->arch);
ret->subsystem = strdup ("wasm");
ret->type = strdup ("EXEC");
ret->bits = 32;
ret->has_va = true;
ret->big_endian = false;
ret->dbg_info = 0;
ret->dbg_info = 0;
return ret;
}
static ut64 size(RBinFile *arch) {
ut64 text, data, syms, spsz;
if (!arch->o->info)
arch->o->info = info (arch);
if (!arch->o->info) return 0;
// TODO: reuse section list
text = r_mem_get_num (arch->buf->buf + 4, 4);
data = r_mem_get_num (arch->buf->buf + 8, 4);
syms = r_mem_get_num (arch->buf->buf + 16, 4);
spsz = r_mem_get_num (arch->buf->buf + 24, 4);
return text + data + syms + spsz + (6*4);
}
/* inspired in http://www.phreedom.org/solar/code/tinype/tiny.97/tiny.asm */
static RBuffer* create(RBin* bin, const ut8 *code, int codelen, const ut8 *data, int datalen) {
RBuffer *buf = r_buf_new ();
#define B(x,y) r_buf_append_bytes(buf,(const ut8*)x,y)
#define D(x) r_buf_append_ut32(buf,x)
B ("\x00" "asm", 4);
D (0xc); // TODO: last version is 0xd
return buf;
}
RBinPlugin r_bin_plugin_wasm = {
.name = "wasm",
.desc = "WebAssembly bin plugin",
.license = "MIT",
.load = &load,
.load_bytes = &load_bytes,
.size = &size,
.destroy = &destroy,
.check = &check,
.check_bytes = &check_bytes,
.baddr = &baddr,
.binsym = &binsym,
.entries = &entries,
.sections = &sections,
.symbols = &symbols,
.imports = &imports,
.info = &info,
.libs = &libs,
.create = &create,
};
#ifndef CORELIB
RLibStruct radare_plugin = {
.type = R_LIB_TYPE_BIN,
.data = &r_bin_plugin_wasm,
.version = R2_VERSION
};
#endif

9
libr/bin/p/wasm.mk Normal file
View File

@ -0,0 +1,9 @@
OBJ_WASM=bin_wasm.o
STATIC_OBJ+=${OBJ_WASM}
TARGET_WASM=bin_wasm.${EXT_SO}
ALL_TARGETS+=${TARGET_WASM}
${TARGET_WASM}: ${OBJ_WASM}
-${CC} $(call libname,bin_wasm) ${CFLAGS} ${OBJ_WASM}

View File

@ -347,7 +347,7 @@ static int cmd_section(void *data, const char *input) {
break;
}
break;
case '=':
case '=': // "S="
r_io_section_list_visual (core->io, core->offset, core->blocksize,
r_config_get_i (core->config, "scr.color"),
r_cons_get_size (NULL));

View File

@ -245,6 +245,8 @@ extern RAsmPlugin r_asm_plugin_xtensa;
extern RAsmPlugin r_asm_plugin_tricore;
extern RAsmPlugin r_asm_plugin_pic18c;
extern RAsmPlugin r_asm_plugin_rsp;
extern RAsmPlugin r_asm_plugin_wasm;
#endif
#ifdef __cplusplus

View File

@ -673,6 +673,7 @@ extern RBinPlugin r_bin_plugin_vsf;
extern RBinPlugin r_bin_plugin_dyldcache;
extern RBinPlugin r_bin_plugin_avr;
extern RBinPlugin r_bin_plugin_menuet;
extern RBinPlugin r_bin_plugin_wasm;
#ifdef __cplusplus
}

View File

@ -87,6 +87,7 @@ asm.v850
asm.ws
asm.xap
asm.arm_as
asm.wasm
asm.x86_as
asm.x86_cs
asm.x86_nz
@ -100,6 +101,7 @@ asm.vax
asm.mcs96
asm.pic18c
bin.any
bin.wasm
bin.art
bin.bf
bin.bflt