OLD | NEW |
1 // Copyright (c) 2014 The Native Client Authors. All rights reserved. | 1 // Copyright (c) 2014 The Native Client Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // Emulates spawning/waiting process by asking JavaScript to do so. | 5 // Emulates spawning/waiting process by asking JavaScript to do so. |
6 | 6 |
7 // Include quoted spawn.h first so we can build in the presence of an installed | 7 // Include quoted spawn.h first so we can build in the presence of an installed |
8 // copy of nacl-spawn. | 8 // copy of nacl-spawn. |
9 #define IN_NACL_SPAWN_CC | 9 #define IN_NACL_SPAWN_CC |
10 #include "spawn.h" | 10 #include "spawn.h" |
(...skipping 222 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
233 nspawn_var_release(args_var); | 233 nspawn_var_release(args_var); |
234 } else { | 234 } else { |
235 // If the path does not contain a slash and we cannot find it | 235 // If the path does not contain a slash and we cannot find it |
236 // from PATH, we use NMF served with the JavaScript. | 236 // from PATH, we use NMF served with the JavaScript. |
237 return true; | 237 return true; |
238 } | 238 } |
239 } | 239 } |
240 return false; | 240 return false; |
241 } | 241 } |
242 | 242 |
243 // Check if a file is a pnacl type file. | 243 static bool CheckFileMagic(const std::string& filename, |
244 // If the file can't be read, return false. | 244 const std::string& magic) { |
245 static bool IsPNaClType(const std::string& filename) { | |
246 // Open script. | |
247 int fh = open(filename.c_str(), O_RDONLY); | 245 int fh = open(filename.c_str(), O_RDONLY); |
248 if (fh < 0) { | 246 if (fh < 0) { |
249 // Default to nacl type if the file can't be read. | |
250 return false; | 247 return false; |
251 } | 248 } |
252 // Read first 4 bytes. | 249 // Read first 4 bytes. |
253 char buffer[4]; | 250 char buffer[4]; |
254 ssize_t len = read(fh, buffer, sizeof buffer); | 251 ssize_t len = read(fh, buffer, sizeof buffer); |
255 close(fh); | 252 close(fh); |
256 // Decide based on the header. | 253 // Decide based on the header. |
257 return len == 4 && memcmp(buffer, "PEXE", sizeof buffer) == 0; | 254 return len == 4 && memcmp(buffer, magic.c_str(), sizeof buffer) == 0; |
| 255 } |
| 256 |
| 257 // Check if a file contains finalised PNaCl bitcode |
| 258 static bool IsPNaClType(const std::string& filename) { |
| 259 return CheckFileMagic(filename, "PEXE"); |
| 260 } |
| 261 |
| 262 // Check if a file contains LLVM bitcode |
| 263 static bool IsBitcode(const std::string& filename) { |
| 264 return CheckFileMagic(filename, "BC\xc0\xde"); |
258 } | 265 } |
259 | 266 |
260 // Adds a NMF to the request if |prog| is stored in HTML5 filesystem. | 267 // Adds a NMF to the request if |prog| is stored in HTML5 filesystem. |
261 static bool AddNmfToRequest(std::string prog, struct PP_Var req_var) { | 268 static bool AddNmfToRequest(std::string prog, struct PP_Var req_var) { |
262 if (UseBuiltInFallback(&prog, req_var)) { | 269 if (UseBuiltInFallback(&prog, req_var)) { |
263 return true; | 270 return true; |
264 } | 271 } |
265 if (access(prog.c_str(), R_OK) != 0) { | 272 if (access(prog.c_str(), R_OK) != 0) { |
266 errno = ENOENT; | 273 errno = ENOENT; |
267 return false; | 274 return false; |
268 } | 275 } |
269 | 276 |
270 if (!ExpandShBang(&prog, req_var)) { | 277 if (!ExpandShBang(&prog, req_var)) { |
271 return false; | 278 return false; |
272 } | 279 } |
273 | 280 |
274 // Check fallback again in case of #! expanded to something else. | 281 // Check fallback again in case of #! expanded to something else. |
275 if (UseBuiltInFallback(&prog, req_var)) { | 282 if (UseBuiltInFallback(&prog, req_var)) { |
276 return true; | 283 return true; |
277 } | 284 } |
278 | 285 |
| 286 bool debug = getenv("LD_DEBUG") != NULL; |
| 287 |
279 // Check for pnacl. | 288 // Check for pnacl. |
280 if (IsPNaClType(prog)) { | 289 if (IsPNaClType(prog)) { |
| 290 if (debug) { |
| 291 fprintf(stderr, "%s: loading PNaCl bitcode: %s\n", |
| 292 LOADER_NAME, prog.c_str()); |
| 293 } |
281 AddNmfToRequestForPNaCl(prog, req_var); | 294 AddNmfToRequestForPNaCl(prog, req_var); |
282 return true; | 295 return true; |
283 } | 296 } |
284 | 297 |
| 298 if (IsBitcode(prog)) { |
| 299 fprintf(stderr, "%s: cannot execute unfinalized bitcode\n", prog.c_str()); |
| 300 return false; |
| 301 } |
| 302 |
285 std::string arch; | 303 std::string arch; |
286 std::vector<std::string> dependencies; | 304 std::vector<std::string> dependencies; |
287 if (!nspawn_find_arch_and_library_deps(prog, &arch, &dependencies)) | 305 if (!nspawn_find_arch_and_library_deps(prog, &arch, &dependencies)) |
288 return false; | 306 return false; |
289 | 307 |
290 if (!dependencies.empty()) { | 308 if (!dependencies.empty()) { |
291 AddNmfToRequestForShared(prog, arch, dependencies, req_var); | 309 AddNmfToRequestForShared(prog, arch, dependencies, req_var); |
292 } else { | 310 } else { |
293 // No dependencies means the main binary is statically linked. | 311 // No dependencies means the main binary is statically linked. |
294 AddNmfToRequestForStatic(prog, arch, req_var); | 312 AddNmfToRequestForStatic(prog, arch, req_var); |
(...skipping 512 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
807 } | 825 } |
808 | 826 |
809 int execlpe(const char *path, const char *arg, ...) { /* char* const envp[] */ | 827 int execlpe(const char *path, const char *arg, ...) { /* char* const envp[] */ |
810 NSPAWN_LOG("execlpe: %s", path); | 828 NSPAWN_LOG("execlpe: %s", path); |
811 VARG_TO_ARGV_ENVP; | 829 VARG_TO_ARGV_ENVP; |
812 // TODO(bradnelson): Limit path resolution to 'p' variants. | 830 // TODO(bradnelson): Limit path resolution to 'p' variants. |
813 return spawnve_impl(P_OVERLAY, path, argv, envp); | 831 return spawnve_impl(P_OVERLAY, path, argv, envp); |
814 } | 832 } |
815 | 833 |
816 }; // extern "C" | 834 }; // extern "C" |
OLD | NEW |