| OLD | NEW |
| (Empty) |
| 1 //===- subzero/runtime/wasm-runtime.c - Subzero WASM runtime source -------===// | |
| 2 // | |
| 3 // The Subzero Code Generator | |
| 4 // | |
| 5 // This file is distributed under the University of Illinois Open Source | |
| 6 // License. See LICENSE.TXT for details. | |
| 7 // | |
| 8 //===----------------------------------------------------------------------===// | |
| 9 // | |
| 10 // This file implements the system calls required by the libc that is included | |
| 11 // in WebAssembly programs. | |
| 12 // | |
| 13 //===----------------------------------------------------------------------===// | |
| 14 | |
| 15 #include <errno.h> | |
| 16 #include <fcntl.h> | |
| 17 #include <stdio.h> | |
| 18 #include <stdlib.h> | |
| 19 #include <string.h> | |
| 20 #include <sys/types.h> | |
| 21 #include <sys/stat.h> | |
| 22 #include <unistd.h> | |
| 23 | |
| 24 extern char WASM_MEMORY[]; | |
| 25 | |
| 26 void env$$abort() { | |
| 27 fprintf(stderr, "Aborting...\n"); | |
| 28 abort(); | |
| 29 } | |
| 30 | |
| 31 void env$$_abort() { env$$abort(); } | |
| 32 | |
| 33 void env$$exit(int Status) { exit(Status); } | |
| 34 void env$$_exit(int Status) { env$$exit(Status); } | |
| 35 | |
| 36 #define UNIMPLEMENTED(f) \ | |
| 37 void env$$##f() { \ | |
| 38 fprintf(stderr, "Unimplemented: " #f "\n"); \ | |
| 39 abort(); \ | |
| 40 } | |
| 41 | |
| 42 UNIMPLEMENTED(sbrk) | |
| 43 UNIMPLEMENTED(setjmp) | |
| 44 UNIMPLEMENTED(longjmp) | |
| 45 UNIMPLEMENTED(__assert_fail) | |
| 46 UNIMPLEMENTED(__builtin_malloc) | |
| 47 UNIMPLEMENTED(__builtin_apply) | |
| 48 UNIMPLEMENTED(__builtin_apply_args) | |
| 49 UNIMPLEMENTED(pthread_cleanup_push) | |
| 50 UNIMPLEMENTED(pthread_cleanup_pop) | |
| 51 UNIMPLEMENTED(pthread_self) | |
| 52 UNIMPLEMENTED(abs) | |
| 53 UNIMPLEMENTED(__floatditf) | |
| 54 UNIMPLEMENTED(__floatsitf) | |
| 55 UNIMPLEMENTED(__fixtfdi) | |
| 56 UNIMPLEMENTED(__fixtfsi) | |
| 57 UNIMPLEMENTED(__fixsfti) | |
| 58 UNIMPLEMENTED(__netf2) | |
| 59 UNIMPLEMENTED(__getf2) | |
| 60 UNIMPLEMENTED(__eqtf2) | |
| 61 UNIMPLEMENTED(__lttf2) | |
| 62 UNIMPLEMENTED(__addtf3) | |
| 63 UNIMPLEMENTED(__subtf3) | |
| 64 UNIMPLEMENTED(__divtf3) | |
| 65 UNIMPLEMENTED(__multf3) | |
| 66 UNIMPLEMENTED(__multi3) | |
| 67 UNIMPLEMENTED(__lock) | |
| 68 UNIMPLEMENTED(__unlock) | |
| 69 UNIMPLEMENTED(__syscall6) | |
| 70 UNIMPLEMENTED(__syscall20) | |
| 71 UNIMPLEMENTED(__syscall140) | |
| 72 UNIMPLEMENTED(__syscall192) | |
| 73 | |
| 74 void *wasmPtr(int Index) { | |
| 75 // TODO (eholk): get the mask from the WASM file. | |
| 76 const int MASK = 0x3fffff; | |
| 77 Index &= MASK; | |
| 78 | |
| 79 return WASM_MEMORY + Index; | |
| 80 } | |
| 81 | |
| 82 extern int __szwasm_main(int, const char **); | |
| 83 | |
| 84 #define WASM_REF(Type, Index) ((Type *)wasmPtr(Index)) | |
| 85 #define WASM_DEREF(Type, Index) (*WASM_REF(Type, Index)) | |
| 86 | |
| 87 int main(int argc, const char **argv) { return __szwasm_main(argc, argv); } | |
| 88 | |
| 89 /// sys_write | |
| 90 int env$$__syscall4(int Which, int VarArgs) { | |
| 91 int Fd = WASM_DEREF(int, VarArgs + 0 * sizeof(int)); | |
| 92 int Buffer = WASM_DEREF(int, VarArgs + 1 * sizeof(int)); | |
| 93 int Length = WASM_DEREF(int, VarArgs + 2 * sizeof(int)); | |
| 94 | |
| 95 return write(Fd, WASM_REF(char *, Buffer), Length); | |
| 96 } | |
| 97 | |
| 98 /// sys_open | |
| 99 int env$$__syscall5(int Which, int VarArgs) { | |
| 100 int WasmPath = WASM_DEREF(int, VarArgs); | |
| 101 int Flags = WASM_DEREF(int, VarArgs + 4); | |
| 102 int Mode = WASM_DEREF(int, VarArgs + 8); | |
| 103 const char *Path = WASM_REF(char, WasmPath); | |
| 104 | |
| 105 fprintf(stderr, "sys_open(%s, %d, %d)\n", Path, Flags, Mode); | |
| 106 | |
| 107 return open(Path, Flags, Mode); | |
| 108 } | |
| 109 | |
| 110 /// sys_ioctl | |
| 111 int env$$__syscall54(int A, int B) { | |
| 112 int Fd = WASM_DEREF(int, B + 0 * sizeof(int)); | |
| 113 int Op = WASM_DEREF(int, B + 1 * sizeof(int)); | |
| 114 int ArgP = WASM_DEREF(int, B + 2 * sizeof(int)); | |
| 115 // TODO (eholk): implement sys_ioctl | |
| 116 return -ENOTTY; | |
| 117 } | |
| 118 | |
| 119 int env$$__syscall146(int Which, int VarArgs) { | |
| 120 fprintf(stderr, "syscall146\n"); | |
| 121 | |
| 122 int Fd = WASM_DEREF(int, VarArgs); | |
| 123 int Iov = WASM_DEREF(int, VarArgs + sizeof(int)); | |
| 124 int Iovcnt = WASM_DEREF(int, VarArgs + 2 * sizeof(int)); | |
| 125 | |
| 126 fprintf(stderr, " Fd=%d, Iov=%d (%p), Iovcnt=%d\n", Fd, Iov, wasmPtr(Iov), | |
| 127 Iovcnt); | |
| 128 | |
| 129 int Count = 0; | |
| 130 | |
| 131 for (int I = 0; I < Iovcnt; ++I) { | |
| 132 void *Ptr = WASM_REF(void, WASM_DEREF(int, Iov + I * 8)); | |
| 133 int Length = WASM_DEREF(int, Iov + I * 8 + 4); | |
| 134 | |
| 135 fprintf(stderr, " [%d] write(%d, %p, %d) = ", I, Fd, Ptr, Length); | |
| 136 int Curr = write(Fd, Ptr, Length); | |
| 137 | |
| 138 fprintf(stderr, "%d\n", Curr); | |
| 139 | |
| 140 if (Curr < 0) { | |
| 141 return -1; | |
| 142 } | |
| 143 Count += Curr; | |
| 144 } | |
| 145 fprintf(stderr, " Count = %d\n", Count); | |
| 146 return Count; | |
| 147 } | |
| OLD | NEW |