From 2e8a22b7100670c7b686a4e6abfba13189379407 Mon Sep 17 00:00:00 2001 From: phani Date: Tue, 24 Dec 2019 16:50:14 -0500 Subject: [PATCH] fix for standalone execution --- runtime/src/http.c | 21 +++++++++++++++-- runtime/src/libc/uvio.c | 52 ++++++++++++++++++++++++++++++----------- runtime/src/main.c | 20 +++++++++++++--- runtime/src/module.c | 2 ++ runtime/src/runtime.c | 2 ++ runtime/src/sandbox.c | 12 ++++++++-- 6 files changed, 88 insertions(+), 21 deletions(-) diff --git a/runtime/src/http.c b/runtime/src/http.c index 6b638ef..f9952a8 100644 --- a/runtime/src/http.c +++ b/runtime/src/http.c @@ -35,11 +35,12 @@ http_on_header_end(http_parser *parser) static inline int http_on_url(http_parser* parser, const char *at, size_t length) { +#ifndef STANDALONE struct sandbox *s = sandbox_current(); struct http_request *r = parser->data; assert(strncmp(s->mod->name, (at + 1), length - 1) == 0); - +#endif return 0; } @@ -75,12 +76,14 @@ http_on_header_value(http_parser* parser, const char *at, size_t length) static inline int http_on_body(http_parser* parser, const char *at, size_t length) { +#ifndef STANDALONE struct http_request *r = parser->data; struct sandbox *c = sandbox_current(); assert(length <= c->mod->max_req_sz); r->body = (char *)at; r->bodylen = length; +#endif return 0; } @@ -88,16 +91,21 @@ http_on_body(http_parser* parser, const char *at, size_t length) int http_request_body_get(char **b) { +#ifndef STANDALONE struct sandbox *s = sandbox_current(); struct http_request *r = &s->rqi; *b = r->body; return r->bodylen; +#else + return 0; +#endif } int http_response_header_set(char *key, int len) { +#ifndef STANDALONE // by now, req_resp_data should only be containing response! struct sandbox *c = sandbox_current(); struct http_response *r = &c->rsi; @@ -106,39 +114,45 @@ http_response_header_set(char *key, int len) r->nheaders++; r->headers[r->nheaders-1].hdr = key; r->headers[r->nheaders-1].len = len; +#endif return 0; } int http_response_body_set(char *body, int len) { +#ifndef STANDALONE struct sandbox *c = sandbox_current(); struct http_response *r = &c->rsi; assert(len < c->mod->max_resp_sz); r->body = body; r->bodylen = len; +#endif return 0; } int http_response_status_set(char *status, int len) { +#ifndef STANDALONE struct sandbox *c = sandbox_current(); struct http_response *r = &c->rsi; r->status = status; r->stlen = len; +#endif return 0; } int http_response_uv(void) { + int nb = 0; +#ifndef STANDALONE struct sandbox *c = sandbox_current(); struct http_response *r = &c->rsi; - int nb = 0; r->bufs[nb] = uv_buf_init(r->status, r->stlen); nb++; @@ -153,6 +167,7 @@ int http_response_uv(void) r->bufs[nb] = uv_buf_init(r->status + r->stlen - 2, 2); //for crlf nb++; } +#endif return nb; } @@ -160,8 +175,10 @@ int http_response_uv(void) int http_request_parse(void) { +#ifndef STANDALONE struct sandbox *s = sandbox_current(); http_parser_execute(&s->hp, &settings, s->req_resp_data, s->rr_data_len); +#endif return 0; } diff --git a/runtime/src/libc/uvio.c b/runtime/src/libc/uvio.c index 76a5d77..c834d0c 100644 --- a/runtime/src/libc/uvio.c +++ b/runtime/src/libc/uvio.c @@ -1,6 +1,7 @@ #ifdef USE_UVIO #include #include +#include // What should we tell the child program its UID and GID are? #define UID 0xFF @@ -434,6 +435,7 @@ i32 wasm_readv(i32 fd, i32 iov_offset, i32 iovcnt) { if (fd == 0) { +#ifdef STANDALONE int len = 0, r = 0; struct wasm_iovec *iov = get_memory_ptr_void(iov_offset, iovcnt * sizeof(struct wasm_iovec)); for (int i = 0; i < iovcnt; i+= RDWR_VEC_MAX) { @@ -450,6 +452,25 @@ wasm_readv(i32 fd, i32 iov_offset, i32 iovcnt) } return r < 0 ? r : len; +#else + //both 1 and 2 go to client. + int len = 0; + struct wasm_iovec *iov = get_memory_ptr_void(iov_offset, iovcnt * sizeof(struct wasm_iovec)); + struct sandbox *s = sandbox_current(); + struct http_request *r = &s->rqi; + if (r->bodylen <= 0) return 0; + for (int i = 0; i < iovcnt; i++) { + int l = iov[i].len > r->body_len ? r->body_len : iov[i].len; + if (l <= 0) break; + char *b = get_memory_ptr_void(iov[i].base_offset, iov[i].len); + //http request body! + memcpy(b, r->body + len, l); + len += l; + r->bodylen -= l; + } + + return len; +#endif } // TODO: read on other file types int gret = 0; @@ -485,6 +506,7 @@ wasm_writev(i32 fd, i32 iov_offset, i32 iovcnt) { struct sandbox *c = sandbox_current(); if (fd == 1 || fd == 2) { +#ifndef STANDALONE //both 1 and 2 go to client. int len = 0; struct wasm_iovec *iov = get_memory_ptr_void(iov_offset, iovcnt * sizeof(struct wasm_iovec)); @@ -496,20 +518,22 @@ wasm_writev(i32 fd, i32 iov_offset, i32 iovcnt) } return len; -// for (int i = 0; i < iovcnt; i+= RDWR_VEC_MAX) { -// struct iovec bufs[RDWR_VEC_MAX] = { 0 }; -// int j = 0; -// for (j = 0; j < RDWR_VEC_MAX && i + j < iovcnt; j++) { -// bufs[j].iov_base = get_memory_ptr_void(iov[i + j].base_offset, iov[i + j].len); -// bufs[j].iov_len = iov[i + j].len; -// } -// -// r = writev(fd, bufs, j); -// if (r <= 0) break; -// len += r; -// } -// -// return r < 0 ? r : len; +#else + for (int i = 0; i < iovcnt; i+= RDWR_VEC_MAX) { + struct iovec bufs[RDWR_VEC_MAX] = { 0 }; + int j = 0; + for (j = 0; j < RDWR_VEC_MAX && i + j < iovcnt; j++) { + bufs[j].iov_base = get_memory_ptr_void(iov[i + j].base_offset, iov[i + j].len); + bufs[j].iov_len = iov[i + j].len; + } + + r = writev(fd, bufs, j); + if (r <= 0) break; + len += r; + } + + return r < 0 ? r : len; +#endif } // TODO: read on other file types int d = io_handle_fd(fd); diff --git a/runtime/src/main.c b/runtime/src/main.c index 3265337..2883205 100644 --- a/runtime/src/main.c +++ b/runtime/src/main.c @@ -28,7 +28,7 @@ usage(char *cmd) } int -main(int argc, char* argv[]) +main(int argc, char** argv) { #ifndef STANDALONE int i = 0, rtthd_ret[SBOX_NCORES] = { 0 }; @@ -124,10 +124,24 @@ main(int argc, char* argv[]) arch_context_init(&base_context, 0, 0); uv_loop_init(&uvio); + int ac = 1; + char *args = argv[1]; + if (argc - 1 > 1) { + ac = argc - 1; + char **av = argv + 1; + args = malloc(sizeof(char) * MOD_ARG_MAX_SZ * ac); + memset(args, 0, sizeof(char) * MOD_ARG_MAX_SZ * ac); + + for (int i = 0; i < ac; i++) { + char *a = args + (i * MOD_ARG_MAX_SZ * sizeof(char)); + strcpy(a, av[i]); + } + } + /* in current dir! */ - struct module *m = module_alloc(argv[1], argv[1], 0, 0, 0, 0, 0, 0, 0); + struct module *m = module_alloc(args, args, ac, 0, 0, 0, 0, 0, 0); assert(m); - struct sandbox *s = sandbox_alloc(m, argv[1], 0, NULL); + struct sandbox *s = sandbox_alloc(m, args, 0, NULL); exit(0); #endif diff --git a/runtime/src/module.c b/runtime/src/module.c index 5deb6a9..264f744 100644 --- a/runtime/src/module.c +++ b/runtime/src/module.c @@ -23,11 +23,13 @@ module_find_by_name(char *name) struct module * module_find_by_sock(int sock) { +#ifndef STANDALONE int f = __mod_free_off; for (int i = 0; i < f; i++) { assert(__mod_db[i]); if (__mod_db[i]->srvsock == sock) return __mod_db[i]; } +#endif return NULL; } diff --git a/runtime/src/runtime.c b/runtime/src/runtime.c index 2c6ecaa..8efe587 100644 --- a/runtime/src/runtime.c +++ b/runtime/src/runtime.c @@ -246,6 +246,7 @@ sandbox_exit(void) void * runtime_accept_thdfn(void *d) { +#ifndef STANDALONE struct epoll_event *epevts = (struct epoll_event *)malloc(EPOLL_MAX * sizeof(struct epoll_event)); int nreqs = 0; while (1) { @@ -274,6 +275,7 @@ runtime_accept_thdfn(void *d) } free(epevts); +#endif return NULL; } diff --git a/runtime/src/sandbox.c b/runtime/src/sandbox.c index 5d5e3e5..a2b4912 100644 --- a/runtime/src/sandbox.c +++ b/runtime/src/sandbox.c @@ -10,7 +10,11 @@ static inline struct sandbox * sandbox_memory_map(struct module *m) { unsigned long mem_sz = SBOX_MAX_MEM; // 4GB +#ifndef STANDALONE unsigned long sb_sz = sizeof(struct sandbox) + m->max_rr_sz; +#else + unsigned long sb_sz = sizeof(struct sandbox); +#endif unsigned long lm_sz = WASM_PAGE_SIZE * WASM_START_PAGES; if (lm_sz + sb_sz > mem_sz) return NULL; @@ -56,7 +60,7 @@ sandbox_args_setup(i32 argc) array_ptr[i] = string_off; // why get_memory_ptr_for_runtime?? - strncpy(get_memory_ptr_for_runtime(string_off, strlen(arg) + 1), arg, strlen(arg)); + strncpy(get_memory_ptr_for_runtime(string_off, str_sz), arg, strlen(arg)); string_off += str_sz; } @@ -85,8 +89,10 @@ sb_alloc_callback(uv_handle_t *h, size_t suggested, uv_buf_t *buf) { struct sandbox *c = h->data; +#ifndef STANDALONE buf->base = (c->req_resp_data + c->rr_data_len); buf->len = (c->mod->max_rr_sz - c->rr_data_len); +#endif } static inline void @@ -275,10 +281,12 @@ sandbox_alloc(struct module *mod, char *args, int sock, const struct sockaddr *a perror("mmap"); assert(0); } +#ifndef STANDALONE sb->csock = sock; + if (addr) memcpy(&sb->client, addr, sizeof(struct sockaddr)); +#endif for (int i = 0; i < SBOX_MAX_OPEN; i++) sb->handles[i].fd = -1; ps_list_init_d(sb); - if (addr) memcpy(&sb->client, addr, sizeof(struct sockaddr)); arch_context_init(&sb->ctxt, (reg_t)sandbox_entry, (reg_t)(sb->stack_start + sb->stack_size)); sandbox_run(sb);