mirror of
https://github.com/c64scene-ar/llvm-6502.git
synced 2025-01-05 12:31:33 +00:00
Avoid leaking argv and env arrays from lli.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@99589 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
parent
449f31cb9d
commit
b19383818b
@ -221,35 +221,55 @@ const GlobalValue *ExecutionEngine::getGlobalValueAtAddress(void *Addr) {
|
||||
return I != EEState.getGlobalAddressReverseMap(locked).end() ? I->second : 0;
|
||||
}
|
||||
|
||||
// CreateArgv - Turn a vector of strings into a nice argv style array of
|
||||
// pointers to null terminated strings.
|
||||
//
|
||||
static void *CreateArgv(LLVMContext &C, ExecutionEngine *EE,
|
||||
const std::vector<std::string> &InputArgv) {
|
||||
namespace {
|
||||
class ArgvArray {
|
||||
char *Array;
|
||||
std::vector<char*> Values;
|
||||
public:
|
||||
ArgvArray() : Array(NULL) {}
|
||||
~ArgvArray() { clear(); }
|
||||
void clear() {
|
||||
delete[] Array;
|
||||
Array = NULL;
|
||||
for (size_t I = 0, E = Values.size(); I != E; ++I) {
|
||||
delete[] Values[I];
|
||||
}
|
||||
Values.clear();
|
||||
}
|
||||
/// Turn a vector of strings into a nice argv style array of pointers to null
|
||||
/// terminated strings.
|
||||
void *reset(LLVMContext &C, ExecutionEngine *EE,
|
||||
const std::vector<std::string> &InputArgv);
|
||||
};
|
||||
} // anonymous namespace
|
||||
void *ArgvArray::reset(LLVMContext &C, ExecutionEngine *EE,
|
||||
const std::vector<std::string> &InputArgv) {
|
||||
clear(); // Free the old contents.
|
||||
unsigned PtrSize = EE->getTargetData()->getPointerSize();
|
||||
char *Result = new char[(InputArgv.size()+1)*PtrSize];
|
||||
Array = new char[(InputArgv.size()+1)*PtrSize];
|
||||
|
||||
DEBUG(dbgs() << "JIT: ARGV = " << (void*)Result << "\n");
|
||||
DEBUG(dbgs() << "JIT: ARGV = " << (void*)Array << "\n");
|
||||
const Type *SBytePtr = Type::getInt8PtrTy(C);
|
||||
|
||||
for (unsigned i = 0; i != InputArgv.size(); ++i) {
|
||||
unsigned Size = InputArgv[i].size()+1;
|
||||
char *Dest = new char[Size];
|
||||
Values.push_back(Dest);
|
||||
DEBUG(dbgs() << "JIT: ARGV[" << i << "] = " << (void*)Dest << "\n");
|
||||
|
||||
std::copy(InputArgv[i].begin(), InputArgv[i].end(), Dest);
|
||||
Dest[Size-1] = 0;
|
||||
|
||||
// Endian safe: Result[i] = (PointerTy)Dest;
|
||||
EE->StoreValueToMemory(PTOGV(Dest), (GenericValue*)(Result+i*PtrSize),
|
||||
// Endian safe: Array[i] = (PointerTy)Dest;
|
||||
EE->StoreValueToMemory(PTOGV(Dest), (GenericValue*)(Array+i*PtrSize),
|
||||
SBytePtr);
|
||||
}
|
||||
|
||||
// Null terminate it
|
||||
EE->StoreValueToMemory(PTOGV(0),
|
||||
(GenericValue*)(Result+InputArgv.size()*PtrSize),
|
||||
(GenericValue*)(Array+InputArgv.size()*PtrSize),
|
||||
SBytePtr);
|
||||
return Result;
|
||||
return Array;
|
||||
}
|
||||
|
||||
|
||||
@ -353,11 +373,13 @@ int ExecutionEngine::runFunctionAsMain(Function *Fn,
|
||||
llvm_report_error("Invalid number of arguments of main() supplied");
|
||||
}
|
||||
|
||||
ArgvArray CArgv;
|
||||
ArgvArray CEnv;
|
||||
if (NumArgs) {
|
||||
GVArgs.push_back(GVArgc); // Arg #0 = argc.
|
||||
if (NumArgs > 1) {
|
||||
// Arg #1 = argv.
|
||||
GVArgs.push_back(PTOGV(CreateArgv(Fn->getContext(), this, argv)));
|
||||
GVArgs.push_back(PTOGV(CArgv.reset(Fn->getContext(), this, argv)));
|
||||
assert(!isTargetNullPtr(this, GVTOP(GVArgs[1])) &&
|
||||
"argv[0] was null after CreateArgv");
|
||||
if (NumArgs > 2) {
|
||||
@ -365,7 +387,7 @@ int ExecutionEngine::runFunctionAsMain(Function *Fn,
|
||||
for (unsigned i = 0; envp[i]; ++i)
|
||||
EnvVars.push_back(envp[i]);
|
||||
// Arg #2 = envp.
|
||||
GVArgs.push_back(PTOGV(CreateArgv(Fn->getContext(), this, EnvVars)));
|
||||
GVArgs.push_back(PTOGV(CEnv.reset(Fn->getContext(), this, EnvVars)));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user