1//===- Library.cpp - Library calls for llubi ------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file implements common libcalls for llubi.
10//
11//===----------------------------------------------------------------------===//
12
13#include "Library.h"
14#include "llvm/ADT/STLExtras.h"
15#include "llvm/Analysis/TargetLibraryInfo.h"
16#include "llvm/IR/InstrTypes.h"
17#include "llvm/Support/Format.h"
18#include "llvm/Support/raw_ostream.h"
19
20namespace llvm::ubi {
21
22static uint64_t getMaxAlign(const DataLayout &DL, unsigned AS) {
23 // Return an alignment of 16 for 64-bit platforms, and 8 for 32-bit ones.
24 return DL.getPointerABIAlignment(AS).value() >= 8 ? 16 : 8;
25}
26
27Library::Library(Context &Ctx, EventHandler &Handler, const DataLayout &DL,
28 ExecutorBase &Executor)
29 : Ctx(Ctx), Handler(Handler), DL(DL), Executor(Executor) {}
30
31std::optional<std::string> Library::readStringFromMemory(const Pointer &Ptr) {
32 std::string Result;
33 const APInt &Address = Ptr.address();
34 uint64_t Offset = 0;
35
36 while (true) {
37 auto [MO, ValidOffset] = Executor.verifyMemAccess(
38 Ptr: Ptr.getWithNewAddr(NewAddr: Address + Offset), AccessSize: 1, Alignment: Align(1), /*IsStore=*/false);
39 if (!MO)
40 return std::nullopt;
41
42 Byte B = (*MO)[ValidOffset];
43 if (B.ConcreteMask != 0xFF) {
44 Executor.reportImmediateUB()
45 << "Read uninitialized or poison memory while "
46 "parsing C-string at offset "
47 << Offset << ".";
48 return std::nullopt;
49 }
50
51 if (B.Value == 0)
52 break;
53
54 Result.push_back(c: static_cast<char>(B.Value));
55 ++Offset;
56 }
57
58 return Result;
59}
60
61AnyValue Library::executeMalloc(StringRef Name, Type *Type,
62 ArrayRef<AnyValue> Args,
63 MemAllocKind AllocKind) {
64 assert((AllocKind == MemAllocKind::Malloc || AllocKind == MemAllocKind::New ||
65 AllocKind == MemAllocKind::NewArray) &&
66 "Unexpected MemAllocKind for malloc()/new/new[]");
67
68 const auto &SizeVal = Args[0];
69
70 const uint64_t AllocSize = SizeVal.asInteger().getLimitedValue();
71 const unsigned AS = Type->getPointerAddressSpace();
72
73 const IntrusiveRefCntPtr<MemoryObject> Obj =
74 Ctx.allocate(Size: AllocSize, Align: getMaxAlign(DL, AS), Name, AS,
75 InitKind: MemInitKind::Uninitialized, AllocKind);
76
77 if (!Obj) {
78 if (AllocKind == MemAllocKind::New || AllocKind == MemAllocKind::NewArray) {
79 // FIXME: As llubi doesn't support stack unwinding yet, we report an error
80 // when new/new[] fails.
81 Executor.reportError() << "Insufficient heap space.";
82 return AnyValue::poison();
83 }
84 return AnyValue::getNullValue(Ctx, Ty: Type);
85 }
86
87 return Ctx.deriveFromMemoryObject(Obj);
88}
89
90AnyValue Library::executeCalloc(StringRef Name, Type *Type,
91 ArrayRef<AnyValue> Args,
92 MemAllocKind AllocKind) {
93 assert(AllocKind == MemAllocKind::Malloc &&
94 "Unexpected MemAllocKind for calloc()");
95
96 const auto &CountVal = Args[0];
97 const auto &SizeVal = Args[1];
98
99 const APInt &Count = CountVal.asInteger();
100 const APInt &Size = SizeVal.asInteger();
101
102 bool Overflow = false;
103 const APInt AllocSize = Count.umul_ov(RHS: Size, Overflow);
104 if (Overflow)
105 return AnyValue::getNullValue(Ctx, Ty: Type);
106 const unsigned AS = Type->getPointerAddressSpace();
107
108 const IntrusiveRefCntPtr<MemoryObject> Obj =
109 Ctx.allocate(Size: AllocSize.getLimitedValue(), Align: getMaxAlign(DL, AS), Name, AS,
110 InitKind: MemInitKind::Zeroed, AllocKind);
111
112 if (!Obj)
113 return AnyValue::getNullValue(Ctx, Ty: Type);
114
115 return Ctx.deriveFromMemoryObject(Obj);
116}
117
118AnyValue Library::executeFree(ArrayRef<AnyValue> Args, unsigned AS) {
119 const auto &PtrVal = Args[0];
120
121 auto &Ptr = PtrVal.asPointer();
122 // no-op when free is called with a null pointer.
123 if (Ptr.isNullPtr(AS, DL))
124 return AnyValue();
125
126 MemoryObject *Obj = Ctx.checkProvenance(Ptr, Check: [](const Provenance &) {
127 // TODO: check nofree
128 return true;
129 });
130 if (!Obj) {
131 Executor.reportImmediateUB()
132 << "freeing a pointer with nullary provenance.";
133 return AnyValue();
134 }
135
136 if (Ptr.address() != Obj->getAddress()) {
137 SmallString<32> AddrStr;
138 Ptr.address().toStringUnsigned(Str&: AddrStr, Radix: 16);
139 Executor.reportImmediateUB()
140 << "freeing a pointer that does not point to "
141 "the start of an allocation. Pointer address: 0x"
142 << AddrStr << ", allocation base: 0x"
143 << Twine::utohexstr(Val: Obj->getAddress()) << ".";
144 return AnyValue();
145 }
146
147 if (Obj->getState() == MemoryObjectState::Freed) {
148 Executor.reportImmediateUB()
149 << "double-freeing a memory object allocated at 0x"
150 << Twine::utohexstr(Val: Obj->getAddress()) << ".";
151 return AnyValue();
152 }
153
154 if (!Obj->isHeapAllocated()) {
155 Executor.reportImmediateUB() << "freeing a non-heap allocation at 0x"
156 << Twine::utohexstr(Val: Obj->getAddress()) << ".";
157 return AnyValue();
158 }
159
160 // Currently we don't check for cases where a memory allocated with C
161 // allocation family (malloc, calloc, etc.) is freed with a different free
162 // function comes from a different family (C++ delete, etc.)
163
164 if (!Ctx.free(Obj: *Obj)) {
165 Executor.reportImmediateUB() << "freeing an invalid pointer at 0x"
166 << Twine::utohexstr(Val: Obj->getAddress()) << ".";
167 return AnyValue::poison();
168 }
169
170 return AnyValue();
171}
172
173AnyValue Library::executePuts(ArrayRef<AnyValue> Args) {
174 const auto &PtrVal = Args[0];
175
176 const auto StrOpt = readStringFromMemory(Ptr: PtrVal.asPointer());
177 if (!StrOpt)
178 return AnyValue::poison();
179
180 Handler.onPrint(Msg: *StrOpt + "\n");
181 return AnyValue(APInt(Executor.getIntSize(), 1));
182}
183
184AnyValue Library::executePrintf(ArrayRef<AnyValue> Args) {
185 const auto &FormatPtrVal = Args[0];
186
187 const auto FormatStrOpt = readStringFromMemory(Ptr: FormatPtrVal.asPointer());
188 if (!FormatStrOpt)
189 return AnyValue::poison();
190
191 const std::string &FormatStr = *FormatStrOpt;
192 std::string Output;
193 raw_string_ostream OS(Output);
194 unsigned ArgIndex = 1; // Start from 1 since 0 is the format string.
195
196 for (unsigned I = 0; I < FormatStr.size();) {
197 if (FormatStr[I] != '%') {
198 OS << FormatStr[I++];
199 continue;
200 }
201
202 const size_t Start = I++;
203 if (I < FormatStr.size() && FormatStr[I] == '%') {
204 OS << '%';
205 ++I;
206 continue;
207 }
208
209 while (I < FormatStr.size() &&
210 StringRef("-= #0123456789").contains(C: FormatStr[I]))
211 ++I;
212
213 while (I < FormatStr.size() && StringRef("hljzt").contains(C: FormatStr[I]))
214 ++I;
215
216 if (I >= FormatStr.size()) {
217 Executor.reportImmediateUB()
218 << "Invalid format string in printf: missing conversion specifier.";
219 return AnyValue::poison();
220 }
221
222 char Specifier = FormatStr[I++];
223 std::string CleanChunk = FormatStr.substr(pos: Start, n: I - Start - 1);
224 CleanChunk.erase(
225 first: llvm::remove_if(Range&: CleanChunk,
226 P: [](char C) { return StringRef("hljzt").contains(C); }),
227 last: CleanChunk.end());
228
229 if (ArgIndex >= Args.size()) {
230 Executor.reportImmediateUB() << "Not enough arguments provided for the "
231 "format string. Required argument for '"
232 << Specifier << "'.";
233 return AnyValue::poison();
234 }
235
236 const auto &Arg = Args[ArgIndex++];
237 if (Arg.isPoison()) {
238 Executor.reportImmediateUB()
239 << "Poison argument passed to printf for format specifier '"
240 << Specifier << "' at argument index " << ArgIndex << ".";
241 return AnyValue::poison();
242 }
243
244 bool TypeMismatch =
245 (StringRef("diuoxXc").contains(C: Specifier) &&
246 !(Arg.isInteger() && (Arg.asInteger().getBitWidth() == 32 ||
247 Arg.asInteger().getBitWidth() == 64))) ||
248 (StringRef("fFeEgGaA").contains(C: Specifier) &&
249 !(Arg.isFloat() &&
250 &Arg.asFloat().getSemantics() == &APFloat::IEEEdouble())) ||
251 (StringRef("nps").contains(C: Specifier) &&
252 !(Arg.isPointer() && Arg.asPointer().address().getBitWidth() <= 64));
253
254 if (TypeMismatch) {
255 Executor.reportImmediateUB()
256 << "Argument type mismatch in printf for format specifier '"
257 << Specifier << "' at argument index " << (ArgIndex - 1) << ".";
258 return AnyValue::poison();
259 }
260
261 switch (Specifier) {
262 case 'd':
263 case 'i': {
264 std::string HostFmt = CleanChunk + "ll" + Specifier;
265 OS << format(Fmt: HostFmt.c_str(),
266 Vals: static_cast<long long>(Arg.asInteger().getSExtValue()));
267 break;
268 }
269 case 'u':
270 case 'o':
271 case 'x':
272 case 'X': {
273 // FIXME: The format specifiers "b" and "B" are not implemented here
274 // since currently MSVC doesn't support it.
275 std::string HostFmt = CleanChunk + "ll" + Specifier;
276 OS << format(Fmt: HostFmt.c_str(), Vals: static_cast<unsigned long long>(
277 Arg.asInteger().getZExtValue()));
278 break;
279 }
280 case 'c': {
281 std::string HostFmt = CleanChunk + Specifier;
282 OS << format(Fmt: HostFmt.c_str(),
283 Vals: static_cast<int>(Arg.asInteger().getZExtValue()));
284 break;
285 }
286 case 'f':
287 case 'F':
288 case 'e':
289 case 'E':
290 case 'g':
291 case 'G':
292 case 'a':
293 case 'A': {
294 std::string HostFmt = CleanChunk + Specifier;
295 OS << format(Fmt: HostFmt.c_str(), Vals: Arg.asFloat().convertToDouble());
296 break;
297 }
298 case 'n': {
299 OS.flush();
300 Executor.store(Ptr: Arg, Alignment: Align(4), Val: AnyValue(APInt(32, Output.size())),
301 ValTy: Type::getInt32Ty(C&: Ctx.getContext()));
302 break;
303 }
304 case 'p': {
305 std::string HostFmt = CleanChunk + "llx";
306 OS << "0x"
307 << format(Fmt: HostFmt.c_str(),
308 Vals: static_cast<unsigned long long>(
309 Arg.asPointer().address().getZExtValue()));
310 break;
311 }
312 case 's': {
313 auto StrOpt = readStringFromMemory(Ptr: Arg.asPointer());
314 if (!StrOpt)
315 return AnyValue::poison();
316 std::string HostFmt = CleanChunk + "s";
317 OS << format(Fmt: HostFmt.c_str(), Vals: StrOpt->c_str());
318 break;
319 }
320 default:
321 Executor.reportImmediateUB()
322 << "Unknown or unsupported format specifier '" << Specifier
323 << "' in printf.";
324 return AnyValue::poison();
325 }
326 }
327
328 OS.flush();
329 Handler.onPrint(Msg: Output);
330 return AnyValue(APInt(Executor.getIntSize(), Output.size()));
331}
332
333AnyValue Library::executeExit(ArrayRef<AnyValue> Args) {
334 const auto &RetCodeVal = Args[0];
335
336 Executor.requestProgramExit(Kind: ProgramExitInfo::ProgramExitKind::Exited,
337 ExitCode: RetCodeVal.asInteger().getZExtValue());
338 return AnyValue();
339}
340
341AnyValue Library::executeAbort() {
342 Executor.requestProgramExit(Kind: ProgramExitInfo::ProgramExitKind::Aborted);
343 return AnyValue();
344}
345
346AnyValue Library::executeTerminate() {
347 Executor.requestProgramExit(Kind: ProgramExitInfo::ProgramExitKind::Terminated);
348 return AnyValue();
349}
350
351std::optional<AnyValue> Library::executeLibcall(LibFunc LF, StringRef Name,
352 FunctionType *FuncType,
353 ArrayRef<AnyValue> Args) {
354 Type *Type = FuncType->getReturnType();
355
356 unsigned Index = 0;
357 for (const AnyValue &Arg : Args) {
358 if (Arg.isPoison()) {
359 Executor.reportImmediateUB()
360 << "Poison argument passed to a library call at argument index "
361 << Index << ".";
362 return AnyValue::poison();
363 }
364 ++Index;
365 }
366
367 switch (LF) {
368 case LibFunc_malloc:
369 return executeMalloc(Name, Type, Args, AllocKind: MemAllocKind::Malloc);
370 case LibFunc_Znwm:
371 return executeMalloc(Name, Type, Args, AllocKind: MemAllocKind::New);
372 case LibFunc_Znam:
373 return executeMalloc(Name, Type, Args, AllocKind: MemAllocKind::NewArray);
374
375 case LibFunc_calloc:
376 return executeCalloc(Name, Type, Args, AllocKind: MemAllocKind::Malloc);
377
378 case LibFunc_free:
379 case LibFunc_ZdaPv:
380 case LibFunc_ZdlPv:
381 return executeFree(Args,
382 AS: FuncType->getParamType(i: 0)->getPointerAddressSpace());
383
384 case LibFunc_puts:
385 return executePuts(Args);
386
387 case LibFunc_printf:
388 return executePrintf(Args);
389
390 case LibFunc_exit:
391 return executeExit(Args);
392
393 case LibFunc_abort:
394 return executeAbort();
395
396 case LibFunc_terminate:
397 return executeTerminate();
398
399 default:
400 return std::nullopt;
401 }
402}
403} // namespace llvm::ubi
404