blob: 5fd1ab1d29570e7e9a08525753ec317fd0d8193c [file] [log] [blame]
Jan Voung44c3a802015-03-27 16:29:08 -07001//===- subzero/src/IceBrowserCompileServer.cpp - Browser compile server ---===//
2//
3// The Subzero Code Generator
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Andrew Scull9612d322015-07-06 14:53:25 -07009///
10/// \file
Jim Stichnoth92a6e5b2015-12-02 16:52:44 -080011/// \brief Defines the browser-based compile server.
Andrew Scull9612d322015-07-06 14:53:25 -070012///
Jan Voung44c3a802015-03-27 16:29:08 -070013//===----------------------------------------------------------------------===//
14
15// Can only compile this with the NaCl compiler (needs irt.h, and the
16// unsandboxed LLVM build using the trusted compiler does not have irt.h).
John Porto67f8de92015-06-25 10:14:17 -070017#include "IceBrowserCompileServer.h"
Jim Stichnothfd07ad02016-04-20 10:12:46 -070018#include "IceRangeSpec.h"
Jim Stichnoth816fd682016-02-16 05:47:32 -080019
John Porto44b3ce82016-02-26 13:10:55 -080020#if PNACL_BROWSER_TRANSLATOR
21
Jim Stichnoth816fd682016-02-16 05:47:32 -080022// Headers which are not properly part of the SDK are included by their path in
23// the NaCl tree.
24#ifdef __pnacl__
25#include "native_client/src/untrusted/nacl/pnacl.h"
26#endif // __pnacl__
27
John Porto67f8de92015-06-25 10:14:17 -070028#include "llvm/Support/QueueStreamer.h"
29
Jan Voung44c3a802015-03-27 16:29:08 -070030#include <cstring>
Jim Stichnothfd07ad02016-04-20 10:12:46 -070031#include <fstream>
Jan Voung44c3a802015-03-27 16:29:08 -070032#include <irt.h>
33#include <irt_dev.h>
Jan Voung2f7f2b72015-06-03 17:50:20 -070034#include <pthread.h>
Jan Voung44c3a802015-03-27 16:29:08 -070035#include <thread>
36
Jan Voung44c3a802015-03-27 16:29:08 -070037namespace Ice {
38
39// Create C wrappers around callback handlers for the IRT interface.
40namespace {
41
42BrowserCompileServer *gCompileServer;
43struct nacl_irt_private_pnacl_translator_compile gIRTFuncs;
44
45void getIRTInterfaces() {
46 size_t QueryResult =
47 nacl_interface_query(NACL_IRT_PRIVATE_PNACL_TRANSLATOR_COMPILE_v0_1,
48 &gIRTFuncs, sizeof(gIRTFuncs));
49 if (QueryResult != sizeof(gIRTFuncs))
50 llvm::report_fatal_error("Failed to get translator compile IRT interface");
51}
52
Jim Stichnothfd07ad02016-04-20 10:12:46 -070053// Allow pnacl-sz arguments to be supplied externally, instead of coming from
54// the browser. This is meant to be used for debugging.
55//
Jim Stichnotha64156e2016-08-08 14:25:51 -070056// NOTE: This functionality is only enabled in non-MINIMAL Subzero builds, for
57// security/safety reasons.
58//
Jim Stichnothfd07ad02016-04-20 10:12:46 -070059// If the SZARGFILE environment variable is set to a file name, arguments are
60// read from that file, one argument per line. This requires setting 3
61// environment variables before starting the browser:
62//
63// NACL_ENV_PASSTHROUGH=NACL_DANGEROUS_ENABLE_FILE_ACCESS,NACLENV_SZARGFILE
64// NACL_DANGEROUS_ENABLE_FILE_ACCESS=1
65// NACLENV_SZARGFILE=/path/to/myargs.txt
66//
67// In addition, Chrome needs to be launched with the "--no-sandbox" argument.
68//
69// If the SZARGLIST environment variable is set, arguments are extracted from
70// that variable's value, separated by the '|' character (being careful to
71// escape/quote special shell characters). This requires setting 2 environment
72// variables before starting the browser:
73//
74// NACL_ENV_PASSTHROUGH=NACLENV_SZARGLIST
75// NACLENV_SZARGLIST=arg
76//
77// This does not require the "--no-sandbox" argument, and is therefore much
78// safer, but does require restarting the browser to change the arguments.
79//
80// If external arguments are supplied, the browser's NumThreads specification is
81// ignored, to allow -threads to be specified as an external argument. Note
82// that the browser normally supplies the "-O2" argument, so externally supplied
83// arguments might want to provide an explicit -O argument.
Jim Stichnotha64156e2016-08-08 14:25:51 -070084//
85// See Chrome's src/components/nacl/zygote/nacl_fork_delegate_linux.cc for the
86// NACL_ENV_PASSTHROUGH mechanism.
87//
88// See NaCl's src/trusted/service_runtime/env_cleanser.c for the NACLENV_
89// mechanism.
Jim Stichnothfd07ad02016-04-20 10:12:46 -070090std::vector<std::string> getExternalArgs() {
91 std::vector<std::string> ExternalArgs;
92 if (BuildDefs::minimal())
93 return ExternalArgs;
94 char ArgsFileVar[] = "SZARGFILE";
95 char ArgsListVar[] = "SZARGLIST";
96 if (const char *ArgsFilename = getenv(ArgsFileVar)) {
97 std::ifstream ArgsStream(ArgsFilename);
98 std::string Arg;
99 while (ArgsStream >> std::ws, std::getline(ArgsStream, Arg)) {
100 if (!Arg.empty() && Arg[0] == '#')
101 continue;
102 ExternalArgs.emplace_back(Arg);
103 }
104 if (ExternalArgs.empty()) {
105 llvm::report_fatal_error("Failed to read arguments from file '" +
106 std::string(ArgsFilename) + "'");
107 }
108 } else if (const char *ArgsList = getenv(ArgsListVar)) {
109 // Leverage the RangeSpec tokenizer.
110 auto Args = RangeSpec::tokenize(ArgsList, '|');
111 ExternalArgs.insert(ExternalArgs.end(), Args.begin(), Args.end());
112 }
113 return ExternalArgs;
114}
115
Jan Voung44c3a802015-03-27 16:29:08 -0700116char *onInitCallback(uint32_t NumThreads, int *ObjFileFDs,
Jan Voung9c1d3862015-03-31 14:14:20 -0700117 size_t ObjFileFDCount, char **CLArgs, size_t CLArgsLen) {
Jan Voung44c3a802015-03-27 16:29:08 -0700118 if (ObjFileFDCount < 1) {
119 std::string Buffer;
120 llvm::raw_string_ostream StrBuf(Buffer);
121 StrBuf << "Invalid number of FDs for onInitCallback " << ObjFileFDCount
122 << "\n";
123 return strdup(StrBuf.str().c_str());
124 }
125 int ObjFileFD = ObjFileFDs[0];
126 if (ObjFileFD < 0) {
127 std::string Buffer;
128 llvm::raw_string_ostream StrBuf(Buffer);
129 StrBuf << "Invalid FD given for onInitCallback " << ObjFileFD << "\n";
130 return strdup(StrBuf.str().c_str());
131 }
Jan Voung9c1d3862015-03-31 14:14:20 -0700132 // CLArgs is almost an "argv", but is missing the argv[0] program name.
Jim Stichnothfd07ad02016-04-20 10:12:46 -0700133 std::vector<const char *> Argv;
134 constexpr static char ProgramName[] = "pnacl-sz.nexe";
Jan Voung9c1d3862015-03-31 14:14:20 -0700135 Argv.reserve(CLArgsLen + 1);
136 Argv.push_back(ProgramName);
Jim Stichnothfd07ad02016-04-20 10:12:46 -0700137
138 bool UseNumThreadsFromBrowser = true;
139 auto ExternalArgs = getExternalArgs();
140 if (ExternalArgs.empty()) {
141 for (size_t i = 0; i < CLArgsLen; ++i) {
142 Argv.push_back(CLArgs[i]);
143 }
144 } else {
145 for (auto &Arg : ExternalArgs) {
146 Argv.emplace_back(Arg.c_str());
147 }
148 UseNumThreadsFromBrowser = false;
Jan Voung9c1d3862015-03-31 14:14:20 -0700149 }
150 // NOTE: strings pointed to by argv are owned by the caller, but we parse
151 // here before returning and don't store them.
Jim Stichnothfd07ad02016-04-20 10:12:46 -0700152 gCompileServer->getParsedFlags(UseNumThreadsFromBrowser, NumThreads,
153 Argv.size(), Argv.data());
Jan Voung44c3a802015-03-27 16:29:08 -0700154 gCompileServer->startCompileThread(ObjFileFD);
155 return nullptr;
156}
157
158int onDataCallback(const void *Data, size_t NumBytes) {
159 return gCompileServer->pushInputBytes(Data, NumBytes) ? 1 : 0;
160}
161
162char *onEndCallback() {
163 gCompileServer->endInputStream();
164 gCompileServer->waitForCompileThread();
Jan Voung2f7f2b72015-06-03 17:50:20 -0700165 // TODO(jvoung): Also return UMA data.
Jan Voung44c3a802015-03-27 16:29:08 -0700166 if (gCompileServer->getErrorCode().value()) {
Karl Schimpf2f67b922015-04-22 15:20:16 -0700167 const std::string Error = gCompileServer->getErrorStream().getContents();
168 return strdup(Error.empty() ? "Some error occurred" : Error.c_str());
Jan Voung44c3a802015-03-27 16:29:08 -0700169 }
170 return nullptr;
171}
172
173struct nacl_irt_pnacl_compile_funcs SubzeroCallbacks {
174 &onInitCallback, &onDataCallback, &onEndCallback
175};
176
177std::unique_ptr<llvm::raw_fd_ostream> getOutputStream(int FD) {
178 if (FD <= 0)
179 llvm::report_fatal_error("Invalid output FD");
Jim Stichnoth5bff61c2015-10-28 09:26:00 -0700180 constexpr bool CloseOnDtor = true;
181 constexpr bool Unbuffered = false;
Jan Voung44c3a802015-03-27 16:29:08 -0700182 return std::unique_ptr<llvm::raw_fd_ostream>(
183 new llvm::raw_fd_ostream(FD, CloseOnDtor, Unbuffered));
184}
185
Jan Voung2f7f2b72015-06-03 17:50:20 -0700186void fatalErrorHandler(void *UserData, const std::string &Reason,
187 bool GenCrashDialog) {
Jan Voung871b97f2015-06-30 13:31:02 -0700188 (void)GenCrashDialog;
Jan Voung2f7f2b72015-06-03 17:50:20 -0700189 BrowserCompileServer *Server =
190 reinterpret_cast<BrowserCompileServer *>(UserData);
191 Server->setFatalError(Reason);
Andrew Scull57e12682015-09-16 11:30:19 -0700192 // Only kill the current thread instead of the whole process. We need the
193 // server thread to remain alive in order to respond with the error message.
Jan Voung2f7f2b72015-06-03 17:50:20 -0700194 // We could also try to pthread_kill all other worker threads, but
Andrew Scull57e12682015-09-16 11:30:19 -0700195 // pthread_kill / raising signals is not supported by NaCl. We'll have to
196 // assume that the worker/emitter threads will be well behaved after a fatal
197 // error in other threads, and either get stuck waiting on input from a
198 // previous stage, or also call report_fatal_error.
Jan Voung2f7f2b72015-06-03 17:50:20 -0700199 pthread_exit(0);
200}
201
Jim Stichnoth816fd682016-02-16 05:47:32 -0800202/// Adapted from pnacl-llc's AddDefaultCPU() in srpc_main.cpp.
203TargetArch getTargetArch() {
204#if defined(__pnacl__)
205 switch (__builtin_nacl_target_arch()) {
206 case PnaclTargetArchitectureX86_32:
207 case PnaclTargetArchitectureX86_32_NonSFI:
208 return Target_X8632;
209 case PnaclTargetArchitectureX86_64:
210 return Target_X8664;
211 case PnaclTargetArchitectureARM_32:
212 case PnaclTargetArchitectureARM_32_NonSFI:
213 return Target_ARM32;
214 case PnaclTargetArchitectureMips_32:
215 return Target_MIPS32;
216 default:
217 llvm::report_fatal_error("no target architecture match.");
218 }
219#elif defined(__i386__)
220 return Target_X8632;
221#elif defined(__x86_64__)
222 return Target_X8664;
223#elif defined(__arm__)
224 return Target_ARM32;
225#else
226// TODO(stichnot): Add mips.
227#error "Unknown architecture"
228#endif
229}
230
Jan Voung44c3a802015-03-27 16:29:08 -0700231} // end of anonymous namespace
232
Jim Stichnotheafb56c2015-06-22 10:35:22 -0700233BrowserCompileServer::~BrowserCompileServer() = default;
Jan Voung44c3a802015-03-27 16:29:08 -0700234
235void BrowserCompileServer::run() {
236 gCompileServer = this;
Jan Voung44c3a802015-03-27 16:29:08 -0700237 getIRTInterfaces();
238 gIRTFuncs.serve_translate_request(&SubzeroCallbacks);
239}
240
Jim Stichnothfd07ad02016-04-20 10:12:46 -0700241void BrowserCompileServer::getParsedFlags(bool UseNumThreadsFromBrowser,
242 uint32_t NumThreads, int argc,
243 const char *const *argv) {
Jan Voung44c3a802015-03-27 16:29:08 -0700244 ClFlags::parseFlags(argc, argv);
Karl Schimpfd4699942016-04-02 09:55:31 -0700245 ClFlags::getParsedClFlags(ClFlags::Flags);
Jan Voung44c3a802015-03-27 16:29:08 -0700246 // Set some defaults which aren't specified via the argv string.
Jim Stichnothfd07ad02016-04-20 10:12:46 -0700247 if (UseNumThreadsFromBrowser)
248 ClFlags::Flags.setNumTranslationThreads(NumThreads);
Karl Schimpfd4699942016-04-02 09:55:31 -0700249 ClFlags::Flags.setUseSandboxing(true);
250 ClFlags::Flags.setOutFileType(FT_Elf);
251 ClFlags::Flags.setTargetArch(getTargetArch());
252 ClFlags::Flags.setInputFileFormat(llvm::PNaClFormat);
Jan Voung44c3a802015-03-27 16:29:08 -0700253}
254
255bool BrowserCompileServer::pushInputBytes(const void *Data, size_t NumBytes) {
Andrew Scull57e12682015-09-16 11:30:19 -0700256 // If there was an earlier error, do not attempt to push bytes to the
257 // QueueStreamer. Otherwise the thread could become blocked.
Jan Voung2f7f2b72015-06-03 17:50:20 -0700258 if (HadError.load())
259 return true;
Jan Voung44c3a802015-03-27 16:29:08 -0700260 return InputStream->PutBytes(
261 const_cast<unsigned char *>(
262 reinterpret_cast<const unsigned char *>(Data)),
263 NumBytes) != NumBytes;
264}
265
Jim Stichnoth467ffe52016-03-29 15:01:06 -0700266void BrowserCompileServer::setFatalError(const std::string &Reason) {
Jan Voung2f7f2b72015-06-03 17:50:20 -0700267 HadError.store(true);
268 Ctx->getStrError() << Reason;
269 // Make sure that the QueueStreamer is not stuck by signaling an early end.
270 InputStream->SetDone();
271}
272
273ErrorCode &BrowserCompileServer::getErrorCode() {
274 if (HadError.load()) {
275 // HadError means report_fatal_error is called. Make sure that the
Andrew Scull57e12682015-09-16 11:30:19 -0700276 // LastError is not EC_None. We don't know the type of error so just pick
277 // some error category.
Jan Voung2f7f2b72015-06-03 17:50:20 -0700278 LastError.assign(EC_Translation);
279 }
280 return LastError;
281}
282
Jan Voung44c3a802015-03-27 16:29:08 -0700283void BrowserCompileServer::endInputStream() { InputStream->SetDone(); }
284
285void BrowserCompileServer::startCompileThread(int ObjFD) {
286 InputStream = new llvm::QueueStreamer();
Jim Stichnothfd07ad02016-04-20 10:12:46 -0700287 bool LogStreamFailure = false;
288 int LogFD = STDOUT_FILENO;
Jim Stichnoth64045042016-04-21 05:38:49 -0700289 if (getFlags().getLogFilename() == "-") {
290 // Common case, do nothing.
291 } else if (getFlags().getLogFilename() == "/dev/stderr") {
Jim Stichnothfd07ad02016-04-20 10:12:46 -0700292 LogFD = STDERR_FILENO;
293 } else {
294 LogStreamFailure = true;
295 }
296 LogStream = getOutputStream(LogFD);
Jan Voung44c3a802015-03-27 16:29:08 -0700297 LogStream->SetUnbuffered();
Jim Stichnothfd07ad02016-04-20 10:12:46 -0700298 if (LogStreamFailure) {
299 *LogStream
300 << "Warning: Log file name must be either '-' or '/dev/stderr'\n";
301 }
Jan Voung44c3a802015-03-27 16:29:08 -0700302 EmitStream = getOutputStream(ObjFD);
303 EmitStream->SetBufferSize(1 << 14);
Karl Schimpf2f67b922015-04-22 15:20:16 -0700304 std::unique_ptr<StringStream> ErrStrm(new StringStream());
305 ErrorStream = std::move(ErrStrm);
Nicolas Capens3e376472016-09-13 11:35:57 -0400306 ELFStream.reset(new ELFFileStreamer(*EmitStream.get()));
Jan Voung44c3a802015-03-27 16:29:08 -0700307 Ctx.reset(new GlobalContext(LogStream.get(), EmitStream.get(),
Jim Stichnoth98ba0062016-03-07 09:26:22 -0800308 &ErrorStream->getStream(), ELFStream.get()));
Jan Voung44c3a802015-03-27 16:29:08 -0700309 CompileThread = std::thread([this]() {
Jan Voung2f7f2b72015-06-03 17:50:20 -0700310 llvm::install_fatal_error_handler(fatalErrorHandler, this);
Jan Voung44c3a802015-03-27 16:29:08 -0700311 Ctx->initParserThread();
Karl Schimpfd4699942016-04-02 09:55:31 -0700312 this->getCompiler().run(ClFlags::Flags, *Ctx.get(),
Jan Voung44c3a802015-03-27 16:29:08 -0700313 // Retain original reference, but the compiler
314 // (LLVM's MemoryObject) wants to handle deletion.
315 std::unique_ptr<llvm::DataStreamer>(InputStream));
316 });
317}
318
319} // end of namespace Ice
320
John Porto44b3ce82016-02-26 13:10:55 -0800321#else // !PNACL_BROWSER_TRANSLATOR
322
323#include "llvm/Support/ErrorHandling.h"
324
325namespace Ice {
326
327BrowserCompileServer::~BrowserCompileServer() {}
328
329void BrowserCompileServer::run() {
330 llvm::report_fatal_error("no browser hookups");
331}
332
333ErrorCode &BrowserCompileServer::getErrorCode() {
334 llvm::report_fatal_error("no browser hookups");
335}
336
337} // end of namespace Ice
338
Jan Voung44c3a802015-03-27 16:29:08 -0700339#endif // PNACL_BROWSER_TRANSLATOR