Add initial support for building r2 on WASI ##build

This commit is contained in:
pancake 2021-06-20 17:14:43 +02:00 committed by GitHub
parent 7631039d88
commit 7c14bac05b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
57 changed files with 457 additions and 70 deletions

View File

@ -25,6 +25,15 @@ jobs:
export CFLAGS=-I/tmp/fakeincs/
export LDFLAGS=-L/tmp/fakelibs/
sys/install.sh
build-wasi:
name: linux-wasi
runs-on: ubuntu-20.04
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Installing with symlinks
run: |
sys/wasi.sh
build-acr-gperf:
name: linux-acr-gperf
runs-on: ubuntu-20.04

View File

@ -1058,7 +1058,7 @@ static void replace_cmd_kv_file(const char *path, ut64 line_begin, ut64 line_end
return;
}
if (r_file_dump (path, (const ut8 *)newc, -1, false)) {
#if __UNIX__
#if __UNIX__ && !(__wasi__ || __EMSCRIPTEN__)
sync ();
#endif
} else {

View File

@ -376,7 +376,11 @@ R_API void r2r_subprocess_free(R2RSubprocess *proc) {
#else
#include <errno.h>
#ifndef __wasi__
#include <sys/wait.h>
#else
#define WNOHANG 0
#endif
struct r2r_subprocess_t {
pid_t pid;

View File

@ -98,4 +98,5 @@ include ../../shlr/qnx/deps.mk
include ../../shlr/ar/deps.mk
include ../../shlr/yxml/deps.mk
include ../../shlr/sdb.mk
LDFLAGS+=$(LINK)

View File

@ -12,4 +12,5 @@ include ../../shlr/gdb/deps.mk
include ../../shlr/bochs/deps.mk
include ../../shlr/qnx/deps.mk
include ../../shlr/ar/deps.mk
LDFLAGS+=$(LINK)

View File

@ -8,4 +8,3 @@ include ../../shlr/java/deps.mk
include ../../shlr/capstone.mk
include ../rules.mk
LDFLAGS+=$(LINK)

View File

@ -23,7 +23,37 @@ ifeq ($(USE_LTO),1)
LDFLAGS+=-flto
endif
ifeq (${COMPILER},emscripten)
ifeq (${COMPILER},wasi)
LINK+=$(SHLR)/zip/librz.a
LINK+=$(SHLR)/gdb/lib/libgdbr.a
LINK+=$(SHLR)/capstone/libcapstone.a
LINK+=$(SHLR)/sdb/src/libsdb.a
# instead of libr.a
LINK+=$(LIBR)/util/libr_util.a
LINK+=$(LIBR)/core/libr_core.a
LINK+=$(LIBR)/magic/libr_magic.a
LINK+=$(LIBR)/socket/libr_socket.a
LINK+=$(LIBR)/debug/libr_debug.a
LINK+=$(LIBR)/anal/libr_anal.a
LINK+=$(LIBR)/reg/libr_reg.a
LINK+=$(LIBR)/bp/libr_bp.a
LINK+=$(LIBR)/io/libr_io.a
LINK+=$(LIBR)/flag/libr_flag.a
LINK+=$(LIBR)/hash/libr_hash.a
LINK+=$(LIBR)/syscall/libr_syscall.a
LINK+=$(LIBR)/egg/libr_egg.a
LINK+=$(LIBR)/fs/libr_fs.a
LINK+=$(LIBR)/parse/libr_parse.a
LINK+=$(LIBR)/bin/libr_bin.a
LINK+=$(LIBR)/asm/libr_asm.a
LINK+=$(LIBR)/search/libr_search.a
LINK+=$(LIBR)/cons/libr_cons.a
LINK+=$(LIBR)/lang/libr_lang.a
LINK+=$(LIBR)/config/libr_config.a
LINK+=$(LIBR)/crypto/libr_crypto.a
LINK+=$(LIBR)/main/libr_main.a
else ifeq (${COMPILER},emscripten)
LINK+=$(SHLR)/libr_shlr.a
LINK+=$(SHLR)/sdb/src/libsdb.a
include $(SHLR)/capstone.mk
@ -79,11 +109,15 @@ ${BINS}: ${OBJS}
${CC} ${CFLAGS} $@.c ${OBJS} ../../libr/libr.a -o $@ $(LDFLAGS)
${BEXE}: ${OBJ} ${SHARED_OBJ}
ifeq ($(COMPILER),wasi)
${CC} ${CFLAGS} $+ -L.. -o $@ $(LDFLAGS)
else
ifeq ($(CC),emcc)
emcc $(BIN).c ../../shlr/libr_shlr.a ../../shlr/capstone/libcapstone.a ../../libr/libr.a ../../shlr/gdb/lib/libgdbr.a ../../shlr/zip/librz.a -I ../../libr/include -o $(BIN).js
else
${CC} ${CFLAGS} $+ -L.. -o $@ ../../libr/libr.a $(LDFLAGS)
endif
endif
else
${BINS}: ${OBJS}

65
dist/plugins-cfg/plugins.wasi.cfg vendored Normal file
View File

@ -0,0 +1,65 @@
STATIC="
asm.6502
asm.arm_cs
asm.gb
asm.z80
asm.dalvik
asm.wasm
anal.dalvik
anal.wasm
anal.arm_cs
anal.gb
anal.z80
anal.6502
esil.dummy
crypto.base64
crypto.xor
crypto.aes
fs.posix
fs.io
fs.r2
bin.any
bin.elf
bin.elf64
bin.dex
bin.pe
bin.wad
bin.art
bin.mz
bin.pe64
bin.mach0
bin.mach064
bin.wasm
bin_xtr.xtr_fatmach0
bin_xtr.xtr_sep64
core.a2f
core.sixref
bp.arm
bp.x86
cmd.dummy
egg.exec
egg.xor
debug.esil
debug.null
io.debug
io.malloc
io.mmap
io.self
io.socket
io.sparse
io.fd
io.rbuf
io.ihex
io.http
io.tcpslurp
io.null
io.default
parse.arm_pseudo
parse.att2intel
parse.wasm_pseudo
parse.6502_pseudo
parse.z80_pseudo
"
SHARED="
parse.6502_pseudo
"

View File

@ -5,6 +5,10 @@
#include <r_util.h>
#include <r_bind.h>
#ifdef __wasi__
#define FE_OVERFLOW 0
#define feclearexcept(x)
#endif
// should these be here?
#include <math.h>
#include <float.h>

View File

@ -346,7 +346,7 @@ R_API void r_cons_context_break_pop(RConsContext *context, bool sig) {
break_stack_free (b);
} else {
//there is not more elements in the stack
#if __UNIX__
#if __UNIX__ && !__wasi__
if (sig && r_cons_context_is_main ()) {
r_sys_signal (SIGINT, SIG_IGN);
}
@ -404,7 +404,7 @@ R_API int r_cons_get_cur_line(void) {
curline = point.y;
}
#endif
#if __UNIX__
#if __UNIX__ && !__wasi__
char buf[8];
struct termios save,raw;
// flush the Arrow keys escape keys which was messing up the output
@ -436,7 +436,7 @@ R_API void r_cons_break_timeout(int timeout) {
R_API void r_cons_break_end(void) {
I.context->breaked = false;
I.timeout = 0;
#if __UNIX__
#if __UNIX__ && !__wasi__
r_sys_signal (SIGINT, SIG_IGN);
#endif
if (!r_stack_is_empty (I.context->break_stack)) {
@ -588,7 +588,7 @@ R_API RCons *r_cons_new(void) {
#else
I.vtmode = 2;
#endif
#if EMSCRIPTEN
#if EMSCRIPTEN || __wasi__
/* do nothing here :? */
#elif __UNIX__
tcgetattr (0, &I.term_buf);
@ -1429,7 +1429,9 @@ R_API int r_cons_get_cursor(int *rows) {
}
R_API bool r_cons_isatty(void) {
#if __UNIX__
#if EMSCRIPTEN || __wasi__
return false;
#elif __UNIX__
struct winsize win = { 0 };
const char *tty;
struct stat sb;
@ -1451,9 +1453,10 @@ R_API bool r_cons_isatty(void) {
return false;
}
return true;
#endif
#else
/* non-UNIX do not have ttys */
return false;
#endif
}
#if __WINDOWS__
@ -1545,7 +1548,7 @@ R_API int r_cons_get_size(int *rows) {
I.rows = 23;
}
}
#elif EMSCRIPTEN
#elif EMSCRIPTEN || __wasi__
I.columns = 80;
I.rows = 23;
#elif __UNIX__
@ -1708,7 +1711,7 @@ R_API void r_cons_set_raw(bool is_raw) {
return;
}
}
#if EMSCRIPTEN
#if EMSCRIPTEN || __wasi__
/* do nothing here */
#elif __UNIX__
// enforce echo off

View File

@ -13,7 +13,9 @@ static int backup_fdn = 1;
#endif
static bool __dupDescriptor(int fd, int fdn) {
#if __WINDOWS__
#if __wasi__
return false;
#elif __WINDOWS__
backup_fd = 2002 - (fd - 2); // windows xp has 2048 as limit fd
return _dup2 (fdn, backup_fd) != -1;
#else
@ -26,6 +28,9 @@ static bool __dupDescriptor(int fd, int fdn) {
}
R_API int r_cons_pipe_open(const char *file, int fdn, int append) {
#if __wasi__
return -1;
#else
if (fdn < 1) {
return -1;
}
@ -52,9 +57,11 @@ R_API int r_cons_pipe_open(const char *file, int fdn, int append) {
dup2 (fd, fdn);
free (targetFile);
return fd;
#endif
}
R_API void r_cons_pipe_close(int fd) {
#if !__wasi__
if (fd != -1) {
close (fd);
if (backup_fd != -1) {
@ -63,4 +70,5 @@ R_API void r_cons_pipe_close(int fd) {
backup_fd = -1;
}
}
#endif
}

View File

@ -6,7 +6,9 @@
#if __UNIX__
#include <stdio.h>
#include <fcntl.h>
#if HAVE_PTY
#include <termios.h>
#endif
#include <errno.h>
#if R_UTF8_DETECT_LOCALE
#include <locale.h>
@ -51,6 +53,8 @@ static inline int rd(const int fd) {
int current_tty(void) {
#if __WINDOWS__
return 0;
#elif __wasi__ || __EMSCRIPTEN__
return 0;
#elif __ANDROID__
return 1;
#else

View File

@ -13,7 +13,9 @@
static int r_line_readchar_win(ut8 *s, int slen);
#else
#include <sys/ioctl.h>
#ifndef HAVE_PTY
#include <termios.h>
#endif
#include <signal.h>
#define USE_UTF8 1
#endif

View File

@ -620,6 +620,17 @@ R_API int r_cons_readchar(void) {
r_cons_set_raw (1);
#if __WINDOWS__
return __cons_readchar_w32 (0);
#elif __wasi__
void *bed = r_cons_sleep_begin ();
int ret = read (STDIN_FILENO, buf, 1);
r_cons_sleep_end (bed);
if (ret < 1) {
return -1;
}
if (bufactive) {
r_cons_set_raw (0);
}
return r_cons_controlz (buf[0]);
#else
void *bed = r_cons_sleep_begin ();

View File

@ -3573,7 +3573,11 @@ R_API int r_core_config_init(RCore *core) {
SETCB ("dir.source", "", &cb_dirsrc, "Path to find source files");
SETPREF ("dir.types", "/usr/include", "Default colon-separated list of paths to find C headers to cparse types");
SETPREF ("dir.libs", "", "Specify path to find libraries to load when bin.libs=true");
#if __EMSCRIPTEN__ || __wasi__
p = strdup ("/tmp");
#else
p = r_sys_getenv (R_SYS_HOME);
#endif
SETCB ("dir.home", r_str_get_fail (p, "/"), &cb_dirhome, "Path for the home directory");
free (p);
p = r_sys_getenv (R_SYS_TMP);
@ -3826,7 +3830,7 @@ R_API int r_core_config_init(RCore *core) {
/* scr */
#if __EMSCRIPTEN__
#if __EMSCRIPTEN__ || __wasi__
r_config_set_cb (cfg, "scr.fgets", "true", cb_scrfgets);
#else
r_config_set_cb (cfg, "scr.fgets", "false", cb_scrfgets);

View File

@ -13,8 +13,10 @@
#include "cmd_helps.h"
#if __UNIX__
#include <sys/utsname.h>
#ifndef __wasi__
#include <pwd.h>
#endif
#endif
static const char *SPECIAL_CHARS_REGULAR = "@;~$#|`\"'()<>";
#if 0
@ -2995,7 +2997,7 @@ err_r_w32_cmd_pipe:
#endif
R_API int r_core_cmd_pipe(RCore *core, char *radare_cmd, char *shell_cmd) {
#if __UNIX__
#if __UNIX__ && !__wasi__
int stdout_fd, fds[2];
int child;
#endif

View File

@ -223,7 +223,7 @@ static void dietime(int sig) {
static void activateDieTime(RCore *core) {
int dt = r_config_get_i (core->config, "http.dietime");
if (dt > 0) {
#if __UNIX__
#if __UNIX__ && !__wasi__
r_sys_signal (SIGALRM, dietime);
alarm (dt);
#else
@ -907,7 +907,7 @@ R_API void r_core_rtr_event(RCore *core, const char *input) {
}
if (!strcmp (input, "errmsg")) {
// TODO: support udp, tcp, rap, ...
#if __UNIX__
#if __UNIX__ && !__wasi__
char *f = r_file_temp ("errmsg");
r_cons_printf ("%s\n", f);
r_file_rm (f);

View File

@ -4258,7 +4258,9 @@ static bool is_mintty(RCons *cons) {
}
static void flush_stdin(void) {
#ifndef __wasi__
tcflush (STDIN_FILENO, TCIFLUSH);
#endif
}
#endif

View File

@ -28,9 +28,11 @@ extern "C" {
#include <sys/stat.h>
#include <fcntl.h>
#if __UNIX__
#ifndef __wasi__
#include <termios.h>
#include <sys/ioctl.h>
#include <sys/wait.h>
#endif
#include <sys/ioctl.h>
#include <sys/socket.h>
#endif
#if __WINDOWS__
@ -453,7 +455,7 @@ typedef struct r_cons_t {
RConsFunctionKey cb_fkey;
void *user; // Used by <RCore*>
#if __UNIX__
#if __UNIX__ && !__wasi__
struct termios term_raw, term_buf;
#elif __WINDOWS__
DWORD term_raw, term_buf, term_xterm;

View File

@ -16,10 +16,12 @@ R_LIB_VERSION_HEADER (r_socket);
#include <sys/un.h>
#include <poll.h>
#include <arpa/inet.h>
#ifndef __wasi__
#include <netdb.h>
#include <sys/socket.h>
#include <sys/wait.h>
#endif
#include <sys/socket.h>
#endif
#if HAVE_LIB_SSL
#include <openssl/ssl.h>

View File

@ -135,6 +135,10 @@
# define UNUSED_FUNCTION(x) UNUSED_ ## x
#endif
#ifdef __wasi__
# define __UNIX__ 1
#endif
#ifdef __EMSCRIPTEN__
# define __UNIX__ 1
#endif
@ -467,7 +471,7 @@ static inline void *r_new_copy(int size, void *data) {
#define R_SYS_ARCH "x86"
#define R_SYS_BITS R_SYS_BITS_32
#define R_SYS_ENDIAN 0
#elif __EMSCRIPTEN__
#elif __EMSCRIPTEN__ || __wasi__
#define R_SYS_ARCH "wasm"
#define R_SYS_BITS (R_SYS_BITS_32 | R_SYS_BITS_64)
#define R_SYS_ENDIAN 0
@ -608,6 +612,8 @@ typedef enum {
#define R_SYS_OS "qnx"
//#elif TARGET_OS_IPHONE
//#define R_SYS_OS "ios"
#elif defined (__wasi__)
#define R_SYS_OS "wasi"
#elif defined (__APPLE__)
#define R_SYS_OS "darwin"
#elif defined (__linux__)

View File

@ -40,7 +40,7 @@ R_API void *r_mem_alloc(int sz);
R_API void r_mem_free(void *);
R_API void r_mem_memzero(void *, size_t);
R_API void r_mem_reverse(ut8 *b, int l);
R_API int r_mem_protect(void *ptr, int size, const char *prot);
R_API bool r_mem_protect(void *ptr, int size, const char *prot);
R_API int r_mem_set_num(ut8 *dest, int dest_size, ut64 num);
R_API int r_mem_eq(ut8 *a, ut8 *b, int len);
R_API void r_mem_copybits(ut8 *dst, const ut8 *src, int bits);

View File

@ -236,6 +236,16 @@ static int r_io_def_mmap_write(RIO *io, RIODesc *fd, const ut8 *buf, int count)
static RIODesc *r_io_def_mmap_open(RIO *io, const char *file, int perm, int mode) {
r_return_val_if_fail (io && file, NULL);
#if __wasi__
RIOPlugin *_plugin = r_io_plugin_resolve (io, (const char *)"slurp://", false);
if (!_plugin || !_plugin->open) {
return NULL;
}
char *uri = r_str_newf ("slurp://%s", file);
RIODesc *d = _plugin->open (io, uri, perm, mode);
free (uri);
return d;
#else
RIOMMapFileObj *mmo = r_io_def_mmap_create_new_file (io, file, perm, mode);
if (!mmo) {
return NULL;
@ -250,6 +260,7 @@ static RIODesc *r_io_def_mmap_open(RIO *io, const char *file, int perm, int mode
free (oldname);
}
return d;
#endif
}
static ut64 r_io_def_mmap_lseek(RIO *io, RIODesc *fd, ut64 offset, int whence) {

View File

@ -13,7 +13,7 @@ static bool __check(RIO *io, const char *pathname, bool many) {
static ut8 *tcpme(const char *pathname, int *code, int *len) {
pathname += strlen ("tcp-slurp://");
*code = 404;
#if __UNIX__
#if __UNIX__ && !__wasi__
r_sys_signal (SIGINT, SIG_IGN);
#endif
if (*pathname == '?') {

View File

@ -6,7 +6,7 @@
#include "r_core.h"
#include "r_lang.h"
#if __UNIX__
#if __UNIX__ && !__wasi__
static int ac = 0;
static const char **av = NULL;

View File

@ -225,7 +225,9 @@ static bool lang_pipe_run(RLang *lang, const char *code, int len) {
if (safe_in != -1) {
close (safe_in);
}
#ifndef __wasi__
waitpid (child, NULL, WNOHANG);
#endif
return true;
#else
#if __WINDOWS__

View File

@ -38,7 +38,7 @@
#include "file.h"
#include "patchlevel.h"
#if __UNIX__ && !defined(_MSC_VER)
#if __UNIX__ && !defined(_MSC_VER) && !(defined(__wasi__))
# define QUICK 1
# include <sys/mman.h>
# include <sys/param.h>
@ -46,6 +46,10 @@
# define QUICK 0
#endif
#ifdef __wasi__
# define MAXPATHLEN 255
#endif
#ifdef _MSC_VER
# include <sys\stat.h>
# define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)

View File

@ -258,11 +258,13 @@ int file_fsmagic(struct r_magic_set *ms, const char *fn, struct stat *sb) {
}
return 1;
#endif
#if 0
#ifdef S_IFSOCK
case S_IFSOCK:
if (file_printf(ms, "socket") == -1)
return -1;
return 1;
#endif
#endif
case S_IFREG:
break;

View File

@ -720,6 +720,7 @@ R_API int r_main_radare2(int argc, const char **argv) {
return 1;
}
if (2 != new_stderr) {
#if !__wasi__
if (-1 == dup2 (new_stderr, 2)) {
eprintf ("Failed to dup2 stderr");
free (envprofile);
@ -727,6 +728,7 @@ R_API int r_main_radare2(int argc, const char **argv) {
R_FREE (debugbackend);
return 1;
}
#endif
if (-1 == close (new_stderr)) {
eprintf ("Failed to close %s", nul);
LISTS_FREE ();

View File

@ -162,7 +162,7 @@ static int openfile(const char *f, int x) {
return -1;
}
}
#if __UNIX__
#if __UNIX__ && !__wasi__
if (x) {
fchmod (fd, 0755);
}
@ -176,7 +176,9 @@ static int openfile(const char *f, int x) {
eprintf ("Could not resize\n");
}
close (1);
#if !__wasi__
dup2 (fd, 1);
#endif
return fd;
}
#define ISEXEC (fmt!='r')

View File

@ -671,9 +671,11 @@ R_API int r_main_rasm2(int argc, const char *argv[]) {
break;
case 'O':
fd = open (opt.arg, O_TRUNC | O_RDWR | O_CREAT, 0644);
#ifndef __wasi__
if (fd != -1) {
dup2 (fd, 1);
}
#endif
break;
case 'p':
use_spp = true;

View File

@ -394,6 +394,7 @@ static int tcc_compile(TCCState *s1)
define_start = define_stack;
nocode_wanted = 1;
#ifndef __wasi__
if (setjmp (s1->error_jmp_buf) == 0) {
s1->nb_errors = 0;
s1->error_set_jmp_enabled = true;
@ -403,7 +404,7 @@ static int tcc_compile(TCCState *s1)
parse_flags = PARSE_FLAG_PREPROCESS | PARSE_FLAG_TOK_NUM;
// pvtop = vtop;
next ();
decl (VT_CONST);
decl0 (VT_CONST, 0);
if (tok != TOK_EOF) {
expect ("declaration");
}
@ -414,6 +415,7 @@ static int tcc_compile(TCCState *s1)
}
#endif
}
#endif
s1->error_set_jmp_enabled = false;

View File

@ -38,7 +38,9 @@
#endif
#include <signal.h>
#include <fcntl.h>
#ifndef __wasi__
#include <setjmp.h>
#endif
#include <time.h>
#ifdef CONFIG_TCCASSERT
@ -54,9 +56,6 @@
# ifndef __HAIKU__
# endif
# include <sys/mman.h>
# ifndef CONFIG_TCC_STATIC
# include <dlfcn.h>
# endif
#else
# include <windows.h>
# include <sys/timeb.h>
@ -335,6 +334,7 @@ typedef struct BufferedFile {
unsigned char buffer[IO_BUF_SIZE + 1]; /* extra size for CH_EOB char */
} BufferedFile;
#define CH_EOB '\\' /* end of buffer or '\0' char in file */
#define CH_EOF (-1) /* end of file */
@ -407,7 +407,9 @@ struct TCCState {
void *error_opaque;
void (*error_func)(void *opaque, const char *msg);
bool error_set_jmp_enabled;
#ifndef __wasi__
jmp_buf error_jmp_buf;
#endif
int nb_errors;
/* output file for preprocessing (-E) */
@ -908,7 +910,6 @@ ST_FUNC void expr_prod(void);
ST_FUNC void expr_sum(void);
ST_FUNC void gexpr(void);
ST_FUNC long long expr_const(void);
ST_FUNC void decl(int l);
/********************************************************/
#undef ST_DATA
@ -924,4 +925,22 @@ PUB_FUNC void tcc_typedef_alias_fields(const char *alias);
extern void (*tcc_cb)(const char *, char **);
ST_DATA bool nocode_wanted;
// XXX?
static BufferedFile *file;
static char *funcname;
ST_DATA Sym *define_stack;
ST_DATA int tok_flags;
ST_DATA int parse_flags;
ST_DATA Sym *global_stack;
ST_DATA Sym *local_stack;
ST_DATA Sym *define_stack;
ST_DATA void **sym_pools;
ST_DATA int nb_sym_pools;
ST_DATA Sym *sym_free_first;
ST_DATA char *dir_name;
#endif /* _TCC_H */

View File

@ -49,10 +49,12 @@ ST_DATA Sym *local_stack;
ST_DATA Sym *define_stack;
ST_DATA bool const_wanted = 0; /* true if constant wanted */
ST_DATA bool nocode_wanted; /* true if no code generation wanted for an expression */
ST_DATA int global_expr; /* true if compound literals must be allocated globally (used during initializers parsing */
#if 0
ST_DATA bool nocode_wanted; /* true if no code generation wanted for an expression */
ST_DATA char *funcname;
ST_DATA char *dir_name;
#endif
ST_DATA CType char_pointer_type, func_old_type;
ST_DATA CType int8_type, int16_type, int32_type, int64_type, size_type;
@ -311,6 +313,7 @@ ST_FUNC Sym *sym_push(int v, CType *type, int r, long long c) {
return s;
}
#if 1
/* push a global identifier */
ST_FUNC Sym *global_identifier_push(int v, int t, long long c) {
Sym *s, **ps;
@ -333,6 +336,7 @@ ST_FUNC Sym *global_identifier_push(int v, int t, long long c) {
}
return s;
}
#endif
/* pop symbols until top reaches 'b' */
ST_FUNC void sym_pop(Sym **ptop, Sym *b) {
@ -2959,6 +2963,7 @@ no_alloc:
;
}
#if 1
/* parse an old style function declaration list */
/* XXX: check multiple parameter */
static void func_decl_list(Sym *func_sym) {
@ -3013,7 +3018,9 @@ static void func_decl_list(Sym *func_sym) {
skip (';');
}
}
#endif
#if 1
/* 'l' is VT_LOCAL or VT_CONST to define default storage type */
static int decl0(int l, int is_for_loop_init) {
int v, has_init, r;
@ -3205,7 +3212,4 @@ func_error1:
}
return 0;
}
ST_FUNC void decl(int l) {
decl0 (l, 0);
}
#endif

View File

@ -39,7 +39,7 @@ ST_DATA int parse_flags;
#define PARSE_FLAG_ASM_COMMENTS 0x0008 /* '#' can be used for line comment */
#define PARSE_FLAG_SPACES 0x0010 /* next() returns space tokens (for -E) */
ST_DATA struct BufferedFile *file;
extern struct BufferedFile *file;
ST_DATA int ch, tok;
ST_DATA CValue tokc;
ST_DATA const int *macro_ptr;

View File

@ -34,7 +34,9 @@
#if __UNIX__
#include <sys/ioctl.h>
#include <sys/resource.h>
#ifndef __wasi__
#include <grp.h>
#endif
#include <errno.h>
#if defined(__sun)
#include <sys/filio.h>
@ -57,7 +59,7 @@
#define pid_t int
#endif
#if EMSCRIPTEN
#if EMSCRIPTEN || __wasi__
#undef HAVE_PTY
#define HAVE_PTY 0
#else
@ -159,6 +161,7 @@ R_API void r_run_free(RRunProfile *r) {
#if __UNIX__
static void set_limit(int n, int a, ut64 b) {
#ifndef __wasi__
if (n) {
struct rlimit cl = {b, b};
setrlimit (RLIMIT_CORE, &cl);
@ -166,6 +169,7 @@ static void set_limit(int n, int a, ut64 b) {
struct rlimit cl = {0, 0};
setrlimit (a, &cl);
}
#endif
}
#endif
@ -416,7 +420,9 @@ static int handle_redirection(const char *cmd, bool in, bool out, bool err) {
return 0;
}
if (cmd[0] == '"') {
#if __UNIX__
#ifdef __wasi__
eprintf ("[ERROR] rarun2: Cannot create pipe\n");
#elif __UNIX__
if (in) {
int pipes[2];
if (pipe (pipes) != -1) {
@ -463,6 +469,7 @@ static int handle_redirection(const char *cmd, bool in, bool out, bool err) {
eprintf ("[ERROR] rarun2: Cannot open: %s\n", cmd);
return 1;
}
#ifndef __wasi__
#define DUP(x) { close(x); dup2(f,x); }
if (in) {
DUP(0);
@ -473,6 +480,7 @@ static int handle_redirection(const char *cmd, bool in, bool out, bool err) {
if (err) {
DUP(2);
}
#endif
close (f);
}
return 0;
@ -725,11 +733,11 @@ static int redirect_socket_to_stdio(RSocket *sock) {
close (0);
close (1);
close (2);
#ifndef __wasi__
dup2 (sock->fd, 0);
dup2 (sock->fd, 1);
dup2 (sock->fd, 2);
#endif
return 0;
}
@ -854,7 +862,7 @@ R_API int r_run_config_env(RRunProfile *p) {
if (p->_aslr != -1) {
setASLR (p, p->_aslr);
}
#if __UNIX__
#if __UNIX__ && !__wasi__
set_limit (p->_docore, RLIMIT_CORE, RLIM_INFINITY);
if (p->_maxfd) {
set_limit (p->_maxfd, RLIMIT_NOFILE, p->_maxfd);
@ -1019,7 +1027,9 @@ R_API int r_run_config_env(RRunProfile *p) {
int f2[2];
if (pipe (f2) != -1) {
close (0);
#if !__wasi__
dup2 (f2[0], 0);
#endif
} else {
eprintf ("[ERROR] rarun2: Cannot create pipe\n");
return 1;

View File

@ -6,7 +6,7 @@
#include <r_util.h>
#include <errno.h>
#if EMSCRIPTEN
#if EMSCRIPTEN || __wasi__
#define NETWORK_DISABLED 1
#else
#define NETWORK_DISABLED 0
@ -64,7 +64,7 @@ R_API int r_socket_ready(RSocket *s, int secs, int usecs) {
R_API char *r_socket_to_string(RSocket *s) {
return NULL;
}
R_API int r_socket_write(RSocket *s, void *buf, int len) {
R_API int r_socket_write(RSocket *s, const void *buf, int len) {
return -1;
}
R_API int r_socket_puts(RSocket *s, char *buf) {

View File

@ -7,7 +7,7 @@
#include <r_util.h>
#include <signal.h>
#if __UNIX__
#if __UNIX__ && !__wasi__
#include <sys/wait.h>
#endif

View File

@ -1,7 +1,8 @@
/* radare - LGPL - Copyright 2017 - pancake */
/* radare - LGPL - Copyright 2017-2021 - pancake */
#include <r_socket.h>
#if __UNIX__
#if __UNIX__ && !__wasi__
#include <errno.h>
#include <string.h>

View File

@ -15,7 +15,7 @@ static char oper = '=';
static mode_t mode = 0;
#endif
R_API bool r_file_chmod (const char *file, const char *mod, int recursive) {
R_API bool r_file_chmod(const char *file, const char *mod, int recursive) {
#if __UNIX__
oper = '=';
mode = 0;
@ -48,10 +48,12 @@ static bool chmodr(const char *path, int rflag) {
st.st_mode = mode;
break;
}
#if !__wasi__
if (chmod (path, st.st_mode) == -1) {
eprintf ("chmod %s:", path);
return false;
}
#endif
if (rflag) {
recurse (path, rflag, chmodr);
}

View File

@ -253,7 +253,7 @@ R_API char *r_file_abspath_rel(const char *cwd, const char *file) {
if (!ret) {
ret = strdup (file);
}
#if __UNIX__
#if __UNIX__ && !__wasi__
char *abspath = realpath (ret, NULL);
if (abspath) {
free (ret);
@ -325,8 +325,11 @@ R_API char *r_file_path(const char *bin) {
return strdup (bin);
}
R_API char *r_stdin_slurp (int *sz) {
#if __UNIX__ || __WINDOWS__
R_API char *r_stdin_slurp(int *sz) {
#if __wasi__
#warning r_stdin_slurp not available for wasi
return NULL;
#elif __UNIX__ || __WINDOWS__
int i, ret, newfd;
if ((newfd = dup (0)) < 0) {
return NULL;
@ -390,6 +393,13 @@ R_API char *r_file_slurp(const char *str, R_NULLABLE size_t *usz) {
fclose (fd);
return NULL;
}
#if __wasi__
fclose (fd);
fd = r_sandbox_fopen (str, "rb");
if (!fd) {
return NULL;
}
#endif
if (!sz) {
if (r_file_is_regular (str)) {
char *buf = NULL;
@ -426,14 +436,14 @@ R_API char *r_file_slurp(const char *str, R_NULLABLE size_t *usz) {
sz = UT16_MAX;
}
rewind (fd);
char *ret = (char *)malloc (sz + 1);
char *ret = (char *)calloc (sz + 1, 1);
if (!ret) {
fclose (fd);
return NULL;
}
size_t rsz = fread (ret, 1, sz, fd);
if (rsz != sz) {
eprintf ("Warning: r_file_slurp: fread: truncated read\n");
eprintf ("Warning: r_file_slurp: fread: truncated read (%d / %d)\n", (int)rsz, (int)sz);
sz = rsz;
}
fclose (fd);
@ -881,6 +891,8 @@ err_r_file_mmap_write:
CloseHandle (fh);
}
return ret;
#elif __wasi__
return -1;
#elif __UNIX__
int fd = r_sandbox_open (file, O_RDWR|O_SYNC, 0644);
const int pagesize = getpagesize ();
@ -942,6 +954,8 @@ err_r_file_mmap_read:
}
free (file_);
return ret;
#elif __wasi__
return 0;
#elif __UNIX__
int fd = r_sandbox_open (file, O_RDONLY, 0644);
const int pagesize = 4096;
@ -959,12 +973,17 @@ err_r_file_mmap_read:
munmap (mmap_buf, mmlen*2);
close (fd);
return len;
#endif
#else
return 0;
#endif
}
#if __UNIX__
static RMmap *r_file_mmap_unix (RMmap *m, int fd) {
#if __wasi__
static RMmap *r_file_mmap_unix(RMmap *m, int fd) {
return NULL;
}
#elif __UNIX__
static RMmap *r_file_mmap_unix(RMmap *m, int fd) {
ut8 empty = m->len == 0;
m->buf = mmap (NULL, (empty?BS:m->len) ,
m->rw?PROT_READ|PROT_WRITE:PROT_READ,
@ -1102,7 +1121,7 @@ R_API void r_file_mmap_free(RMmap *m) {
return;
}
free (m->filename);
#if __UNIX__
#if __UNIX__ && !__wasi__
munmap (m->buf, m->len);
#endif
close (m->fd);
@ -1165,6 +1184,9 @@ err_r_file_mkstemp:
}
char *name = r_str_newf ("%s/r2.%s.XXXXXX%s", path, prefix, suffix);
#if __wasi__
// nothing to do
#else
mode_t mask = umask (S_IWGRP | S_IWOTH);
if (suffix && *suffix) {
#if defined(__GLIBC__) && defined(__GLIBC_MINOR__) && 2 <= __GLIBC__ && 19 <= __GLIBC__MINOR__
@ -1184,6 +1206,7 @@ err_r_file_mkstemp:
h = mkstemp (name);
}
umask (mask);
#endif
if (oname) {
*oname = (h!=-1)? strdup (name): NULL;
}

View File

@ -1,4 +1,4 @@
/* radare - LGPL - Copyright 2007-2020 - pancake */
/* radare - LGPL - Copyright 2007-2021 - pancake */
#include <r_util.h>
#if __UNIX__
@ -246,8 +246,10 @@ R_API const ut8 *r_mem_mem_aligned(const ut8 *haystack, int hlen, const ut8 *nee
return NULL;
}
R_API int r_mem_protect(void *ptr, int size, const char *prot) {
#if __UNIX__
R_API bool r_mem_protect(void *ptr, int size, const char *prot) {
#if __wasi__
return false;
#elif __UNIX__
int p = 0;
if (strchr (prot, 'x')) {
p |= PROT_EXEC;
@ -358,7 +360,7 @@ R_API void *r_mem_mmap_resize(RMmap *m, ut64 newsize) {
if (m->buf) {
UnmapViewOfFile (m->buf);
}
#elif __UNIX__
#elif __UNIX__ && !__wasi__
if (munmap (m->buf, m->len) != 0) {
return NULL;
}

View File

@ -3,6 +3,10 @@ SDBLIB=${SDBPATH}/libsdb.a
EXTRA_TARGETS+=${SDBLIB}
EXTRA_PRE+=$(SDBLIB)
# __wasi__
# CFLAGS+=-DUSE_MMAN=0
# CFLAGS+=-DHAVE_MMAN=0
SDB_OBJS=
SDB_OBJS+=buffer.o
SDB_OBJS+=cdb.o

View File

@ -58,10 +58,12 @@ int proc_pidpath(int pid, void * buffer, ut32 buffersize);
#endif
#if __UNIX__
# include <sys/utsname.h>
# include <sys/wait.h>
# include <sys/stat.h>
# include <errno.h>
#ifndef __wasi__
# include <pwd.h>
# include <sys/wait.h>
#endif
# include <signal.h>
extern char **environ;
@ -153,6 +155,10 @@ R_API int r_sys_fork(void) {
R_API int r_sys_sigaction(int *sig, void (*handler) (int)) {
return -1;
}
#elif __wasi__
R_API int r_sys_sigaction(int *sig, void (*handler)(int)) {
return 0;
}
#elif HAVE_SIGACTION
R_API int r_sys_sigaction(int *sig, void (*handler) (int)) {
struct sigaction sigact = { };
@ -1252,6 +1258,8 @@ R_API char *r_sys_whoami(void) {
if (!GetUserName (buf, (LPDWORD)&buf_sz) ) {
return strdup ("?");
}
#elif __wasi__
strcpy (buf, "user");
#else
struct passwd *pw = getpwuid (getuid ());
if (pw) {
@ -1270,13 +1278,17 @@ R_API int r_sys_uid(void) {
if (!GetUserName (buf, (LPDWORD)&buf_sz) ) {
return strdup ("?");
}
#elif __wasi__
return 0;
#else
return getuid ();
#endif
}
R_API int r_sys_getpid(void) {
#if __UNIX__
#if __wasi__
return 0;
#elif __UNIX__
return getpid ();
#elif __WINDOWS__
return GetCurrentProcessId();

View File

@ -62,8 +62,10 @@ static char *showfile(char *res, const int nth, const char *fpath, const char *n
case S_IFBLK: fch = 'b'; break;
case S_IFLNK: fch = 'l'; break;
case S_IFIFO: fch = 'p'; break;
#ifndef __wasi__
#ifdef S_IFSOCK
case S_IFSOCK: fch = 's'; break;
#endif
#endif
}
}
@ -76,9 +78,7 @@ static char *showfile(char *res, const int nth, const char *fpath, const char *n
res = r_str_appendf (res, "%s\n", nn);
} else if (printfmt == 'e') {
const char *eDIR = "📁";
const char *eLNK = "📎";
const char *eIMG = "🌅";
const char *eUID = "🔼";
const char *eHID = "👀";
const char *eANY = " ";
// --
@ -87,8 +87,10 @@ static char *showfile(char *res, const int nth, const char *fpath, const char *n
icon = eDIR;
#if __UNIX__
} else if ((sb.st_mode & S_IFMT) == S_IFLNK) {
const char *eLNK = "📎";
icon = eLNK;
} else if (sb.st_mode & S_ISUID) {
const char *eUID = "🔼";
icon = eUID;
#endif
} else if (r_str_casestr (nn, ".jpg") || r_str_casestr (nn, ".png") || r_str_casestr (nn, ".gif")) {

36
mk/wasi.mk Normal file
View File

@ -0,0 +1,36 @@
WASI_SDK=$(HOME)/Downloads/wasi/wasi-sdk-12.0
WASI_SYSROOT=$(HOME)/Downloads/wasi/wasi-sysroot
ifeq (${_INCLUDE_MK_GCC_},)
_INCLUDE_MK_GCC_=1
EXT_EXE=.wasm
EXT_SO=.o
WITH_LIBS=0
EXT_AR=a
CC=$(WASI_SDK)/bin/clang --sysroot=$(WASI_SYSROOT) -DHAVE_PTHREAD=0 -D_WASI_EMULATED_SIGNAL -D_WASI_EMULATED_MMAN -DHAVE_PTY=0 -lc-printscan-long-double
AR=$(WASI_SDK)/bin/ar
LINK=
RANLIB=$(WASI_SDK)/bin/ranlib
ONELIB=0
CC_AR=$(AR) q ${LIBAR}
PARTIALLD=$(CC) -nostdlib -Wl,--whole-archive -Wl,--no-entry
PIC_CFLAGS=-fPIC
CFLAGS+=-MD -D__UNIX__=1
CFLAGS_INCLUDE=-I
LDFLAGS_LINK=-l
LDFLAGS_LINKPATH=-L
CFLAGS_OPT0=-O0
CFLAGS_OPT1=-O1
CFLAGS_OPT2=-O2
CFLAGS_OPT3=-O3
CFLAGS_DEBUG=-g
ifeq ($(OSTYPE),auto)
OSTYPE=$(shell uname | tr 'A-Z' 'a-z')
endif
LDFLAGS_LIB=-shared
LDFLAGS_SONAME=-DA_
CC_LIB=${CC} ${LDFLAGS_LIB} -o ${LIBSO}
endif

View File

@ -60,7 +60,8 @@ endif
SHLR?=$(shell pwd)
AR?=ar
RANLIB?=ranlib
MODS=sdb zip java mpc yxml
# MODS=sdb zip java mpc yxml
MODS=zip java mpc yxml
MODS+=gdb qnx ar
# lz4
ifneq ($(CC),cccl)
@ -113,10 +114,10 @@ $(SDB_HOST):
$(MAKE) sdb-host HOST_CC=$(HOST_CC) CC=$(HOST_CC)
$(SDB_LIBA):
$(MAKE) sdb-target
$(MAKE) sdb-target RANLIB=$(RANLIB)
sdbs: $(SDB_HOST)
$(MAKE) $(SDB_LIBA)
$(MAKE) $(SDB_LIBA) RANLIB=$(RANLIB)
sdb-host:
@echo
@ -137,11 +138,17 @@ sdb-target:
@echo "TARGET SDB"
@echo ">>>>>>>>>>"
@echo
rm -f src/libsdb.$(EXT_AR)
$(MAKE) -C sdb clean ; rm -f sdb/src/*.o sdb/src/sdb_version.h
$(MAKE) -C sdb/src sdb_version.h
ifeq ($(EXT_EXE),.wasm)
$(MAKE) -C sdb/src ARCH=xxx RANLIB="${RANLIB}" CFLAGS_SHARED="${CFLAGS_SHARED} -DHAVE_MMAN=0" \
CC="${CC}" AR="${AR}" ARCH=undefined CFLAGS='${CFLAGS} -DHAVE_MMAN=0' LDFLAGS='${LDFLAGS}' libsdb.$(EXT_AR)
else
$(MAKE) -C sdb/src ARCH=xxx RANLIB="${RANLIB}" CFLAGS_SHARED="${CFLAGS_SHARED}" \
CC="${CC}" AR="${AR}" ARCH=undefined CFLAGS='${CFLAGS}' LDFLAGS='${LDFLAGS}' libsdb.a
${RANLIB} sdb/src/libsdb.a
CC="${CC}" AR="${AR}" ARCH=undefined LDFLAGS='${LDFLAGS}' libsdb.$(EXT_AR)
endif
$(RANLIB) sdb/src/libsdb.$(EXT_AR)
.PHONY: sdb-sync sync-sdb sdbclean sdb-native sdb-target
SDB_F=README.md config.mk src Makefile meson.build

View File

@ -16,8 +16,10 @@
#include <errno.h>
#include <fcntl.h>
#include <string.h>
#ifndef __wasi__
#include <termios.h>
#endif
#endif
#if __UNIX__
#include <signal.h>

View File

@ -280,6 +280,12 @@ typedef unsigned szind_t;
# ifdef __tile__
# define LG_QUANTUM 4
# endif
# ifdef __WASM__
# define LG_QUANTUM 4
# endif
# ifdef __wasi__
# define LG_QUANTUM 4
# endif
# ifdef __le32__
# define LG_QUANTUM 4
# endif

View File

@ -16,7 +16,9 @@
# endif
# include <sys/uio.h>
# endif
#ifndef __wasi__
# include <pthread.h>
#endif
# ifdef JEMALLOC_OS_UNFAIR_LOCK
# include <os/lock.h>
# endif

View File

@ -1,6 +1,12 @@
/******************************************************************************/
#ifdef JEMALLOC_H_TYPES
#ifdef __wasi__
// #define nstime_t time_t
#undef JM_NBINS
#define JM_NBINS 1
#endif
typedef struct nstime_s nstime_t;
/* Maximum supported number of seconds (~584 years). */

View File

@ -24,6 +24,16 @@ endif
.PHONY: test sdb.js pkgconfig dist w32dista asan
WASI_SDK=$(HOME)/Downloads/wasi/wasi-sdk-12.0
WASI_SYSROOT=$(HOME)/Downloads/wasi/wasi-sysroot
WASI_CC="$(WASI_SDK)/bin/clang --sysroot=$(WASI_SYSROOT) -D_WASI_EMULATED_MMAN -D_WASI_EMULATED_SIGNAL -DUSE_MMAN=0 -DHAVE_SYSTEM=0"
wasi wasm:
${MAKE} src/sdb_version.h
CC=$(WASI_CC) CFLAGS="$(WASI_CFLAGS)" $(MAKE) -C src all WITHPIC=0
mv src/sdb src/sdb.wasm
file src/sdb.wasm
test:
${MAKE} -C test

View File

@ -4,6 +4,9 @@
#include <string.h>
#include <fcntl.h>
#include "sdb.h"
#ifdef __wasi__
static int getpid(void) { return 0; }
#endif
SDB_API const char *sdb_lock_file(const char *f) {
static char buf[128];

View File

@ -761,12 +761,17 @@ static int gen_gperf(MainOptions *mo, const char *file, const char *name) {
}
int rc = -1;
if (wd != -1) {
#ifdef __wasi__
rc = sdb_dump (mo); // file, MODE_CGEN, false, NULL);
fflush (stdout);
#else
dup2 (1, 999);
dup2 (wd, 1);
rc = sdb_dump (mo); // file, MODE_CGEN, false, NULL);
fflush (stdout);
close (wd);
dup2 (999, 1);
#endif
} else {
eprintf ("Cannot create .%s\n", out);
}

View File

@ -46,6 +46,7 @@
#include <windows.h>
#include <io.h>
#define ULLFMT "I64"
#undef HAVE_MMAN
#define HAVE_MMAN 0
#else
// CYGWIN AND UNIX
@ -53,10 +54,16 @@
#undef DIRSEP
#define DIRSEP '/'
#include <unistd.h>
#undef HAVE_MMAN
#define HAVE_MMAN 1
#define ULLFMT "ll"
#endif
#if __wasi__
#undef HAVE_MMAN
#define HAVE_MMAN 0
#endif
#ifndef USE_MMAN
#define USE_MMAN HAVE_MMAN
#endif

View File

@ -70,9 +70,10 @@ static void *iob_pipe_open(const char *path) {
}
memset (&sa, 0, sizeof (struct sockaddr_un));
#ifndef __wasi__
sa.sun_family = AF_UNIX;
r_str_ncpy (sa.sun_path, path, sizeof (sa.sun_path) - 1);
#endif
if (connect (sock, (struct sockaddr *) &sa, sizeof (struct sockaddr_un)) == -1) {
perror ("connect");
close (sock);

33
sys/wasi.sh Executable file
View File

@ -0,0 +1,33 @@
#!/bin/sh
export WASI_SDK=${HOME}/Downloads/wasi/wasi-sdk-12.0
export WASI_SYSROOT=${HOME}/Downloads/wasi/wasi-sysroot
if [ ! -d "$WASI_SDK" ]; then
#OS=linux,macos,mingw
OS=`uname`
case "$OS" in
linux|Linux) OS=linux ; ;;
darwin|Darwin) OS=macos ; ;;
windows|Windows) OS=mingw ; ;;
esac
mkdir -p ~/Downloads/wasi
wget -c -O ~/Downloads/wasi/wasi-sdk.tar.gz https://github.com/WebAssembly/wasi-sdk/releases/download/wasi-sdk-12/wasi-sdk-12.0-$OS.tar.gz || exit 1
wget -c -O ~/Downloads/wasi/wasi-root.tar.gz https://github.com/WebAssembly/wasi-sdk/releases/download/wasi-sdk-12/wasi-sysroot-12.0.tar.gz || exit 1
(
cd ~/Downloads/wasi
tar xzvf wasi-sdk.tar.gz
tar xzvf wasi-root.tar.gz
)
fi
export CFLAGS=-D_WASI_EMULATED_SIGNAL
cp dist/plugins-cfg/plugins.wasi.cfg plugins.cfg
# export CC="${WASI_SDK}/bin/clang -D
./configure --with-compiler=wasi --disable-debugger --without-fork --with-ostype=wasi --with-checks-level=0 --disable-threads --without-dylink --with-libr --without-libuv --without-gpl
make -j
for a in rax2 radare2 rasm2 rabin2 rafind2 ; do
make -C binr/$a
done