Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 /* | |
| 2 * Copyright (c) 2011 The Native Client Authors. All rights reserved. | |
| 3 * Use of this source code is governed by a BSD-style license that can be | |
| 4 * found in the LICENSE file. | |
| 5 */ | |
| 6 | |
| 7 // | |
| 8 // Post-message based test for simple rpc based access to name services. | |
| 9 // | |
| 10 | |
| 11 #include <string> | |
| 12 | |
| 13 #include <assert.h> | |
| 14 #include <stdio.h> | |
| 15 #include <stdlib.h> | |
| 16 #include <inttypes.h> | |
| 17 #include <sys/fcntl.h> | |
| 18 #include <string.h> | |
| 19 #include <unistd.h> | |
| 20 #include <pthread.h> | |
| 21 | |
| 22 #include "native_client/src/include/elf32.h" | |
| 23 #include "native_client/src/include/elf_auxv.h" | |
| 24 #include "native_client/src/include/nacl_base.h" | |
| 25 | |
| 26 #include "native_client/src/shared/platform/nacl_sync.h" | |
| 27 #include "native_client/src/shared/platform/nacl_sync_checked.h" | |
| 28 #include "native_client/src/shared/platform/nacl_sync_raii.h" | |
| 29 | |
| 30 #include "native_client/src/untrusted/irt/irt.h" | |
| 31 #include "native_client/src/untrusted/irt/irt_ppapi.h" | |
| 32 #include "native_client/src/untrusted/nacl_ppapi_util/nacl_ppapi_util.h" | |
| 33 #include "native_client/src/untrusted/nacl_ppapi_util/string_buffer.h" | |
| 34 | |
| 35 #include "ppapi/c/pp_errors.h" | |
| 36 #include "ppapi/c/pp_module.h" | |
| 37 #include "ppapi/c/pp_var.h" | |
| 38 #include "ppapi/c/ppb.h" | |
| 39 #include "ppapi/c/ppb_instance.h" | |
| 40 #include "ppapi/c/ppb_messaging.h" | |
| 41 #include "ppapi/c/ppb_var.h" | |
| 42 #include "ppapi/c/ppp.h" | |
| 43 #include "ppapi/c/ppp_instance.h" | |
| 44 #include "ppapi/c/ppp_messaging.h" | |
| 45 //#include "ppapi/cpp/instance.h" | |
| 46 //#include "ppapi/cpp/module.h" | |
| 47 //#include "ppapi/cpp/var.h" | |
| 48 | |
| 49 #include <nacl/nacl_srpc.h> | |
| 50 #include <sys/nacl_name_service.h> | |
| 51 #include <sys/nacl_syscalls.h> | |
| 52 | |
| 53 /* | |
| 54 * TODO(mcgrathr): This extremely stupid function should not exist. | |
| 55 * If the startup calling sequence were sane, this would be done | |
| 56 * someplace that has the initial pointer locally rather than stealing | |
| 57 * it from environ. | |
| 58 * See http://code.google.com/p/nativeclient/issues/detail?id=651 | |
| 59 */ | |
| 60 static Elf32_auxv_t *find_auxv(void) { | |
| 61 /* | |
| 62 * This presumes environ has its startup-time value on the stack. | |
| 63 */ | |
| 64 char **ep = environ; | |
| 65 while (*ep != NULL) | |
| 66 ++ep; | |
| 67 return (Elf32_auxv_t *) (ep + 1); | |
| 68 } | |
| 69 | |
| 70 /* | |
| 71 * Scan the auxv for AT_SYSINFO, which is the pointer to the IRT query function. | |
| 72 * TODO(mcgrathr): Could get this from __nacl_irt_query, where the libnacl | |
| 73 * startup code stored it, but that would have to be also added as part of | |
| 74 * the glibc ABI. | |
| 75 */ | |
| 76 static TYPE_nacl_irt_query grok_auxv(const Elf32_auxv_t *auxv) { | |
| 77 const Elf32_auxv_t *av; | |
| 78 for (av = auxv; av->a_type != AT_NULL; ++av) { | |
| 79 if (av->a_type == AT_SYSINFO) | |
| 80 return (TYPE_nacl_irt_query) av->a_un.a_val; | |
| 81 } | |
| 82 return NULL; | |
| 83 } | |
| 84 | |
| 85 int module_id; | |
| 86 struct PPB_Messaging *ppb_message_interface; | |
| 87 struct PPB_Var *ppb_var_interface; | |
| 88 const char *kHello = "hello"; | |
| 89 std::string str; | |
| 90 const char *kWorld = "world"; | |
| 91 | |
| 92 static char *VarToCStr(struct PP_Var var) { | |
| 93 uint32_t len = 0; | |
| 94 if (ppb_var_interface != NULL) { | |
| 95 const char *var_c_str = ppb_var_interface->VarToUtf8(var, &len); | |
| 96 if (len > 0) { | |
| 97 char *c_str = (char*)malloc(len + 1); | |
| 98 memcpy(c_str, var_c_str, len); | |
| 99 c_str[len] = (char)0; | |
| 100 return c_str; | |
| 101 } | |
| 102 } | |
| 103 return NULL; | |
| 104 } | |
| 105 | |
| 106 static PP_Bool Instance_DidCreate(PP_Instance instance, | |
| 107 uint32_t argc, | |
| 108 const char* argn[], | |
| 109 const char* argv[]) { | |
| 110 return PP_TRUE; | |
| 111 } | |
| 112 | |
| 113 static void Instance_DidDestroy(PP_Instance instance) { | |
| 114 } | |
| 115 | |
| 116 static void Instance_DidChangeView(PP_Instance instance, | |
| 117 const struct PP_Rect* position, | |
| 118 const struct PP_Rect* clip) { | |
| 119 } | |
| 120 | |
| 121 static void Instance_DidChangeFocus(PP_Instance instance, | |
| 122 PP_Bool has_focus) { | |
| 123 } | |
| 124 | |
| 125 static PP_Bool Instance_HandleDocumentLoad(PP_Instance instance, | |
| 126 PP_Resource url_loader) { | |
| 127 /* NaCl modules do not need to handle the document load function. */ | |
| 128 return PP_FALSE; | |
| 129 } | |
| 130 | |
| 131 void Messaging_HandleMessage(PP_Instance instance, struct PP_Var var_message) { | |
| 132 if (var_message.type != PP_VARTYPE_STRING) { | |
| 133 /* Only handle string messages */ | |
| 134 return; | |
| 135 } | |
| 136 char *message = VarToCStr(var_message); | |
| 137 if (message == NULL) | |
| 138 return; | |
| 139 struct PP_Var var_result = PP_MakeUndefined(); | |
| 140 if (strncmp(message, kHello, strlen(kHello)) == 0) { | |
|
bsy
2011/08/11 02:32:43
why use strncmp instead of strcmp? did you intend
halyavin
2011/08/12 08:52:07
I rewrote test to use C++ API.
| |
| 141 var_result = ppb_var_interface->VarFromUtf8(module_id, str.c_str(), | |
| 142 strlen(str.c_str())); | |
| 143 } | |
| 144 free(message); | |
| 145 /* Echo the return result back to browser. Note that HandleMessage is always | |
| 146 * called on the main thread, so it's OK to post the message back to the | |
| 147 * browser directly from here. This return post is asynchronous. | |
| 148 */ | |
| 149 ppb_message_interface->PostMessage(instance, var_result); | |
| 150 /* If the message was created using VarFromUtf8() it needs to be released. | |
| 151 * See the comments about VarFromUtf8() in ppapi/c/ppb_var.h for more | |
| 152 * information. | |
| 153 */ | |
| 154 if (var_result.type == PP_VARTYPE_STRING) { | |
| 155 ppb_var_interface->Release(var_result); | |
| 156 } | |
| 157 } | |
| 158 | |
| 159 int32_t PPP_InitializeModule( | |
| 160 PP_Module param_module_id, | |
| 161 PPB_GetInterface browser_interface) { | |
| 162 module_id = param_module_id; | |
| 163 ppb_message_interface = | |
| 164 (struct PPB_Messaging*)browser_interface(PPB_MESSAGING_INTERFACE); | |
| 165 ppb_var_interface = (struct PPB_Var*)(browser_interface(PPB_VAR_INTERFACE)); | |
| 166 return PP_OK; | |
| 167 } | |
| 168 | |
| 169 void PPP_ShutdownModule() { | |
| 170 } | |
| 171 | |
| 172 const void *PPP_GetInterface(const char *interface_name) { | |
| 173 if (strcmp(interface_name, PPP_INSTANCE_INTERFACE) == 0) { | |
| 174 static struct PPP_Instance instance_interface = { | |
| 175 &Instance_DidCreate, | |
| 176 &Instance_DidDestroy, | |
| 177 &Instance_DidChangeView, | |
| 178 &Instance_DidChangeFocus, | |
| 179 &Instance_HandleDocumentLoad, | |
| 180 }; | |
| 181 return &instance_interface; | |
| 182 } else if (strcmp(interface_name, PPP_MESSAGING_INTERFACE) == 0) { | |
| 183 static struct PPP_Messaging messaging_interface = { | |
| 184 &Messaging_HandleMessage, | |
| 185 }; | |
| 186 return &messaging_interface; | |
| 187 } | |
| 188 return NULL; | |
| 189 } | |
| 190 | |
| 191 const struct PP_StartFunctions funcs = { | |
| 192 &PPP_InitializeModule, | |
| 193 &PPP_ShutdownModule, | |
| 194 &PPP_GetInterface, | |
| 195 }; | |
| 196 | |
| 197 void load_thread(void *arg) { | |
| 198 TYPE_nacl_irt_query *query_func = (TYPE_nacl_irt_query*)arg; | |
| 199 struct nacl_irt_manifest_open nacl_irt_manifest_open; | |
| 200 printf("inside\n"); | |
| 201 if (sizeof(nacl_irt_manifest_open) != | |
| 202 (*query_func)( | |
| 203 NACL_IRT_MANIFEST_OPEN_v0_0, | |
| 204 &nacl_irt_manifest_open, | |
| 205 sizeof(nacl_irt_manifest_open))) { | |
| 206 printf("irt manifest api not found\n"); | |
| 207 return; | |
| 208 } | |
| 209 int desc; | |
| 210 desc = nacl_irt_manifest_open.open_file_in_manifest("files/test_file"); | |
| 211 if (-1 == desc) { | |
| 212 str = "Invalid file descriptor\n"; | |
| 213 printf("%s", str.c_str()); | |
| 214 return; | |
| 215 } | |
| 216 | |
| 217 str = "File Contents:\n"; | |
| 218 | |
| 219 FILE *iob = fdopen(desc, "r"); | |
| 220 char buffer[4096]; | |
| 221 while (fgets(buffer, sizeof buffer, iob) != NULL) { | |
| 222 // NB: fgets does not discard the newline nor any carriage return | |
| 223 // character before that. | |
| 224 // | |
| 225 // Note that CR LF is the default end-of-line style for Windows. | |
| 226 // Furthermore, when the test_file (input data, which happens to | |
| 227 // be the nmf file) is initially created in a change list, the | |
| 228 // patch is sent to our try bots as text. This means that when | |
| 229 // the file arrives, it has CR LF endings instead of the original | |
| 230 // LF line endings. Since the expected or golden data is | |
| 231 // (manually) encoded in the HTML file's JavaScript, there will be | |
| 232 // a mismatch. After submission, the svn property svn:eol-style | |
| 233 // will be set to LF, so a clean check out should have LF and not | |
| 234 // CR LF endings, and the tests will pass without CR removal. | |
| 235 // However -- and there's always a however in long discourses -- | |
| 236 // if the nmf file is edited, say, because the test is being | |
| 237 // modified, and the modification is being done on a Windows | |
| 238 // machine, then it is likely that the editor used by the | |
| 239 // programmer will convert the file to CR LF endings. Which, | |
| 240 // unfortunatly, implies that the test will mysteriously fail | |
| 241 // again. | |
| 242 // | |
| 243 // To defend against such nonsense, we weaken the test slighty, | |
| 244 // and just strip the CR if it is present. | |
| 245 int len = strlen(buffer); | |
| 246 if (len >= 2 && buffer[len-1] == '\n' && buffer[len-2] == '\r') { | |
| 247 buffer[len-2] = '\n'; | |
| 248 buffer[len-1] = '\0'; | |
| 249 } | |
| 250 str += buffer; | |
| 251 } | |
| 252 printf("here %d\n", strlen(str.c_str())); | |
| 253 printf("%s\n", str.c_str()); | |
| 254 fclose(iob); // closed desc | |
| 255 struct nacl_irt_ppapihook hooks; | |
| 256 if (sizeof(hooks) == (*query_func)( | |
| 257 NACL_IRT_PPAPIHOOK_v0_1, &hooks, sizeof(hooks))) { | |
| 258 hooks.ppapi_start(&funcs); | |
| 259 } else { | |
| 260 printf("PpapiPluginStart: PPAPI hooks not found\n"); | |
| 261 } | |
| 262 return; | |
| 263 } | |
| 264 | |
| 265 int main() { | |
| 266 TYPE_nacl_irt_query query_func = grok_auxv(find_auxv()); | |
| 267 printf("main\n"); | |
| 268 | |
| 269 if (NULL == query_func) | |
| 270 printf("PpapiPluginStart: No AT_SYSINFO item found in auxv, " | |
| 271 "so cannot start PPAPI. Is the IRT library not present?\n"); | |
| 272 | |
| 273 struct nacl_irt_ppapihook_0_2 hooks; | |
| 274 int result; | |
| 275 result = query_func(NACL_IRT_PPAPIHOOK_v0_2, &hooks, sizeof(hooks)); | |
| 276 if (sizeof(hooks) != result) | |
| 277 printf("PpapiPluginStart: PPAPI advanced hooks not found %d %d\n", | |
| 278 sizeof(hooks), result); | |
| 279 hooks.ppapi_pre_start(); | |
| 280 load_thread(&query_func); | |
| 281 printf("shouldn't reach here\n"); | |
| 282 return 0; | |
| 283 //return hooks.ppapi_ld_start(&load_thread, &query_func); | |
| 284 } | |
| OLD | NEW |