blob: 03127a9eafe3b50f3f6e62df2781e00dba66dc57 [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
11/// This file defines the browser-based compile server.
12///
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).
17#if PNACL_BROWSER_TRANSLATOR
18
John Porto67f8de92015-06-25 10:14:17 -070019#include "IceBrowserCompileServer.h"
20#include "llvm/Support/QueueStreamer.h"
21
Jan Voung44c3a802015-03-27 16:29:08 -070022#include <cstring>
23#include <irt.h>
24#include <irt_dev.h>
Jan Voung2f7f2b72015-06-03 17:50:20 -070025#include <pthread.h>
Jan Voung44c3a802015-03-27 16:29:08 -070026#include <thread>
27
Jan Voung44c3a802015-03-27 16:29:08 -070028namespace Ice {
29
30// Create C wrappers around callback handlers for the IRT interface.
31namespace {
32
33BrowserCompileServer *gCompileServer;
34struct nacl_irt_private_pnacl_translator_compile gIRTFuncs;
35
36void getIRTInterfaces() {
37 size_t QueryResult =
38 nacl_interface_query(NACL_IRT_PRIVATE_PNACL_TRANSLATOR_COMPILE_v0_1,
39 &gIRTFuncs, sizeof(gIRTFuncs));
40 if (QueryResult != sizeof(gIRTFuncs))
41 llvm::report_fatal_error("Failed to get translator compile IRT interface");
42}
43
44char *onInitCallback(uint32_t NumThreads, int *ObjFileFDs,
Jan Voung9c1d3862015-03-31 14:14:20 -070045 size_t ObjFileFDCount, char **CLArgs, size_t CLArgsLen) {
Jan Voung44c3a802015-03-27 16:29:08 -070046 if (ObjFileFDCount < 1) {
47 std::string Buffer;
48 llvm::raw_string_ostream StrBuf(Buffer);
49 StrBuf << "Invalid number of FDs for onInitCallback " << ObjFileFDCount
50 << "\n";
51 return strdup(StrBuf.str().c_str());
52 }
53 int ObjFileFD = ObjFileFDs[0];
54 if (ObjFileFD < 0) {
55 std::string Buffer;
56 llvm::raw_string_ostream StrBuf(Buffer);
57 StrBuf << "Invalid FD given for onInitCallback " << ObjFileFD << "\n";
58 return strdup(StrBuf.str().c_str());
59 }
Jan Voung9c1d3862015-03-31 14:14:20 -070060 // CLArgs is almost an "argv", but is missing the argv[0] program name.
61 std::vector<char *> Argv;
62 char ProgramName[] = "pnacl-sz.nexe";
63 Argv.reserve(CLArgsLen + 1);
64 Argv.push_back(ProgramName);
65 for (size_t i = 0; i < CLArgsLen; ++i) {
66 Argv.push_back(CLArgs[i]);
67 }
68 // NOTE: strings pointed to by argv are owned by the caller, but we parse
69 // here before returning and don't store them.
70 gCompileServer->getParsedFlags(NumThreads, Argv.size(), Argv.data());
Jan Voung44c3a802015-03-27 16:29:08 -070071 gCompileServer->startCompileThread(ObjFileFD);
72 return nullptr;
73}
74
75int onDataCallback(const void *Data, size_t NumBytes) {
76 return gCompileServer->pushInputBytes(Data, NumBytes) ? 1 : 0;
77}
78
79char *onEndCallback() {
80 gCompileServer->endInputStream();
81 gCompileServer->waitForCompileThread();
Jan Voung2f7f2b72015-06-03 17:50:20 -070082 // TODO(jvoung): Also return UMA data.
Jan Voung44c3a802015-03-27 16:29:08 -070083 if (gCompileServer->getErrorCode().value()) {
Karl Schimpf2f67b922015-04-22 15:20:16 -070084 const std::string Error = gCompileServer->getErrorStream().getContents();
85 return strdup(Error.empty() ? "Some error occurred" : Error.c_str());
Jan Voung44c3a802015-03-27 16:29:08 -070086 }
87 return nullptr;
88}
89
90struct nacl_irt_pnacl_compile_funcs SubzeroCallbacks {
91 &onInitCallback, &onDataCallback, &onEndCallback
92};
93
94std::unique_ptr<llvm::raw_fd_ostream> getOutputStream(int FD) {
95 if (FD <= 0)
96 llvm::report_fatal_error("Invalid output FD");
97 const bool CloseOnDtor = true;
98 const bool Unbuffered = false;
99 return std::unique_ptr<llvm::raw_fd_ostream>(
100 new llvm::raw_fd_ostream(FD, CloseOnDtor, Unbuffered));
101}
102
Jan Voung2f7f2b72015-06-03 17:50:20 -0700103void fatalErrorHandler(void *UserData, const std::string &Reason,
104 bool GenCrashDialog) {
Jan Voung871b97f2015-06-30 13:31:02 -0700105 (void)GenCrashDialog;
Jan Voung2f7f2b72015-06-03 17:50:20 -0700106 BrowserCompileServer *Server =
107 reinterpret_cast<BrowserCompileServer *>(UserData);
108 Server->setFatalError(Reason);
109 // Only kill the current thread instead of the whole process.
110 // We need the server thread to remain alive in order to respond with the
111 // error message.
112 // We could also try to pthread_kill all other worker threads, but
113 // pthread_kill / raising signals is not supported by NaCl.
114 // We'll have to assume that the worker/emitter threads will be well behaved
115 // after a fatal error in other threads, and either get stuck waiting
116 // on input from a previous stage, or also call report_fatal_error.
117 pthread_exit(0);
118}
119
Jan Voung44c3a802015-03-27 16:29:08 -0700120} // end of anonymous namespace
121
Jim Stichnotheafb56c2015-06-22 10:35:22 -0700122BrowserCompileServer::~BrowserCompileServer() = default;
Jan Voung44c3a802015-03-27 16:29:08 -0700123
124void BrowserCompileServer::run() {
125 gCompileServer = this;
Jan Voung44c3a802015-03-27 16:29:08 -0700126 getIRTInterfaces();
127 gIRTFuncs.serve_translate_request(&SubzeroCallbacks);
128}
129
130void BrowserCompileServer::getParsedFlags(uint32_t NumThreads, int argc,
131 char **argv) {
132 ClFlags::parseFlags(argc, argv);
133 ClFlags::getParsedClFlags(Flags);
134 ClFlags::getParsedClFlagsExtra(ExtraFlags);
135 // Set some defaults which aren't specified via the argv string.
136 Flags.setNumTranslationThreads(NumThreads);
137 Flags.setUseSandboxing(true);
138 Flags.setOutFileType(FT_Elf);
139 // TODO(jvoung): allow setting different target arches.
140 Flags.setTargetArch(Target_X8632);
141 ExtraFlags.setBuildOnRead(true);
142 ExtraFlags.setInputFileFormat(llvm::PNaClFormat);
143}
144
145bool BrowserCompileServer::pushInputBytes(const void *Data, size_t NumBytes) {
Jan Voung2f7f2b72015-06-03 17:50:20 -0700146 // If there was an earlier error, do not attempt to push bytes to
147 // the QueueStreamer. Otherwise the thread could become blocked.
148 if (HadError.load())
149 return true;
Jan Voung44c3a802015-03-27 16:29:08 -0700150 return InputStream->PutBytes(
151 const_cast<unsigned char *>(
152 reinterpret_cast<const unsigned char *>(Data)),
153 NumBytes) != NumBytes;
154}
155
Jan Voung2f7f2b72015-06-03 17:50:20 -0700156void BrowserCompileServer::setFatalError(const IceString &Reason) {
157 HadError.store(true);
158 Ctx->getStrError() << Reason;
159 // Make sure that the QueueStreamer is not stuck by signaling an early end.
160 InputStream->SetDone();
161}
162
163ErrorCode &BrowserCompileServer::getErrorCode() {
164 if (HadError.load()) {
165 // HadError means report_fatal_error is called. Make sure that the
166 // LastError is not EC_None. We don't know the type of error so
167 // just pick some error category.
168 LastError.assign(EC_Translation);
169 }
170 return LastError;
171}
172
Jan Voung44c3a802015-03-27 16:29:08 -0700173void BrowserCompileServer::endInputStream() { InputStream->SetDone(); }
174
175void BrowserCompileServer::startCompileThread(int ObjFD) {
176 InputStream = new llvm::QueueStreamer();
177 LogStream = getOutputStream(STDOUT_FILENO);
178 LogStream->SetUnbuffered();
179 EmitStream = getOutputStream(ObjFD);
180 EmitStream->SetBufferSize(1 << 14);
Karl Schimpf2f67b922015-04-22 15:20:16 -0700181 std::unique_ptr<StringStream> ErrStrm(new StringStream());
182 ErrorStream = std::move(ErrStrm);
Jan Voung44c3a802015-03-27 16:29:08 -0700183 ELFStream.reset(new ELFStreamer(*EmitStream.get()));
184 Ctx.reset(new GlobalContext(LogStream.get(), EmitStream.get(),
Karl Schimpf2f67b922015-04-22 15:20:16 -0700185 &ErrorStream->getStream(), ELFStream.get(),
186 Flags));
Jan Voung44c3a802015-03-27 16:29:08 -0700187 CompileThread = std::thread([this]() {
Jan Voung2f7f2b72015-06-03 17:50:20 -0700188 llvm::install_fatal_error_handler(fatalErrorHandler, this);
Jan Voung44c3a802015-03-27 16:29:08 -0700189 Ctx->initParserThread();
190 this->getCompiler().run(ExtraFlags, *Ctx.get(),
191 // Retain original reference, but the compiler
192 // (LLVM's MemoryObject) wants to handle deletion.
193 std::unique_ptr<llvm::DataStreamer>(InputStream));
194 });
195}
196
197} // end of namespace Ice
198
199#endif // PNACL_BROWSER_TRANSLATOR