1 // Copyright (c) 2012 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.
5 #include "ppapi/native_client/src/trusted/plugin/pnacl_translate_thread.h"
9 #include "native_client/src/trusted/desc/nacl_desc_wrapper.h"
10 #include "ppapi/cpp/var.h"
11 #include "ppapi/native_client/src/trusted/plugin/plugin.h"
12 #include "ppapi/native_client/src/trusted/plugin/plugin_error.h"
13 #include "ppapi/native_client/src/trusted/plugin/pnacl_resources.h"
14 #include "ppapi/native_client/src/trusted/plugin/srpc_params.h"
15 #include "ppapi/native_client/src/trusted/plugin/temporary_file.h"
16 #include "ppapi/native_client/src/trusted/plugin/utility.h"
21 template <typename Val>
22 nacl::string MakeCommandLineArg(const char* key, const Val val) {
23 nacl::stringstream ss;
28 void GetLlcCommandLine(Plugin* plugin,
29 std::vector<char>* split_args,
30 size_t obj_files_size,
33 const nacl::string &architecture_attributes) {
34 typedef std::vector<nacl::string> Args;
37 // TODO(dschuff): This CL override is ugly. Change llc to default to
38 // using the number of modules specified in the first param, and
39 // ignore multiple uses of -split-module
40 args.push_back(MakeCommandLineArg("-split-module=", obj_files_size));
41 args.push_back(MakeCommandLineArg("-O=", opt_level));
43 args.push_back("-bitcode-format=llvm");
44 if (!architecture_attributes.empty())
45 args.push_back("-mattr=" + architecture_attributes);
47 for (Args::const_iterator arg(args.begin()); arg != args.end(); ++arg) {
48 std::copy(arg->begin(), arg->end(), std::back_inserter(*split_args));
49 split_args->push_back('\x00');
55 PnaclTranslateThread::PnaclTranslateThread() : llc_subprocess_active_(false),
56 ld_subprocess_active_(false),
62 coordinator_error_info_(NULL),
66 NaClXMutexCtor(&subprocess_mu_);
67 NaClXMutexCtor(&cond_mu_);
68 NaClXCondVarCtor(&buffer_cond_);
71 void PnaclTranslateThread::RunTranslate(
72 const pp::CompletionCallback& finish_callback,
74 const std::vector<TempFile*>* obj_files,
76 nacl::DescWrapper* invalid_desc_wrapper,
77 ErrorInfo* error_info,
78 PnaclResources* resources,
79 PP_PNaClOptions* pnacl_options,
80 const nacl::string &architecture_attributes,
81 PnaclCoordinator* coordinator,
83 PLUGIN_PRINTF(("PnaclStreamingTranslateThread::RunTranslate)\n"));
84 manifest_id_ = manifest_id;
85 obj_files_ = obj_files;
86 nexe_file_ = nexe_file;
87 invalid_desc_wrapper_ = invalid_desc_wrapper;
88 coordinator_error_info_ = error_info;
89 resources_ = resources;
90 pnacl_options_ = pnacl_options;
91 architecture_attributes_ = architecture_attributes;
92 coordinator_ = coordinator;
95 // Invoke llc followed by ld off the main thread. This allows use of
96 // blocking RPCs that would otherwise block the JavaScript main thread.
97 report_translate_finished_ = finish_callback;
98 translate_thread_.reset(new NaClThread);
99 if (translate_thread_ == NULL) {
100 TranslateFailed(PP_NACL_ERROR_PNACL_THREAD_CREATE,
101 "could not allocate thread struct.");
104 const int32_t kArbitraryStackSize = 128 * 1024;
105 if (!NaClThreadCreateJoinable(translate_thread_.get(),
108 kArbitraryStackSize)) {
109 TranslateFailed(PP_NACL_ERROR_PNACL_THREAD_CREATE,
110 "could not create thread.");
111 translate_thread_.reset(NULL);
115 // Called from main thread to send bytes to the translator.
116 void PnaclTranslateThread::PutBytes(std::vector<char>* bytes,
118 PLUGIN_PRINTF(("PutBytes (this=%p, bytes=%p, size=%" NACL_PRIuS
120 this, bytes, bytes ? bytes->size() : 0, count));
121 size_t buffer_size = 0;
122 // If we are done (error or not), Signal the translation thread to stop.
123 if (count <= PP_OK) {
124 NaClXMutexLock(&cond_mu_);
126 NaClXCondVarSignal(&buffer_cond_);
127 NaClXMutexUnlock(&cond_mu_);
131 CHECK(bytes != NULL);
132 // Ensure that the buffer we send to the translation thread is the right size
133 // (count can be < the buffer size). This can be done without the lock.
134 buffer_size = bytes->size();
135 bytes->resize(count);
137 NaClXMutexLock(&cond_mu_);
139 data_buffers_.push_back(std::vector<char>());
140 bytes->swap(data_buffers_.back()); // Avoid copying the buffer data.
142 NaClXCondVarSignal(&buffer_cond_);
143 NaClXMutexUnlock(&cond_mu_);
145 // Ensure the buffer we send back to the coordinator is the expected size
146 bytes->resize(buffer_size);
149 NaClSubprocess* PnaclTranslateThread::StartSubprocess(
150 const nacl::string& url_for_nexe,
152 ErrorInfo* error_info) {
153 PLUGIN_PRINTF(("PnaclTranslateThread::StartSubprocess (url_for_nexe=%s)\n",
154 url_for_nexe.c_str()));
155 nacl::DescWrapper* wrapper = resources_->WrapperForUrl(url_for_nexe);
156 // Supply a URL for the translator components, different from the app URL,
157 // so that NaCl GDB can filter-out the translator processes (and not debug
158 // the translator itself). Must have a full URL with schema, otherwise the
159 // string gets silently dropped by GURL.
160 nacl::string full_url = resources_->GetFullUrl(
161 url_for_nexe, plugin_->nacl_interface()->GetSandboxArch());
162 nacl::scoped_ptr<NaClSubprocess> subprocess(plugin_->LoadHelperNaClModule(
163 full_url, wrapper, manifest_id, error_info));
164 if (subprocess.get() == NULL) {
166 "PnaclTranslateThread::StartSubprocess: subprocess creation failed\n"));
169 return subprocess.release();
172 void WINAPI PnaclTranslateThread::DoTranslateThread(void* arg) {
173 PnaclTranslateThread* translator =
174 reinterpret_cast<PnaclTranslateThread*>(arg);
175 translator->DoTranslate();
178 void PnaclTranslateThread::DoTranslate() {
179 ErrorInfo error_info;
181 std::vector<nacl::DescWrapper*> llc_out_files;
183 for (i = 0; i < obj_files_->size(); i++) {
184 llc_out_files.push_back((*obj_files_)[i]->write_wrapper());
186 for (; i < PnaclCoordinator::kMaxTranslatorObjectFiles; i++) {
187 llc_out_files.push_back(invalid_desc_wrapper_);
190 pp::Core* core = pp::Module::Get()->core();
192 nacl::MutexLocker ml(&subprocess_mu_);
193 int64_t llc_start_time = NaClGetTimeOfDayMicroseconds();
194 llc_subprocess_.reset(
195 StartSubprocess(resources_->GetLlcUrl(), manifest_id_, &error_info));
196 if (llc_subprocess_ == NULL) {
197 TranslateFailed(PP_NACL_ERROR_PNACL_LLC_SETUP,
198 "Compile process could not be created: " +
199 error_info.message());
202 llc_subprocess_active_ = true;
203 core->CallOnMainThread(0,
204 coordinator_->GetUMATimeCallback(
205 "NaCl.Perf.PNaClLoadTime.LoadCompiler",
206 NaClGetTimeOfDayMicroseconds() - llc_start_time),
209 PluginReverseInterface* llc_reverse =
210 llc_subprocess_->service_runtime()->rev_interface();
211 for (size_t i = 0; i < obj_files_->size(); i++) {
212 llc_reverse->AddTempQuotaManagedFile((*obj_files_)[i]->identifier());
216 int64_t compile_start_time = NaClGetTimeOfDayMicroseconds();
219 std::vector<char> split_args;
220 GetLlcCommandLine(plugin_,
223 pnacl_options_->opt_level,
224 pnacl_options_->is_debug,
225 architecture_attributes_);
226 init_success = llc_subprocess_->InvokeSrpcMethod(
227 "StreamInitWithSplit",
228 "ihhhhhhhhhhhhhhhhC",
230 static_cast<int>(obj_files_->size()),
231 llc_out_files[0]->desc(),
232 llc_out_files[1]->desc(),
233 llc_out_files[2]->desc(),
234 llc_out_files[3]->desc(),
235 llc_out_files[4]->desc(),
236 llc_out_files[5]->desc(),
237 llc_out_files[6]->desc(),
238 llc_out_files[7]->desc(),
239 llc_out_files[8]->desc(),
240 llc_out_files[9]->desc(),
241 llc_out_files[10]->desc(),
242 llc_out_files[11]->desc(),
243 llc_out_files[12]->desc(),
244 llc_out_files[13]->desc(),
245 llc_out_files[14]->desc(),
246 llc_out_files[15]->desc(),
250 if (llc_subprocess_->srpc_client()->GetLastError() ==
251 NACL_SRPC_RESULT_APP_ERROR) {
252 // The error message is only present if the error was returned from llc
253 TranslateFailed(PP_NACL_ERROR_PNACL_LLC_INTERNAL,
254 nacl::string("Stream init failed: ") +
255 nacl::string(params.outs()[0]->arrays.str));
257 TranslateFailed(PP_NACL_ERROR_PNACL_LLC_INTERNAL,
258 "Stream init internal error");
262 PLUGIN_PRINTF(("PnaclCoordinator: StreamInit successful\n"));
264 // llc process is started.
265 while(!done_ || data_buffers_.size() > 0) {
266 NaClXMutexLock(&cond_mu_);
267 while(!done_ && data_buffers_.size() == 0) {
268 NaClXCondVarWait(&buffer_cond_, &cond_mu_);
270 PLUGIN_PRINTF(("PnaclTranslateThread awake (done=%d, size=%" NACL_PRIuS
272 done_, data_buffers_.size()));
273 if (data_buffers_.size() > 0) {
274 std::vector<char> data;
275 data.swap(data_buffers_.front());
276 data_buffers_.pop_front();
277 NaClXMutexUnlock(&cond_mu_);
278 PLUGIN_PRINTF(("StreamChunk\n"));
279 if (!llc_subprocess_->InvokeSrpcMethod("StreamChunk",
284 if (llc_subprocess_->srpc_client()->GetLastError() !=
285 NACL_SRPC_RESULT_APP_ERROR) {
286 // If the error was reported by the translator, then we fall through
287 // and call StreamEnd, which returns a string describing the error,
288 // which we can then send to the Javascript console. Otherwise just
289 // fail here, since the translator has probably crashed or asserted.
290 TranslateFailed(PP_NACL_ERROR_PNACL_LLC_INTERNAL,
291 "Compile stream chunk failed. "
292 "The PNaCl translator has probably crashed.");
297 PLUGIN_PRINTF(("StreamChunk Successful\n"));
298 core->CallOnMainThread(
300 coordinator_->GetCompileProgressCallback(data.size()),
304 NaClXMutexUnlock(&cond_mu_);
307 PLUGIN_PRINTF(("PnaclTranslateThread done with chunks\n"));
309 if (!llc_subprocess_->InvokeSrpcMethod("StreamEnd", std::string(), ¶ms)) {
310 PLUGIN_PRINTF(("PnaclTranslateThread StreamEnd failed\n"));
311 if (llc_subprocess_->srpc_client()->GetLastError() ==
312 NACL_SRPC_RESULT_APP_ERROR) {
313 // The error string is only present if the error was sent back from llc.
314 TranslateFailed(PP_NACL_ERROR_PNACL_LLC_INTERNAL,
315 params.outs()[3]->arrays.str);
317 TranslateFailed(PP_NACL_ERROR_PNACL_LLC_INTERNAL,
318 "Compile StreamEnd internal error");
322 compile_time_ = NaClGetTimeOfDayMicroseconds() - compile_start_time;
323 core->CallOnMainThread(0,
324 coordinator_->GetUMATimeCallback(
325 "NaCl.Perf.PNaClLoadTime.CompileTime",
329 // Shut down the llc subprocess.
330 NaClXMutexLock(&subprocess_mu_);
331 llc_subprocess_active_ = false;
332 llc_subprocess_.reset(NULL);
333 NaClXMutexUnlock(&subprocess_mu_);
335 if(!RunLdSubprocess()) {
338 core->CallOnMainThread(0, report_translate_finished_, PP_OK);
341 bool PnaclTranslateThread::RunLdSubprocess() {
342 ErrorInfo error_info;
345 std::vector<nacl::DescWrapper*> ld_in_files;
347 for (i = 0; i < obj_files_->size(); i++) {
348 // Reset object file for reading first.
349 if (!(*obj_files_)[i]->Reset()) {
350 TranslateFailed(PP_NACL_ERROR_PNACL_LD_SETUP,
351 "Link process could not reset object file");
354 ld_in_files.push_back((*obj_files_)[i]->read_wrapper());
356 for (; i < PnaclCoordinator::kMaxTranslatorObjectFiles; i++) {
357 ld_in_files.push_back(invalid_desc_wrapper_);
360 nacl::DescWrapper* ld_out_file = nexe_file_->write_wrapper();
361 pp::Core* core = pp::Module::Get()->core();
364 nacl::MutexLocker ml(&subprocess_mu_);
365 int64_t ld_start_time = NaClGetTimeOfDayMicroseconds();
366 ld_subprocess_.reset(
367 StartSubprocess(resources_->GetLdUrl(), manifest_id_, &error_info));
368 if (ld_subprocess_ == NULL) {
369 TranslateFailed(PP_NACL_ERROR_PNACL_LD_SETUP,
370 "Link process could not be created: " +
371 error_info.message());
374 ld_subprocess_active_ = true;
375 core->CallOnMainThread(0,
376 coordinator_->GetUMATimeCallback(
377 "NaCl.Perf.PNaClLoadTime.LoadLinker",
378 NaClGetTimeOfDayMicroseconds() - ld_start_time),
380 PluginReverseInterface* ld_reverse =
381 ld_subprocess_->service_runtime()->rev_interface();
382 ld_reverse->AddTempQuotaManagedFile(nexe_file_->identifier());
385 int64_t link_start_time = NaClGetTimeOfDayMicroseconds();
387 bool success = ld_subprocess_->InvokeSrpcMethod(
389 "ihhhhhhhhhhhhhhhhh",
391 static_cast<int>(obj_files_->size()),
392 ld_in_files[0]->desc(),
393 ld_in_files[1]->desc(),
394 ld_in_files[2]->desc(),
395 ld_in_files[3]->desc(),
396 ld_in_files[4]->desc(),
397 ld_in_files[5]->desc(),
398 ld_in_files[6]->desc(),
399 ld_in_files[7]->desc(),
400 ld_in_files[8]->desc(),
401 ld_in_files[9]->desc(),
402 ld_in_files[10]->desc(),
403 ld_in_files[11]->desc(),
404 ld_in_files[12]->desc(),
405 ld_in_files[13]->desc(),
406 ld_in_files[14]->desc(),
407 ld_in_files[15]->desc(),
408 ld_out_file->desc());
410 TranslateFailed(PP_NACL_ERROR_PNACL_LD_INTERNAL,
414 core->CallOnMainThread(0,
415 coordinator_->GetUMATimeCallback(
416 "NaCl.Perf.PNaClLoadTime.LinkTime",
417 NaClGetTimeOfDayMicroseconds() - link_start_time),
419 PLUGIN_PRINTF(("PnaclCoordinator: link (translator=%p) succeeded\n",
421 // Shut down the ld subprocess.
422 NaClXMutexLock(&subprocess_mu_);
423 ld_subprocess_active_ = false;
424 ld_subprocess_.reset(NULL);
425 NaClXMutexUnlock(&subprocess_mu_);
429 void PnaclTranslateThread::TranslateFailed(
430 PP_NaClError err_code,
431 const nacl::string& error_string) {
432 PLUGIN_PRINTF(("PnaclTranslateThread::TranslateFailed (error_string='%s')\n",
433 error_string.c_str()));
434 pp::Core* core = pp::Module::Get()->core();
435 if (coordinator_error_info_->message().empty()) {
436 // Only use our message if one hasn't already been set by the coordinator
437 // (e.g. pexe load failed).
438 coordinator_error_info_->SetReport(err_code,
439 nacl::string("PnaclCoordinator: ") +
442 core->CallOnMainThread(0, report_translate_finished_, PP_ERROR_FAILED);
445 void PnaclTranslateThread::AbortSubprocesses() {
446 PLUGIN_PRINTF(("PnaclTranslateThread::AbortSubprocesses\n"));
447 NaClXMutexLock(&subprocess_mu_);
448 if (llc_subprocess_ != NULL && llc_subprocess_active_) {
449 llc_subprocess_->service_runtime()->Shutdown();
450 llc_subprocess_active_ = false;
452 if (ld_subprocess_ != NULL && ld_subprocess_active_) {
453 ld_subprocess_->service_runtime()->Shutdown();
454 ld_subprocess_active_ = false;
456 NaClXMutexUnlock(&subprocess_mu_);
457 nacl::MutexLocker ml(&cond_mu_);
459 // Free all buffered bitcode chunks
460 data_buffers_.clear();
461 NaClXCondVarSignal(&buffer_cond_);
464 PnaclTranslateThread::~PnaclTranslateThread() {
465 PLUGIN_PRINTF(("~PnaclTranslateThread (translate_thread=%p)\n", this));
467 if (translate_thread_ != NULL)
468 NaClThreadJoin(translate_thread_.get());
469 PLUGIN_PRINTF(("~PnaclTranslateThread joined\n"));
470 NaClCondVarDtor(&buffer_cond_);
471 NaClMutexDtor(&cond_mu_);
472 NaClMutexDtor(&subprocess_mu_);
475 } // namespace plugin