Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(192)

Side by Side Diff: runtime/wasm-runtime.cpp

Issue 1913153003: Subzero. Wasm. Implement sbrk and correctly do bounds checks. (Closed) Base URL: https://chromium.googlesource.com/native_client/pnacl-subzero.git@master
Patch Set: Code review feedback Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 //===- subzero/runtime/wasm-runtime.cpp - Subzero WASM runtime source -----===// 1 //===- subzero/runtime/wasm-runtime.cpp - Subzero WASM runtime source -----===//
2 // 2 //
3 // The Subzero Code Generator 3 // The Subzero Code Generator
4 // 4 //
5 // This file is distributed under the University of Illinois Open Source 5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details. 6 // License. See LICENSE.TXT for details.
7 // 7 //
8 //===----------------------------------------------------------------------===// 8 //===----------------------------------------------------------------------===//
9 // 9 //
10 // This file implements the system calls required by the libc that is included 10 // This file implements the system calls required by the libc that is included
11 // in WebAssembly programs. 11 // in WebAssembly programs.
12 // 12 //
13 //===----------------------------------------------------------------------===// 13 //===----------------------------------------------------------------------===//
14 14
15 #include <cassert>
15 #include <cmath> 16 #include <cmath>
17 // TODO (eholk): change to cstdint
18 #include <stdint.h>
19
20 namespace {
21 uint32_t HeapBreak;
22
23 // TODO (eholk): make all of these constexpr.
24 const uint32_t PageSizeLog2 = 16;
25 const uint32_t PageSize = 1 << PageSizeLog2; // 64KB
26 const uint32_t StackPtrLoc = 1024; // defined by emscripten
27
28 uint32_t pageNum(uint32_t Index) { return Index >> PageSizeLog2; }
29 } // end of anonymous namespace
16 30
17 namespace env { 31 namespace env {
18 double floor(double X) { return std::floor(X); } 32 double floor(double X) { return std::floor(X); }
19 33
20 float floor(float X) { return std::floor(X); } 34 float floor(float X) { return std::floor(X); }
21 } 35 } // end of namespace env
22 36
23 // TODO (eholk): move the C parts outside and use C++ name mangling. 37 // TODO (eholk): move the C parts outside and use C++ name mangling.
24 extern "C" { 38 extern "C" {
25 #include <errno.h> 39 #include <errno.h>
26 #include <fcntl.h> 40 #include <fcntl.h>
27 #include <math.h> 41 #include <math.h>
28 #include <stdio.h> 42 #include <stdio.h>
29 #include <stdlib.h> 43 #include <stdlib.h>
30 #include <string.h> 44 #include <string.h>
31 #include <sys/types.h> 45 #include <sys/types.h>
32 #include <sys/stat.h> 46 #include <sys/stat.h>
33 #include <unistd.h> 47 #include <unistd.h>
34 48
35 extern char WASM_MEMORY[]; 49 extern char WASM_MEMORY[];
50 extern uint32_t WASM_DATA_SIZE;
51 extern uint32_t WASM_NUM_PAGES;
36 52
37 void env$$abort() { 53 void env$$abort() {
38 fprintf(stderr, "Aborting...\n"); 54 fprintf(stderr, "Aborting...\n");
39 abort(); 55 abort();
40 } 56 }
41 57
42 void env$$_abort() { env$$abort(); } 58 void env$$_abort() { env$$abort(); }
43 59
44 double env$$floor_f(float X) { return env::floor(X); } 60 double env$$floor_f(float X) { return env::floor(X); }
45 double env$$floor_d(double X) { return env::floor(X); } 61 double env$$floor_d(double X) { return env::floor(X); }
46 62
47 void env$$exit(int Status) { exit(Status); } 63 void env$$exit(int Status) { exit(Status); }
48 void env$$_exit(int Status) { env$$exit(Status); } 64 void env$$_exit(int Status) { env$$exit(Status); }
49 65
50 #define UNIMPLEMENTED(f) \ 66 #define UNIMPLEMENTED(f) \
51 void env$$##f() { \ 67 void env$$##f() { \
52 fprintf(stderr, "Unimplemented: " #f "\n"); \ 68 fprintf(stderr, "Unimplemented: " #f "\n"); \
53 abort(); \ 69 abort(); \
54 } 70 }
55 71
56 UNIMPLEMENTED(sbrk) 72 int32_t env$$sbrk(int32_t Increment) {
73 HeapBreak += Increment;
74 return HeapBreak;
75 }
76
57 UNIMPLEMENTED(setjmp) 77 UNIMPLEMENTED(setjmp)
58 UNIMPLEMENTED(longjmp) 78 UNIMPLEMENTED(longjmp)
59 UNIMPLEMENTED(__assert_fail) 79 UNIMPLEMENTED(__assert_fail)
60 UNIMPLEMENTED(__builtin_malloc) 80 UNIMPLEMENTED(__builtin_malloc)
61 UNIMPLEMENTED(__builtin_isinff) 81 UNIMPLEMENTED(__builtin_isinff)
62 UNIMPLEMENTED(__builtin_isinfl) 82 UNIMPLEMENTED(__builtin_isinfl)
63 UNIMPLEMENTED(__builtin_apply) 83 UNIMPLEMENTED(__builtin_apply)
64 UNIMPLEMENTED(__builtin_apply_args) 84 UNIMPLEMENTED(__builtin_apply_args)
65 UNIMPLEMENTED(pthread_cleanup_push) 85 UNIMPLEMENTED(pthread_cleanup_push)
66 UNIMPLEMENTED(pthread_cleanup_pop) 86 UNIMPLEMENTED(pthread_cleanup_pop)
67 UNIMPLEMENTED(pthread_self) 87 UNIMPLEMENTED(pthread_self)
68 UNIMPLEMENTED(__floatditf) 88 UNIMPLEMENTED(__floatditf)
69 UNIMPLEMENTED(__floatsitf) 89 UNIMPLEMENTED(__floatsitf)
70 UNIMPLEMENTED(__fixtfdi) 90 UNIMPLEMENTED(__fixtfdi)
71 UNIMPLEMENTED(__fixtfsi) 91 UNIMPLEMENTED(__fixtfsi)
72 UNIMPLEMENTED(__fixsfti) 92 UNIMPLEMENTED(__fixsfti)
73 UNIMPLEMENTED(__netf2) 93 UNIMPLEMENTED(__netf2)
74 UNIMPLEMENTED(__getf2) 94 UNIMPLEMENTED(__getf2)
75 UNIMPLEMENTED(__eqtf2) 95 UNIMPLEMENTED(__eqtf2)
76 UNIMPLEMENTED(__lttf2) 96 UNIMPLEMENTED(__lttf2)
77 UNIMPLEMENTED(__addtf3) 97 UNIMPLEMENTED(__addtf3)
78 UNIMPLEMENTED(__subtf3) 98 UNIMPLEMENTED(__subtf3)
79 UNIMPLEMENTED(__divtf3) 99 UNIMPLEMENTED(__divtf3)
80 UNIMPLEMENTED(__multf3) 100 UNIMPLEMENTED(__multf3)
81 UNIMPLEMENTED(__multi3) 101 UNIMPLEMENTED(__multi3)
82 UNIMPLEMENTED(__lock) 102 UNIMPLEMENTED(__lock)
83 UNIMPLEMENTED(__unlock) 103 UNIMPLEMENTED(__unlock)
84 UNIMPLEMENTED(__syscall6) // sys_close 104 UNIMPLEMENTED(__syscall6) // sys_close
85 UNIMPLEMENTED(__syscall140) // sys_llseek 105 UNIMPLEMENTED(__syscall140) // sys_llseek
86 UNIMPLEMENTED(__syscall192) // sys_mmap?
87 UNIMPLEMENTED(__unordtf2) 106 UNIMPLEMENTED(__unordtf2)
88 UNIMPLEMENTED(__fixunstfsi) 107 UNIMPLEMENTED(__fixunstfsi)
89 UNIMPLEMENTED(__floatunsitf) 108 UNIMPLEMENTED(__floatunsitf)
90 UNIMPLEMENTED(__extenddftf2) 109 UNIMPLEMENTED(__extenddftf2)
91 110
92 void *wasmPtr(int Index) { 111 void *wasmPtr(uint32_t Index) {
93 // TODO (eholk): get the mask from the WASM file. 112 if (pageNum(Index) < WASM_NUM_PAGES) {
94 const int MASK = 0xffffff; 113 return WASM_MEMORY + Index;
95 Index &= MASK; 114 }
96 115 abort();
97 return WASM_MEMORY + Index;
98 } 116 }
99 117
100 extern int __szwasm_main(int, const char **); 118 extern int __szwasm_main(int, const char **);
101 119
102 #define WASM_REF(Type, Index) ((Type *)wasmPtr(Index)) 120 #define WASM_REF(Type, Index) ((Type *)wasmPtr(Index))
103 #define WASM_DEREF(Type, Index) (*WASM_REF(Type, Index)) 121 #define WASM_DEREF(Type, Index) (*WASM_REF(Type, Index))
104 122
105 int main(int argc, const char **argv) { return __szwasm_main(argc, argv); } 123 int main(int argc, const char **argv) {
124 // Initialize the break to the nearest page boundary after the data segment
125 HeapBreak = (WASM_DATA_SIZE + PageSize - 1) & ~(PageSize - 1);
126
127 // Initialize the stack pointer.
128 WASM_DEREF(int32_t, StackPtrLoc) = WASM_NUM_PAGES << PageSizeLog2;
129
130 return __szwasm_main(argc, argv);
131 }
106 132
107 int env$$abs(int a) { return abs(a); } 133 int env$$abs(int a) { return abs(a); }
108 134
109 double env$$pow(double x, double y) { return pow(x, y); } 135 double env$$pow(double x, double y) { return pow(x, y); }
110 136
111 /// sys_write 137 /// sys_write
112 int env$$__syscall4(int Which, int VarArgs) { 138 int env$$__syscall4(int Which, int VarArgs) {
113 int Fd = WASM_DEREF(int, VarArgs + 0 * sizeof(int)); 139 int Fd = WASM_DEREF(int, VarArgs + 0 * sizeof(int));
114 int Buffer = WASM_DEREF(int, VarArgs + 1 * sizeof(int)); 140 int Buffer = WASM_DEREF(int, VarArgs + 1 * sizeof(int));
115 int Length = WASM_DEREF(int, VarArgs + 2 * sizeof(int)); 141 int Length = WASM_DEREF(int, VarArgs + 2 * sizeof(int));
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
161 187
162 int Curr = write(Fd, Ptr, Length); 188 int Curr = write(Fd, Ptr, Length);
163 189
164 if (Curr < 0) { 190 if (Curr < 0) {
165 return -1; 191 return -1;
166 } 192 }
167 Count += Curr; 193 Count += Curr;
168 } 194 }
169 return Count; 195 return Count;
170 } 196 }
197
198 /// sys_mmap_pgoff
199 int env$$__syscall192(int Which, int VarArgs) {
200 (void)Which;
201 (void)VarArgs;
202
203 // TODO (eholk): figure out how to implement this.
204
205 return -ENOMEM;
206 }
171 } // end of extern "C" 207 } // end of extern "C"
OLDNEW
« no previous file with comments | « pydir/wasm-run-torture-tests.py ('k') | src/WasmTranslator.cpp » ('j') | src/WasmTranslator.cpp » ('J')

Powered by Google App Engine
This is Rietveld 408576698