1 // Copyright 2012 The Chromium Authors 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef COMPONENTS_NACL_RENDERER_PLUGIN_PNACL_TRANSLATE_THREAD_H_ 6 #define COMPONENTS_NACL_RENDERER_PLUGIN_PNACL_TRANSLATE_THREAD_H_ 7 8 #include <stdint.h> 9 10 #include <memory> 11 #include <vector> 12 13 #include "base/containers/circular_deque.h" 14 #include "base/files/file.h" 15 #include "base/memory/raw_ptr.h" 16 #include "base/synchronization/condition_variable.h" 17 #include "base/synchronization/lock.h" 18 #include "base/threading/simple_thread.h" 19 #include "components/nacl/renderer/plugin/plugin_error.h" 20 #include "ppapi/cpp/completion_callback.h" 21 #include "ppapi/proxy/serialized_handle.h" 22 23 struct PP_PNaClOptions; 24 25 namespace plugin { 26 27 class NaClSubprocess; 28 class PnaclCoordinator; 29 30 class PnaclTranslateThread { 31 public: 32 PnaclTranslateThread(); 33 34 PnaclTranslateThread(const PnaclTranslateThread&) = delete; 35 PnaclTranslateThread& operator=(const PnaclTranslateThread&) = delete; 36 37 ~PnaclTranslateThread(); 38 39 // Set up the state for RunCompile and RunLink. When an error is 40 // encountered, or RunLink is complete the finish_callback is run 41 // to notify the main thread. 42 void SetupState(const pp::CompletionCallback& finish_callback, 43 NaClSubprocess* compiler_subprocess, 44 NaClSubprocess* ld_subprocess, 45 std::vector<base::File>* obj_files, 46 int num_threads, 47 base::File* nexe_file, 48 ErrorInfo* error_info, 49 PP_PNaClOptions* pnacl_options, 50 const std::string& architecture_attributes, 51 PnaclCoordinator* coordinator); 52 53 // Create a compile thread and run/command the compiler_subprocess. 54 // It will continue to run and consume data as it is passed in with PutBytes. 55 // On success, runs compile_finished_callback. 56 // On error, runs finish_callback. 57 // The compiler_subprocess must already be loaded. 58 void RunCompile(const pp::CompletionCallback& compile_finished_callback); 59 60 // Create a link thread and run/command the ld_subprocess. 61 // On completion (success or error), runs finish_callback. 62 // The ld_subprocess must already be loaded. 63 void RunLink(); 64 65 // Kill the llc and/or ld subprocesses. This happens by closing the command 66 // channel on the plugin side, which causes the trusted code in the nexe to 67 // exit, which will cause any pending SRPCs to error. Because this is called 68 // on the main thread, the translation thread must not use the subprocess 69 // objects without the lock, other than InvokeSrpcMethod, which does not 70 // race with service runtime shutdown. 71 void AbortSubprocesses(); 72 73 // Send bitcode bytes to the translator. Called from the main thread. 74 void PutBytes(const void* data, int count); 75 76 // Notify the translator that the end of the bitcode stream has been reached. 77 // Called from the main thread. 78 void EndStream(); 79 GetCompileTime()80 int64_t GetCompileTime() const { return compile_time_; } 81 82 // Returns true if the translation process is initiated via SetupState. started()83 bool started() const { return !!coordinator_; } 84 85 private: 86 ppapi::proxy::SerializedHandle GetHandleForSubprocess(base::File* file, 87 int32_t open_flags); 88 89 // Runs the streaming compilation. Called from the helper thread. 90 void DoCompile(); 91 // Similar to DoCompile(), but for linking. 92 void DoLink(); 93 94 class CompileThread : public base::SimpleThread { 95 public: CompileThread(PnaclTranslateThread * obj)96 CompileThread(PnaclTranslateThread* obj) 97 : base::SimpleThread("pnacl_compile"), pnacl_translate_thread_(obj) {} 98 99 CompileThread(const CompileThread&) = delete; 100 CompileThread& operator=(const CompileThread&) = delete; 101 102 private: 103 raw_ptr<PnaclTranslateThread> pnacl_translate_thread_; 104 void Run() override; 105 }; 106 107 class LinkThread : public base::SimpleThread { 108 public: LinkThread(PnaclTranslateThread * obj)109 LinkThread(PnaclTranslateThread* obj) 110 : base::SimpleThread("pnacl_link"), pnacl_translate_thread_(obj) {} 111 112 LinkThread(const LinkThread&) = delete; 113 LinkThread& operator=(const LinkThread&) = delete; 114 115 private: 116 raw_ptr<PnaclTranslateThread> pnacl_translate_thread_; 117 void Run() override; 118 }; 119 120 // Signal that Pnacl translation failed, from the translation thread only. 121 void TranslateFailed(PP_NaClError err_code, 122 const std::string& error_string); 123 124 // Callback to run when compile is completed and linking can start. 125 pp::CompletionCallback compile_finished_callback_; 126 127 // Callback to run when tasks are completed or an error has occurred. 128 pp::CompletionCallback report_translate_finished_; 129 130 std::unique_ptr<base::SimpleThread> translate_thread_; 131 132 // Used to guard compiler_subprocess, ld_subprocess, 133 // compiler_subprocess_active_, and ld_subprocess_active_ 134 // (touched by the main thread and the translate thread). 135 base::Lock subprocess_mu_; 136 // The compiler_subprocess and ld_subprocess memory is owned by the 137 // coordinator so we do not delete them. However, the main thread delegates 138 // shutdown to this thread, since this thread may still be accessing the 139 // subprocesses. The *_subprocess_active flags indicate which subprocesses 140 // are active to ensure the subprocesses don't get shutdown more than once. 141 // The subprocess_mu_ must be held when shutting down the subprocesses 142 // or otherwise accessing the service_runtime component of the subprocess. 143 // There are some accesses to the subprocesses without locks held 144 // (invoking srpc_client methods -- in contrast to using the service_runtime). 145 raw_ptr<NaClSubprocess> compiler_subprocess_; 146 raw_ptr<NaClSubprocess> ld_subprocess_; 147 bool compiler_subprocess_active_; 148 bool ld_subprocess_active_; 149 150 // Mutex for buffer_cond_. 151 base::Lock cond_mu_; 152 // Condition variable to synchronize communication with the SRPC thread. 153 // SRPC thread waits on this condvar if data_buffers_ is empty (meaning 154 // there is no bitcode to send to the translator), and the main thread 155 // appends to data_buffers_ and signals it when it receives bitcode. 156 base::ConditionVariable buffer_cond_; 157 // Data buffers from FileDownloader are enqueued here to pass from the 158 // main thread to the SRPC thread. Protected by cond_mu_ 159 base::circular_deque<std::string> data_buffers_; 160 // Whether all data has been downloaded and copied to translation thread. 161 // Associated with buffer_cond_ 162 bool done_; 163 164 int64_t compile_time_; 165 166 // Data about the translation files, owned by the coordinator 167 raw_ptr<std::vector<base::File>> obj_files_; 168 int num_threads_; 169 raw_ptr<base::File> nexe_file_; 170 raw_ptr<ErrorInfo> coordinator_error_info_; 171 raw_ptr<PP_PNaClOptions> pnacl_options_; 172 std::string architecture_attributes_; 173 raw_ptr<PnaclCoordinator> coordinator_; 174 175 // These IPC::SyncChannels can only be used and freed by the parent thread. 176 std::unique_ptr<IPC::SyncChannel> compiler_channel_; 177 std::unique_ptr<IPC::SyncChannel> ld_channel_; 178 // These IPC::SyncMessageFilters can be used by the child thread. 179 scoped_refptr<IPC::SyncMessageFilter> compiler_channel_filter_; 180 scoped_refptr<IPC::SyncMessageFilter> ld_channel_filter_; 181 }; 182 183 } 184 #endif // COMPONENTS_NACL_RENDERER_PLUGIN_PNACL_TRANSLATE_THREAD_H_ 185