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

Side by Side Diff: src/IceBrowserCompileServer.cpp

Issue 1168543002: Use report_fatal_error before destroying input object on error. (Closed) Base URL: https://chromium.googlesource.com/native_client/pnacl-subzero.git@master
Patch Set: Created 5 years, 6 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/IceCfgNode.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 //===- subzero/src/IceBrowserCompileServer.cpp - Browser compile server ---===// 1 //===- subzero/src/IceBrowserCompileServer.cpp - Browser compile server ---===//
2 // 2 //
3 // The Subzero Code Generator 3 // The Subzero Code Generator
4 // 4 //
5 // This file is distributed under the University of Illinois Open Source 5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details. 6 // License. See LICENSE.TXT for details.
7 // 7 //
8 //===----------------------------------------------------------------------===// 8 //===----------------------------------------------------------------------===//
9 // 9 //
10 // This file defines the browser-based compile server. 10 // This file defines the browser-based compile server.
11 // 11 //
12 //===----------------------------------------------------------------------===// 12 //===----------------------------------------------------------------------===//
13 13
14 // Can only compile this with the NaCl compiler (needs irt.h, and the 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). 15 // unsandboxed LLVM build using the trusted compiler does not have irt.h).
16 #if PNACL_BROWSER_TRANSLATOR 16 #if PNACL_BROWSER_TRANSLATOR
17 17
18 #include <cstring> 18 #include <cstring>
19 #include <irt.h> 19 #include <irt.h>
20 #include <irt_dev.h> 20 #include <irt_dev.h>
21 #include <pthread.h>
21 #include <thread> 22 #include <thread>
22 23
23 #include "llvm/Support/QueueStreamer.h" 24 #include "llvm/Support/QueueStreamer.h"
24 25
25 #include "IceBrowserCompileServer.h" 26 #include "IceBrowserCompileServer.h"
26 27
27 namespace Ice { 28 namespace Ice {
28 29
29 // Create C wrappers around callback handlers for the IRT interface. 30 // Create C wrappers around callback handlers for the IRT interface.
30 namespace { 31 namespace {
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
71 return nullptr; 72 return nullptr;
72 } 73 }
73 74
74 int onDataCallback(const void *Data, size_t NumBytes) { 75 int onDataCallback(const void *Data, size_t NumBytes) {
75 return gCompileServer->pushInputBytes(Data, NumBytes) ? 1 : 0; 76 return gCompileServer->pushInputBytes(Data, NumBytes) ? 1 : 0;
76 } 77 }
77 78
78 char *onEndCallback() { 79 char *onEndCallback() {
79 gCompileServer->endInputStream(); 80 gCompileServer->endInputStream();
80 gCompileServer->waitForCompileThread(); 81 gCompileServer->waitForCompileThread();
81 // TODO(jvoung): Also return an error string, and UMA data. 82 // TODO(jvoung): Also return UMA data.
82 // Set up a report_fatal_error handler to grab that string.
83 if (gCompileServer->getErrorCode().value()) { 83 if (gCompileServer->getErrorCode().value()) {
84 const std::string Error = gCompileServer->getErrorStream().getContents(); 84 const std::string Error = gCompileServer->getErrorStream().getContents();
85 return strdup(Error.empty() ? "Some error occurred" : Error.c_str()); 85 return strdup(Error.empty() ? "Some error occurred" : Error.c_str());
86 } 86 }
87 return nullptr; 87 return nullptr;
88 } 88 }
89 89
90 struct nacl_irt_pnacl_compile_funcs SubzeroCallbacks { 90 struct nacl_irt_pnacl_compile_funcs SubzeroCallbacks {
91 &onInitCallback, &onDataCallback, &onEndCallback 91 &onInitCallback, &onDataCallback, &onEndCallback
92 }; 92 };
93 93
94 std::unique_ptr<llvm::raw_fd_ostream> getOutputStream(int FD) { 94 std::unique_ptr<llvm::raw_fd_ostream> getOutputStream(int FD) {
95 if (FD <= 0) 95 if (FD <= 0)
96 llvm::report_fatal_error("Invalid output FD"); 96 llvm::report_fatal_error("Invalid output FD");
97 const bool CloseOnDtor = true; 97 const bool CloseOnDtor = true;
98 const bool Unbuffered = false; 98 const bool Unbuffered = false;
99 return std::unique_ptr<llvm::raw_fd_ostream>( 99 return std::unique_ptr<llvm::raw_fd_ostream>(
100 new llvm::raw_fd_ostream(FD, CloseOnDtor, Unbuffered)); 100 new llvm::raw_fd_ostream(FD, CloseOnDtor, Unbuffered));
101 } 101 }
102 102
103 void fatalErrorHandler(void *UserData, const std::string &Reason,
104 bool GenCrashDialog) {
105 BrowserCompileServer *Server =
106 reinterpret_cast<BrowserCompileServer *>(UserData);
107 Server->setFatalError(Reason);
108 // Only kill the current thread instead of the whole process.
109 // We need the server thread to remain alive in order to respond with the
110 // error message.
111 // We could also try to pthread_kill all other worker threads, but
112 // pthread_kill / raising signals is not supported by NaCl.
113 // We'll have to assume that the worker/emitter threads will be well behaved
114 // after a fatal error in other threads, and either get stuck waiting
115 // on input from a previous stage, or also call report_fatal_error.
116 pthread_exit(0);
117 }
118
103 } // end of anonymous namespace 119 } // end of anonymous namespace
104 120
105 BrowserCompileServer::~BrowserCompileServer() {} 121 BrowserCompileServer::~BrowserCompileServer() {}
106 122
107 void BrowserCompileServer::run() { 123 void BrowserCompileServer::run() {
108 gCompileServer = this; 124 gCompileServer = this;
109 // TODO(jvoung): make a useful fatal error handler that can
110 // exit all *worker* threads, keep the server thread alive,
111 // and be able to handle a server request for the last error string.
112 getIRTInterfaces(); 125 getIRTInterfaces();
113 gIRTFuncs.serve_translate_request(&SubzeroCallbacks); 126 gIRTFuncs.serve_translate_request(&SubzeroCallbacks);
114 } 127 }
115 128
116 void BrowserCompileServer::getParsedFlags(uint32_t NumThreads, int argc, 129 void BrowserCompileServer::getParsedFlags(uint32_t NumThreads, int argc,
117 char **argv) { 130 char **argv) {
118 ClFlags::parseFlags(argc, argv); 131 ClFlags::parseFlags(argc, argv);
119 ClFlags::getParsedClFlags(Flags); 132 ClFlags::getParsedClFlags(Flags);
120 ClFlags::getParsedClFlagsExtra(ExtraFlags); 133 ClFlags::getParsedClFlagsExtra(ExtraFlags);
121 // Set some defaults which aren't specified via the argv string. 134 // Set some defaults which aren't specified via the argv string.
122 Flags.setNumTranslationThreads(NumThreads); 135 Flags.setNumTranslationThreads(NumThreads);
123 Flags.setUseSandboxing(true); 136 Flags.setUseSandboxing(true);
124 Flags.setOutFileType(FT_Elf); 137 Flags.setOutFileType(FT_Elf);
125 // TODO(jvoung): allow setting different target arches. 138 // TODO(jvoung): allow setting different target arches.
126 Flags.setTargetArch(Target_X8632); 139 Flags.setTargetArch(Target_X8632);
127 ExtraFlags.setBuildOnRead(true); 140 ExtraFlags.setBuildOnRead(true);
128 ExtraFlags.setInputFileFormat(llvm::PNaClFormat); 141 ExtraFlags.setInputFileFormat(llvm::PNaClFormat);
129 } 142 }
130 143
131 bool BrowserCompileServer::pushInputBytes(const void *Data, size_t NumBytes) { 144 bool BrowserCompileServer::pushInputBytes(const void *Data, size_t NumBytes) {
145 // If there was an earlier error, do not attempt to push bytes to
146 // the QueueStreamer. Otherwise the thread could become blocked.
147 if (HadError.load())
148 return true;
132 return InputStream->PutBytes( 149 return InputStream->PutBytes(
133 const_cast<unsigned char *>( 150 const_cast<unsigned char *>(
134 reinterpret_cast<const unsigned char *>(Data)), 151 reinterpret_cast<const unsigned char *>(Data)),
135 NumBytes) != NumBytes; 152 NumBytes) != NumBytes;
136 } 153 }
137 154
155 void BrowserCompileServer::setFatalError(const IceString &Reason) {
156 HadError.store(true);
157 Ctx->getStrError() << Reason;
Jim Stichnoth 2015/06/03 16:16:01 I was wondering about using OstreamLocker for mult
jvoung (off chromium) 2015/06/04 00:33:16 Hmm good point. I forgot about OstreamLocker, but
158 // Make sure that the QueueStreamer is not stuck by signaling an early end.
159 InputStream->SetDone();
160 }
161
162 ErrorCode &BrowserCompileServer::getErrorCode() {
163 if (HadError.load()) {
164 // HadError means report_fatal_error is called. Make sure that the
165 // LastError is not EC_None. We don't know the type of error so
166 // just pick some error category.
167 LastError.assign(EC_Translation);
168 }
169 return LastError;
170 }
171
138 void BrowserCompileServer::endInputStream() { InputStream->SetDone(); } 172 void BrowserCompileServer::endInputStream() { InputStream->SetDone(); }
139 173
140 void BrowserCompileServer::startCompileThread(int ObjFD) { 174 void BrowserCompileServer::startCompileThread(int ObjFD) {
141 InputStream = new llvm::QueueStreamer(); 175 InputStream = new llvm::QueueStreamer();
142 LogStream = getOutputStream(STDOUT_FILENO); 176 LogStream = getOutputStream(STDOUT_FILENO);
143 LogStream->SetUnbuffered(); 177 LogStream->SetUnbuffered();
144 EmitStream = getOutputStream(ObjFD); 178 EmitStream = getOutputStream(ObjFD);
145 EmitStream->SetBufferSize(1 << 14); 179 EmitStream->SetBufferSize(1 << 14);
146 std::unique_ptr<StringStream> ErrStrm(new StringStream()); 180 std::unique_ptr<StringStream> ErrStrm(new StringStream());
147 ErrorStream = std::move(ErrStrm); 181 ErrorStream = std::move(ErrStrm);
148 ELFStream.reset(new ELFStreamer(*EmitStream.get())); 182 ELFStream.reset(new ELFStreamer(*EmitStream.get()));
149 Ctx.reset(new GlobalContext(LogStream.get(), EmitStream.get(), 183 Ctx.reset(new GlobalContext(LogStream.get(), EmitStream.get(),
150 &ErrorStream->getStream(), ELFStream.get(), 184 &ErrorStream->getStream(), ELFStream.get(),
151 Flags)); 185 Flags));
152 CompileThread = std::thread([this]() { 186 CompileThread = std::thread([this]() {
187 llvm::install_fatal_error_handler(fatalErrorHandler, this);
153 Ctx->initParserThread(); 188 Ctx->initParserThread();
154 this->getCompiler().run(ExtraFlags, *Ctx.get(), 189 this->getCompiler().run(ExtraFlags, *Ctx.get(),
155 // Retain original reference, but the compiler 190 // Retain original reference, but the compiler
156 // (LLVM's MemoryObject) wants to handle deletion. 191 // (LLVM's MemoryObject) wants to handle deletion.
157 std::unique_ptr<llvm::DataStreamer>(InputStream)); 192 std::unique_ptr<llvm::DataStreamer>(InputStream));
158 }); 193 });
159 } 194 }
160 195
161 } // end of namespace Ice 196 } // end of namespace Ice
162 197
163 #endif // PNACL_BROWSER_TRANSLATOR 198 #endif // PNACL_BROWSER_TRANSLATOR
OLDNEW
« no previous file with comments | « src/IceBrowserCompileServer.h ('k') | src/IceCfgNode.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698