Chromium Code Reviews| 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 "mojo/public/cpp/bindings/strong_binding.h" | |
| 12 #include "mojo/public/cpp/utility/run_loop.h" | |
| 13 #include "native_client/src/untrusted/irt/irt_dev.h" | |
| 14 #include "services/nacl/pnacl_compile.mojom.h" | |
| 15 | |
| 16 namespace { | |
| 17 | |
| 18 // Implements a mojom interface which allows the content handler to communicate | |
| 19 // with the nexe compiler service. | |
| 20 class PexeCompilerImpl : public mojo::nacl::PexeCompiler { | |
| 21 public: | |
| 22 PexeCompilerImpl(mojo::ScopedMessagePipeHandle handle, | |
| 23 const struct nacl_irt_pnacl_compile_funcs* funcs) | |
| 24 : funcs_(funcs), strong_binding_(this, handle.Pass()) {} | |
| 25 void PexeCompile(const mojo::String& pexe_file_name, | |
| 26 const mojo::Callback<void(mojo::String)>& callback) | |
| 27 override { | |
| 28 base::FilePath obj_file_name; | |
| 29 if (!CreateTemporaryFile(&obj_file_name)) | |
| 30 LOG(FATAL) << "Could not make temporary object file"; | |
| 31 // TODO(smklein): Use multiple object files to increase parallelism. | |
| 32 int obj_file_fd = open(obj_file_name.value().c_str(), O_RDWR, O_TRUNC); | |
| 33 | |
| 34 if (obj_file_fd < 0) | |
| 35 LOG(FATAL) << "Could not create temp file for compiled pexe"; | |
| 36 | |
| 37 // TODO(smklein): Is there a less arbitrary number to choose? | |
| 38 uint32_t num_threads = 8; | |
| 39 size_t obj_file_fd_count = 1; | |
| 40 char relocation_model[] = "-relocation-model=pic"; | |
| 41 char force_tls[] = "-force-tls-non-pic"; | |
| 42 char bitcode_format[] = "-bitcode-format=pnacl"; | |
| 43 char *args[] = { relocation_model, force_tls, bitcode_format }; | |
|
Mark Seaborn
2015/10/27 17:30:20
irt_dev.h says this array should be NULL-terminate
Sean Klein
2015/10/28 17:02:41
Added sentinel
| |
| 44 size_t argc = 3; | |
| 45 funcs_->init_callback(num_threads, &obj_file_fd, obj_file_fd_count, | |
| 46 args, argc); | |
| 47 | |
| 48 // Read the pexe using fread, and write the pexe into the callback function. | |
| 49 size_t buf_size = 0x100000; | |
| 50 char* buf = new char[buf_size]; | |
|
Mark Seaborn
2015/10/27 17:30:20
I believe you can use "scoped_ptr<char[]> buf(new
Sean Klein
2015/10/28 17:02:41
Done -- also, usage of "buf" changed to "&buf[0]",
| |
| 51 FILE* pexe_file_stream = fopen(pexe_file_name.get().c_str(), "r"); | |
| 52 // Once the pexe has been opened, it is no longer needed, so we unlink it. | |
| 53 if (unlink(pexe_file_name.get().c_str())) | |
| 54 LOG(FATAL) << "Could not unlink temporary pexe file"; | |
| 55 if (pexe_file_stream == nullptr) | |
| 56 LOG(FATAL) << "Could not open pexe for reading"; | |
| 57 for(;;) { | |
|
Mark Seaborn
2015/10/27 17:30:20
Add space after "for"
Sean Klein
2015/10/28 17:02:41
Done.
| |
| 58 size_t num_bytes_from_pexe = fread(buf, 1, buf_size, pexe_file_stream); | |
| 59 if (ferror(pexe_file_stream)) { | |
| 60 LOG(FATAL) << "Error reading from pexe file stream"; | |
| 61 } | |
| 62 if (num_bytes_from_pexe == 0) { | |
| 63 break; | |
| 64 } | |
| 65 funcs_->data_callback(buf, num_bytes_from_pexe); | |
| 66 } | |
| 67 delete[] buf; | |
| 68 | |
| 69 if (fclose(pexe_file_stream)) | |
| 70 LOG(FATAL) << "Failed to close pexe file stream from compiler nexe"; | |
| 71 funcs_->end_callback(); | |
| 72 | |
| 73 // Return the name of the object file. | |
| 74 callback.Run(mojo::String(obj_file_name.value())); | |
| 75 } | |
| 76 private: | |
| 77 const struct nacl_irt_pnacl_compile_funcs* funcs_; | |
| 78 mojo::StrongBinding<mojo::nacl::PexeCompiler> strong_binding_; | |
| 79 }; | |
| 80 | |
| 81 void ServeTranslateRequest(const struct nacl_irt_pnacl_compile_funcs* funcs) { | |
| 82 // Acquire the handle -- this is our mechanism to contact the | |
| 83 // content handler which called us. | |
| 84 MojoHandle handle; | |
| 85 nacl::MojoGetInitialHandle(&handle); | |
| 86 | |
| 87 // Convert the MojoHandle into a ScopedMessagePipeHandle, and use that to | |
| 88 // implement the PexeCompiler interface. | |
| 89 PexeCompilerImpl impl( | |
| 90 mojo::ScopedMessagePipeHandle(mojo::MessagePipeHandle(handle)).Pass(), | |
| 91 funcs); | |
| 92 mojo::RunLoop::current()->RunUntilIdle(); | |
| 93 } | |
| 94 | |
| 95 } // namespace anonymous | |
| 96 | |
| 97 namespace nacl { | |
| 98 | |
| 99 const struct nacl_irt_private_pnacl_translator_compile | |
| 100 nacl_irt_private_pnacl_translator_compile = { | |
| 101 ServeTranslateRequest | |
| 102 }; | |
| 103 | |
| 104 } // namespace nacl | |
| OLD | NEW |