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