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

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: Cleanup 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 #include <stdint.h>
Jim Stichnoth 2016/04/22 20:48:53 <cstdint> ? (I think cstdint requires C++11 or lat
Eric Holk 2016/04/22 22:32:47 So far I've been avoiding using C++11 because of s
18
19 namespace {
20 int32_t HeapBreak;
21 const int32_t PageSizeLog2 = 16;
Jim Stichnoth 2016/04/22 20:48:53 constexpr
Jim Stichnoth 2016/04/22 20:48:54 If it's a Log2 value, make it uint32_t
Eric Holk 2016/04/22 22:32:47 Done.
Eric Holk 2016/04/22 22:32:47 See my previous comment about C++11. I added a TOD
22 const int32_t PageSize = 1 << PageSizeLog2; // 64KB
Jim Stichnoth 2016/04/22 20:48:53 uint32_t ?
Eric Holk 2016/04/22 22:32:47 Done. I'll replace most of the other int32_t with
23 const int32_t StackPtrLoc = 1024; // defined by emscripten
24
25 int32_t pageNum(int32_t Index) { return Index >> PageSizeLog2; }
26 } // end of anonymous namespace
16 27
17 namespace env { 28 namespace env {
18 double floor(double X) { return std::floor(X); } 29 double floor(double X) { return std::floor(X); }
19 30
20 float floor(float X) { return std::floor(X); } 31 float floor(float X) { return std::floor(X); }
21 } 32 } // end of env namespace
Jim Stichnoth 2016/04/22 20:48:53 end of namespace env
Eric Holk 2016/04/22 22:32:48 Done.
22 33
23 // TODO (eholk): move the C parts outside and use C++ name mangling. 34 // TODO (eholk): move the C parts outside and use C++ name mangling.
24 extern "C" { 35 extern "C" {
25 #include <errno.h> 36 #include <errno.h>
26 #include <fcntl.h> 37 #include <fcntl.h>
27 #include <math.h> 38 #include <math.h>
28 #include <stdio.h> 39 #include <stdio.h>
29 #include <stdlib.h> 40 #include <stdlib.h>
30 #include <string.h> 41 #include <string.h>
31 #include <sys/types.h> 42 #include <sys/types.h>
32 #include <sys/stat.h> 43 #include <sys/stat.h>
33 #include <unistd.h> 44 #include <unistd.h>
34 45
35 extern char WASM_MEMORY[]; 46 extern char WASM_MEMORY[];
47 extern int32_t WASM_DATA_SIZE;
Jim Stichnoth 2016/04/22 20:48:53 uint32_t ?
Eric Holk 2016/04/22 22:32:48 Done.
48 extern int32_t WASM_NUM_PAGES;
36 49
37 void env$$abort() { 50 void env$$abort() {
38 fprintf(stderr, "Aborting...\n"); 51 fprintf(stderr, "Aborting...\n");
39 abort(); 52 abort();
40 } 53 }
41 54
42 void env$$_abort() { env$$abort(); } 55 void env$$_abort() { env$$abort(); }
43 56
44 double env$$floor_f(float X) { return env::floor(X); } 57 double env$$floor_f(float X) { return env::floor(X); }
45 double env$$floor_d(double X) { return env::floor(X); } 58 double env$$floor_d(double X) { return env::floor(X); }
46 59
47 void env$$exit(int Status) { exit(Status); } 60 void env$$exit(int Status) { exit(Status); }
48 void env$$_exit(int Status) { env$$exit(Status); } 61 void env$$_exit(int Status) { env$$exit(Status); }
49 62
50 #define UNIMPLEMENTED(f) \ 63 #define UNIMPLEMENTED(f) \
51 void env$$##f() { \ 64 void env$$##f() { \
52 fprintf(stderr, "Unimplemented: " #f "\n"); \ 65 fprintf(stderr, "Unimplemented: " #f "\n"); \
53 abort(); \ 66 abort(); \
54 } 67 }
55 68
56 UNIMPLEMENTED(sbrk) 69 int32_t env$$sbrk(int32_t Increment) {
70 HeapBreak += Increment;
71 return HeapBreak;
72 }
73
57 UNIMPLEMENTED(setjmp) 74 UNIMPLEMENTED(setjmp)
58 UNIMPLEMENTED(longjmp) 75 UNIMPLEMENTED(longjmp)
59 UNIMPLEMENTED(__assert_fail) 76 UNIMPLEMENTED(__assert_fail)
60 UNIMPLEMENTED(__builtin_malloc) 77 UNIMPLEMENTED(__builtin_malloc)
61 UNIMPLEMENTED(__builtin_isinff) 78 UNIMPLEMENTED(__builtin_isinff)
62 UNIMPLEMENTED(__builtin_isinfl) 79 UNIMPLEMENTED(__builtin_isinfl)
63 UNIMPLEMENTED(__builtin_apply) 80 UNIMPLEMENTED(__builtin_apply)
64 UNIMPLEMENTED(__builtin_apply_args) 81 UNIMPLEMENTED(__builtin_apply_args)
65 UNIMPLEMENTED(pthread_cleanup_push) 82 UNIMPLEMENTED(pthread_cleanup_push)
66 UNIMPLEMENTED(pthread_cleanup_pop) 83 UNIMPLEMENTED(pthread_cleanup_pop)
67 UNIMPLEMENTED(pthread_self) 84 UNIMPLEMENTED(pthread_self)
68 UNIMPLEMENTED(__floatditf) 85 UNIMPLEMENTED(__floatditf)
69 UNIMPLEMENTED(__floatsitf) 86 UNIMPLEMENTED(__floatsitf)
70 UNIMPLEMENTED(__fixtfdi) 87 UNIMPLEMENTED(__fixtfdi)
71 UNIMPLEMENTED(__fixtfsi) 88 UNIMPLEMENTED(__fixtfsi)
72 UNIMPLEMENTED(__fixsfti) 89 UNIMPLEMENTED(__fixsfti)
73 UNIMPLEMENTED(__netf2) 90 UNIMPLEMENTED(__netf2)
74 UNIMPLEMENTED(__getf2) 91 UNIMPLEMENTED(__getf2)
75 UNIMPLEMENTED(__eqtf2) 92 UNIMPLEMENTED(__eqtf2)
76 UNIMPLEMENTED(__lttf2) 93 UNIMPLEMENTED(__lttf2)
77 UNIMPLEMENTED(__addtf3) 94 UNIMPLEMENTED(__addtf3)
78 UNIMPLEMENTED(__subtf3) 95 UNIMPLEMENTED(__subtf3)
79 UNIMPLEMENTED(__divtf3) 96 UNIMPLEMENTED(__divtf3)
80 UNIMPLEMENTED(__multf3) 97 UNIMPLEMENTED(__multf3)
81 UNIMPLEMENTED(__multi3) 98 UNIMPLEMENTED(__multi3)
82 UNIMPLEMENTED(__lock) 99 UNIMPLEMENTED(__lock)
83 UNIMPLEMENTED(__unlock) 100 UNIMPLEMENTED(__unlock)
84 UNIMPLEMENTED(__syscall6) // sys_close 101 UNIMPLEMENTED(__syscall6) // sys_close
85 UNIMPLEMENTED(__syscall140) // sys_llseek 102 UNIMPLEMENTED(__syscall140) // sys_llseek
86 UNIMPLEMENTED(__syscall192) // sys_mmap?
87 UNIMPLEMENTED(__unordtf2) 103 UNIMPLEMENTED(__unordtf2)
88 UNIMPLEMENTED(__fixunstfsi) 104 UNIMPLEMENTED(__fixunstfsi)
89 UNIMPLEMENTED(__floatunsitf) 105 UNIMPLEMENTED(__floatunsitf)
90 UNIMPLEMENTED(__extenddftf2) 106 UNIMPLEMENTED(__extenddftf2)
91 107
92 void *wasmPtr(int Index) { 108 void *wasmPtr(int Index) {
93 // TODO (eholk): get the mask from the WASM file. 109 if (pageNum(Index) < WASM_NUM_PAGES && pageNum(Index) >= 0) {
94 const int MASK = 0xffffff; 110 return WASM_MEMORY + Index;
95 Index &= MASK; 111 }
96 112 abort();
97 return WASM_MEMORY + Index;
98 } 113 }
99 114
100 extern int __szwasm_main(int, const char **); 115 extern int __szwasm_main(int, const char **);
101 116
102 #define WASM_REF(Type, Index) ((Type *)wasmPtr(Index)) 117 #define WASM_REF(Type, Index) ((Type *)wasmPtr(Index))
103 #define WASM_DEREF(Type, Index) (*WASM_REF(Type, Index)) 118 #define WASM_DEREF(Type, Index) (*WASM_REF(Type, Index))
104 119
105 int main(int argc, const char **argv) { return __szwasm_main(argc, argv); } 120 int main(int argc, const char **argv) {
121 // Initialize the break to the nearest page boundary after the data segment
122 HeapBreak = (WASM_DATA_SIZE + PageSize - 1) & ~(PageSize - 1);
123
124 // Initialize the stack pointer.
125 WASM_DEREF(int32_t, StackPtrLoc) = WASM_NUM_PAGES * PageSize;
Jim Stichnoth 2016/04/22 20:48:53 Instead of "* PageSize", how about "<< PageSizeLog
Eric Holk 2016/04/22 22:32:47 I like the shift better, so done. I'd rather keep
126
127 return __szwasm_main(argc, argv);
128 }
106 129
107 int env$$abs(int a) { return abs(a); } 130 int env$$abs(int a) { return abs(a); }
108 131
109 double env$$pow(double x, double y) { return pow(x, y); } 132 double env$$pow(double x, double y) { return pow(x, y); }
110 133
111 /// sys_write 134 /// sys_write
112 int env$$__syscall4(int Which, int VarArgs) { 135 int env$$__syscall4(int Which, int VarArgs) {
113 int Fd = WASM_DEREF(int, VarArgs + 0 * sizeof(int)); 136 int Fd = WASM_DEREF(int, VarArgs + 0 * sizeof(int));
114 int Buffer = WASM_DEREF(int, VarArgs + 1 * sizeof(int)); 137 int Buffer = WASM_DEREF(int, VarArgs + 1 * sizeof(int));
115 int Length = WASM_DEREF(int, VarArgs + 2 * sizeof(int)); 138 int Length = WASM_DEREF(int, VarArgs + 2 * sizeof(int));
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
161 184
162 int Curr = write(Fd, Ptr, Length); 185 int Curr = write(Fd, Ptr, Length);
163 186
164 if (Curr < 0) { 187 if (Curr < 0) {
165 return -1; 188 return -1;
166 } 189 }
167 Count += Curr; 190 Count += Curr;
168 } 191 }
169 return Count; 192 return Count;
170 } 193 }
194
195 /// sys_mmap_pgoff
196 int env$$__syscall192(int Which, int VarArgs) {
197 (void)Which;
198 (void)VarArgs;
199
200 // TODO (eholk): figure out how to implement this.
201
202 return -ENOMEM;
203 }
171 } // end of extern "C" 204 } // end of extern "C"
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698