1 //===-- ExternalFunctions.cpp - Implement External Functions --------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file contains both code to deal with invoking "external" functions, but
11 // also contains code that implements "exported" external functions.
12 //
13 // There are currently two mechanisms for handling external functions in the
14 // Interpreter. The first is to implement lle_* wrapper functions that are
15 // specific to well-known library functions which manually translate the
16 // arguments from GenericValues and make the call. If such a wrapper does
17 // not exist, and libffi is available, then the Interpreter will attempt to
18 // invoke the function using libffi, after finding its address.
19 //
20 //===----------------------------------------------------------------------===//
21
22 #include "Interpreter.h"
23 #include "llvm/Config/config.h" // Detect libffi
24 #include "llvm/IR/DataLayout.h"
25 #include "llvm/IR/DerivedTypes.h"
26 #include "llvm/IR/Module.h"
27 #include "llvm/Support/DynamicLibrary.h"
28 #include "llvm/Support/ErrorHandling.h"
29 #include "llvm/Support/ManagedStatic.h"
30 #include "llvm/Support/Mutex.h"
31 #include "llvm/Support/UniqueLock.h"
32 #include <cmath>
33 #include <csignal>
34 #include <cstdio>
35 #include <cstring>
36 #include <map>
37
38 #ifdef HAVE_FFI_CALL
39 #ifdef HAVE_FFI_H
40 #include <ffi.h>
41 #define USE_LIBFFI
42 #elif HAVE_FFI_FFI_H
43 #include <ffi/ffi.h>
44 #define USE_LIBFFI
45 #endif
46 #endif
47
48 using namespace llvm;
49
50 static ManagedStatic<sys::Mutex> FunctionsLock;
51
52 typedef GenericValue (*ExFunc)(FunctionType *, ArrayRef<GenericValue>);
53 static ManagedStatic<std::map<const Function *, ExFunc> > ExportedFunctions;
54 static ManagedStatic<std::map<std::string, ExFunc> > FuncNames;
55
56 #ifdef USE_LIBFFI
57 typedef void (*RawFunc)();
58 static ManagedStatic<std::map<const Function *, RawFunc> > RawFunctions;
59 #endif
60
61 static Interpreter *TheInterpreter;
62
getTypeID(Type * Ty)63 static char getTypeID(Type *Ty) {
64 switch (Ty->getTypeID()) {
65 case Type::VoidTyID: return 'V';
66 case Type::IntegerTyID:
67 switch (cast<IntegerType>(Ty)->getBitWidth()) {
68 case 1: return 'o';
69 case 8: return 'B';
70 case 16: return 'S';
71 case 32: return 'I';
72 case 64: return 'L';
73 default: return 'N';
74 }
75 case Type::FloatTyID: return 'F';
76 case Type::DoubleTyID: return 'D';
77 case Type::PointerTyID: return 'P';
78 case Type::FunctionTyID:return 'M';
79 case Type::StructTyID: return 'T';
80 case Type::ArrayTyID: return 'A';
81 default: return 'U';
82 }
83 }
84
85 // Try to find address of external function given a Function object.
86 // Please note, that interpreter doesn't know how to assemble a
87 // real call in general case (this is JIT job), that's why it assumes,
88 // that all external functions has the same (and pretty "general") signature.
89 // The typical example of such functions are "lle_X_" ones.
lookupFunction(const Function * F)90 static ExFunc lookupFunction(const Function *F) {
91 // Function not found, look it up... start by figuring out what the
92 // composite function name should be.
93 std::string ExtName = "lle_";
94 FunctionType *FT = F->getFunctionType();
95 for (unsigned i = 0, e = FT->getNumContainedTypes(); i != e; ++i)
96 ExtName += getTypeID(FT->getContainedType(i));
97 ExtName += ("_" + F->getName()).str();
98
99 sys::ScopedLock Writer(*FunctionsLock);
100 ExFunc FnPtr = (*FuncNames)[ExtName];
101 if (!FnPtr)
102 FnPtr = (*FuncNames)[("lle_X_" + F->getName()).str()];
103 if (!FnPtr) // Try calling a generic function... if it exists...
104 FnPtr = (ExFunc)(intptr_t)sys::DynamicLibrary::SearchForAddressOfSymbol(
105 ("lle_X_" + F->getName()).str());
106 if (FnPtr)
107 ExportedFunctions->insert(std::make_pair(F, FnPtr)); // Cache for later
108 return FnPtr;
109 }
110
111 #ifdef USE_LIBFFI
ffiTypeFor(Type * Ty)112 static ffi_type *ffiTypeFor(Type *Ty) {
113 switch (Ty->getTypeID()) {
114 case Type::VoidTyID: return &ffi_type_void;
115 case Type::IntegerTyID:
116 switch (cast<IntegerType>(Ty)->getBitWidth()) {
117 case 8: return &ffi_type_sint8;
118 case 16: return &ffi_type_sint16;
119 case 32: return &ffi_type_sint32;
120 case 64: return &ffi_type_sint64;
121 }
122 case Type::FloatTyID: return &ffi_type_float;
123 case Type::DoubleTyID: return &ffi_type_double;
124 case Type::PointerTyID: return &ffi_type_pointer;
125 default: break;
126 }
127 // TODO: Support other types such as StructTyID, ArrayTyID, OpaqueTyID, etc.
128 report_fatal_error("Type could not be mapped for use with libffi.");
129 return NULL;
130 }
131
ffiValueFor(Type * Ty,const GenericValue & AV,void * ArgDataPtr)132 static void *ffiValueFor(Type *Ty, const GenericValue &AV,
133 void *ArgDataPtr) {
134 switch (Ty->getTypeID()) {
135 case Type::IntegerTyID:
136 switch (cast<IntegerType>(Ty)->getBitWidth()) {
137 case 8: {
138 int8_t *I8Ptr = (int8_t *) ArgDataPtr;
139 *I8Ptr = (int8_t) AV.IntVal.getZExtValue();
140 return ArgDataPtr;
141 }
142 case 16: {
143 int16_t *I16Ptr = (int16_t *) ArgDataPtr;
144 *I16Ptr = (int16_t) AV.IntVal.getZExtValue();
145 return ArgDataPtr;
146 }
147 case 32: {
148 int32_t *I32Ptr = (int32_t *) ArgDataPtr;
149 *I32Ptr = (int32_t) AV.IntVal.getZExtValue();
150 return ArgDataPtr;
151 }
152 case 64: {
153 int64_t *I64Ptr = (int64_t *) ArgDataPtr;
154 *I64Ptr = (int64_t) AV.IntVal.getZExtValue();
155 return ArgDataPtr;
156 }
157 }
158 case Type::FloatTyID: {
159 float *FloatPtr = (float *) ArgDataPtr;
160 *FloatPtr = AV.FloatVal;
161 return ArgDataPtr;
162 }
163 case Type::DoubleTyID: {
164 double *DoublePtr = (double *) ArgDataPtr;
165 *DoublePtr = AV.DoubleVal;
166 return ArgDataPtr;
167 }
168 case Type::PointerTyID: {
169 void **PtrPtr = (void **) ArgDataPtr;
170 *PtrPtr = GVTOP(AV);
171 return ArgDataPtr;
172 }
173 default: break;
174 }
175 // TODO: Support other types such as StructTyID, ArrayTyID, OpaqueTyID, etc.
176 report_fatal_error("Type value could not be mapped for use with libffi.");
177 return NULL;
178 }
179
ffiInvoke(RawFunc Fn,Function * F,ArrayRef<GenericValue> ArgVals,const DataLayout & TD,GenericValue & Result)180 static bool ffiInvoke(RawFunc Fn, Function *F, ArrayRef<GenericValue> ArgVals,
181 const DataLayout &TD, GenericValue &Result) {
182 ffi_cif cif;
183 FunctionType *FTy = F->getFunctionType();
184 const unsigned NumArgs = F->arg_size();
185
186 // TODO: We don't have type information about the remaining arguments, because
187 // this information is never passed into ExecutionEngine::runFunction().
188 if (ArgVals.size() > NumArgs && F->isVarArg()) {
189 report_fatal_error("Calling external var arg function '" + F->getName()
190 + "' is not supported by the Interpreter.");
191 }
192
193 unsigned ArgBytes = 0;
194
195 std::vector<ffi_type*> args(NumArgs);
196 for (Function::const_arg_iterator A = F->arg_begin(), E = F->arg_end();
197 A != E; ++A) {
198 const unsigned ArgNo = A->getArgNo();
199 Type *ArgTy = FTy->getParamType(ArgNo);
200 args[ArgNo] = ffiTypeFor(ArgTy);
201 ArgBytes += TD.getTypeStoreSize(ArgTy);
202 }
203
204 SmallVector<uint8_t, 128> ArgData;
205 ArgData.resize(ArgBytes);
206 uint8_t *ArgDataPtr = ArgData.data();
207 SmallVector<void*, 16> values(NumArgs);
208 for (Function::const_arg_iterator A = F->arg_begin(), E = F->arg_end();
209 A != E; ++A) {
210 const unsigned ArgNo = A->getArgNo();
211 Type *ArgTy = FTy->getParamType(ArgNo);
212 values[ArgNo] = ffiValueFor(ArgTy, ArgVals[ArgNo], ArgDataPtr);
213 ArgDataPtr += TD.getTypeStoreSize(ArgTy);
214 }
215
216 Type *RetTy = FTy->getReturnType();
217 ffi_type *rtype = ffiTypeFor(RetTy);
218
219 if (ffi_prep_cif(&cif, FFI_DEFAULT_ABI, NumArgs, rtype, &args[0]) == FFI_OK) {
220 SmallVector<uint8_t, 128> ret;
221 if (RetTy->getTypeID() != Type::VoidTyID)
222 ret.resize(TD.getTypeStoreSize(RetTy));
223 ffi_call(&cif, Fn, ret.data(), values.data());
224 switch (RetTy->getTypeID()) {
225 case Type::IntegerTyID:
226 switch (cast<IntegerType>(RetTy)->getBitWidth()) {
227 case 8: Result.IntVal = APInt(8 , *(int8_t *) ret.data()); break;
228 case 16: Result.IntVal = APInt(16, *(int16_t*) ret.data()); break;
229 case 32: Result.IntVal = APInt(32, *(int32_t*) ret.data()); break;
230 case 64: Result.IntVal = APInt(64, *(int64_t*) ret.data()); break;
231 }
232 break;
233 case Type::FloatTyID: Result.FloatVal = *(float *) ret.data(); break;
234 case Type::DoubleTyID: Result.DoubleVal = *(double*) ret.data(); break;
235 case Type::PointerTyID: Result.PointerVal = *(void **) ret.data(); break;
236 default: break;
237 }
238 return true;
239 }
240
241 return false;
242 }
243 #endif // USE_LIBFFI
244
callExternalFunction(Function * F,ArrayRef<GenericValue> ArgVals)245 GenericValue Interpreter::callExternalFunction(Function *F,
246 ArrayRef<GenericValue> ArgVals) {
247 TheInterpreter = this;
248
249 unique_lock<sys::Mutex> Guard(*FunctionsLock);
250
251 // Do a lookup to see if the function is in our cache... this should just be a
252 // deferred annotation!
253 std::map<const Function *, ExFunc>::iterator FI = ExportedFunctions->find(F);
254 if (ExFunc Fn = (FI == ExportedFunctions->end()) ? lookupFunction(F)
255 : FI->second) {
256 Guard.unlock();
257 return Fn(F->getFunctionType(), ArgVals);
258 }
259
260 #ifdef USE_LIBFFI
261 std::map<const Function *, RawFunc>::iterator RF = RawFunctions->find(F);
262 RawFunc RawFn;
263 if (RF == RawFunctions->end()) {
264 RawFn = (RawFunc)(intptr_t)
265 sys::DynamicLibrary::SearchForAddressOfSymbol(F->getName());
266 if (!RawFn)
267 RawFn = (RawFunc)(intptr_t)getPointerToGlobalIfAvailable(F);
268 if (RawFn != 0)
269 RawFunctions->insert(std::make_pair(F, RawFn)); // Cache for later
270 } else {
271 RawFn = RF->second;
272 }
273
274 Guard.unlock();
275
276 GenericValue Result;
277 if (RawFn != 0 && ffiInvoke(RawFn, F, ArgVals, getDataLayout(), Result))
278 return Result;
279 #endif // USE_LIBFFI
280
281 if (F->getName() == "__main")
282 errs() << "Tried to execute an unknown external function: "
283 << *F->getType() << " __main\n";
284 else
285 report_fatal_error("Tried to execute an unknown external function: " +
286 F->getName());
287 #ifndef USE_LIBFFI
288 errs() << "Recompiling LLVM with --enable-libffi might help.\n";
289 #endif
290 return GenericValue();
291 }
292
293
294 //===----------------------------------------------------------------------===//
295 // Functions "exported" to the running application...
296 //
297
298 // void atexit(Function*)
lle_X_atexit(FunctionType * FT,ArrayRef<GenericValue> Args)299 static GenericValue lle_X_atexit(FunctionType *FT,
300 ArrayRef<GenericValue> Args) {
301 assert(Args.size() == 1);
302 TheInterpreter->addAtExitHandler((Function*)GVTOP(Args[0]));
303 GenericValue GV;
304 GV.IntVal = 0;
305 return GV;
306 }
307
308 // void exit(int)
lle_X_exit(FunctionType * FT,ArrayRef<GenericValue> Args)309 static GenericValue lle_X_exit(FunctionType *FT, ArrayRef<GenericValue> Args) {
310 TheInterpreter->exitCalled(Args[0]);
311 return GenericValue();
312 }
313
314 // void abort(void)
lle_X_abort(FunctionType * FT,ArrayRef<GenericValue> Args)315 static GenericValue lle_X_abort(FunctionType *FT, ArrayRef<GenericValue> Args) {
316 //FIXME: should we report or raise here?
317 //report_fatal_error("Interpreted program raised SIGABRT");
318 raise (SIGABRT);
319 return GenericValue();
320 }
321
322 // int sprintf(char *, const char *, ...) - a very rough implementation to make
323 // output useful.
lle_X_sprintf(FunctionType * FT,ArrayRef<GenericValue> Args)324 static GenericValue lle_X_sprintf(FunctionType *FT,
325 ArrayRef<GenericValue> Args) {
326 char *OutputBuffer = (char *)GVTOP(Args[0]);
327 const char *FmtStr = (const char *)GVTOP(Args[1]);
328 unsigned ArgNo = 2;
329
330 // printf should return # chars printed. This is completely incorrect, but
331 // close enough for now.
332 GenericValue GV;
333 GV.IntVal = APInt(32, strlen(FmtStr));
334 while (1) {
335 switch (*FmtStr) {
336 case 0: return GV; // Null terminator...
337 default: // Normal nonspecial character
338 sprintf(OutputBuffer++, "%c", *FmtStr++);
339 break;
340 case '\\': { // Handle escape codes
341 sprintf(OutputBuffer, "%c%c", *FmtStr, *(FmtStr+1));
342 FmtStr += 2; OutputBuffer += 2;
343 break;
344 }
345 case '%': { // Handle format specifiers
346 char FmtBuf[100] = "", Buffer[1000] = "";
347 char *FB = FmtBuf;
348 *FB++ = *FmtStr++;
349 char Last = *FB++ = *FmtStr++;
350 unsigned HowLong = 0;
351 while (Last != 'c' && Last != 'd' && Last != 'i' && Last != 'u' &&
352 Last != 'o' && Last != 'x' && Last != 'X' && Last != 'e' &&
353 Last != 'E' && Last != 'g' && Last != 'G' && Last != 'f' &&
354 Last != 'p' && Last != 's' && Last != '%') {
355 if (Last == 'l' || Last == 'L') HowLong++; // Keep track of l's
356 Last = *FB++ = *FmtStr++;
357 }
358 *FB = 0;
359
360 switch (Last) {
361 case '%':
362 memcpy(Buffer, "%", 2); break;
363 case 'c':
364 sprintf(Buffer, FmtBuf, uint32_t(Args[ArgNo++].IntVal.getZExtValue()));
365 break;
366 case 'd': case 'i':
367 case 'u': case 'o':
368 case 'x': case 'X':
369 if (HowLong >= 1) {
370 if (HowLong == 1 &&
371 TheInterpreter->getDataLayout().getPointerSizeInBits() == 64 &&
372 sizeof(long) < sizeof(int64_t)) {
373 // Make sure we use %lld with a 64 bit argument because we might be
374 // compiling LLI on a 32 bit compiler.
375 unsigned Size = strlen(FmtBuf);
376 FmtBuf[Size] = FmtBuf[Size-1];
377 FmtBuf[Size+1] = 0;
378 FmtBuf[Size-1] = 'l';
379 }
380 sprintf(Buffer, FmtBuf, Args[ArgNo++].IntVal.getZExtValue());
381 } else
382 sprintf(Buffer, FmtBuf,uint32_t(Args[ArgNo++].IntVal.getZExtValue()));
383 break;
384 case 'e': case 'E': case 'g': case 'G': case 'f':
385 sprintf(Buffer, FmtBuf, Args[ArgNo++].DoubleVal); break;
386 case 'p':
387 sprintf(Buffer, FmtBuf, (void*)GVTOP(Args[ArgNo++])); break;
388 case 's':
389 sprintf(Buffer, FmtBuf, (char*)GVTOP(Args[ArgNo++])); break;
390 default:
391 errs() << "<unknown printf code '" << *FmtStr << "'!>";
392 ArgNo++; break;
393 }
394 size_t Len = strlen(Buffer);
395 memcpy(OutputBuffer, Buffer, Len + 1);
396 OutputBuffer += Len;
397 }
398 break;
399 }
400 }
401 return GV;
402 }
403
404 // int printf(const char *, ...) - a very rough implementation to make output
405 // useful.
lle_X_printf(FunctionType * FT,ArrayRef<GenericValue> Args)406 static GenericValue lle_X_printf(FunctionType *FT,
407 ArrayRef<GenericValue> Args) {
408 char Buffer[10000];
409 std::vector<GenericValue> NewArgs;
410 NewArgs.push_back(PTOGV((void*)&Buffer[0]));
411 NewArgs.insert(NewArgs.end(), Args.begin(), Args.end());
412 GenericValue GV = lle_X_sprintf(FT, NewArgs);
413 outs() << Buffer;
414 return GV;
415 }
416
417 // int sscanf(const char *format, ...);
lle_X_sscanf(FunctionType * FT,ArrayRef<GenericValue> args)418 static GenericValue lle_X_sscanf(FunctionType *FT,
419 ArrayRef<GenericValue> args) {
420 assert(args.size() < 10 && "Only handle up to 10 args to sscanf right now!");
421
422 char *Args[10];
423 for (unsigned i = 0; i < args.size(); ++i)
424 Args[i] = (char*)GVTOP(args[i]);
425
426 GenericValue GV;
427 GV.IntVal = APInt(32, sscanf(Args[0], Args[1], Args[2], Args[3], Args[4],
428 Args[5], Args[6], Args[7], Args[8], Args[9]));
429 return GV;
430 }
431
432 // int scanf(const char *format, ...);
lle_X_scanf(FunctionType * FT,ArrayRef<GenericValue> args)433 static GenericValue lle_X_scanf(FunctionType *FT, ArrayRef<GenericValue> args) {
434 assert(args.size() < 10 && "Only handle up to 10 args to scanf right now!");
435
436 char *Args[10];
437 for (unsigned i = 0; i < args.size(); ++i)
438 Args[i] = (char*)GVTOP(args[i]);
439
440 GenericValue GV;
441 GV.IntVal = APInt(32, scanf( Args[0], Args[1], Args[2], Args[3], Args[4],
442 Args[5], Args[6], Args[7], Args[8], Args[9]));
443 return GV;
444 }
445
446 // int fprintf(FILE *, const char *, ...) - a very rough implementation to make
447 // output useful.
lle_X_fprintf(FunctionType * FT,ArrayRef<GenericValue> Args)448 static GenericValue lle_X_fprintf(FunctionType *FT,
449 ArrayRef<GenericValue> Args) {
450 assert(Args.size() >= 2);
451 char Buffer[10000];
452 std::vector<GenericValue> NewArgs;
453 NewArgs.push_back(PTOGV(Buffer));
454 NewArgs.insert(NewArgs.end(), Args.begin()+1, Args.end());
455 GenericValue GV = lle_X_sprintf(FT, NewArgs);
456
457 fputs(Buffer, (FILE *) GVTOP(Args[0]));
458 return GV;
459 }
460
lle_X_memset(FunctionType * FT,ArrayRef<GenericValue> Args)461 static GenericValue lle_X_memset(FunctionType *FT,
462 ArrayRef<GenericValue> Args) {
463 int val = (int)Args[1].IntVal.getSExtValue();
464 size_t len = (size_t)Args[2].IntVal.getZExtValue();
465 memset((void *)GVTOP(Args[0]), val, len);
466 // llvm.memset.* returns void, lle_X_* returns GenericValue,
467 // so here we return GenericValue with IntVal set to zero
468 GenericValue GV;
469 GV.IntVal = 0;
470 return GV;
471 }
472
lle_X_memcpy(FunctionType * FT,ArrayRef<GenericValue> Args)473 static GenericValue lle_X_memcpy(FunctionType *FT,
474 ArrayRef<GenericValue> Args) {
475 memcpy(GVTOP(Args[0]), GVTOP(Args[1]),
476 (size_t)(Args[2].IntVal.getLimitedValue()));
477
478 // llvm.memcpy* returns void, lle_X_* returns GenericValue,
479 // so here we return GenericValue with IntVal set to zero
480 GenericValue GV;
481 GV.IntVal = 0;
482 return GV;
483 }
484
initializeExternalFunctions()485 void Interpreter::initializeExternalFunctions() {
486 sys::ScopedLock Writer(*FunctionsLock);
487 (*FuncNames)["lle_X_atexit"] = lle_X_atexit;
488 (*FuncNames)["lle_X_exit"] = lle_X_exit;
489 (*FuncNames)["lle_X_abort"] = lle_X_abort;
490
491 (*FuncNames)["lle_X_printf"] = lle_X_printf;
492 (*FuncNames)["lle_X_sprintf"] = lle_X_sprintf;
493 (*FuncNames)["lle_X_sscanf"] = lle_X_sscanf;
494 (*FuncNames)["lle_X_scanf"] = lle_X_scanf;
495 (*FuncNames)["lle_X_fprintf"] = lle_X_fprintf;
496 (*FuncNames)["lle_X_memset"] = lle_X_memset;
497 (*FuncNames)["lle_X_memcpy"] = lle_X_memcpy;
498 }
499