OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2014 The Chromium Authors. All rights reserved. | 2 * Copyright 2014 The Chromium Authors. All rights reserved. |
3 * Use of this source code is governed by a BSD-style license that can be | 3 * Use of this source code is governed by a BSD-style license that can be |
4 * found in the LICENSE file. | 4 * found in the LICENSE file. |
5 */ | 5 */ |
6 | 6 |
7 /* | 7 /* |
8 * This is a minimal NaCl program without libc. It uses NaCl's stable IRT ABI. | 8 * This is a minimal NaCl program without libc. It uses NaCl's stable IRT ABI. |
9 */ | 9 */ |
10 | 10 |
11 #include <stdint.h> | 11 #include <stdint.h> |
12 | 12 |
13 #include "native_client/src/include/elf_auxv.h" | 13 #include "native_client/src/include/elf_auxv.h" |
14 #include "native_client/src/untrusted/irt/irt.h" | 14 #include "native_client/src/untrusted/irt/irt.h" |
15 #include "ppapi/c/pp_errors.h" | 15 #include "ppapi/c/pp_errors.h" |
16 #include "ppapi/c/ppb_messaging.h" | 16 #include "ppapi/c/ppb_messaging.h" |
17 #include "ppapi/c/ppb_var.h" | 17 #include "ppapi/c/ppb_var.h" |
18 #include "ppapi/c/ppp_instance.h" | 18 #include "ppapi/c/ppp_instance.h" |
19 #include "ppapi/c/ppp_messaging.h" | 19 #include "ppapi/c/ppp_messaging.h" |
20 #include "ppapi/nacl_irt/public/irt_ppapi.h" | 20 #include "ppapi/nacl_irt/public/irt_ppapi.h" |
21 | 21 |
22 static struct nacl_irt_basic __libnacl_irt_basic; | 22 static struct nacl_irt_basic __libnacl_irt_basic; |
| 23 static struct nacl_irt_exception_handling __libnacl_irt_exception_handling; |
23 static struct nacl_irt_random __libnacl_irt_random; | 24 static struct nacl_irt_random __libnacl_irt_random; |
24 static TYPE_nacl_irt_query __nacl_irt_query; | 25 static TYPE_nacl_irt_query __nacl_irt_query; |
25 static struct PPP_Instance_1_0 ppp_instance; | 26 static struct PPP_Instance_1_0 ppp_instance; |
26 static struct PPP_Messaging_1_0 ppp_messaging; | 27 static struct PPP_Messaging_1_0 ppp_messaging; |
27 static const struct PPB_Messaging_1_0* ppb_messaging; | 28 static const struct PPB_Messaging_1_0* ppb_messaging; |
28 static const struct PPB_Var_1_2* ppb_var; | 29 static const struct PPB_Var_1_2* ppb_var; |
29 | 30 |
30 /* | 31 /* |
31 * To support 64bit binary, we declare Elf_auxv_t by using uintptr_t. | 32 * To support 64bit binary, we declare Elf_auxv_t by using uintptr_t. |
32 * See also native_client/src/include/elf32.h. | 33 * See also native_client/src/include/elf32.h. |
(...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
181 } | 182 } |
182 } | 183 } |
183 if (num_zeroes > 100) { | 184 if (num_zeroes > 100) { |
184 PostStringMessage(instance, "Too many zeros in this random stream."); | 185 PostStringMessage(instance, "Too many zeros in this random stream."); |
185 return; | 186 return; |
186 } | 187 } |
187 | 188 |
188 PostStringMessage(instance, "randomsuccess"); | 189 PostStringMessage(instance, "randomsuccess"); |
189 } | 190 } |
190 | 191 |
| 192 static PP_Instance g_instance_for_exception_handler = -1; |
| 193 static void MyNaClExceptionHandler(struct NaClExceptionContext *context) { |
| 194 // TODO verify that I have a stack that is set by altstack. |
| 195 |
| 196 PostStringMessage(g_instance_for_exception_handler, "exceptionsuccess"); |
| 197 |
| 198 // Wait until I get killed by testing infrastructure to give chance |
| 199 // to PostMessage to finish asynchronously notifying javascript |
| 200 // instead of crashing immediately. Doing ppb_console.Log() seems |
| 201 // to also do the job, take your pick. |
| 202 while(1) { |
| 203 __libnacl_irt_basic.sched_yield(); |
| 204 } |
| 205 } |
| 206 |
| 207 static void TestException(PP_Instance instance) { |
| 208 // Exception Handling. |
| 209 g_instance_for_exception_handler = instance; |
| 210 if (__libnacl_irt_exception_handling.exception_handler( |
| 211 MyNaClExceptionHandler, NULL)) { |
| 212 PostStringMessage(instance, "Error on registering exception"); |
| 213 return; |
| 214 } |
| 215 |
| 216 // Crash myself here to invoke the crash handler. |
| 217 *(char*)0 = 0; |
| 218 |
| 219 PostStringMessage(instance, "I think I should have crashed already."); |
| 220 } |
| 221 |
191 // Handle message from javascript, javascript side will tell me what | 222 // Handle message from javascript, javascript side will tell me what |
192 // kind of test it wants, and I will respond accordingly. | 223 // kind of test it wants, and I will respond accordingly. |
193 static void HandleMessage(PP_Instance instance, struct PP_Var message) { | 224 static void HandleMessage(PP_Instance instance, struct PP_Var message) { |
194 if (!ppb_messaging || !ppb_var || message.type != PP_VARTYPE_STRING) { | 225 if (!ppb_messaging || !ppb_var || message.type != PP_VARTYPE_STRING) { |
195 __libnacl_irt_basic.exit(1); | 226 __libnacl_irt_basic.exit(1); |
196 } | 227 } |
197 | 228 |
198 uint32_t message_len; | 229 uint32_t message_len; |
199 const char* message_string = ppb_var->VarToUtf8(message, &message_len); | 230 const char* message_string = ppb_var->VarToUtf8(message, &message_len); |
200 | 231 |
201 if (!my_strncmp("Message from Javascript to NaCl", | 232 if (!my_strncmp("Message from Javascript to NaCl", |
202 message_string, message_len)) { | 233 message_string, message_len)) { |
203 // Reply back with what we received. | 234 // Reply back with what we received. |
204 ppb_messaging->PostMessage(instance, message); | 235 ppb_messaging->PostMessage(instance, message); |
205 } else if (!my_strncmp("random", message_string, message_len)) { | 236 } else if (!my_strncmp("random", message_string, message_len)) { |
206 TestRandom(instance); | 237 TestRandom(instance); |
| 238 } else if (!my_strncmp("exception", message_string, message_len)) { |
| 239 TestException(instance); |
207 } else { | 240 } else { |
208 // Unknown message came. | 241 // Unknown message came. |
209 PostStringMessage(instance, "Unknown message type"); | 242 PostStringMessage(instance, "Unknown message type"); |
210 } | 243 } |
211 } | 244 } |
212 | 245 |
213 void _start(uintptr_t info[]) { | 246 void _start(uintptr_t info[]) { |
214 Elf_auxv_t* auxv = nacl_startup_auxv(info); | 247 Elf_auxv_t* auxv = nacl_startup_auxv(info); |
215 grok_auxv(auxv); | 248 grok_auxv(auxv); |
216 DO_QUERY(NACL_IRT_BASIC_v0_1, __libnacl_irt_basic); | 249 DO_QUERY(NACL_IRT_BASIC_v0_1, __libnacl_irt_basic); |
217 DO_QUERY(NACL_IRT_RANDOM_v0_1, __libnacl_irt_random); | 250 DO_QUERY(NACL_IRT_RANDOM_v0_1, __libnacl_irt_random); |
| 251 DO_QUERY(NACL_IRT_EXCEPTION_HANDLING_v0_1, __libnacl_irt_exception_handling); |
218 | 252 |
219 struct nacl_irt_ppapihook ppapihook; | 253 struct nacl_irt_ppapihook ppapihook; |
220 DO_QUERY(NACL_IRT_PPAPIHOOK_v0_1, ppapihook); | 254 DO_QUERY(NACL_IRT_PPAPIHOOK_v0_1, ppapihook); |
221 | 255 |
222 /* This is local as a workaround to avoid having to apply | 256 /* This is local as a workaround to avoid having to apply |
223 * relocations to global variables. */ | 257 * relocations to global variables. */ |
224 struct PP_StartFunctions start_funcs = { | 258 struct PP_StartFunctions start_funcs = { |
225 MyPPP_InitializeModule, | 259 MyPPP_InitializeModule, |
226 MyPPP_ShutdownModule, | 260 MyPPP_ShutdownModule, |
227 MyPPP_GetInterface, | 261 MyPPP_GetInterface, |
228 }; | 262 }; |
229 /* Similarly, initialise some global variables, avoiding relocations. */ | 263 /* Similarly, initialise some global variables, avoiding relocations. */ |
230 struct PPP_Instance_1_0 local_ppp_instance = { | 264 struct PPP_Instance_1_0 local_ppp_instance = { |
231 DidCreate, | 265 DidCreate, |
232 DidDestroy, | 266 DidDestroy, |
233 DidChangeView, | 267 DidChangeView, |
234 DidChangeFocus, | 268 DidChangeFocus, |
235 HandleDocumentLoad, | 269 HandleDocumentLoad, |
236 }; | 270 }; |
237 ppp_instance = local_ppp_instance; | 271 ppp_instance = local_ppp_instance; |
238 struct PPP_Messaging_1_0 local_ppp_messaging = { | 272 struct PPP_Messaging_1_0 local_ppp_messaging = { |
239 HandleMessage, | 273 HandleMessage, |
240 }; | 274 }; |
241 ppp_messaging = local_ppp_messaging; | 275 ppp_messaging = local_ppp_messaging; |
242 | 276 |
243 ppapihook.ppapi_start(&start_funcs); | 277 ppapihook.ppapi_start(&start_funcs); |
244 | 278 |
245 __libnacl_irt_basic.exit(0); | 279 __libnacl_irt_basic.exit(0); |
246 } | 280 } |
OLD | NEW |