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

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: sandbox it Created 5 years, 9 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
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 "IceBrowserCompileServer.h"
19
20 #include <argz.h>
21 #include <cstring>
22 #include <cstdlib>
23 #include <thread>
24
25 #include "llvm/Support/FileSystem.h"
26 #include "llvm/Support/raw_os_ostream.h"
27 #include "llvm/Support/QueueStreamer.h"
28
29 #include <irt.h>
30 #include <irt_dev.h>
31
32 namespace Ice {
33
34 // Create C wrappers around callback handlers for the IRT interface.
35 namespace {
36
37 struct nacl_irt_private_pnacl_translator_subzero gIRTTranslatorSubzero;
38 BrowserCompileServer *gCompileServer;
39
40 void getIRTInterfaces() {
41 size_t query_result = nacl_interface_query(
42 NACL_IRT_PRIVATE_PNACL_TRANSLATOR_SUBZERO_v0_1, &gIRTTranslatorSubzero,
43 sizeof(gIRTTranslatorSubzero));
44 if (query_result != sizeof(gIRTTranslatorSubzero))
45 llvm::report_fatal_error("Failed to get translator_subzero IRT interface");
46 }
47
48 int onInitCallback(uint32_t NumThreads, int ObjFileFD, const char *CmdFlags,
49 size_t CmdFlagsLen) {
50 (void)NumThreads;
51 std::vector<char *> CmdFlagList;
52 // Push some dummy arguments for the program name, input file, and
53 // output file to satisfy the argument parser and the sanity checks
54 // (e.g., never write ELF binary to stdout).
55 // We also always do binary output, so set that as the default.
56 CmdFlagList.push_back(strdup("pnacl-sz"));
57 CmdFlagList.push_back(strdup("browser_input.pexe"));
58 CmdFlagList.push_back(strdup("-o=browser_output.o"));
59 CmdFlagList.push_back(strdup("--filetype=obj"));
60 CmdFlagList.push_back(strdup("--sandbox"));
61 // TODO(jvoung): Make sure we don't send unnecessary arguments
62 // like -split-module=N.
63 IceString ThreadStrBuffer;
64 llvm::raw_string_ostream ThreadStr(ThreadStrBuffer);
65 ThreadStr << "-threads=" << NumThreads;
66 CmdFlagList.push_back(strdup(ThreadStr.str().c_str()));
67 const char *NextEntry = CmdFlags;
68 while (NextEntry != nullptr && CmdFlagsLen) {
69 // Make a copy of each null-terminated segment, since the
70 // incoming string is owned by the caller.
71 CmdFlagList.push_back(strdup(NextEntry));
72 // re: const cast -- the string pointed-to by each "NextEntry" is
73 // ultimately strdup'ed so the original won't be modified.
74 NextEntry = argz_next(const_cast<char *>(CmdFlags), CmdFlagsLen, NextEntry);
75 }
76 // Make an argv array from the input vector.
77 size_t argc = CmdFlagList.size();
78 char **argv = new char *[argc + 1];
79 for (size_t i = 0; i < argc; ++i) {
80 argv[i] = CmdFlagList[i];
81 }
82 argv[argc] = nullptr;
83 gCompileServer->takeArgs(static_cast<int>(argc), argv);
84 gCompileServer->setOutputFD(ObjFileFD);
85 gCompileServer->startCompileThread();
86 return gCompileServer->getReturnValue();
87 }
88
89 int onDataCallback(unsigned char *Data, size_t NumBytes) {
90 return gCompileServer->pushInputBytes(Data, NumBytes) ? 1 : 0;
91 }
92
93 int onEndCallback() {
94 gCompileServer->endInputStream();
95 gCompileServer->waitForCompileThread();
96 // TODO(jvoung): Also return an error string, and UMA data.
97 // Set up a report_fatal_error handler to grab that string.
98 return gCompileServer->getReturnValue();
99 }
100
101 struct PNaClSubzeroFunctions SubzeroCallbacks {
Mircea Trofin 2015/03/13 22:01:34 are we still using the subzero name?
Jim Stichnoth 2015/03/14 02:31:05 Yeah, Subzero (maybe abbreviated sz) is the "offic
jvoung (off chromium) 2015/03/18 15:39:09 Acknowledged.
102 &onInitCallback, &onDataCallback, &onEndCallback
103 };
104
105 } // end of anonymous namespace
106
107 BrowserCompileServer::~BrowserCompileServer() {
108 // strings were strdup'ed so free instead of delete.
109 for (int i = 0; i < argc; ++i)
110 free(argv[i]);
111 delete[] argv;
112 }
113
114 void BrowserCompileServer::run() {
115 gCompileServer = this;
116 getIRTInterfaces();
117 gIRTTranslatorSubzero.serve_translate_request(&SubzeroCallbacks);
118 }
119
120 std::unique_ptr<llvm::DataStreamer>
121 BrowserCompileServer::getInputStream(const IceString &InputFilename,
122 std::string &ErrorString) {
123 // There is no real filename / and there should be no error.
124 (void)InputFilename;
125 (void)ErrorString;
126 std::unique_ptr<llvm::QueueStreamer> Result(new llvm::QueueStreamer());
127 InputStream = Result.get();
128 return std::move(Result);
129 }
130
131 std::unique_ptr<llvm::raw_fd_ostream>
132 BrowserCompileServer::getOutputStream(const IceString &OutputFilename,
133 std::error_code &EC) {
134 // There is no real filename / and there should be no error
135 // since the fd is already opened.
136 (void)OutputFilename;
137 (void)EC;
138 if (OutputFD <= 0)
139 llvm::report_fatal_error("Invalid output FD");
140 const bool CloseOnDtor = true;
141 const bool Unbuffered = false;
142 std::unique_ptr<llvm::raw_fd_ostream> Result(
143 new llvm::raw_fd_ostream(OutputFD, CloseOnDtor, Unbuffered));
144 OutputFD = -1;
145 return std::move(Result);
146 }
147
148 bool BrowserCompileServer::pushInputBytes(unsigned char *Data,
149 size_t NumBytes) {
150 return InputStream->PutBytes(Data, NumBytes) != NumBytes;
151 }
152
153 void BrowserCompileServer::endInputStream() { return InputStream->SetDone(); }
154
155 void BrowserCompileServer::startCompileThread() {
156 CompileThread =
157 std::thread([this]() { this->getCompiler().run(argc, argv, *this); });
158 }
159
160 } // end of namespace Ice
161
162 #endif // PNACL_BROWSER_TRANSLATOR
OLDNEW
« no previous file with comments | « src/IceBrowserCompileServer.h ('k') | src/IceCompileServer.h » ('j') | src/IceCompiler.h » ('J')

Powered by Google App Engine
This is Rietveld 408576698