1 | //===-- ToolRunner.cpp ----------------------------------------------------===// |
2 | // |
3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | // See https://llvm.org/LICENSE.txt for license information. |
5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | // |
9 | // This file implements the interfaces described in the ToolRunner.h file. |
10 | // |
11 | //===----------------------------------------------------------------------===// |
12 | |
13 | #include "ToolRunner.h" |
14 | #include "llvm/Config/config.h" |
15 | #include "llvm/Support/CommandLine.h" |
16 | #include "llvm/Support/Debug.h" |
17 | #include "llvm/Support/FileSystem.h" |
18 | #include "llvm/Support/FileUtilities.h" |
19 | #include "llvm/Support/Program.h" |
20 | #include "llvm/Support/raw_ostream.h" |
21 | #include <fstream> |
22 | #include <sstream> |
23 | #include <utility> |
24 | using namespace llvm; |
25 | |
26 | #define DEBUG_TYPE "toolrunner" |
27 | |
28 | namespace llvm { |
29 | cl::opt<bool> SaveTemps("save-temps" , cl::init(Val: false), |
30 | cl::desc("Save temporary files" )); |
31 | } |
32 | |
33 | namespace { |
34 | cl::opt<std::string> |
35 | RemoteClient("remote-client" , |
36 | cl::desc("Remote execution client (rsh/ssh)" )); |
37 | |
38 | cl::opt<std::string> RemoteHost("remote-host" , |
39 | cl::desc("Remote execution (rsh/ssh) host" )); |
40 | |
41 | cl::opt<std::string> RemotePort("remote-port" , |
42 | cl::desc("Remote execution (rsh/ssh) port" )); |
43 | |
44 | cl::opt<std::string> RemoteUser("remote-user" , |
45 | cl::desc("Remote execution (rsh/ssh) user id" )); |
46 | |
47 | cl::opt<std::string> |
48 | ("remote-extra-options" , |
49 | cl::desc("Remote execution (rsh/ssh) extra options" )); |
50 | } |
51 | |
52 | /// RunProgramWithTimeout - This function provides an alternate interface |
53 | /// to the sys::Program::ExecuteAndWait interface. |
54 | /// @see sys::Program::ExecuteAndWait |
55 | static int RunProgramWithTimeout(StringRef ProgramPath, |
56 | ArrayRef<StringRef> Args, StringRef StdInFile, |
57 | StringRef StdOutFile, StringRef StdErrFile, |
58 | unsigned NumSeconds = 0, |
59 | unsigned MemoryLimit = 0, |
60 | std::string *ErrMsg = nullptr) { |
61 | std::optional<StringRef> Redirects[3] = {StdInFile, StdOutFile, StdErrFile}; |
62 | return sys::ExecuteAndWait(Program: ProgramPath, Args, Env: std::nullopt, Redirects, |
63 | SecondsToWait: NumSeconds, MemoryLimit, ErrMsg); |
64 | } |
65 | |
66 | /// RunProgramRemotelyWithTimeout - This function runs the given program |
67 | /// remotely using the given remote client and the sys::Program::ExecuteAndWait. |
68 | /// Returns the remote program exit code or reports a remote client error if it |
69 | /// fails. Remote client is required to return 255 if it failed or program exit |
70 | /// code otherwise. |
71 | /// @see sys::Program::ExecuteAndWait |
72 | static int RunProgramRemotelyWithTimeout( |
73 | StringRef RemoteClientPath, ArrayRef<StringRef> Args, StringRef StdInFile, |
74 | StringRef StdOutFile, StringRef StdErrFile, unsigned NumSeconds = 0, |
75 | unsigned MemoryLimit = 0) { |
76 | std::optional<StringRef> Redirects[3] = {StdInFile, StdOutFile, StdErrFile}; |
77 | |
78 | // Run the program remotely with the remote client |
79 | int ReturnCode = sys::ExecuteAndWait(Program: RemoteClientPath, Args, Env: std::nullopt, |
80 | Redirects, SecondsToWait: NumSeconds, MemoryLimit); |
81 | |
82 | // Has the remote client fail? |
83 | if (255 == ReturnCode) { |
84 | std::ostringstream OS; |
85 | OS << "\nError running remote client:\n " ; |
86 | for (StringRef Arg : Args) |
87 | OS << " " << Arg.str(); |
88 | OS << "\n" ; |
89 | |
90 | // The error message is in the output file, let's print it out from there. |
91 | std::string StdOutFileName = StdOutFile.str(); |
92 | std::ifstream ErrorFile(StdOutFileName.c_str()); |
93 | if (ErrorFile) { |
94 | std::copy(first: std::istreambuf_iterator<char>(ErrorFile), |
95 | last: std::istreambuf_iterator<char>(), |
96 | result: std::ostreambuf_iterator<char>(OS)); |
97 | ErrorFile.close(); |
98 | } |
99 | |
100 | errs() << OS.str(); |
101 | } |
102 | |
103 | return ReturnCode; |
104 | } |
105 | |
106 | static Error ProcessFailure(StringRef ProgPath, ArrayRef<StringRef> Args, |
107 | unsigned Timeout = 0, unsigned MemoryLimit = 0) { |
108 | std::ostringstream OS; |
109 | OS << "\nError running tool:\n " ; |
110 | for (StringRef Arg : Args) |
111 | OS << " " << Arg.str(); |
112 | OS << "\n" ; |
113 | |
114 | // Rerun the compiler, capturing any error messages to print them. |
115 | SmallString<128> ErrorFilename; |
116 | std::error_code EC = sys::fs::createTemporaryFile( |
117 | Prefix: "bugpoint.program_error_messages" , Suffix: "" , ResultPath&: ErrorFilename); |
118 | if (EC) { |
119 | errs() << "Error making unique filename: " << EC.message() << "\n" ; |
120 | exit(status: 1); |
121 | } |
122 | |
123 | RunProgramWithTimeout(ProgramPath: ProgPath, Args, StdInFile: "" , StdOutFile: ErrorFilename.str(), |
124 | StdErrFile: ErrorFilename.str(), NumSeconds: Timeout, MemoryLimit); |
125 | // FIXME: check return code ? |
126 | |
127 | // Print out the error messages generated by CC if possible... |
128 | std::ifstream ErrorFile(ErrorFilename.c_str()); |
129 | if (ErrorFile) { |
130 | std::copy(first: std::istreambuf_iterator<char>(ErrorFile), |
131 | last: std::istreambuf_iterator<char>(), |
132 | result: std::ostreambuf_iterator<char>(OS)); |
133 | ErrorFile.close(); |
134 | } |
135 | |
136 | sys::fs::remove(path: ErrorFilename.c_str()); |
137 | return make_error<StringError>(Args: OS.str(), Args: inconvertibleErrorCode()); |
138 | } |
139 | |
140 | //===---------------------------------------------------------------------===// |
141 | // LLI Implementation of AbstractIntepreter interface |
142 | // |
143 | namespace { |
144 | class LLI : public AbstractInterpreter { |
145 | std::string LLIPath; // The path to the LLI executable |
146 | std::vector<std::string> ToolArgs; // Args to pass to LLI |
147 | public: |
148 | LLI(const std::string &Path, const std::vector<std::string> *Args) |
149 | : LLIPath(Path) { |
150 | ToolArgs.clear(); |
151 | if (Args) { |
152 | ToolArgs = *Args; |
153 | } |
154 | } |
155 | |
156 | Expected<int> ExecuteProgram( |
157 | const std::string &Bitcode, const std::vector<std::string> &Args, |
158 | const std::string &InputFile, const std::string &OutputFile, |
159 | const std::vector<std::string> &CCArgs, |
160 | const std::vector<std::string> &SharedLibs = std::vector<std::string>(), |
161 | unsigned Timeout = 0, unsigned MemoryLimit = 0) override; |
162 | }; |
163 | } |
164 | |
165 | Expected<int> LLI::ExecuteProgram(const std::string &Bitcode, |
166 | const std::vector<std::string> &Args, |
167 | const std::string &InputFile, |
168 | const std::string &OutputFile, |
169 | const std::vector<std::string> &CCArgs, |
170 | const std::vector<std::string> &SharedLibs, |
171 | unsigned Timeout, unsigned MemoryLimit) { |
172 | std::vector<StringRef> LLIArgs; |
173 | LLIArgs.push_back(x: LLIPath); |
174 | LLIArgs.push_back(x: "-force-interpreter=true" ); |
175 | |
176 | for (std::vector<std::string>::const_iterator i = SharedLibs.begin(), |
177 | e = SharedLibs.end(); |
178 | i != e; ++i) { |
179 | LLIArgs.push_back(x: "-load" ); |
180 | LLIArgs.push_back(x: *i); |
181 | } |
182 | |
183 | // Add any extra LLI args. |
184 | llvm::append_range(C&: LLIArgs, R&: ToolArgs); |
185 | |
186 | LLIArgs.push_back(x: Bitcode); |
187 | // Add optional parameters to the running program from Argv |
188 | llvm::append_range(C&: LLIArgs, R: Args); |
189 | |
190 | outs() << "<lli>" ; |
191 | outs().flush(); |
192 | LLVM_DEBUG(errs() << "\nAbout to run:\t" ; |
193 | for (unsigned i = 0, e = LLIArgs.size(); i != e; ++i) errs() |
194 | << " " << LLIArgs[i]; |
195 | errs() << "\n" ;); |
196 | return RunProgramWithTimeout(ProgramPath: LLIPath, Args: LLIArgs, StdInFile: InputFile, StdOutFile: OutputFile, |
197 | StdErrFile: OutputFile, NumSeconds: Timeout, MemoryLimit); |
198 | } |
199 | |
200 | void AbstractInterpreter::anchor() {} |
201 | |
202 | ErrorOr<std::string> llvm::FindProgramByName(const std::string &ExeName, |
203 | const char *Argv0, |
204 | void *MainAddr) { |
205 | // Check the directory that the calling program is in. We can do |
206 | // this if ProgramPath contains at least one / character, indicating that it |
207 | // is a relative path to the executable itself. |
208 | std::string Main = sys::fs::getMainExecutable(argv0: Argv0, MainExecAddr: MainAddr); |
209 | StringRef Result = sys::path::parent_path(path: Main); |
210 | if (ErrorOr<std::string> Path = sys::findProgramByName(Name: ExeName, Paths: Result)) |
211 | return *Path; |
212 | |
213 | // Check the user PATH. |
214 | return sys::findProgramByName(Name: ExeName); |
215 | } |
216 | |
217 | // LLI create method - Try to find the LLI executable |
218 | AbstractInterpreter * |
219 | AbstractInterpreter::createLLI(const char *Argv0, std::string &Message, |
220 | const std::vector<std::string> *ToolArgs) { |
221 | if (ErrorOr<std::string> LLIPath = |
222 | FindProgramByName(ExeName: "lli" , Argv0, MainAddr: (void *)(intptr_t)&createLLI)) { |
223 | Message = "Found lli: " + *LLIPath + "\n" ; |
224 | return new LLI(*LLIPath, ToolArgs); |
225 | } else { |
226 | Message = LLIPath.getError().message() + "\n" ; |
227 | return nullptr; |
228 | } |
229 | } |
230 | |
231 | //===---------------------------------------------------------------------===// |
232 | // Custom compiler command implementation of AbstractIntepreter interface |
233 | // |
234 | // Allows using a custom command for compiling the bitcode, thus allows, for |
235 | // example, to compile a bitcode fragment without linking or executing, then |
236 | // using a custom wrapper script to check for compiler errors. |
237 | namespace { |
238 | class CustomCompiler : public AbstractInterpreter { |
239 | std::string CompilerCommand; |
240 | std::vector<std::string> CompilerArgs; |
241 | |
242 | public: |
243 | CustomCompiler(const std::string &CompilerCmd, |
244 | std::vector<std::string> CompArgs) |
245 | : CompilerCommand(CompilerCmd), CompilerArgs(std::move(CompArgs)) {} |
246 | |
247 | Error compileProgram(const std::string &Bitcode, unsigned Timeout = 0, |
248 | unsigned MemoryLimit = 0) override; |
249 | |
250 | Expected<int> ExecuteProgram( |
251 | const std::string &Bitcode, const std::vector<std::string> &Args, |
252 | const std::string &InputFile, const std::string &OutputFile, |
253 | const std::vector<std::string> &CCArgs = std::vector<std::string>(), |
254 | const std::vector<std::string> &SharedLibs = std::vector<std::string>(), |
255 | unsigned Timeout = 0, unsigned MemoryLimit = 0) override { |
256 | return make_error<StringError>( |
257 | Args: "Execution not supported with -compile-custom" , |
258 | Args: inconvertibleErrorCode()); |
259 | } |
260 | }; |
261 | } |
262 | |
263 | Error CustomCompiler::compileProgram(const std::string &Bitcode, |
264 | unsigned Timeout, unsigned MemoryLimit) { |
265 | |
266 | std::vector<StringRef> ProgramArgs; |
267 | ProgramArgs.push_back(x: CompilerCommand); |
268 | |
269 | llvm::append_range(C&: ProgramArgs, R&: CompilerArgs); |
270 | ProgramArgs.push_back(x: Bitcode); |
271 | |
272 | // Add optional parameters to the running program from Argv |
273 | llvm::append_range(C&: ProgramArgs, R&: CompilerArgs); |
274 | |
275 | if (RunProgramWithTimeout(ProgramPath: CompilerCommand, Args: ProgramArgs, StdInFile: "" , StdOutFile: "" , StdErrFile: "" , NumSeconds: Timeout, |
276 | MemoryLimit)) |
277 | return ProcessFailure(ProgPath: CompilerCommand, Args: ProgramArgs, Timeout, MemoryLimit); |
278 | return Error::success(); |
279 | } |
280 | |
281 | //===---------------------------------------------------------------------===// |
282 | // Custom execution command implementation of AbstractIntepreter interface |
283 | // |
284 | // Allows using a custom command for executing the bitcode, thus allows, |
285 | // for example, to invoke a cross compiler for code generation followed by |
286 | // a simulator that executes the generated binary. |
287 | namespace { |
288 | class CustomExecutor : public AbstractInterpreter { |
289 | std::string ExecutionCommand; |
290 | std::vector<std::string> ExecutorArgs; |
291 | |
292 | public: |
293 | CustomExecutor(const std::string &ExecutionCmd, |
294 | std::vector<std::string> ExecArgs) |
295 | : ExecutionCommand(ExecutionCmd), ExecutorArgs(std::move(ExecArgs)) {} |
296 | |
297 | Expected<int> ExecuteProgram( |
298 | const std::string &Bitcode, const std::vector<std::string> &Args, |
299 | const std::string &InputFile, const std::string &OutputFile, |
300 | const std::vector<std::string> &CCArgs, |
301 | const std::vector<std::string> &SharedLibs = std::vector<std::string>(), |
302 | unsigned Timeout = 0, unsigned MemoryLimit = 0) override; |
303 | }; |
304 | } |
305 | |
306 | Expected<int> CustomExecutor::ExecuteProgram( |
307 | const std::string &Bitcode, const std::vector<std::string> &Args, |
308 | const std::string &InputFile, const std::string &OutputFile, |
309 | const std::vector<std::string> &CCArgs, |
310 | const std::vector<std::string> &SharedLibs, unsigned Timeout, |
311 | unsigned MemoryLimit) { |
312 | |
313 | std::vector<StringRef> ProgramArgs; |
314 | ProgramArgs.push_back(x: ExecutionCommand); |
315 | |
316 | llvm::append_range(C&: ProgramArgs, R&: ExecutorArgs); |
317 | ProgramArgs.push_back(x: Bitcode); |
318 | |
319 | // Add optional parameters to the running program from Argv |
320 | llvm::append_range(C&: ProgramArgs, R: Args); |
321 | |
322 | return RunProgramWithTimeout(ProgramPath: ExecutionCommand, Args: ProgramArgs, StdInFile: InputFile, |
323 | StdOutFile: OutputFile, StdErrFile: OutputFile, NumSeconds: Timeout, MemoryLimit); |
324 | } |
325 | |
326 | // Tokenize the CommandLine to the command and the args to allow |
327 | // defining a full command line as the command instead of just the |
328 | // executed program. We cannot just pass the whole string after the command |
329 | // as a single argument because then the program sees only a single |
330 | // command line argument (with spaces in it: "foo bar" instead |
331 | // of "foo" and "bar"). |
332 | // |
333 | // Spaces are used as a delimiter; however repeated, leading, and trailing |
334 | // whitespace are ignored. Simple escaping is allowed via the '\' |
335 | // character, as seen below: |
336 | // |
337 | // Two consecutive '\' evaluate to a single '\'. |
338 | // A space after a '\' evaluates to a space that is not interpreted as a |
339 | // delimiter. |
340 | // Any other instances of the '\' character are removed. |
341 | // |
342 | // Example: |
343 | // '\\' -> '\' |
344 | // '\ ' -> ' ' |
345 | // 'exa\mple' -> 'example' |
346 | // |
347 | static void lexCommand(const char *Argv0, std::string &Message, |
348 | const std::string &CommandLine, std::string &CmdPath, |
349 | std::vector<std::string> &Args) { |
350 | |
351 | std::string Token; |
352 | std::string Command; |
353 | bool FoundPath = false; |
354 | |
355 | // first argument is the PATH. |
356 | // Skip repeated whitespace, leading whitespace and trailing whitespace. |
357 | for (std::size_t Pos = 0u; Pos <= CommandLine.size(); ++Pos) { |
358 | if ('\\' == CommandLine[Pos]) { |
359 | if (Pos + 1 < CommandLine.size()) |
360 | Token.push_back(c: CommandLine[++Pos]); |
361 | |
362 | continue; |
363 | } |
364 | if (' ' == CommandLine[Pos] || CommandLine.size() == Pos) { |
365 | if (Token.empty()) |
366 | continue; |
367 | |
368 | if (!FoundPath) { |
369 | Command = Token; |
370 | FoundPath = true; |
371 | Token.clear(); |
372 | continue; |
373 | } |
374 | |
375 | Args.push_back(x: Token); |
376 | Token.clear(); |
377 | continue; |
378 | } |
379 | Token.push_back(c: CommandLine[Pos]); |
380 | } |
381 | |
382 | auto Path = FindProgramByName(ExeName: Command, Argv0, MainAddr: (void *)(intptr_t)&lexCommand); |
383 | if (!Path) { |
384 | Message = std::string("Cannot find '" ) + Command + |
385 | "' in PATH: " + Path.getError().message() + "\n" ; |
386 | return; |
387 | } |
388 | CmdPath = *Path; |
389 | |
390 | Message = "Found command in: " + CmdPath + "\n" ; |
391 | } |
392 | |
393 | // Custom execution environment create method, takes the execution command |
394 | // as arguments |
395 | AbstractInterpreter *AbstractInterpreter::createCustomCompiler( |
396 | const char *Argv0, std::string &Message, |
397 | const std::string &CompileCommandLine) { |
398 | |
399 | std::string CmdPath; |
400 | std::vector<std::string> Args; |
401 | lexCommand(Argv0, Message, CommandLine: CompileCommandLine, CmdPath, Args); |
402 | if (CmdPath.empty()) |
403 | return nullptr; |
404 | |
405 | return new CustomCompiler(CmdPath, Args); |
406 | } |
407 | |
408 | // Custom execution environment create method, takes the execution command |
409 | // as arguments |
410 | AbstractInterpreter * |
411 | AbstractInterpreter::createCustomExecutor(const char *Argv0, |
412 | std::string &Message, |
413 | const std::string &ExecCommandLine) { |
414 | |
415 | std::string CmdPath; |
416 | std::vector<std::string> Args; |
417 | lexCommand(Argv0, Message, CommandLine: ExecCommandLine, CmdPath, Args); |
418 | if (CmdPath.empty()) |
419 | return nullptr; |
420 | |
421 | return new CustomExecutor(CmdPath, Args); |
422 | } |
423 | |
424 | //===----------------------------------------------------------------------===// |
425 | // LLC Implementation of AbstractIntepreter interface |
426 | // |
427 | Expected<CC::FileType> LLC::OutputCode(const std::string &Bitcode, |
428 | std::string &OutputAsmFile, |
429 | unsigned Timeout, unsigned MemoryLimit) { |
430 | const char *Suffix = (UseIntegratedAssembler ? ".llc.o" : ".llc.s" ); |
431 | |
432 | SmallString<128> UniqueFile; |
433 | std::error_code EC = |
434 | sys::fs::createUniqueFile(Model: Bitcode + "-%%%%%%%" + Suffix, ResultPath&: UniqueFile); |
435 | if (EC) { |
436 | errs() << "Error making unique filename: " << EC.message() << "\n" ; |
437 | exit(status: 1); |
438 | } |
439 | OutputAsmFile = std::string(UniqueFile); |
440 | std::vector<StringRef> LLCArgs; |
441 | LLCArgs.push_back(x: LLCPath); |
442 | |
443 | // Add any extra LLC args. |
444 | llvm::append_range(C&: LLCArgs, R&: ToolArgs); |
445 | |
446 | LLCArgs.push_back(x: "-o" ); |
447 | LLCArgs.push_back(x: OutputAsmFile); // Output to the Asm file |
448 | LLCArgs.push_back(x: Bitcode); // This is the input bitcode |
449 | |
450 | if (UseIntegratedAssembler) |
451 | LLCArgs.push_back(x: "-filetype=obj" ); |
452 | |
453 | outs() << (UseIntegratedAssembler ? "<llc-ia>" : "<llc>" ); |
454 | outs().flush(); |
455 | LLVM_DEBUG(errs() << "\nAbout to run:\t" ; |
456 | for (unsigned i = 0, e = LLCArgs.size(); i != e; ++i) errs() |
457 | << " " << LLCArgs[i]; |
458 | errs() << "\n" ;); |
459 | if (RunProgramWithTimeout(ProgramPath: LLCPath, Args: LLCArgs, StdInFile: "" , StdOutFile: "" , StdErrFile: "" , NumSeconds: Timeout, MemoryLimit)) |
460 | return ProcessFailure(ProgPath: LLCPath, Args: LLCArgs, Timeout, MemoryLimit); |
461 | return UseIntegratedAssembler ? CC::ObjectFile : CC::AsmFile; |
462 | } |
463 | |
464 | Error LLC::compileProgram(const std::string &Bitcode, unsigned Timeout, |
465 | unsigned MemoryLimit) { |
466 | std::string OutputAsmFile; |
467 | Expected<CC::FileType> Result = |
468 | OutputCode(Bitcode, OutputAsmFile, Timeout, MemoryLimit); |
469 | sys::fs::remove(path: OutputAsmFile); |
470 | if (Error E = Result.takeError()) |
471 | return E; |
472 | return Error::success(); |
473 | } |
474 | |
475 | Expected<int> LLC::ExecuteProgram(const std::string &Bitcode, |
476 | const std::vector<std::string> &Args, |
477 | const std::string &InputFile, |
478 | const std::string &OutputFile, |
479 | const std::vector<std::string> &ArgsForCC, |
480 | const std::vector<std::string> &SharedLibs, |
481 | unsigned Timeout, unsigned MemoryLimit) { |
482 | |
483 | std::string OutputAsmFile; |
484 | Expected<CC::FileType> FileKind = |
485 | OutputCode(Bitcode, OutputAsmFile, Timeout, MemoryLimit); |
486 | FileRemover OutFileRemover(OutputAsmFile, !SaveTemps); |
487 | if (Error E = FileKind.takeError()) |
488 | return std::move(E); |
489 | |
490 | std::vector<std::string> CCArgs(ArgsForCC); |
491 | llvm::append_range(C&: CCArgs, R: SharedLibs); |
492 | |
493 | // Assuming LLC worked, compile the result with CC and run it. |
494 | return cc->ExecuteProgram(ProgramFile: OutputAsmFile, Args, fileType: *FileKind, InputFile, |
495 | OutputFile, CCArgs, Timeout, MemoryLimit); |
496 | } |
497 | |
498 | /// createLLC - Try to find the LLC executable |
499 | /// |
500 | LLC *AbstractInterpreter::createLLC(const char *Argv0, std::string &Message, |
501 | const std::string &CCBinary, |
502 | const std::vector<std::string> *Args, |
503 | const std::vector<std::string> *CCArgs, |
504 | bool UseIntegratedAssembler) { |
505 | ErrorOr<std::string> LLCPath = |
506 | FindProgramByName(ExeName: "llc" , Argv0, MainAddr: (void *)(intptr_t)&createLLC); |
507 | if (!LLCPath) { |
508 | Message = LLCPath.getError().message() + "\n" ; |
509 | return nullptr; |
510 | } |
511 | |
512 | CC *cc = CC::create(Argv0, Message, CCBinary, Args: CCArgs); |
513 | if (!cc) { |
514 | errs() << Message << "\n" ; |
515 | exit(status: 1); |
516 | } |
517 | Message = "Found llc: " + *LLCPath + "\n" ; |
518 | return new LLC(*LLCPath, cc, Args, UseIntegratedAssembler); |
519 | } |
520 | |
521 | //===---------------------------------------------------------------------===// |
522 | // JIT Implementation of AbstractIntepreter interface |
523 | // |
524 | namespace { |
525 | class JIT : public AbstractInterpreter { |
526 | std::string LLIPath; // The path to the LLI executable |
527 | std::vector<std::string> ToolArgs; // Args to pass to LLI |
528 | public: |
529 | JIT(const std::string &Path, const std::vector<std::string> *Args) |
530 | : LLIPath(Path) { |
531 | ToolArgs.clear(); |
532 | if (Args) { |
533 | ToolArgs = *Args; |
534 | } |
535 | } |
536 | |
537 | Expected<int> ExecuteProgram( |
538 | const std::string &Bitcode, const std::vector<std::string> &Args, |
539 | const std::string &InputFile, const std::string &OutputFile, |
540 | const std::vector<std::string> &CCArgs = std::vector<std::string>(), |
541 | const std::vector<std::string> &SharedLibs = std::vector<std::string>(), |
542 | unsigned Timeout = 0, unsigned MemoryLimit = 0) override; |
543 | }; |
544 | } |
545 | |
546 | Expected<int> JIT::ExecuteProgram(const std::string &Bitcode, |
547 | const std::vector<std::string> &Args, |
548 | const std::string &InputFile, |
549 | const std::string &OutputFile, |
550 | const std::vector<std::string> &CCArgs, |
551 | const std::vector<std::string> &SharedLibs, |
552 | unsigned Timeout, unsigned MemoryLimit) { |
553 | // Construct a vector of parameters, incorporating those from the command-line |
554 | std::vector<StringRef> JITArgs; |
555 | JITArgs.push_back(x: LLIPath); |
556 | JITArgs.push_back(x: "-force-interpreter=false" ); |
557 | |
558 | // Add any extra LLI args. |
559 | llvm::append_range(C&: JITArgs, R&: ToolArgs); |
560 | |
561 | for (unsigned i = 0, e = SharedLibs.size(); i != e; ++i) { |
562 | JITArgs.push_back(x: "-load" ); |
563 | JITArgs.push_back(x: SharedLibs[i]); |
564 | } |
565 | JITArgs.push_back(x: Bitcode); |
566 | // Add optional parameters to the running program from Argv |
567 | llvm::append_range(C&: JITArgs, R: Args); |
568 | |
569 | outs() << "<jit>" ; |
570 | outs().flush(); |
571 | LLVM_DEBUG(errs() << "\nAbout to run:\t" ; |
572 | for (unsigned i = 0, e = JITArgs.size(); i != e; ++i) errs() |
573 | << " " << JITArgs[i]; |
574 | errs() << "\n" ;); |
575 | LLVM_DEBUG(errs() << "\nSending output to " << OutputFile << "\n" ); |
576 | return RunProgramWithTimeout(ProgramPath: LLIPath, Args: JITArgs, StdInFile: InputFile, StdOutFile: OutputFile, |
577 | StdErrFile: OutputFile, NumSeconds: Timeout, MemoryLimit); |
578 | } |
579 | |
580 | /// createJIT - Try to find the LLI executable |
581 | /// |
582 | AbstractInterpreter * |
583 | AbstractInterpreter::createJIT(const char *Argv0, std::string &Message, |
584 | const std::vector<std::string> *Args) { |
585 | if (ErrorOr<std::string> LLIPath = |
586 | FindProgramByName(ExeName: "lli" , Argv0, MainAddr: (void *)(intptr_t)&createJIT)) { |
587 | Message = "Found lli: " + *LLIPath + "\n" ; |
588 | return new JIT(*LLIPath, Args); |
589 | } else { |
590 | Message = LLIPath.getError().message() + "\n" ; |
591 | return nullptr; |
592 | } |
593 | } |
594 | |
595 | //===---------------------------------------------------------------------===// |
596 | // CC abstraction |
597 | // |
598 | |
599 | static bool IsARMArchitecture(std::vector<StringRef> Args) { |
600 | for (size_t I = 0; I < Args.size(); ++I) { |
601 | if (!Args[I].equals_insensitive(RHS: "-arch" )) |
602 | continue; |
603 | ++I; |
604 | if (I == Args.size()) |
605 | break; |
606 | if (Args[I].starts_with_insensitive(Prefix: "arm" )) |
607 | return true; |
608 | } |
609 | |
610 | return false; |
611 | } |
612 | |
613 | Expected<int> CC::ExecuteProgram(const std::string &ProgramFile, |
614 | const std::vector<std::string> &Args, |
615 | FileType fileType, |
616 | const std::string &InputFile, |
617 | const std::string &OutputFile, |
618 | const std::vector<std::string> &ArgsForCC, |
619 | unsigned Timeout, unsigned MemoryLimit) { |
620 | std::vector<StringRef> CCArgs; |
621 | |
622 | CCArgs.push_back(x: CCPath); |
623 | |
624 | if (TargetTriple.getArch() == Triple::x86) |
625 | CCArgs.push_back(x: "-m32" ); |
626 | |
627 | for (std::vector<std::string>::const_iterator I = ccArgs.begin(), |
628 | E = ccArgs.end(); |
629 | I != E; ++I) |
630 | CCArgs.push_back(x: *I); |
631 | |
632 | // Specify -x explicitly in case the extension is wonky |
633 | if (fileType != ObjectFile) { |
634 | CCArgs.push_back(x: "-x" ); |
635 | if (fileType == CFile) { |
636 | CCArgs.push_back(x: "c" ); |
637 | CCArgs.push_back(x: "-fno-strict-aliasing" ); |
638 | } else { |
639 | CCArgs.push_back(x: "assembler" ); |
640 | |
641 | // For ARM architectures we don't want this flag. bugpoint isn't |
642 | // explicitly told what architecture it is working on, so we get |
643 | // it from cc flags |
644 | if (TargetTriple.isOSDarwin() && !IsARMArchitecture(Args: CCArgs)) |
645 | CCArgs.push_back(x: "-force_cpusubtype_ALL" ); |
646 | } |
647 | } |
648 | |
649 | CCArgs.push_back(x: ProgramFile); // Specify the input filename. |
650 | |
651 | CCArgs.push_back(x: "-x" ); |
652 | CCArgs.push_back(x: "none" ); |
653 | CCArgs.push_back(x: "-o" ); |
654 | |
655 | SmallString<128> OutputBinary; |
656 | std::error_code EC = |
657 | sys::fs::createUniqueFile(Model: ProgramFile + "-%%%%%%%.cc.exe" , ResultPath&: OutputBinary); |
658 | if (EC) { |
659 | errs() << "Error making unique filename: " << EC.message() << "\n" ; |
660 | exit(status: 1); |
661 | } |
662 | CCArgs.push_back(x: OutputBinary); // Output to the right file... |
663 | |
664 | // Add any arguments intended for CC. We locate them here because this is |
665 | // most likely -L and -l options that need to come before other libraries but |
666 | // after the source. Other options won't be sensitive to placement on the |
667 | // command line, so this should be safe. |
668 | llvm::append_range(C&: CCArgs, R: ArgsForCC); |
669 | |
670 | CCArgs.push_back(x: "-lm" ); // Hard-code the math library... |
671 | CCArgs.push_back(x: "-O2" ); // Optimize the program a bit... |
672 | if (TargetTriple.getArch() == Triple::sparc) |
673 | CCArgs.push_back(x: "-mcpu=v9" ); |
674 | |
675 | outs() << "<CC>" ; |
676 | outs().flush(); |
677 | LLVM_DEBUG(errs() << "\nAbout to run:\t" ; |
678 | for (unsigned i = 0, e = CCArgs.size(); i != e; ++i) errs() |
679 | << " " << CCArgs[i]; |
680 | errs() << "\n" ;); |
681 | if (RunProgramWithTimeout(ProgramPath: CCPath, Args: CCArgs, StdInFile: "" , StdOutFile: "" , StdErrFile: "" )) |
682 | return ProcessFailure(ProgPath: CCPath, Args: CCArgs); |
683 | |
684 | std::vector<StringRef> ProgramArgs; |
685 | |
686 | // Declared here so that the destructor only runs after |
687 | // ProgramArgs is used. |
688 | std::string Exec; |
689 | |
690 | if (RemoteClientPath.empty()) |
691 | ProgramArgs.push_back(x: OutputBinary); |
692 | else { |
693 | ProgramArgs.push_back(x: RemoteClientPath); |
694 | ProgramArgs.push_back(x: RemoteHost); |
695 | if (!RemoteUser.empty()) { |
696 | ProgramArgs.push_back(x: "-l" ); |
697 | ProgramArgs.push_back(x: RemoteUser); |
698 | } |
699 | if (!RemotePort.empty()) { |
700 | ProgramArgs.push_back(x: "-p" ); |
701 | ProgramArgs.push_back(x: RemotePort); |
702 | } |
703 | if (!RemoteExtra.empty()) { |
704 | ProgramArgs.push_back(x: RemoteExtra); |
705 | } |
706 | |
707 | // Full path to the binary. We need to cd to the exec directory because |
708 | // there is a dylib there that the exec expects to find in the CWD |
709 | char *env_pwd = getenv(name: "PWD" ); |
710 | Exec = "cd " ; |
711 | Exec += env_pwd; |
712 | Exec += "; ./" ; |
713 | Exec += OutputBinary.c_str(); |
714 | ProgramArgs.push_back(x: Exec); |
715 | } |
716 | |
717 | // Add optional parameters to the running program from Argv |
718 | llvm::append_range(C&: ProgramArgs, R: Args); |
719 | |
720 | // Now that we have a binary, run it! |
721 | outs() << "<program>" ; |
722 | outs().flush(); |
723 | LLVM_DEBUG( |
724 | errs() << "\nAbout to run:\t" ; |
725 | for (unsigned i = 0, e = ProgramArgs.size(); i != e; ++i) errs() |
726 | << " " << ProgramArgs[i]; |
727 | errs() << "\n" ;); |
728 | |
729 | FileRemover OutputBinaryRemover(OutputBinary.str(), !SaveTemps); |
730 | |
731 | if (RemoteClientPath.empty()) { |
732 | LLVM_DEBUG(errs() << "<run locally>" ); |
733 | std::string Error; |
734 | int ExitCode = RunProgramWithTimeout(ProgramPath: OutputBinary.str(), Args: ProgramArgs, |
735 | StdInFile: InputFile, StdOutFile: OutputFile, StdErrFile: OutputFile, |
736 | NumSeconds: Timeout, MemoryLimit, ErrMsg: &Error); |
737 | // Treat a signal (usually SIGSEGV) or timeout as part of the program output |
738 | // so that crash-causing miscompilation is handled seamlessly. |
739 | if (ExitCode < -1) { |
740 | std::ofstream outFile(OutputFile.c_str(), std::ios_base::app); |
741 | outFile << Error << '\n'; |
742 | outFile.close(); |
743 | } |
744 | return ExitCode; |
745 | } else { |
746 | outs() << "<run remotely>" ; |
747 | outs().flush(); |
748 | return RunProgramRemotelyWithTimeout(RemoteClientPath, Args: ProgramArgs, |
749 | StdInFile: InputFile, StdOutFile: OutputFile, StdErrFile: OutputFile, |
750 | NumSeconds: Timeout, MemoryLimit); |
751 | } |
752 | } |
753 | |
754 | Error CC::MakeSharedObject(const std::string &InputFile, FileType fileType, |
755 | std::string &OutputFile, |
756 | const std::vector<std::string> &ArgsForCC) { |
757 | SmallString<128> UniqueFilename; |
758 | std::error_code EC = sys::fs::createUniqueFile( |
759 | Model: InputFile + "-%%%%%%%" + LTDL_SHLIB_EXT, ResultPath&: UniqueFilename); |
760 | if (EC) { |
761 | errs() << "Error making unique filename: " << EC.message() << "\n" ; |
762 | exit(status: 1); |
763 | } |
764 | OutputFile = std::string(UniqueFilename); |
765 | |
766 | std::vector<StringRef> CCArgs; |
767 | |
768 | CCArgs.push_back(x: CCPath); |
769 | |
770 | if (TargetTriple.getArch() == Triple::x86) |
771 | CCArgs.push_back(x: "-m32" ); |
772 | |
773 | for (std::vector<std::string>::const_iterator I = ccArgs.begin(), |
774 | E = ccArgs.end(); |
775 | I != E; ++I) |
776 | CCArgs.push_back(x: *I); |
777 | |
778 | // Compile the C/asm file into a shared object |
779 | if (fileType != ObjectFile) { |
780 | CCArgs.push_back(x: "-x" ); |
781 | CCArgs.push_back(x: fileType == AsmFile ? "assembler" : "c" ); |
782 | } |
783 | CCArgs.push_back(x: "-fno-strict-aliasing" ); |
784 | CCArgs.push_back(x: InputFile); // Specify the input filename. |
785 | CCArgs.push_back(x: "-x" ); |
786 | CCArgs.push_back(x: "none" ); |
787 | if (TargetTriple.getArch() == Triple::sparc) |
788 | CCArgs.push_back(x: "-G" ); // Compile a shared library, `-G' for Sparc |
789 | else if (TargetTriple.isOSDarwin()) { |
790 | // link all source files into a single module in data segment, rather than |
791 | // generating blocks. dynamic_lookup requires that you set |
792 | // MACOSX_DEPLOYMENT_TARGET=10.3 in your env. FIXME: it would be better for |
793 | // bugpoint to just pass that in the environment of CC. |
794 | CCArgs.push_back(x: "-single_module" ); |
795 | CCArgs.push_back(x: "-dynamiclib" ); // `-dynamiclib' for MacOS X/PowerPC |
796 | CCArgs.push_back(x: "-undefined" ); |
797 | CCArgs.push_back(x: "dynamic_lookup" ); |
798 | } else |
799 | CCArgs.push_back(x: "-shared" ); // `-shared' for Linux/X86, maybe others |
800 | |
801 | if (TargetTriple.getArch() == Triple::x86_64) |
802 | CCArgs.push_back(x: "-fPIC" ); // Requires shared objs to contain PIC |
803 | |
804 | if (TargetTriple.getArch() == Triple::sparc) |
805 | CCArgs.push_back(x: "-mcpu=v9" ); |
806 | |
807 | CCArgs.push_back(x: "-o" ); |
808 | CCArgs.push_back(x: OutputFile); // Output to the right filename. |
809 | CCArgs.push_back(x: "-O2" ); // Optimize the program a bit. |
810 | |
811 | // Add any arguments intended for CC. We locate them here because this is |
812 | // most likely -L and -l options that need to come before other libraries but |
813 | // after the source. Other options won't be sensitive to placement on the |
814 | // command line, so this should be safe. |
815 | llvm::append_range(C&: CCArgs, R: ArgsForCC); |
816 | |
817 | outs() << "<CC>" ; |
818 | outs().flush(); |
819 | LLVM_DEBUG(errs() << "\nAbout to run:\t" ; |
820 | for (unsigned i = 0, e = CCArgs.size(); i != e; ++i) errs() |
821 | << " " << CCArgs[i]; |
822 | errs() << "\n" ;); |
823 | if (RunProgramWithTimeout(ProgramPath: CCPath, Args: CCArgs, StdInFile: "" , StdOutFile: "" , StdErrFile: "" )) |
824 | return ProcessFailure(ProgPath: CCPath, Args: CCArgs); |
825 | return Error::success(); |
826 | } |
827 | |
828 | /// create - Try to find the CC executable |
829 | /// |
830 | CC *CC::create(const char *Argv0, std::string &Message, |
831 | const std::string &CCBinary, |
832 | const std::vector<std::string> *Args) { |
833 | auto CCPath = FindProgramByName(ExeName: CCBinary, Argv0, MainAddr: (void *)(intptr_t)&create); |
834 | if (!CCPath) { |
835 | Message = "Cannot find `" + CCBinary + "' in PATH: " + |
836 | CCPath.getError().message() + "\n" ; |
837 | return nullptr; |
838 | } |
839 | |
840 | std::string RemoteClientPath; |
841 | if (!RemoteClient.empty()) { |
842 | auto Path = sys::findProgramByName(Name: RemoteClient); |
843 | if (!Path) { |
844 | Message = "Cannot find `" + RemoteClient + "' in PATH: " + |
845 | Path.getError().message() + "\n" ; |
846 | return nullptr; |
847 | } |
848 | RemoteClientPath = *Path; |
849 | } |
850 | |
851 | Message = "Found CC: " + *CCPath + "\n" ; |
852 | return new CC(*CCPath, RemoteClientPath, Args); |
853 | } |
854 | |