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