Bug Summary

File:lib/ExecutionEngine/Interpreter/ExternalFunctions.cpp
Warning:line 452, column 25
2nd function call argument is an uninitialized value

Annotated Source Code

Press '?' to see keyboard shortcuts

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