OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2015 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. | |
4 | |
5 #include <fcntl.h> | |
6 | |
7 #include "base/files/file_util.h" | |
8 #include "base/logging.h" | |
9 #include "mojo/nacl/nonsfi/irt_mojo_nonsfi.h" | |
10 #include "mojo/public/cpp/bindings/string.h" | |
11 #include "native_client/src/untrusted/irt/irt_dev.h" | |
12 | |
13 void serve_translate_request(const struct nacl_irt_pnacl_compile_funcs* funcs) { | |
14 // Acquire the handle -- this is our mechanism to contact the | |
15 // content handler which called us. | |
16 MojoHandle handle; | |
17 nacl::_MojoGetInitialHandle(&handle); | |
18 | |
19 // Read / Wait until we can get the pexe file name. | |
20 MojoResult result; | |
21 char pexe_file_name[PATH_MAX]; | |
22 uint32_t path_size = sizeof(pexe_file_name); | |
23 do { | |
24 result = MojoReadMessage(handle, | |
Mark Seaborn
2015/10/20 21:32:40
I don't think you should be using raw Mojo IPC. C
Sean Klein
2015/10/22 21:50:00
I think this is somewhere that vtl should get invo
| |
25 pexe_file_name, | |
26 &path_size, | |
27 nullptr, | |
28 nullptr, | |
29 MOJO_READ_MESSAGE_FLAG_NONE); | |
30 if (result == MOJO_RESULT_SHOULD_WAIT) { | |
31 result = MojoWait(handle, | |
32 MOJO_HANDLE_SIGNAL_READABLE, | |
33 MOJO_DEADLINE_INDEFINITE, | |
34 nullptr); | |
35 if (result != MOJO_RESULT_OK) | |
36 LOG(FATAL) << "Pexe translator could not wait for pexe name"; | |
37 } else { | |
38 break; | |
39 } | |
40 } while (true); | |
41 if (result != MOJO_RESULT_OK) | |
42 LOG(FATAL) << "Pexe translator could not read pexe name"; | |
43 pexe_file_name[path_size] = '\0'; | |
Mark Seaborn
2015/10/20 21:32:40
Couldn't path_size be sizeof(pexe_file_name)? In
Sean Klein
2015/10/22 21:50:00
Using "PATH_MAX + 1" to allow room for null termin
| |
44 | |
45 // Now that we have the pexe file name, make an object file. This is necessary | |
46 // for the callback into LLVM through 'funcs'. Open the object file, and | |
47 // prepare to pass the nexe through the compiler callback. | |
48 base::FilePath obj_file_name; | |
49 if (!CreateTemporaryFile(&obj_file_name)) | |
50 LOG(FATAL) << "Could not make temporary object file"; | |
51 int obj_file_fds[] = { open(obj_file_name.value().c_str(), O_RDWR, O_TRUNC) }; | |
Mark Seaborn
2015/10/20 21:32:40
This looks a bit quirky. :-)
You could just do:
i
Sean Klein
2015/10/22 21:50:00
The "quirky" version was there so the type of obj_
| |
52 | |
53 if (obj_file_fds[0] < 0) | |
54 LOG(FATAL) << "Could not create temp file for compiled pexe"; | |
55 | |
56 // TODO(smklein): Is there a less arbitrary number to choose? | |
Mark Seaborn
2015/10/20 21:32:40
What is Chromium using for the thread count?
This
Sean Klein
2015/10/22 21:50:00
It's passed in as an argument, so I'm not entirely
| |
57 uint32_t num_threads = 8; | |
58 size_t obj_file_fd_count = 1; | |
59 char relocation_model[] = "-relocation-model=pic"; | |
60 char force_tls[] = "-force-tls-non-pic"; | |
61 char bitcode_format[] = "-bitcode-format=pnacl"; | |
62 char *args[] = { relocation_model, force_tls, bitcode_format }; | |
63 size_t argc = 3; | |
64 funcs->init_callback(num_threads, obj_file_fds, obj_file_fd_count, | |
65 args, argc); | |
66 | |
67 // Read the pexe using fread, and write the pexe into the callback function. | |
68 char buf[0x100000]; | |
Mark Seaborn
2015/10/20 21:32:40
That's a lot to stack-allocate. Can you heap-allo
Sean Klein
2015/10/22 21:50:00
Done with new / delete[].
| |
69 FILE* pexe_file_stream = fopen(pexe_file_name, "r"); | |
70 // Once the pexe has been opened, it is no longer needed, so we unlink it. | |
71 if (unlink(pexe_file_name)) | |
72 LOG(FATAL) << "Could not unlink temporary pexe file"; | |
73 if (pexe_file_stream == nullptr) | |
74 LOG(FATAL) << "Could not open pexe for reading"; | |
75 for(;;) { | |
76 size_t num_bytes_from_pexe = fread(buf, 1, sizeof(buf), pexe_file_stream); | |
77 if (ferror(pexe_file_stream)) { | |
78 LOG(FATAL) << "Error reading from pexe file stream"; | |
79 } | |
80 if (num_bytes_from_pexe == 0) { | |
81 break; | |
82 } | |
83 funcs->data_callback(buf, num_bytes_from_pexe); | |
84 } | |
85 | |
86 if (fclose(pexe_file_stream)) | |
87 LOG(FATAL) << "Failed to close pexe file stream from compiler nexe"; | |
88 funcs->end_callback(); | |
89 | |
90 // Write the name of the object file back to the content handler | |
91 // to signify that we are done compiling (and to provide information | |
92 // for the linking stage). | |
93 result = MojoWriteMessage(handle, | |
94 obj_file_name.value().c_str(), | |
95 obj_file_name.value().size(), | |
96 nullptr, | |
97 0, | |
98 MOJO_WRITE_MESSAGE_FLAG_NONE); | |
99 if (result != MOJO_RESULT_OK) | |
100 LOG(FATAL) << "Could not write message to content handler: " << result; | |
101 } | |
102 | |
103 const struct nacl_irt_private_pnacl_translator_compile | |
104 nacl_irt_private_pnacl_translator_compile = { | |
105 serve_translate_request | |
106 }; | |
OLD | NEW |