26 #define DEBUG_TYPE "jit"
69 return unwrap(GenValRef)->IntVal.getBitWidth();
82 return unwrap(GenVal)->PointerVal;
88 return unwrap(GenVal)->FloatVal;
90 return unwrap(GenVal)->DoubleVal;
113 *OutError = strdup(Error.c_str());
125 *OutInterp =
wrap(Interp);
128 *OutError = strdup(Error.c_str());
145 *OutError = strdup(Error.c_str());
150 size_t SizeOfPassedOptions) {
152 memset(&options, 0,
sizeof(options));
155 memcpy(PassedOptions, &options,
156 std::min(
sizeof(options), SizeOfPassedOptions));
166 if (SizeOfPassedOptions >
sizeof(options)) {
168 "Refusing to use options struct that is larger than my own; assuming "
169 "LLVM library mismatch.");
178 memcpy(&options, PassedOptions, SizeOfPassedOptions);
182 std::unique_ptr<Module> Mod(
unwrap(M));
187 for (
auto &
F : *Mod) {
188 auto Attrs =
F.getAttributes();
191 "no-frame-pointer-elim",
Value);
192 F.setAttributes(Attrs);
204 std::unique_ptr<RTDyldMemoryManager>(
unwrap(options.
MCJMM)));
209 *OutError = strdup(Error.c_str());
218 reinterpret_cast<LLVMModuleRef>(MP),
227 reinterpret_cast<LLVMModuleRef>(MP),
237 reinterpret_cast<LLVMModuleRef>(MP),
247 unwrap(EE)->runStaticConstructorsDestructors(
false);
251 unwrap(EE)->runStaticConstructorsDestructors(
true);
255 unsigned ArgC,
const char *
const *ArgV,
256 const char *
const *EnvP) {
257 unwrap(EE)->finalizeObject();
259 std::vector<std::string> ArgVec(ArgV, ArgV + ArgC);
260 return unwrap(EE)->runFunctionAsMain(unwrap<Function>(F), ArgVec, EnvP);
266 unwrap(EE)->finalizeObject();
268 std::vector<GenericValue> ArgVec;
269 ArgVec.reserve(NumArgs);
270 for (
unsigned I = 0;
I != NumArgs; ++
I)
271 ArgVec.push_back(*
unwrap(Args[
I]));
274 *Result =
unwrap(EE)->runFunction(unwrap<Function>(F), ArgVec);
282 unwrap(EE)->addModule(std::unique_ptr<Module>(
unwrap(M)));
293 unwrap(EE)->removeModule(Mod);
331 unwrap(EE)->addGlobalMapping(unwrap<GlobalValue>(Global), Addr);
335 unwrap(EE)->finalizeObject();
337 return unwrap(EE)->getPointerToGlobal(unwrap<GlobalValue>(Global));
341 return unwrap(EE)->getGlobalValueAddress(Name);
345 return unwrap(EE)->getFunctionAddress(Name);
352 struct SimpleBindingMMFunctions {
361 SimpleBindingMemoryManager(
const SimpleBindingMMFunctions& Functions,
363 ~SimpleBindingMemoryManager()
override;
365 uint8_t *allocateCodeSection(uintptr_t Size,
unsigned Alignment,
369 uint8_t *allocateDataSection(uintptr_t Size,
unsigned Alignment,
370 unsigned SectionID,
StringRef SectionName,
371 bool isReadOnly)
override;
373 bool finalizeMemory(std::string *ErrMsg)
override;
376 SimpleBindingMMFunctions Functions;
380 SimpleBindingMemoryManager::SimpleBindingMemoryManager(
381 const SimpleBindingMMFunctions& Functions,
383 : Functions(Functions), Opaque(Opaque) {
384 assert(Functions.AllocateCodeSection &&
385 "No AllocateCodeSection function provided!");
386 assert(Functions.AllocateDataSection &&
387 "No AllocateDataSection function provided!");
388 assert(Functions.FinalizeMemory &&
389 "No FinalizeMemory function provided!");
390 assert(Functions.Destroy &&
391 "No Destroy function provided!");
394 SimpleBindingMemoryManager::~SimpleBindingMemoryManager() {
395 Functions.Destroy(Opaque);
398 uint8_t *SimpleBindingMemoryManager::allocateCodeSection(
399 uintptr_t Size,
unsigned Alignment,
unsigned SectionID,
401 return Functions.AllocateCodeSection(Opaque, Size, Alignment, SectionID,
402 SectionName.
str().c_str());
405 uint8_t *SimpleBindingMemoryManager::allocateDataSection(
406 uintptr_t Size,
unsigned Alignment,
unsigned SectionID,
407 StringRef SectionName,
bool isReadOnly) {
408 return Functions.AllocateDataSection(Opaque, Size, Alignment, SectionID,
409 SectionName.
str().c_str(),
413 bool SimpleBindingMemoryManager::finalizeMemory(std::string *ErrMsg) {
414 char *errMsgCString =
nullptr;
415 bool result = Functions.FinalizeMemory(Opaque, &errMsgCString);
416 assert((result || !errMsgCString) &&
417 "Did not expect an error message if FinalizeMemory succeeded");
420 *ErrMsg = errMsgCString;
435 if (!AllocateCodeSection || !AllocateDataSection || !FinalizeMemory ||
440 functions.AllocateCodeSection = AllocateCodeSection;
441 functions.AllocateDataSection = AllocateDataSection;
442 functions.FinalizeMemory = FinalizeMemory;
443 functions.Destroy = Destroy;
444 return wrap(
new SimpleBindingMemoryManager(functions, Opaque));
static unsigned getBitWidth(Type *Ty, const DataLayout &DL)
Returns the bitwidth of the given scalar or pointer type (if unknown returns 0).
LLVMBool(* LLVMMemoryManagerFinalizeMemoryCallback)(void *Opaque, char **ErrMsg)
struct LLVMOpaqueType * LLVMTypeRef
Each value in the LLVM IR has a type, an LLVMTypeRef.
LLVMGenericValueRef LLVMCreateGenericValueOfFloat(LLVMTypeRef TyRef, double N)
uint64_t getZExtValue() const
Get zero extended value.
void LLVMInitializeMCJITCompilerOptions(LLVMMCJITCompilerOptions *PassedOptions, size_t SizeOfPassedOptions)
void LLVMAddModuleProvider(LLVMExecutionEngineRef EE, LLVMModuleProviderRef MP)
Deprecated: Use LLVMAddModule instead.
LLVMMCJITMemoryManagerRef MCJMM
struct LLVMOpaqueExecutionEngine * LLVMExecutionEngineRef
unsigned EnableFastISel
EnableFastISel - This flag enables fast-path instruction selection which trades away generated code q...
A Module instance is used to store all the information related to an LLVM module. ...
2: 32-bit floating point type
void LLVMRunStaticConstructors(LLVMExecutionEngineRef EE)
void LLVMDisposeGenericValue(LLVMGenericValueRef GenVal)
std::string str() const
str - Get the contents as an std::string.
void * LLVMRecompileAndRelinkFunction(LLVMExecutionEngineRef EE, LLVMValueRef Fn)
LLVMBool LLVMCreateJITCompilerForModule(LLVMExecutionEngineRef *OutJIT, LLVMModuleRef M, unsigned OptLevel, char **OutError)
LLVMBool LLVMRemoveModuleProvider(LLVMExecutionEngineRef EE, LLVMModuleProviderRef MP, LLVMModuleRef *OutMod, char **OutError)
Deprecated: Use LLVMRemoveModule instead.
LLVMBool LLVMCreateJITCompiler(LLVMExecutionEngineRef *OutJIT, LLVMModuleProviderRef MP, unsigned OptLevel, char **OutError)
Deprecated: Use LLVMCreateJITCompilerForModule instead.
double LLVMGenericValueToFloat(LLVMTypeRef TyRef, LLVMGenericValueRef GenVal)
void * LLVMGenericValueToPointer(LLVMGenericValueRef GenVal)
struct LLVMOpaqueMCJITMemoryManager * LLVMMCJITMemoryManagerRef
#define DEFINE_SIMPLE_CONVERSION_FUNCTIONS(ty, ref)
struct LLVMOpaqueTargetMachine * LLVMTargetMachineRef
LLVMGenericValueRef LLVMCreateGenericValueOfPointer(void *P)
void LLVMAddGlobalMapping(LLVMExecutionEngineRef EE, LLVMValueRef Global, void *Addr)
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
LLVMBool LLVMCreateInterpreter(LLVMExecutionEngineRef *OutInterp, LLVMModuleProviderRef MP, char **OutError)
Deprecated: Use LLVMCreateInterpreterForModule instead.
Number of individual test Apply this number of consecutive mutations to each input exit after the first new interesting input is found the minimized corpus is saved into the first input directory Number of jobs to run If min(jobs, NumberOfCpuCores()/2)\" is used.") FUZZER_FLAG_INT(reload
LLVMTargetMachineRef wrap(const TargetMachine *P)
struct LLVMOpaqueModuleProvider * LLVMModuleProviderRef
Interface used to provide a module to JIT or interpreter.
LLVMTargetDataRef wrap(const DataLayout *P)
void(* LLVMMemoryManagerDestroyCallback)(void *Opaque)
uint64_t LLVMGetFunctionAddress(LLVMExecutionEngineRef EE, const char *Name)
EngineBuilder & setCodeModel(CodeModel::Model M)
setCodeModel - Set the CodeModel that the ExecutionEngine target data is using.
LLVMMCJITMemoryManagerRef LLVMCreateSimpleMCJITMemoryManager(void *Opaque, LLVMMemoryManagerAllocateCodeSectionCallback AllocateCodeSection, LLVMMemoryManagerAllocateDataSectionCallback AllocateDataSection, LLVMMemoryManagerFinalizeMemoryCallback FinalizeMemory, LLVMMemoryManagerDestroyCallback Destroy)
Create a simple custom MCJIT memory manager.
int LLVMRunFunctionAsMain(LLVMExecutionEngineRef EE, LLVMValueRef F, unsigned ArgC, const char *const *ArgV, const char *const *EnvP)
EngineBuilder & setEngineKind(EngineKind::Kind w)
setEngineKind - Controls whether the user wants the interpreter, the JIT, or whichever engine works...
LLVMBool NoFramePointerElim
DataLayout * unwrap(LLVMTargetDataRef P)
LLVMBool LLVMCreateExecutionEngineForModule(LLVMExecutionEngineRef *OutEE, LLVMModuleRef M, char **OutError)
int64_t getSExtValue() const
Get sign extended value.
EngineBuilder & setErrorStr(std::string *e)
setErrorStr - Set the error string to write to on error.
LLVMGenericValueRef LLVMCreateGenericValueOfInt(LLVMTypeRef Ty, unsigned long long N, LLVMBool IsSigned)
LLVMTargetDataRef LLVMGetExecutionEngineTargetData(LLVMExecutionEngineRef EE)
void LLVMDisposeMCJITMemoryManager(LLVMMCJITMemoryManagerRef MM)
void LLVMDisposeExecutionEngine(LLVMExecutionEngineRef EE)
Abstract interface for implementation execution of LLVM modules, designed to support both interpreter...
struct LLVMOpaqueValue * LLVMValueRef
Represents an individual value in LLVM IR.
void * LLVMGetPointerToGlobal(LLVMExecutionEngineRef EE, LLVMValueRef Global)
EngineBuilder & setTargetOptions(const TargetOptions &Opts)
setTargetOptions - Set the target options that the ExecutionEngine target is using.
Module.h This file contains the declarations for the Module class.
uint8_t *(* LLVMMemoryManagerAllocateDataSectionCallback)(void *Opaque, uintptr_t Size, unsigned Alignment, unsigned SectionID, const char *SectionName, LLVMBool IsReadOnly)
LLVMBool LLVMCreateInterpreterForModule(LLVMExecutionEngineRef *OutInterp, LLVMModuleRef M, char **OutError)
Class for arbitrary precision integers.
static char getTypeID(Type *Ty)
ExecutionEngine * create()
void LLVMRunStaticDestructors(LLVMExecutionEngineRef EE)
void LLVMAddModule(LLVMExecutionEngineRef EE, LLVMModuleRef M)
uint64_t LLVMGetGlobalValueAddress(LLVMExecutionEngineRef EE, const char *Name)
unsigned long long LLVMGenericValueToInt(LLVMGenericValueRef GenValRef, LLVMBool IsSigned)
LLVMTargetMachineRef LLVMGetExecutionEngineTargetMachine(LLVMExecutionEngineRef EE)
LLVMBool LLVMCreateExecutionEngine(LLVMExecutionEngineRef *OutEE, LLVMModuleProviderRef MP, char **OutError)
Deprecated: Use LLVMCreateExecutionEngineForModule instead.
LLVMGenericValueRef LLVMRunFunction(LLVMExecutionEngineRef EE, LLVMValueRef F, unsigned NumArgs, LLVMGenericValueRef *Args)
uint8_t *(* LLVMMemoryManagerAllocateCodeSectionCallback)(void *Opaque, uintptr_t Size, unsigned Alignment, unsigned SectionID, const char *SectionName)
Builder class for ExecutionEngines.
EngineBuilder & setMCJITMemoryManager(std::unique_ptr< RTDyldMemoryManager > mcjmm)
setMCJITMemoryManager - Sets the MCJIT memory manager to use.
struct LLVMOpaqueModule * LLVMModuleRef
The top-level container for all other LLVM Intermediate Representation (IR) objects.
unsigned LLVMGenericValueIntWidth(LLVMGenericValueRef GenValRef)
3: 64-bit floating point type
LLVMBool LLVMCreateMCJITCompilerForModule(LLVMExecutionEngineRef *OutJIT, LLVMModuleRef M, LLVMMCJITCompilerOptions *PassedOptions, size_t SizeOfPassedOptions, char **OutError)
Create an MCJIT execution engine for a module, with the given options.
LLVM Value Representation.
void LLVMFreeMachineCodeForFunction(LLVMExecutionEngineRef EE, LLVMValueRef F)
Primary interface to the complete machine description for the target machine.
StringRef - Represent a constant reference to a string, i.e.
struct LLVMOpaqueGenericValue * LLVMGenericValueRef
always Inliner for always_inline functions
EngineBuilder & setOptLevel(CodeGenOpt::Level l)
setOptLevel - Set the optimization level for the JIT.
struct LLVMOpaqueTargetData * LLVMTargetDataRef
LLVMBool LLVMFindFunction(LLVMExecutionEngineRef EE, const char *Name, LLVMValueRef *OutFn)
LLVMBool LLVMRemoveModule(LLVMExecutionEngineRef EE, LLVMModuleRef M, LLVMModuleRef *OutMod, char **OutError)