dnl===-----------------------------------------------------------------------===
AC_CHECK_FUNCS([backtrace ceilf floorf roundf rintf nearbyintf getcwd ])
-AC_CHECK_FUNCS([getpagesize getrusage gettimeofday isatty mkdtemp mkstemp ])
+AC_CHECK_FUNCS([getpagesize getrusage getrlimit setrlimit gettimeofday])
+AC_CHECK_FUNCS([isatty mkdtemp mkstemp ])
AC_CHECK_FUNCS([mktemp realpath sbrk setrlimit strdup strerror strerror_r ])
AC_CHECK_FUNCS([strtoll strtoq sysconf malloc_zone_statistics ])
AC_CHECK_FUNCS([setjmp longjmp sigsetjmp siglongjmp])
/* Define to 1 if you have the `getpagesize' function. */
#undef HAVE_GETPAGESIZE
+/* Define to 1 if you have the `getrlimit' function. */
+#undef HAVE_GETRLIMIT
+
/* Define to 1 if you have the `getrusage' function. */
#undef HAVE_GETRUSAGE
///< expires, the child is killed and this call returns. If zero,
///< this function will wait until the child finishes or forever if
///< it doesn't.
+ unsigned memoryLimit = 0, ///< If non-zero, this specifies max. amount
+ ///< of memory can be allocated by process. If memory usage will be
+ ///< higher limit, the child is killed and this call returns. If zero -
+ ///< no memory limit.
std::string* ErrMsg = 0 ///< If non-zero, provides a pointer to a string
///< instance in which error messages will be returned. If the string
///< is non-empty upon return an error occurred while invoking the
args.push_back(0);
cerr << "Running 'Graphviz' program... " << std::flush;
- if (sys::Program::ExecuteAndWait(Graphviz, &args[0],0,0,0,&ErrMsg)) {
+ if (sys::Program::ExecuteAndWait(Graphviz, &args[0],0,0,0,0,&ErrMsg)) {
cerr << "Error viewing graph: " << ErrMsg << "\n";
}
#elif (HAVE_GV && HAVE_DOT)
args.push_back(0);
cerr << "Running 'dot' program... " << std::flush;
- if (sys::Program::ExecuteAndWait(dot, &args[0],0,0,0,&ErrMsg)) {
+ if (sys::Program::ExecuteAndWait(dot, &args[0],0,0,0,0,&ErrMsg)) {
cerr << "Error viewing graph: '" << ErrMsg << "\n";
} else {
cerr << " done. \n";
args.push_back(0);
ErrMsg.clear();
- if (sys::Program::ExecuteAndWait(gv, &args[0],0,0,0,&ErrMsg)) {
+ if (sys::Program::ExecuteAndWait(gv, &args[0],0,0,0,0,&ErrMsg)) {
cerr << "Error viewing graph: " << ErrMsg << "\n";
}
}
args.push_back(0);
cerr << "Running 'dotty' program... " << std::flush;
- if (sys::Program::ExecuteAndWait(dotty, &args[0],0,0,0,&ErrMsg)) {
+ if (sys::Program::ExecuteAndWait(dotty, &args[0],0,0,0,0,&ErrMsg)) {
cerr << "Error viewing graph: " << ErrMsg << "\n";
} else {
#ifdef __MINGW32__ // Dotty spawns another app and doesn't wait until it returns
#if HAVE_SYS_STAT_H
#include <sys/stat.h>
#endif
+#if HAVE_SYS_RESOURCE_H
+#include <sys/resource.h>
+#endif
#if HAVE_SIGNAL_H
#include <signal.h>
#endif
Timeout = true;
}
+static void SetMemoryLimits (unsigned size)
+{
+#if HAVE_SYS_RESOURCE_H
+ struct rlimit r;
+ __typeof__ (r.rlim_cur) limit = (__typeof__ (r.rlim_cur)) (size) * 1048576;
+
+ // Heap size
+ getrlimit (RLIMIT_DATA, &r);
+ r.rlim_cur = limit;
+ setrlimit (RLIMIT_DATA, &r);
+ // Resident set size.
+ getrlimit (RLIMIT_RSS, &r);
+ r.rlim_cur = limit;
+ setrlimit (RLIMIT_RSS, &r);
+ // Virtual memory.
+ getrlimit (RLIMIT_AS, &r);
+ r.rlim_cur = limit;
+ setrlimit (RLIMIT_AS, &r);
+#endif
+}
+
int
Program::ExecuteAndWait(const Path& path,
const char** args,
const char** envp,
const Path** redirects,
unsigned secondsToWait,
+ unsigned memoryLimit,
std::string* ErrMsg)
{
if (!path.canExecute()) {
}
}
+ // Set memory limits
+ if (memoryLimit!=0) {
+ SetMemoryLimits(memoryLimit);
+ }
+
// Execute!
if (envp != 0)
execve (path.c_str(), (char** const)args, (char**)envp);
const char** envp,
const Path** redirects,
unsigned secondsToWait,
+ unsigned memoryLimit,
std::string* ErrMsg) {
if (!path.canExecute()) {
if (ErrMsg)
}
BugDriver::BugDriver(const char *toolname, bool as_child, bool find_bugs,
- unsigned timeout)
+ unsigned timeout, unsigned memlimit)
: ToolName(toolname), ReferenceOutputFile(OutputFile),
Program(0), Interpreter(0), cbe(0), gcc(0), run_as_child(as_child),
- run_find_bugs(find_bugs), Timeout(timeout) {}
+ run_find_bugs(find_bugs), Timeout(timeout), MemoryLimit(memlimit) {}
/// ParseInputFile - Given a bytecode or assembly input filename, parse and
bool run_as_child;
bool run_find_bugs;
unsigned Timeout;
+ unsigned MemoryLimit;
// FIXME: sort out public/private distinctions...
friend class ReducePassList;
public:
BugDriver(const char *toolname, bool as_child, bool find_bugs,
- unsigned timeout);
+ unsigned timeout, unsigned memlimit);
const std::string &getToolName() const { return ToolName; }
InterpreterSel == CBE_bug)
RetVal = AI->ExecuteProgram(BytecodeFile, InputArgv, InputFile,
OutputFile, AdditionalLinkerArgs, SharedObjs,
- Timeout);
+ Timeout, MemoryLimit);
else
RetVal = AI->ExecuteProgram(BytecodeFile, InputArgv, InputFile,
OutputFile, std::vector<std::string>(),
- SharedObjs, Timeout);
+ SharedObjs, Timeout, MemoryLimit);
if (RetVal == -1) {
std::cerr << "<timeout>";
prog = sys::Program::FindProgramByName("valgrind");
else
prog = tool;
- int result = sys::Program::ExecuteAndWait(prog,args,0,0,Timeout,&ErrMsg);
+ int result = sys::Program::ExecuteAndWait(prog, args, 0, 0,
+ Timeout, MemoryLimit, &ErrMsg);
// If we are supposed to delete the bytecode file or if the passes crashed,
// remove it now. This may fail if the file was never created, but that's ok.
const sys::Path &StdInFile,
const sys::Path &StdOutFile,
const sys::Path &StdErrFile,
- unsigned NumSeconds = 0) {
+ unsigned NumSeconds = 0,
+ unsigned MemoryLimit = 0) {
const sys::Path* redirects[3];
redirects[0] = &StdInFile;
redirects[1] = &StdOutFile;
}
return
- sys::Program::ExecuteAndWait(ProgramPath, Args, 0, redirects, NumSeconds);
+ sys::Program::ExecuteAndWait(ProgramPath, Args, 0, redirects,
+ NumSeconds, MemoryLimit);
}
const std::vector<std::string> &GCCArgs,
const std::vector<std::string> &SharedLibs =
std::vector<std::string>(),
- unsigned Timeout = 0);
+ unsigned Timeout = 0,
+ unsigned MemoryLimit = 0);
};
}
const std::string &OutputFile,
const std::vector<std::string> &GCCArgs,
const std::vector<std::string> &SharedLibs,
- unsigned Timeout) {
+ unsigned Timeout,
+ unsigned MemoryLimit) {
if (!SharedLibs.empty())
throw ToolExecutionError("LLI currently does not support "
"loading shared libraries.");
);
return RunProgramWithTimeout(sys::Path(LLIPath), &LLIArgs[0],
sys::Path(InputFile), sys::Path(OutputFile), sys::Path(OutputFile),
- Timeout);
+ Timeout, MemoryLimit);
}
// LLI create method - Try to find the LLI executable
const std::string &OutputFile,
const std::vector<std::string> &ArgsForGCC,
const std::vector<std::string> &SharedLibs,
- unsigned Timeout) {
+ unsigned Timeout,
+ unsigned MemoryLimit) {
sys::Path OutputAsmFile;
OutputCode(Bytecode, OutputAsmFile);
// Assuming LLC worked, compile the result with GCC and run it.
return gcc->ExecuteProgram(OutputAsmFile.toString(), Args, GCC::AsmFile,
- InputFile, OutputFile, GCCArgs, Timeout);
+ InputFile, OutputFile, GCCArgs,
+ Timeout, MemoryLimit);
}
/// createLLC - Try to find the LLC executable
std::vector<std::string>(),
const std::vector<std::string> &SharedLibs =
std::vector<std::string>(),
- unsigned Timeout =0 );
+ unsigned Timeout =0,
+ unsigned MemoryLimit =0);
};
}
const std::string &OutputFile,
const std::vector<std::string> &GCCArgs,
const std::vector<std::string> &SharedLibs,
- unsigned Timeout) {
+ unsigned Timeout,
+ unsigned MemoryLimit) {
if (!GCCArgs.empty())
throw ToolExecutionError("JIT does not support GCC Arguments.");
// Construct a vector of parameters, incorporating those from the command-line
DEBUG(std::cerr << "\nSending output to " << OutputFile << "\n");
return RunProgramWithTimeout(sys::Path(LLIPath), &JITArgs[0],
sys::Path(InputFile), sys::Path(OutputFile), sys::Path(OutputFile),
- Timeout);
+ Timeout, MemoryLimit);
}
/// createJIT - Try to find the LLI executable
const std::string &OutputFile,
const std::vector<std::string> &ArgsForGCC,
const std::vector<std::string> &SharedLibs,
- unsigned Timeout) {
+ unsigned Timeout,
+ unsigned MemoryLimit) {
sys::Path OutputCFile;
OutputCode(Bytecode, OutputCFile);
std::vector<std::string> GCCArgs(ArgsForGCC);
GCCArgs.insert(GCCArgs.end(),SharedLibs.begin(),SharedLibs.end());
return gcc->ExecuteProgram(OutputCFile.toString(), Args, GCC::CFile,
- InputFile, OutputFile, GCCArgs, Timeout);
+ InputFile, OutputFile, GCCArgs,
+ Timeout, MemoryLimit);
}
/// createCBE - Try to find the 'llc' executable
const std::string &InputFile,
const std::string &OutputFile,
const std::vector<std::string> &ArgsForGCC,
- unsigned Timeout ) {
+ unsigned Timeout,
+ unsigned MemoryLimit) {
std::vector<const char*> GCCArgs;
GCCArgs.push_back(GCCPath.c_str());
FileRemover OutputBinaryRemover(OutputBinary);
return RunProgramWithTimeout(OutputBinary, &ProgramArgs[0],
sys::Path(InputFile), sys::Path(OutputFile), sys::Path(OutputFile),
- Timeout);
+ Timeout, MemoryLimit);
}
int GCC::MakeSharedObject(const std::string &InputFile, FileType fileType,
const std::string &OutputFile,
const std::vector<std::string> &GCCArgs =
std::vector<std::string>(),
- unsigned Timeout = 0);
+ unsigned Timeout = 0,
+ unsigned MemoryLimit = 0);
/// MakeSharedObject - This compiles the specified file (which is either a .c
/// file or a .s file) into a shared object.
std::vector<std::string>(),
const std::vector<std::string> &SharedLibs =
std::vector<std::string>(),
- unsigned Timeout = 0) = 0;
+ unsigned Timeout = 0,
+ unsigned MemoryLimit = 0) = 0;
};
//===---------------------------------------------------------------------===//
std::vector<std::string>(),
const std::vector<std::string> &SharedLibs =
std::vector<std::string>(),
- unsigned Timeout = 0);
+ unsigned Timeout = 0,
+ unsigned MemoryLimit = 0);
/// OutputCode - Compile the specified program from bytecode to code
/// understood by the GCC driver (either C or asm). If the code generator
std::vector<std::string>(),
const std::vector<std::string> &SharedLibs =
std::vector<std::string>(),
- unsigned Timeout = 0);
+ unsigned Timeout = 0,
+ unsigned MemoryLimit = 0);
virtual GCC::FileType OutputCode(const std::string &Bytecode,
sys::Path &OutFile);
cl::desc("Number of seconds program is allowed to run before it "
"is killed (default is 300s), 0 disables timeout"));
+static cl::opt<unsigned>
+MemoryLimit("mlimit", cl::init(100), cl::value_desc("MBytes"),
+ cl::desc("Maximum amount of memory to use. 0 disables check."));
+
// The AnalysesList is automatically populated with registered Passes by the
// PassNameParser.
//
sys::PrintStackTraceOnErrorSignal();
sys::SetInterruptFunction(BugpointInterruptFunction);
- BugDriver D(argv[0],AsChild,FindBugs,TimeoutValue);
+ BugDriver D(argv[0], AsChild, FindBugs, TimeoutValue, MemoryLimit);
if (D.addSources(InputFilenames)) return 1;
D.addPasses(PassList.begin(), PassList.end());
args.push_back(InputFilename.c_str());
args.push_back(0);
- return sys::Program::ExecuteAndWait(llc,&args[0],0,0,0,&ErrMsg);
+ return sys::Program::ExecuteAndWait(llc, &args[0], 0, 0, 0, 0, &ErrMsg);
}
/// GenerateCFile - generates a C source file from the specified bytecode file.
args.push_back(OutputFile.c_str());
args.push_back(InputFile.c_str());
args.push_back(0);
- return sys::Program::ExecuteAndWait(llc, &args[0],0,0,0,&ErrMsg);
+ return sys::Program::ExecuteAndWait(llc, &args[0], 0, 0, 0, 0, &ErrMsg);
}
/// GenerateNative - generates a native object file from the
// Run the compiler to assembly and link together the program.
int R = sys::Program::ExecuteAndWait(
- gcc, &args[0], (const char**)clean_env,0,0,&ErrMsg);
+ gcc, &args[0], (const char**)clean_env, 0, 0, 0, &ErrMsg);
delete [] clean_env;
return R;
}
args[1] = RealBytecodeOutput.c_str();
args[2] = tmp_output.c_str();
args[3] = 0;
- if (0 == sys::Program::ExecuteAndWait(prog, args, 0,0,0, &ErrMsg)) {
+ if (0 == sys::Program::ExecuteAndWait(prog, args, 0,0,0,0, &ErrMsg)) {
if (tmp_output.isBytecodeFile()) {
sys::Path target(RealBytecodeOutput);
target.eraseFromDisk();
Timer timer(action->program.toString());
timer.startTimer();
int resultCode =
- sys::Program::ExecuteAndWait(action->program, Args,0,0,0,&ErrMsg);
+ sys::Program::ExecuteAndWait(action->program, Args,0,0,0,0, &ErrMsg);
timer.stopTimer();
timer.print(timer,std::cerr);
return resultCode;
}
else
return
- sys::Program::ExecuteAndWait(action->program, Args, 0,0,0, &ErrMsg);
+ sys::Program::ExecuteAndWait(action->program, Args, 0,0,0,0, &ErrMsg);
}
return 0;
}
args.push_back(tmpAsmFilePath.c_str());
args.push_back(0);
- if (sys::Program::ExecuteAndWait(gcc, &args[0], 0, 0, 1, &ErrMsg)) {
+ if (sys::Program::ExecuteAndWait(gcc, &args[0], 0, 0, 1, 0, &ErrMsg)) {
cerr << "lto: " << ErrMsg << "\n";
return LTO_ASM_FAILURE;
}