Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(660)

Side by Side Diff: src/IceBrowserCompileServer.cpp

Issue 997773002: Refactor Subzero initialization and add a browser callback handler. (Closed) Base URL: https://chromium.googlesource.com/native_client/pnacl-subzero.git@master
Patch Set: Add argv note Created 5 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « src/IceBrowserCompileServer.h ('k') | src/IceClFlags.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 //===- 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
18 #include <cstring>
19 #include <irt.h>
20 #include <irt_dev.h>
21 #include <thread>
22
23 #include "llvm/Support/QueueStreamer.h"
24
25 #include "IceBrowserCompileServer.h"
26
27 namespace Ice {
28
29 // Create C wrappers around callback handlers for the IRT interface.
30 namespace {
31
32 BrowserCompileServer *gCompileServer;
33 struct nacl_irt_private_pnacl_translator_compile gIRTFuncs;
34
35 void 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
43 char *onInitCallback(uint32_t NumThreads, int *ObjFileFDs,
44 size_t ObjFileFDCount, char **argv, size_t argc) {
45 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 }
59 // NOTE: argv is owned by the caller, but we parse here before returning.
60 gCompileServer->getParsedFlags(NumThreads, argc, argv);
61 gCompileServer->startCompileThread(ObjFileFD);
62 return nullptr;
63 }
64
65 int onDataCallback(const void *Data, size_t NumBytes) {
66 return gCompileServer->pushInputBytes(Data, NumBytes) ? 1 : 0;
67 }
68
69 char *onEndCallback() {
70 gCompileServer->endInputStream();
71 gCompileServer->waitForCompileThread();
72 // TODO(jvoung): Also return an error string, and UMA data.
73 // Set up a report_fatal_error handler to grab that string.
74 if (gCompileServer->getErrorCode().value()) {
75 return strdup("Some error occurred");
76 }
77 return nullptr;
78 }
79
80 struct nacl_irt_pnacl_compile_funcs SubzeroCallbacks {
81 &onInitCallback, &onDataCallback, &onEndCallback
82 };
83
84 std::unique_ptr<llvm::raw_fd_ostream> getOutputStream(int FD) {
85 if (FD <= 0)
86 llvm::report_fatal_error("Invalid output FD");
87 const bool CloseOnDtor = true;
88 const bool Unbuffered = false;
89 return std::unique_ptr<llvm::raw_fd_ostream>(
90 new llvm::raw_fd_ostream(FD, CloseOnDtor, Unbuffered));
91 }
92
93 } // end of anonymous namespace
94
95 BrowserCompileServer::~BrowserCompileServer() {}
96
97 void BrowserCompileServer::run() {
98 gCompileServer = this;
99 // TODO(jvoung): make a useful fatal error handler that can
100 // exit all *worker* threads, keep the server thread alive,
101 // and be able to handle a server request for the last error string.
102 getIRTInterfaces();
103 gIRTFuncs.serve_translate_request(&SubzeroCallbacks);
104 }
105
106 void BrowserCompileServer::getParsedFlags(uint32_t NumThreads, int argc,
107 char **argv) {
108 ClFlags::parseFlags(argc, argv);
109 ClFlags::getParsedClFlags(Flags);
110 ClFlags::getParsedClFlagsExtra(ExtraFlags);
111 // Set some defaults which aren't specified via the argv string.
112 Flags.setNumTranslationThreads(NumThreads);
113 Flags.setUseSandboxing(true);
114 Flags.setOutFileType(FT_Elf);
115 // TODO(jvoung): allow setting different target arches.
116 Flags.setTargetArch(Target_X8632);
117 ExtraFlags.setBuildOnRead(true);
118 ExtraFlags.setInputFileFormat(llvm::PNaClFormat);
119 }
120
121 bool BrowserCompileServer::pushInputBytes(const void *Data, size_t NumBytes) {
122 return InputStream->PutBytes(
123 const_cast<unsigned char *>(
124 reinterpret_cast<const unsigned char *>(Data)),
125 NumBytes) != NumBytes;
126 }
127
128 void BrowserCompileServer::endInputStream() { InputStream->SetDone(); }
129
130 void BrowserCompileServer::startCompileThread(int ObjFD) {
131 InputStream = new llvm::QueueStreamer();
132 LogStream = getOutputStream(STDOUT_FILENO);
133 LogStream->SetUnbuffered();
134 EmitStream = getOutputStream(ObjFD);
135 EmitStream->SetBufferSize(1 << 14);
136 ELFStream.reset(new ELFStreamer(*EmitStream.get()));
137 Ctx.reset(new GlobalContext(LogStream.get(), EmitStream.get(),
138 ELFStream.get(), Flags));
139 CompileThread = std::thread([this]() {
140 Ctx->initParserThread();
141 this->getCompiler().run(ExtraFlags, *Ctx.get(),
142 // Retain original reference, but the compiler
143 // (LLVM's MemoryObject) wants to handle deletion.
144 std::unique_ptr<llvm::DataStreamer>(InputStream));
145 });
146 }
147
148 } // end of namespace Ice
149
150 #endif // PNACL_BROWSER_TRANSLATOR
OLDNEW
« no previous file with comments | « src/IceBrowserCompileServer.h ('k') | src/IceClFlags.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698