[llvm] [llubi] Implements common library functions (PR #190147)
Nikita Popov via llvm-commits
llvm-commits at lists.llvm.org
Thu Apr 9 12:07:17 PDT 2026
================
@@ -0,0 +1,371 @@
+//===- Library.cpp - Library calls for llubi ------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+//
+// This file implements common libcalls for llubi.
+//
+//===----------------------------------------------------------------------===//
+
+#include "Library.h"
+#include "llvm/ADT/STLExtras.h"
+#include "llvm/Analysis/TargetLibraryInfo.h"
+#include "llvm/IR/InstrTypes.h"
+#include "llvm/Support/Format.h"
+#include "llvm/Support/raw_ostream.h"
+
+namespace llvm::ubi {
+
+static uint64_t getMaxAlign(const DataLayout &DL) {
+ // Return an alignment of 16 for 64-bit platforms, and 8 for 32-bit ones.
+ return DL.getPointerABIAlignment(0).value() >= 8 ? 16 : 8;
+}
+
+Library::Library(Context &Ctx, EventHandler &Handler, const DataLayout &DL,
+ ExecutorBase &Executor)
+ : Ctx(Ctx), Handler(Handler), DL(DL), Executor(Executor) {}
+
+std::optional<std::string> Library::readStringFromMemory(const Pointer &Ptr) {
+ auto *MO = Ptr.getMemoryObject();
+ if (!MO) {
+ Executor.reportImmediateUB(
+ "Invalid memory access via a pointer with nullary "
+ "provenance.");
+ return std::nullopt;
+ }
+
+ std::string Result;
+ const APInt &Address = Ptr.address();
+ uint64_t Offset = 0;
+
+ while (true) {
+ auto ValidOffset =
+ Executor.verifyMemAccess(*MO, Address + Offset, 1, Align(1), false);
+ if (!ValidOffset)
+ return std::nullopt;
+
+ Byte B = (*MO)[*ValidOffset];
+ if (B.ConcreteMask != 0xFF) {
+ Executor.reportImmediateUB("Read uninitialized or poison memory while "
+ "parsing C-string.");
+ return std::nullopt;
+ }
+
+ if (B.Value == 0)
+ break;
+
+ Result.push_back(static_cast<char>(B.Value));
+ ++Offset;
+ }
+
+ return Result;
+}
+
+AnyValue Library::executeMalloc(StringRef Name, Type *Type,
+ ArrayRef<AnyValue> Args,
+ MemAllocKind AllocKind) {
+ assert(AllocKind == MemAllocKind::Malloc || AllocKind == MemAllocKind::New ||
+ AllocKind == MemAllocKind::NewArray &&
+ "Unexpected MemAllocKind for malloc()/new/new[]");
+
+ const auto &SizeVal = Args[0];
+
+ const uint64_t AllocSize = SizeVal.asInteger().getZExtValue();
+
+ const IntrusiveRefCntPtr<MemoryObject> Obj =
+ Ctx.allocate(AllocSize, getMaxAlign(DL), Name, 0,
+ MemInitKind::Uninitialized, AllocKind);
+
+ if (!Obj) {
+ if (AllocKind == MemAllocKind::New || AllocKind == MemAllocKind::NewArray) {
+ // FIXME: As llubi doesn't support stack unwinding yet, we report an error
+ // when new/new[] fails.
+ Executor.reportError("Insufficient heap space.");
+ return AnyValue::poison();
+ }
+ return AnyValue::getNullValue(Ctx, Type);
+ }
+
+ return Ctx.deriveFromMemoryObject(Obj);
+}
+
+AnyValue Library::executeCalloc(StringRef Name, Type *Type,
+ ArrayRef<AnyValue> Args,
+ MemAllocKind AllocKind) {
+ assert(AllocKind == MemAllocKind::Malloc &&
+ "Unexpected MemAllocKind for calloc()");
+
+ const auto &CountVal = Args[0];
+ const auto &SizeVal = Args[1];
+
+ const APInt &Count = CountVal.asInteger();
+ const APInt &Size = SizeVal.asInteger();
+
+ bool Overflow = false;
+ const APInt AllocSize = Count.umul_ov(Size, Overflow);
+ if (Overflow) {
+ Executor.reportImmediateUB(
+ "calloc() with allocation size that overflows size_t.");
+ return AnyValue::poison();
+ }
+
+ const IntrusiveRefCntPtr<MemoryObject> Obj =
+ Ctx.allocate(AllocSize.getLimitedValue(), getMaxAlign(DL), Name, 0,
+ MemInitKind::Zeroed, AllocKind);
+
+ if (!Obj)
+ return AnyValue::getNullValue(Ctx, Type);
+
+ return Ctx.deriveFromMemoryObject(Obj);
+}
+
+AnyValue Library::executeFree(ArrayRef<AnyValue> Args) {
+ const auto &PtrVal = Args[0];
+
+ auto &Ptr = PtrVal.asPointer();
+ // no-op when free is called with a null pointer.
+ if (Ptr.address().isZero())
+ return AnyValue();
+
+ MemoryObject *Obj = Ptr.getMemoryObject();
+ if (!Obj) {
+ Executor.reportImmediateUB("freeing a pointer with nullary provenance.");
+ return AnyValue::poison();
+ }
+
+ if (const uint64_t Address = Ptr.address().getZExtValue();
+ Address != Obj->getAddress()) {
+ Executor.reportImmediateUB(
+ "freeing a pointer that does not point to the start of an allocation.");
+ return AnyValue::poison();
+ }
+
+ if (Obj->getState() == MemoryObjectState::Freed) {
+ Executor.reportImmediateUB("double-freeing a memory object.");
+ return AnyValue::poison();
+ }
+
+ if (!Obj->isHeapAllocated()) {
+ Executor.reportImmediateUB("freeing an stack allocation.");
+ return AnyValue::poison();
+ }
+
+ // Currently we don't for cases where a memory allocated with C
----------------
nikic wrote:
```suggestion
// Currently we don't check for cases where a memory allocated with C
```
https://github.com/llvm/llvm-project/pull/190147
More information about the llvm-commits
mailing list