2002-12-23 23:50:16 +00:00
|
|
|
//===- SystemUtils.h - Utilities to do low-level system stuff --*- C++ -*--===//
|
2003-10-20 19:43:21 +00:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file was developed by the LLVM research group and is distributed under
|
|
|
|
// the University of Illinois Open Source License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2002-12-23 23:50:16 +00:00
|
|
|
//
|
|
|
|
// This file contains functions used to do a variety of low-level, often
|
|
|
|
// system-specific, tasks.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2004-05-28 00:59:40 +00:00
|
|
|
#define _POSIX_MAPPED_FILES
|
2003-08-07 21:28:50 +00:00
|
|
|
#include "Support/SystemUtils.h"
|
2003-06-30 21:59:07 +00:00
|
|
|
#include "Config/sys/types.h"
|
|
|
|
#include "Config/sys/stat.h"
|
|
|
|
#include "Config/fcntl.h"
|
|
|
|
#include "Config/sys/wait.h"
|
2004-05-28 00:59:40 +00:00
|
|
|
#include "Config/sys/mman.h"
|
2003-06-30 21:59:07 +00:00
|
|
|
#include "Config/unistd.h"
|
2004-01-10 19:15:14 +00:00
|
|
|
#include <algorithm>
|
|
|
|
#include <fstream>
|
|
|
|
#include <iostream>
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <cerrno>
|
2003-12-14 21:35:53 +00:00
|
|
|
using namespace llvm;
|
2003-11-11 22:41:34 +00:00
|
|
|
|
2002-12-23 23:50:16 +00:00
|
|
|
/// isExecutableFile - This function returns true if the filename specified
|
|
|
|
/// exists and is executable.
|
|
|
|
///
|
2003-12-14 21:35:53 +00:00
|
|
|
bool llvm::isExecutableFile(const std::string &ExeFileName) {
|
2002-12-23 23:50:16 +00:00
|
|
|
struct stat Buf;
|
|
|
|
if (stat(ExeFileName.c_str(), &Buf))
|
|
|
|
return false; // Must not be executable!
|
|
|
|
|
|
|
|
if (!(Buf.st_mode & S_IFREG))
|
|
|
|
return false; // Not a regular file?
|
|
|
|
|
|
|
|
if (Buf.st_uid == getuid()) // Owner of file?
|
|
|
|
return Buf.st_mode & S_IXUSR;
|
|
|
|
else if (Buf.st_gid == getgid()) // In group of file?
|
|
|
|
return Buf.st_mode & S_IXGRP;
|
|
|
|
else // Unrelated to file?
|
|
|
|
return Buf.st_mode & S_IXOTH;
|
|
|
|
}
|
|
|
|
|
2004-04-02 05:04:03 +00:00
|
|
|
/// isStandardOutAConsole - Return true if we can tell that the standard output
|
|
|
|
/// stream goes to a terminal window or console.
|
|
|
|
bool llvm::isStandardOutAConsole() {
|
2004-04-02 21:26:04 +00:00
|
|
|
#if HAVE_ISATTY
|
2004-04-02 05:04:03 +00:00
|
|
|
return isatty(1);
|
2004-04-02 21:26:04 +00:00
|
|
|
#endif
|
|
|
|
// If we don't have isatty, just return false.
|
|
|
|
return false;
|
2004-04-02 05:04:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-08-07 21:34:25 +00:00
|
|
|
/// FindExecutable - Find a named executable, giving the argv[0] of program
|
2003-09-29 22:40:07 +00:00
|
|
|
/// being executed. This allows us to find another LLVM tool if it is built
|
|
|
|
/// into the same directory, but that directory is neither the current
|
|
|
|
/// directory, nor in the PATH. If the executable cannot be found, return an
|
|
|
|
/// empty string.
|
2003-08-07 21:34:25 +00:00
|
|
|
///
|
2003-12-14 21:35:53 +00:00
|
|
|
std::string llvm::FindExecutable(const std::string &ExeName,
|
|
|
|
const std::string &ProgramPath) {
|
2002-12-23 23:50:16 +00:00
|
|
|
// First check the directory that bugpoint is in. We can do this if
|
|
|
|
// BugPointPath contains at least one / character, indicating that it is a
|
|
|
|
// relative path to bugpoint itself.
|
|
|
|
//
|
2003-08-07 21:33:33 +00:00
|
|
|
std::string Result = ProgramPath;
|
2002-12-23 23:50:16 +00:00
|
|
|
while (!Result.empty() && Result[Result.size()-1] != '/')
|
|
|
|
Result.erase(Result.size()-1, 1);
|
|
|
|
|
|
|
|
if (!Result.empty()) {
|
|
|
|
Result += ExeName;
|
|
|
|
if (isExecutableFile(Result)) return Result; // Found it?
|
|
|
|
}
|
|
|
|
|
2003-08-07 21:33:33 +00:00
|
|
|
// Okay, if the path to the program didn't tell us anything, try using the
|
|
|
|
// PATH environment variable.
|
2002-12-23 23:50:16 +00:00
|
|
|
const char *PathStr = getenv("PATH");
|
|
|
|
if (PathStr == 0) return "";
|
|
|
|
|
2003-07-14 17:20:40 +00:00
|
|
|
// Now we have a colon separated list of directories to search... try them...
|
2002-12-23 23:50:16 +00:00
|
|
|
unsigned PathLen = strlen(PathStr);
|
|
|
|
while (PathLen) {
|
|
|
|
// Find the first colon...
|
|
|
|
const char *Colon = std::find(PathStr, PathStr+PathLen, ':');
|
|
|
|
|
|
|
|
// Check to see if this first directory contains the executable...
|
|
|
|
std::string FilePath = std::string(PathStr, Colon) + '/' + ExeName;
|
|
|
|
if (isExecutableFile(FilePath))
|
|
|
|
return FilePath; // Found the executable!
|
|
|
|
|
|
|
|
// Nope it wasn't in this directory, check the next range!
|
|
|
|
PathLen -= Colon-PathStr;
|
|
|
|
PathStr = Colon;
|
|
|
|
while (*PathStr == ':') { // Advance past colons
|
|
|
|
PathStr++;
|
|
|
|
PathLen--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we fell out, we ran out of directories in PATH to search, return failure
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
static void RedirectFD(const std::string &File, int FD) {
|
|
|
|
if (File.empty()) return; // Noop
|
|
|
|
|
|
|
|
// Open the file
|
|
|
|
int InFD = open(File.c_str(), FD == 0 ? O_RDONLY : O_WRONLY|O_CREAT, 0666);
|
|
|
|
if (InFD == -1) {
|
|
|
|
std::cerr << "Error opening file '" << File << "' for "
|
2003-09-29 22:40:07 +00:00
|
|
|
<< (FD == 0 ? "input" : "output") << "!\n";
|
2002-12-23 23:50:16 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
dup2(InFD, FD); // Install it as the requested FD
|
|
|
|
close(InFD); // Close the original FD
|
|
|
|
}
|
|
|
|
|
|
|
|
/// RunProgramWithTimeout - This function executes the specified program, with
|
|
|
|
/// the specified null-terminated argument array, with the stdin/out/err fd's
|
2003-09-16 15:31:46 +00:00
|
|
|
/// redirected, with a timeout specified on the command line. This terminates
|
2002-12-23 23:50:16 +00:00
|
|
|
/// the calling program if there is an error executing the specified program.
|
|
|
|
/// It returns the return value of the program, or -1 if a timeout is detected.
|
|
|
|
///
|
2003-12-14 21:35:53 +00:00
|
|
|
int llvm::RunProgramWithTimeout(const std::string &ProgramPath,
|
|
|
|
const char **Args,
|
|
|
|
const std::string &StdInFile,
|
|
|
|
const std::string &StdOutFile,
|
|
|
|
const std::string &StdErrFile) {
|
2002-12-23 23:50:16 +00:00
|
|
|
// FIXME: install sigalarm handler here for timeout...
|
|
|
|
|
2004-05-27 01:20:55 +00:00
|
|
|
#ifdef HAVE_SYS_WAIT_H
|
2002-12-23 23:50:16 +00:00
|
|
|
int Child = fork();
|
|
|
|
switch (Child) {
|
|
|
|
case -1:
|
|
|
|
std::cerr << "ERROR forking!\n";
|
|
|
|
exit(1);
|
|
|
|
case 0: // Child
|
|
|
|
RedirectFD(StdInFile, 0); // Redirect file descriptors...
|
|
|
|
RedirectFD(StdOutFile, 1);
|
2004-04-16 05:35:58 +00:00
|
|
|
if (StdOutFile != StdErrFile)
|
|
|
|
RedirectFD(StdErrFile, 2);
|
|
|
|
else
|
|
|
|
dup2(1, 2);
|
2002-12-23 23:50:16 +00:00
|
|
|
|
|
|
|
execv(ProgramPath.c_str(), (char *const *)Args);
|
2003-10-15 20:46:58 +00:00
|
|
|
std::cerr << "Error executing program: '" << ProgramPath;
|
2002-12-23 23:50:16 +00:00
|
|
|
for (; *Args; ++Args)
|
|
|
|
std::cerr << " " << *Args;
|
2003-10-15 20:46:58 +00:00
|
|
|
std::cerr << "'\n";
|
2002-12-23 23:50:16 +00:00
|
|
|
exit(1);
|
|
|
|
|
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure all output has been written while waiting
|
|
|
|
std::cout << std::flush;
|
|
|
|
|
|
|
|
int Status;
|
|
|
|
if (wait(&Status) != Child) {
|
|
|
|
if (errno == EINTR) {
|
|
|
|
static bool FirstTimeout = true;
|
|
|
|
if (FirstTimeout) {
|
2003-09-29 22:40:07 +00:00
|
|
|
std::cout <<
|
2002-12-23 23:50:16 +00:00
|
|
|
"*** Program execution timed out! This mechanism is designed to handle\n"
|
|
|
|
" programs stuck in infinite loops gracefully. The -timeout option\n"
|
|
|
|
" can be used to change the timeout threshold or disable it completely\n"
|
|
|
|
" (with -timeout=0). This message is only displayed once.\n";
|
2003-09-29 22:40:07 +00:00
|
|
|
FirstTimeout = false;
|
2002-12-23 23:50:16 +00:00
|
|
|
}
|
|
|
|
return -1; // Timeout detected
|
|
|
|
}
|
|
|
|
|
|
|
|
std::cerr << "Error waiting for child process!\n";
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
return Status;
|
2004-05-27 01:20:55 +00:00
|
|
|
|
|
|
|
#else
|
|
|
|
std::cerr << "RunProgramWithTimeout not implemented on this platform!\n";
|
|
|
|
return -1;
|
|
|
|
#endif
|
2002-12-23 23:50:16 +00:00
|
|
|
}
|
2003-09-17 15:13:59 +00:00
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
// Function: ExecWait ()
|
|
|
|
//
|
|
|
|
// Description:
|
|
|
|
// This function executes a program with the specified arguments and
|
|
|
|
// environment. It then waits for the progarm to termiante and then returns
|
|
|
|
// to the caller.
|
|
|
|
//
|
|
|
|
// Inputs:
|
|
|
|
// argv - The arguments to the program as an array of C strings. The first
|
|
|
|
// argument should be the name of the program to execute, and the
|
|
|
|
// last argument should be a pointer to NULL.
|
|
|
|
//
|
|
|
|
// envp - The environment passes to the program as an array of C strings in
|
|
|
|
// the form of "name=value" pairs. The last element should be a
|
|
|
|
// pointer to NULL.
|
|
|
|
//
|
|
|
|
// Outputs:
|
|
|
|
// None.
|
|
|
|
//
|
|
|
|
// Return value:
|
|
|
|
// 0 - No errors.
|
|
|
|
// 1 - The program could not be executed.
|
|
|
|
// 1 - The program returned a non-zero exit status.
|
|
|
|
// 1 - The program terminated abnormally.
|
|
|
|
//
|
|
|
|
// Notes:
|
|
|
|
// The program will inherit the stdin, stdout, and stderr file descriptors
|
|
|
|
// as well as other various configuration settings (umask).
|
|
|
|
//
|
|
|
|
// This function should not print anything to stdout/stderr on its own. It is
|
|
|
|
// a generic library function. The caller or executed program should report
|
|
|
|
// errors in the way it sees fit.
|
|
|
|
//
|
2003-09-17 19:02:49 +00:00
|
|
|
// This function does not use $PATH to find programs.
|
|
|
|
//
|
2003-12-14 21:35:53 +00:00
|
|
|
int llvm::ExecWait(const char * const old_argv[],
|
|
|
|
const char * const old_envp[]) {
|
2004-05-27 01:20:55 +00:00
|
|
|
#ifdef HAVE_SYS_WAIT_H
|
2003-09-17 15:13:59 +00:00
|
|
|
//
|
2003-09-17 19:02:49 +00:00
|
|
|
// Create local versions of the parameters that can be passed into execve()
|
|
|
|
// without creating const problems.
|
2003-09-17 15:13:59 +00:00
|
|
|
//
|
2003-09-17 19:02:49 +00:00
|
|
|
char ** const argv = (char ** const) old_argv;
|
|
|
|
char ** const envp = (char ** const) old_envp;
|
2003-09-17 15:13:59 +00:00
|
|
|
|
|
|
|
// Create a child process.
|
2004-05-27 01:20:55 +00:00
|
|
|
switch (fork()) {
|
2003-09-17 19:02:49 +00:00
|
|
|
// An error occured: Return to the caller.
|
2003-09-17 15:13:59 +00:00
|
|
|
case -1:
|
|
|
|
return 1;
|
|
|
|
break;
|
|
|
|
|
2003-09-17 19:02:49 +00:00
|
|
|
// Child process: Execute the program.
|
2003-09-17 15:13:59 +00:00
|
|
|
case 0:
|
2003-09-17 19:02:49 +00:00
|
|
|
execve (argv[0], argv, envp);
|
|
|
|
// If the execve() failed, we should exit and let the parent pick up
|
|
|
|
// our non-zero exit status.
|
|
|
|
exit (1);
|
2003-09-17 15:13:59 +00:00
|
|
|
|
2003-09-17 19:02:49 +00:00
|
|
|
// Parent process: Break out of the switch to do our processing.
|
2003-09-17 15:13:59 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parent process: Wait for the child process to termiante.
|
2004-05-27 01:20:55 +00:00
|
|
|
int status;
|
2003-09-17 15:13:59 +00:00
|
|
|
if ((wait (&status)) == -1)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
// If the program exited normally with a zero exit status, return success!
|
|
|
|
if (WIFEXITED (status) && (WEXITSTATUS(status) == 0))
|
|
|
|
return 0;
|
2004-05-27 01:20:55 +00:00
|
|
|
#else
|
|
|
|
std::cerr << "llvm::ExecWait not implemented on this platform!\n";
|
|
|
|
#endif
|
2003-09-17 15:13:59 +00:00
|
|
|
|
|
|
|
// Otherwise, return failure.
|
|
|
|
return 1;
|
|
|
|
}
|
2004-05-28 00:59:40 +00:00
|
|
|
|
|
|
|
/// AllocateRWXMemory - Allocate a slab of memory with read/write/execute
|
|
|
|
/// permissions. This is typically used for JIT applications where we want
|
|
|
|
/// to emit code to the memory then jump to it. Getting this type of memory
|
|
|
|
/// is very OS specific.
|
|
|
|
///
|
|
|
|
void *llvm::AllocateRWXMemory(unsigned NumBytes) {
|
|
|
|
if (NumBytes == 0) return 0;
|
|
|
|
static const long pageSize = sysconf(_SC_PAGESIZE);
|
|
|
|
unsigned NumPages = (NumBytes+pageSize-1)/pageSize;
|
|
|
|
|
|
|
|
/* FIXME: This should use the proper autoconf flags */
|
|
|
|
#if defined(i386) || defined(__i386__) || defined(__x86__)
|
|
|
|
/* Linux and *BSD tend to have these flags named differently. */
|
|
|
|
#if defined(MAP_ANON) && !defined(MAP_ANONYMOUS)
|
|
|
|
# define MAP_ANONYMOUS MAP_ANON
|
|
|
|
#endif /* defined(MAP_ANON) && !defined(MAP_ANONYMOUS) */
|
|
|
|
#elif defined(sparc) || defined(__sparc__) || defined(__sparcv9)
|
|
|
|
/* nothing */
|
|
|
|
#else
|
|
|
|
std::cerr << "This architecture is not supported by the JIT!\n";
|
|
|
|
abort();
|
|
|
|
return 0;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAVE_MMAP
|
|
|
|
int fd = -1;
|
|
|
|
#if defined(__linux__)
|
|
|
|
fd = 0;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
unsigned mmapFlags = MAP_PRIVATE|MAP_ANONYMOUS;
|
|
|
|
#ifdef MAP_NORESERVE
|
|
|
|
mmapFlags |= MAP_NORESERVE;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
void *pa = mmap(0, pageSize*NumPages, PROT_READ|PROT_WRITE|PROT_EXEC,
|
|
|
|
mmapFlags, fd, 0);
|
|
|
|
if (pa == MAP_FAILED) {
|
|
|
|
perror("mmap");
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
return pa;
|
|
|
|
#else
|
|
|
|
std::cerr << "Do not know how to allocate mem for the JIT without mmap!\n";
|
|
|
|
abort();
|
|
|
|
return 0;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|