Skip to content

Commit

Permalink
bin2llvmir: Add new parameters: -max-memory and -max-memory-half-ram.
Browse files Browse the repository at this point in the history
They can be used to limit the maximal memory (1) to the given number of bytes
or (2) to half of system RAM.
  • Loading branch information
s3rvac committed Apr 10, 2018
1 parent a7516cb commit 349c595
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions src/bin2llvmirtool/bin2llvmir.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@
#include <llvm/Transforms/Utils/Cloning.h>

#include "retdec/llvm-support/diagnostics.h"
#include "retdec/utils/memory.h"
#include "retdec/utils/conversion.h"
#include "retdec/utils/string.h"

using namespace llvm;
Expand All @@ -69,6 +71,19 @@ static cl::opt<std::string>
OutputFilename("o", cl::desc("Output filename"),
cl::value_desc("filename"));

// Does not work with std::size_t or std::uint64_t (passing -max-memory=100
// fails with "Cannot find option named '100'!"), so we have to use unsigned
// long long, which should be 64b.
static cl::opt<unsigned long long>
MaxMemoryLimit("max-memory",
cl::desc("Limit maximal memory to the given number of bytes (0 means no limit)."),
cl::init(0));

static cl::opt<bool>
MaxMemoryLimitHalfRAM("max-memory-half-ram",
cl::desc("Limit maximal memory to half of system RAM."),
cl::init(false));

static cl::opt<bool>
NoVerify("disable-verify", cl::desc("Do not run the verifier"), cl::Hidden);

Expand Down Expand Up @@ -266,6 +281,31 @@ static inline void addPassWithoutVerification(
PM.add(P);
}

/**
* Limits the maximal memory of the tool based on the command-line parameters.
*/
void limitMaximalMemoryIfRequested()
{
if (MaxMemoryLimitHalfRAM)
{
auto limitationSucceeded = retdec::utils::limitSystemMemoryToHalfOfTotalSystemMemory();
if (!limitationSucceeded)
{
throw std::runtime_error("failed to limit maximal memory to half of system RAM");
}
}
else if (MaxMemoryLimit > 0)
{
auto limitationSucceeded = retdec::utils::limitSystemMemory(MaxMemoryLimit);
if (!limitationSucceeded)
{
throw std::runtime_error(
"failed to limit maximal memory to " + std::to_string(MaxMemoryLimit)
);
}
}
}

/**
* Call a bunch of LLVM initialization functions, same as the original opt.
*/
Expand Down Expand Up @@ -387,6 +427,8 @@ int _main(int argc, char **argv)
// Program overview.
"binary -> llvm .bc modular decompiler and optimizer\n");

limitMaximalMemoryIfRequested();

LLVMContext Context;
std::unique_ptr<Module> M = createLlvmModule(Context);

Expand Down

0 comments on commit 349c595

Please sign in to comment.