Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / ppapi / native_client / src / trusted / plugin / pnacl_translate_thread.cc
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.
4
5 #include "ppapi/native_client/src/trusted/plugin/pnacl_translate_thread.h"
6
7 #include <iterator>
8
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"
17
18 namespace plugin {
19 namespace {
20
21 template <typename Val>
22 nacl::string MakeCommandLineArg(const char* key, const Val val) {
23   nacl::stringstream ss;
24   ss << key << val;
25   return ss.str();
26 }
27
28 void GetLlcCommandLine(Plugin* plugin,
29                        std::vector<char>* split_args,
30                        size_t obj_files_size,
31                        int32_t opt_level,
32                        bool is_debug,
33                        const nacl::string &architecture_attributes) {
34   typedef std::vector<nacl::string> Args;
35   Args args;
36
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));
42   if (is_debug)
43     args.push_back("-bitcode-format=llvm");
44   if (!architecture_attributes.empty())
45     args.push_back("-mattr=" + architecture_attributes);
46
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');
50   }
51 }
52
53 }  // namespace
54
55 PnaclTranslateThread::PnaclTranslateThread() : llc_subprocess_active_(false),
56                                                ld_subprocess_active_(false),
57                                                done_(false),
58                                                compile_time_(0),
59                                                manifest_id_(0),
60                                                obj_files_(NULL),
61                                                nexe_file_(NULL),
62                                                coordinator_error_info_(NULL),
63                                                resources_(NULL),
64                                                coordinator_(NULL),
65                                                plugin_(NULL) {
66   NaClXMutexCtor(&subprocess_mu_);
67   NaClXMutexCtor(&cond_mu_);
68   NaClXCondVarCtor(&buffer_cond_);
69 }
70
71 void PnaclTranslateThread::RunTranslate(
72     const pp::CompletionCallback& finish_callback,
73     int32_t manifest_id,
74     const std::vector<TempFile*>* obj_files,
75     TempFile* nexe_file,
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,
82     Plugin* plugin) {
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;
93   plugin_ = plugin;
94
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.");
102     return;
103   }
104   const int32_t kArbitraryStackSize = 128 * 1024;
105   if (!NaClThreadCreateJoinable(translate_thread_.get(),
106                                 DoTranslateThread,
107                                 this,
108                                 kArbitraryStackSize)) {
109     TranslateFailed(PP_NACL_ERROR_PNACL_THREAD_CREATE,
110                     "could not create thread.");
111     translate_thread_.reset(NULL);
112   }
113 }
114
115 // Called from main thread to send bytes to the translator.
116 void PnaclTranslateThread::PutBytes(std::vector<char>* bytes,
117                                              int count) {
118   PLUGIN_PRINTF(("PutBytes (this=%p, bytes=%p, size=%" NACL_PRIuS
119                  ", count=%d)\n",
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_);
125     done_ = true;
126     NaClXCondVarSignal(&buffer_cond_);
127     NaClXMutexUnlock(&cond_mu_);
128     return;
129   }
130
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);
136
137   NaClXMutexLock(&cond_mu_);
138
139   data_buffers_.push_back(std::vector<char>());
140   bytes->swap(data_buffers_.back()); // Avoid copying the buffer data.
141
142   NaClXCondVarSignal(&buffer_cond_);
143   NaClXMutexUnlock(&cond_mu_);
144
145   // Ensure the buffer we send back to the coordinator is the expected size
146   bytes->resize(buffer_size);
147 }
148
149 NaClSubprocess* PnaclTranslateThread::StartSubprocess(
150     const nacl::string& url_for_nexe,
151     int32_t manifest_id,
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) {
165     PLUGIN_PRINTF((
166         "PnaclTranslateThread::StartSubprocess: subprocess creation failed\n"));
167     return NULL;
168   }
169   return subprocess.release();
170 }
171
172 void WINAPI PnaclTranslateThread::DoTranslateThread(void* arg) {
173   PnaclTranslateThread* translator =
174       reinterpret_cast<PnaclTranslateThread*>(arg);
175   translator->DoTranslate();
176 }
177
178 void PnaclTranslateThread::DoTranslate() {
179   ErrorInfo error_info;
180   SrpcParams params;
181   std::vector<nacl::DescWrapper*> llc_out_files;
182   size_t i;
183   for (i = 0; i < obj_files_->size(); i++) {
184     llc_out_files.push_back((*obj_files_)[i]->write_wrapper());
185   }
186   for (; i < PnaclCoordinator::kMaxTranslatorObjectFiles; i++) {
187     llc_out_files.push_back(invalid_desc_wrapper_);
188   }
189
190   pp::Core* core = pp::Module::Get()->core();
191   {
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());
200       return;
201     }
202     llc_subprocess_active_ = true;
203     core->CallOnMainThread(0,
204                            coordinator_->GetUMATimeCallback(
205                                "NaCl.Perf.PNaClLoadTime.LoadCompiler",
206                                NaClGetTimeOfDayMicroseconds() - llc_start_time),
207                            PP_OK);
208     // Run LLC.
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());
213     }
214   }
215
216   int64_t compile_start_time = NaClGetTimeOfDayMicroseconds();
217   bool init_success;
218
219   std::vector<char> split_args;
220   GetLlcCommandLine(plugin_,
221                     &split_args,
222                     obj_files_->size(),
223                     pnacl_options_->opt_level,
224                     pnacl_options_->is_debug,
225                     architecture_attributes_);
226   init_success = llc_subprocess_->InvokeSrpcMethod(
227       "StreamInitWithSplit",
228       "ihhhhhhhhhhhhhhhhC",
229       &params,
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(),
247       &split_args[0],
248       split_args.size());
249   if (!init_success) {
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));
256     } else {
257       TranslateFailed(PP_NACL_ERROR_PNACL_LLC_INTERNAL,
258                       "Stream init internal error");
259     }
260     return;
261   }
262   PLUGIN_PRINTF(("PnaclCoordinator: StreamInit successful\n"));
263
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_);
269     }
270     PLUGIN_PRINTF(("PnaclTranslateThread awake (done=%d, size=%" NACL_PRIuS
271                    ")\n",
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",
280                                              "C",
281                                              &params,
282                                              &data[0],
283                                              data.size())) {
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.");
293           return;
294         }
295         break;
296       } else {
297         PLUGIN_PRINTF(("StreamChunk Successful\n"));
298         core->CallOnMainThread(
299             0,
300             coordinator_->GetCompileProgressCallback(data.size()),
301             PP_OK);
302       }
303     } else {
304       NaClXMutexUnlock(&cond_mu_);
305     }
306   }
307   PLUGIN_PRINTF(("PnaclTranslateThread done with chunks\n"));
308   // Finish llc.
309   if (!llc_subprocess_->InvokeSrpcMethod("StreamEnd", std::string(), &params)) {
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);
316     } else {
317       TranslateFailed(PP_NACL_ERROR_PNACL_LLC_INTERNAL,
318                       "Compile StreamEnd internal error");
319     }
320     return;
321   }
322   compile_time_ = NaClGetTimeOfDayMicroseconds() - compile_start_time;
323   core->CallOnMainThread(0,
324                          coordinator_->GetUMATimeCallback(
325                              "NaCl.Perf.PNaClLoadTime.CompileTime",
326                              compile_time_),
327                          PP_OK);
328
329   // Shut down the llc subprocess.
330   NaClXMutexLock(&subprocess_mu_);
331   llc_subprocess_active_ = false;
332   llc_subprocess_.reset(NULL);
333   NaClXMutexUnlock(&subprocess_mu_);
334
335   if(!RunLdSubprocess()) {
336     return;
337   }
338   core->CallOnMainThread(0, report_translate_finished_, PP_OK);
339 }
340
341 bool PnaclTranslateThread::RunLdSubprocess() {
342   ErrorInfo error_info;
343   SrpcParams params;
344
345   std::vector<nacl::DescWrapper*> ld_in_files;
346   size_t i;
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");
352       return false;
353     }
354     ld_in_files.push_back((*obj_files_)[i]->read_wrapper());
355   }
356   for (; i < PnaclCoordinator::kMaxTranslatorObjectFiles; i++) {
357     ld_in_files.push_back(invalid_desc_wrapper_);
358   }
359
360   nacl::DescWrapper* ld_out_file = nexe_file_->write_wrapper();
361   pp::Core* core = pp::Module::Get()->core();
362   {
363     // Create LD process
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());
372       return false;
373     }
374     ld_subprocess_active_ = true;
375     core->CallOnMainThread(0,
376                            coordinator_->GetUMATimeCallback(
377                                "NaCl.Perf.PNaClLoadTime.LoadLinker",
378                                NaClGetTimeOfDayMicroseconds() - ld_start_time),
379                            PP_OK);
380     PluginReverseInterface* ld_reverse =
381         ld_subprocess_->service_runtime()->rev_interface();
382     ld_reverse->AddTempQuotaManagedFile(nexe_file_->identifier());
383   }
384
385   int64_t link_start_time = NaClGetTimeOfDayMicroseconds();
386   // Run LD.
387   bool success = ld_subprocess_->InvokeSrpcMethod(
388       "RunWithSplit",
389       "ihhhhhhhhhhhhhhhhh",
390       &params,
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());
409   if (!success) {
410     TranslateFailed(PP_NACL_ERROR_PNACL_LD_INTERNAL,
411                     "link failed.");
412     return false;
413   }
414   core->CallOnMainThread(0,
415                          coordinator_->GetUMATimeCallback(
416                              "NaCl.Perf.PNaClLoadTime.LinkTime",
417                              NaClGetTimeOfDayMicroseconds() - link_start_time),
418                          PP_OK);
419   PLUGIN_PRINTF(("PnaclCoordinator: link (translator=%p) succeeded\n",
420                  this));
421   // Shut down the ld subprocess.
422   NaClXMutexLock(&subprocess_mu_);
423   ld_subprocess_active_ = false;
424   ld_subprocess_.reset(NULL);
425   NaClXMutexUnlock(&subprocess_mu_);
426   return true;
427 }
428
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: ") +
440                                        error_string);
441   }
442   core->CallOnMainThread(0, report_translate_finished_, PP_ERROR_FAILED);
443 }
444
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;
451   }
452   if (ld_subprocess_ != NULL && ld_subprocess_active_) {
453     ld_subprocess_->service_runtime()->Shutdown();
454     ld_subprocess_active_ = false;
455   }
456   NaClXMutexUnlock(&subprocess_mu_);
457   nacl::MutexLocker ml(&cond_mu_);
458   done_ = true;
459   // Free all buffered bitcode chunks
460   data_buffers_.clear();
461   NaClXCondVarSignal(&buffer_cond_);
462 }
463
464 PnaclTranslateThread::~PnaclTranslateThread() {
465   PLUGIN_PRINTF(("~PnaclTranslateThread (translate_thread=%p)\n", this));
466   AbortSubprocesses();
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_);
473 }
474
475 } // namespace plugin