1 /* Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 * Use of this source code is governed by a BSD-style license that can be
3 * found in the LICENSE file.
7 * This example, is a modified version of hello world. It will start a second
8 * thread and cause that thread to crash via a NULL dereference.
14 #include "ppapi/c/pp_errors.h"
15 #include "ppapi/c/pp_module.h"
16 #include "ppapi/c/pp_var.h"
17 #include "ppapi/c/ppb.h"
18 #include "ppapi/c/ppb_core.h"
19 #include "ppapi/c/ppb_instance.h"
20 #include "ppapi/c/ppb_messaging.h"
21 #include "ppapi/c/ppb_var.h"
22 #include "ppapi/c/ppp.h"
23 #include "ppapi/c/ppp_instance.h"
24 #include "ppapi/c/ppp_messaging.h"
28 #include "error_handling/error_handling.h"
30 PPB_Messaging
* ppb_messaging_interface
= NULL
;
31 PPB_Var
* ppb_var_interface
= NULL
;
32 PPB_Core
* ppb_core_interface
= NULL
;
34 pthread_t g_NexeThread
;
35 pthread_t g_PPAPIThread
;
36 PP_Instance g_Instance
;
38 volatile int g_CrashTime
= 0;
40 void PostMessage(const char* str
);
42 void layer5(int x
, int y
) {
44 *(volatile int*)x
= y
;
48 void layer4(int x
) { layer5(x
, 1); }
50 void layer3(int a
, int b
, int c
) { layer4(a
+ b
+ c
); }
52 void layer2(int i
, int j
) { layer3(i
, j
, 7); }
54 void layer1(int s
, int t
) {
55 int* junk
= (int*)alloca(sizeof(int) * 1234);
57 layer2(junk
[0], t
+ 1);
60 void* NexeMain(void* data
) {
61 PostMessage("Running Boom thread.");
68 void PostMessage(const char* str
) {
71 if (NULL
== ppb_messaging_interface
)
76 fprintf(stdout
, "%s\n", str
);
79 if (ppb_var_interface
!= NULL
) {
80 struct PP_Var var
= ppb_var_interface
->VarFromUtf8(str
, strlen(str
));
81 ppb_messaging_interface
->PostMessage(g_Instance
, var
);
82 ppb_var_interface
->Release(var
);
86 void DumpJson(const char* json
) {
87 const char kTrcPrefix
[] = "TRC: ";
88 size_t size
= sizeof(kTrcPrefix
) + strlen(json
) + 1; // +1 for NULL.
89 char* out
= (char*)malloc(size
);
90 strcpy(out
, kTrcPrefix
);
97 static PP_Bool
Instance_DidCreate(PP_Instance instance
,
100 const char* argv
[]) {
101 g_Instance
= instance
;
102 g_PPAPIThread
= pthread_self();
104 PostMessage("LOG: DidCreate");
106 /* Request exception callbacks with JSON. */
107 EHRequestExceptionsJson(DumpJson
);
109 /* Report back if the request was honored. */
110 if (!EHHanderInstalled()) {
111 PostMessage("LOG: Stack traces not available, so don't expect them.\n");
113 PostMessage("LOG: Stack traces are on.");
115 pthread_create(&g_NexeThread
, NULL
, NexeMain
, NULL
);
119 static void Instance_DidDestroy(PP_Instance instance
) {}
121 static void Instance_DidChangeView(PP_Instance instance
,
122 PP_Resource view_resource
) {}
124 static void Instance_DidChangeFocus(PP_Instance instance
, PP_Bool has_focus
) {}
126 static PP_Bool
Instance_HandleDocumentLoad(PP_Instance instance
,
127 PP_Resource url_loader
) {
132 * Handles message from JavaScript.
134 * Any message from JS is a request to cause the main thread to crash.
136 static void Messaging_HandleMessage(PP_Instance instance
,
137 struct PP_Var message
) {
138 PostMessage("LOG: Got BOOM");
143 PPP_InitializeModule(PP_Module a_module_id
, PPB_GetInterface get_browser
) {
144 ppb_messaging_interface
=
145 (PPB_Messaging
*)(get_browser(PPB_MESSAGING_INTERFACE
));
146 ppb_var_interface
= (PPB_Var
*)(get_browser(PPB_VAR_INTERFACE
));
147 ppb_core_interface
= (PPB_Core
*)(get_browser(PPB_CORE_INTERFACE
));
151 PP_EXPORT
const void* PPP_GetInterface(const char* interface_name
) {
152 if (strcmp(interface_name
, PPP_INSTANCE_INTERFACE
) == 0) {
153 static PPP_Instance instance_interface
= {
155 &Instance_DidDestroy
,
156 &Instance_DidChangeView
,
157 &Instance_DidChangeFocus
,
158 &Instance_HandleDocumentLoad
,
160 return &instance_interface
;
162 if (strcmp(interface_name
, PPP_MESSAGING_INTERFACE
) == 0) {
163 static PPP_Messaging messaging_interface
= {
164 &Messaging_HandleMessage
,
166 return &messaging_interface
;
171 PP_EXPORT
void PPP_ShutdownModule() {}