[libc-commits] [libc] [libc][uefi] Add malloc and process termination (PR #240493)
Prabhu Rajasekaran via libc-commits
libc-commits at lists.llvm.org
Fri Nov 13 10:48:40 PST 2026
https://github.com/Prabhuk updated https://github.com/llvm/llvm-project/pull/240493
>From f6b8eae8ce3bf7ddbc46da9a15273f57066398da Mon Sep 17 00:00:00 2001
From: prabhukr <prabhukr at google.com>
Date: Fri, 6 Nov 2026 19:30:35 +0000
Subject: [PATCH 1/2] [libc][uefi] Add malloc and process termination
Implement the heap allocation entrypoints on top of the UEFI AllocatePool and FreePool boot services, and implement abort on top of the Exit boot service. Enable the malloc family and the exit, quick_exit, _Exit, atexit, at_quick_exit and abort entrypoints for UEFI.
Assisted-by: Claude
---
libc/config/uefi/entrypoints.txt | 18 ++-
libc/src/__support/OSUtil/uefi/CMakeLists.txt | 21 +++
.../__support/OSUtil/uefi/pool_allocator.cpp | 137 ++++++++++++++++++
.../__support/OSUtil/uefi/pool_allocator.h | 57 ++++++++
libc/src/stdlib/CMakeLists.txt | 5 +-
libc/src/stdlib/abort_utils.h | 2 +
libc/src/stdlib/uefi/CMakeLists.txt | 70 +++++++++
libc/src/stdlib/uefi/abort_utils.h | 39 +++++
libc/src/stdlib/uefi/aligned_alloc.cpp | 25 ++++
libc/src/stdlib/uefi/calloc.cpp | 25 ++++
libc/src/stdlib/uefi/free.cpp | 23 +++
libc/src/stdlib/uefi/malloc.cpp | 25 ++++
libc/src/stdlib/uefi/malloc_usable_size.cpp | 25 ++++
libc/src/stdlib/uefi/realloc.cpp | 25 ++++
14 files changed, 493 insertions(+), 4 deletions(-)
create mode 100644 libc/src/__support/OSUtil/uefi/pool_allocator.cpp
create mode 100644 libc/src/__support/OSUtil/uefi/pool_allocator.h
create mode 100644 libc/src/stdlib/uefi/CMakeLists.txt
create mode 100644 libc/src/stdlib/uefi/abort_utils.h
create mode 100644 libc/src/stdlib/uefi/aligned_alloc.cpp
create mode 100644 libc/src/stdlib/uefi/calloc.cpp
create mode 100644 libc/src/stdlib/uefi/free.cpp
create mode 100644 libc/src/stdlib/uefi/malloc.cpp
create mode 100644 libc/src/stdlib/uefi/malloc_usable_size.cpp
create mode 100644 libc/src/stdlib/uefi/realloc.cpp
diff --git a/libc/config/uefi/entrypoints.txt b/libc/config/uefi/entrypoints.txt
index 2e11c534a4f3b..c2d49f858da2d 100644
--- a/libc/config/uefi/entrypoints.txt
+++ b/libc/config/uefi/entrypoints.txt
@@ -1,6 +1,20 @@
set(TARGET_LIBC_ENTRYPOINTS
- # errno.h entrypoints
- libc.src.errno.errno
+ # errno.h entrypoints
+ libc.src.errno.errno
+
+ # stdlib.h entrypoints
+ libc.src.stdlib._Exit
+ libc.src.stdlib.abort
+ libc.src.stdlib.aligned_alloc
+ libc.src.stdlib.at_quick_exit
+ libc.src.stdlib.atexit
+ libc.src.stdlib.calloc
+ libc.src.stdlib.exit
+ libc.src.stdlib.free
+ libc.src.stdlib.malloc
+ libc.src.stdlib.malloc_usable_size
+ libc.src.stdlib.quick_exit
+ libc.src.stdlib.realloc
)
set(TARGET_LIBM_ENTRYPOINTS)
diff --git a/libc/src/__support/OSUtil/uefi/CMakeLists.txt b/libc/src/__support/OSUtil/uefi/CMakeLists.txt
index d558ec93b2879..6e37665e9c79f 100644
--- a/libc/src/__support/OSUtil/uefi/CMakeLists.txt
+++ b/libc/src/__support/OSUtil/uefi/CMakeLists.txt
@@ -23,3 +23,24 @@ add_object_library(
libc.src.__support.common
libc.src.__support.CPP.string_view
)
+
+add_object_library(
+ pool_allocator
+ SRCS
+ pool_allocator.cpp
+ HDRS
+ pool_allocator.h
+ DEPENDS
+ libc.config.app_h
+ libc.hdr.stdint_proxy
+ libc.hdr.types.size_t
+ libc.include.llvm-libc-types.EFI_MEMORY_TYPE
+ libc.include.llvm-libc-types.EFI_STATUS
+ libc.include.llvm-libc-types.EFI_SYSTEM_TABLE
+ libc.src.__support.common
+ libc.src.__support.CPP.algorithm
+ libc.src.__support.CPP.bit
+ libc.src.__support.CPP.cstddef
+ libc.src.string.memory_utils.inline_memcpy
+ libc.src.string.memory_utils.inline_memset
+)
diff --git a/libc/src/__support/OSUtil/uefi/pool_allocator.cpp b/libc/src/__support/OSUtil/uefi/pool_allocator.cpp
new file mode 100644
index 0000000000000..66823bc17651b
--- /dev/null
+++ b/libc/src/__support/OSUtil/uefi/pool_allocator.cpp
@@ -0,0 +1,137 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Allocator built on the UEFI boot services pool.
+///
+//===----------------------------------------------------------------------===//
+
+#include "pool_allocator.h"
+
+#include "config/app.h"
+#include "hdr/stdint_proxy.h"
+#include "include/llvm-libc-types/EFI_MEMORY_TYPE.h"
+#include "include/llvm-libc-types/EFI_STATUS.h"
+#include "include/llvm-libc-types/EFI_SYSTEM_TABLE.h"
+#include "src/__support/CPP/algorithm.h"
+#include "src/__support/CPP/bit.h"
+#include "src/__support/CPP/cstddef.h"
+#include "src/__support/macros/config.h"
+#include "src/string/memory_utils/inline_memcpy.h"
+#include "src/string/memory_utils/inline_memset.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace uefi {
+namespace {
+
+// Bookkeeping stored immediately before every pointer handed out.
+struct AllocationHeader {
+ // The buffer returned by AllocatePool() that holds this allocation.
+ void *base;
+ // The size requested by the caller.
+ size_t size;
+};
+
+// AllocatePool() only guarantees 8 byte alignment, so the header is used to
+// re-align the user pointer. The smallest alignment handed out is the
+// fundamental alignment, which also keeps the header itself aligned.
+constexpr size_t MIN_ALIGNMENT =
+ cpp::max(alignof(max_align_t), alignof(AllocationHeader));
+
+static_assert(sizeof(AllocationHeader) % alignof(AllocationHeader) == 0,
+ "The header must be a multiple of its alignment.");
+
+// Memory type used for the heap. Loader data stays usable by a boot loader
+// that keeps running after ExitBootServices(), which is what EFI applications
+// are loaded as themselves.
+constexpr EFI_MEMORY_TYPE POOL_TYPE = EfiLoaderData;
+
+LIBC_INLINE AllocationHeader *header_of(void *ptr) {
+ return reinterpret_cast<AllocationHeader *>(ptr) - 1;
+}
+
+LIBC_INLINE const AllocationHeader *header_of(const void *ptr) {
+ return reinterpret_cast<const AllocationHeader *>(ptr) - 1;
+}
+
+} // namespace
+
+void *pool_allocate(size_t alignment, size_t size) {
+ if (!cpp::has_single_bit(alignment))
+ return nullptr;
+ alignment = cpp::max(alignment, MIN_ALIGNMENT);
+
+ // Reserve room for the header plus the worst case re-alignment slack.
+ size_t total = 0;
+ if (__builtin_add_overflow(size, sizeof(AllocationHeader), &total) ||
+ __builtin_add_overflow(total, alignment - 1, &total))
+ return nullptr;
+
+ void *base = nullptr;
+ EFI_STATUS status =
+ app.system_table->BootServices->AllocatePool(POOL_TYPE, total, &base);
+ if (status != EFI_SUCCESS || base == nullptr)
+ return nullptr;
+
+ uintptr_t user = reinterpret_cast<uintptr_t>(base) + sizeof(AllocationHeader);
+ user = (user + alignment - 1) & ~(static_cast<uintptr_t>(alignment) - 1);
+ void *ptr = reinterpret_cast<void *>(user);
+ *header_of(ptr) = {base, size};
+ return ptr;
+}
+
+void *pool_allocate(size_t size) { return pool_allocate(MIN_ALIGNMENT, size); }
+
+void *pool_calloc(size_t num, size_t size) {
+ size_t total = 0;
+ if (__builtin_mul_overflow(num, size, &total))
+ return nullptr;
+ void *ptr = pool_allocate(total);
+ if (ptr != nullptr)
+ inline_memset(ptr, 0, total);
+ return ptr;
+}
+
+void *pool_realloc(void *ptr, size_t size) {
+ if (ptr == nullptr)
+ return pool_allocate(size);
+ if (size == 0) {
+ pool_free(ptr);
+ return nullptr;
+ }
+
+ size_t old_size = header_of(ptr)->size;
+ if (size <= old_size) {
+ // Shrinking in place is always possible: just remember the new size so
+ // that malloc_usable_size() keeps reporting what the caller asked for.
+ header_of(ptr)->size = size;
+ return ptr;
+ }
+
+ void *new_ptr = pool_allocate(size);
+ if (new_ptr == nullptr)
+ return nullptr;
+ inline_memcpy(new_ptr, ptr, old_size);
+ pool_free(ptr);
+ return new_ptr;
+}
+
+void pool_free(void *ptr) {
+ if (ptr == nullptr)
+ return;
+ app.system_table->BootServices->FreePool(header_of(ptr)->base);
+}
+
+size_t pool_allocation_size(const void *ptr) {
+ if (ptr == nullptr)
+ return 0;
+ return header_of(ptr)->size;
+}
+
+} // namespace uefi
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/__support/OSUtil/uefi/pool_allocator.h b/libc/src/__support/OSUtil/uefi/pool_allocator.h
new file mode 100644
index 0000000000000..cd3ade13a6d4f
--- /dev/null
+++ b/libc/src/__support/OSUtil/uefi/pool_allocator.h
@@ -0,0 +1,57 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Allocator built on the UEFI boot services pool.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC___SUPPORT_OSUTIL_UEFI_POOL_ALLOCATOR_H
+#define LLVM_LIBC_SRC___SUPPORT_OSUTIL_UEFI_POOL_ALLOCATOR_H
+
+#include "hdr/types/size_t.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace uefi {
+
+// malloc() and friends are implemented on top of the firmware's
+// AllocatePool()/FreePool() boot services. Every allocation is prefixed by a
+// small header that records the pool buffer it came from and the requested
+// size, which is what makes realloc(), aligned_alloc() and
+// malloc_usable_size() possible on top of a plain pool.
+//
+// All functions return nullptr on failure. Pointers passed to pool_free(),
+// pool_realloc() and pool_allocation_size() must have been obtained from
+// pool_allocate() or pool_realloc().
+
+// Allocates `size` bytes aligned to `alignment`, which must be a power of two.
+// Alignments smaller than the fundamental alignment are rounded up to it.
+void *pool_allocate(size_t alignment, size_t size);
+
+// Allocates `size` bytes with the fundamental alignment.
+void *pool_allocate(size_t size);
+
+// Allocates zero-initialized memory for `num` objects of `size` bytes.
+void *pool_calloc(size_t num, size_t size);
+
+// Resizes an allocation, preserving its contents up to the smaller of the old
+// and new sizes. A null `ptr` behaves like pool_allocate(size) and a zero
+// `size` frees the allocation and returns nullptr.
+void *pool_realloc(void *ptr, size_t size);
+
+// Releases an allocation. Passing nullptr is a no-op.
+void pool_free(void *ptr);
+
+// Returns the size that was requested for an allocation.
+size_t pool_allocation_size(const void *ptr);
+
+} // namespace uefi
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_OSUTIL_UEFI_POOL_ALLOCATOR_H
diff --git a/libc/src/stdlib/CMakeLists.txt b/libc/src/stdlib/CMakeLists.txt
index a40e6bfa4d2c5..d2c138289746b 100644
--- a/libc/src/stdlib/CMakeLists.txt
+++ b/libc/src/stdlib/CMakeLists.txt
@@ -511,7 +511,8 @@ add_entrypoint_object(
libc.src.__support.libc_errno
)
-if(NOT LIBC_TARGET_OS_IS_BAREMETAL AND NOT LIBC_TARGET_OS_IS_GPU)
+if(NOT LIBC_TARGET_OS_IS_BAREMETAL AND NOT LIBC_TARGET_OS_IS_GPU AND
+ NOT LIBC_TARGET_OS_IS_UEFI)
if(LLVM_LIBC_INCLUDE_SCUDO)
set(SCUDO_DEPS "")
@@ -769,7 +770,7 @@ add_entrypoint_object(
.${LIBC_TARGET_OS}.clearenv
)
-if(LIBC_TARGET_OS_IS_BAREMETAL OR LIBC_TARGET_OS_IS_GPU)
+if(LIBC_TARGET_OS_IS_BAREMETAL OR LIBC_TARGET_OS_IS_GPU OR LIBC_TARGET_OS_IS_UEFI)
add_entrypoint_object(
malloc
ALIAS
diff --git a/libc/src/stdlib/abort_utils.h b/libc/src/stdlib/abort_utils.h
index 2c2e983c9dd7f..69752b364b414 100644
--- a/libc/src/stdlib/abort_utils.h
+++ b/libc/src/stdlib/abort_utils.h
@@ -15,6 +15,8 @@
#include "src/stdlib/gpu/abort_utils.h"
#elif defined(__linux__)
#include "src/stdlib/linux/abort_utils.h"
+#elif defined(__UEFI__)
+#include "src/stdlib/uefi/abort_utils.h"
#elif defined(__ELF__)
// TODO:ELF detection logic is borrowed from io.h (as we are still missing
// LIBC_TARGET_OS_IS_BAREMETAL).
diff --git a/libc/src/stdlib/uefi/CMakeLists.txt b/libc/src/stdlib/uefi/CMakeLists.txt
new file mode 100644
index 0000000000000..2dd81f5c4bd16
--- /dev/null
+++ b/libc/src/stdlib/uefi/CMakeLists.txt
@@ -0,0 +1,70 @@
+add_header_library(
+ abort_utils
+ HDRS
+ abort_utils.h
+ DEPENDS
+ libc.config.app_h
+ libc.include.llvm-libc-types.EFI_STATUS
+ libc.include.llvm-libc-types.EFI_SYSTEM_TABLE
+ libc.src.__support.common
+)
+
+add_entrypoint_object(
+ malloc
+ SRCS
+ malloc.cpp
+ HDRS
+ ../malloc.h
+ DEPENDS
+ libc.src.__support.OSUtil.uefi.pool_allocator
+)
+
+add_entrypoint_object(
+ free
+ SRCS
+ free.cpp
+ HDRS
+ ../free.h
+ DEPENDS
+ libc.src.__support.OSUtil.uefi.pool_allocator
+)
+
+add_entrypoint_object(
+ calloc
+ SRCS
+ calloc.cpp
+ HDRS
+ ../calloc.h
+ DEPENDS
+ libc.src.__support.OSUtil.uefi.pool_allocator
+)
+
+add_entrypoint_object(
+ realloc
+ SRCS
+ realloc.cpp
+ HDRS
+ ../realloc.h
+ DEPENDS
+ libc.src.__support.OSUtil.uefi.pool_allocator
+)
+
+add_entrypoint_object(
+ aligned_alloc
+ SRCS
+ aligned_alloc.cpp
+ HDRS
+ ../aligned_alloc.h
+ DEPENDS
+ libc.src.__support.OSUtil.uefi.pool_allocator
+)
+
+add_entrypoint_object(
+ malloc_usable_size
+ SRCS
+ malloc_usable_size.cpp
+ HDRS
+ ../malloc_usable_size.h
+ DEPENDS
+ libc.src.__support.OSUtil.uefi.pool_allocator
+)
diff --git a/libc/src/stdlib/uefi/abort_utils.h b/libc/src/stdlib/uefi/abort_utils.h
new file mode 100644
index 0000000000000..85ff596db739a
--- /dev/null
+++ b/libc/src/stdlib/uefi/abort_utils.h
@@ -0,0 +1,39 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Implementation of abort_utils for UEFI.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_STDLIB_UEFI_ABORT_UTILS_H
+#define LLVM_LIBC_SRC_STDLIB_UEFI_ABORT_UTILS_H
+
+#include "config/app.h"
+#include "include/llvm-libc-types/EFI_STATUS.h"
+#include "include/llvm-libc-types/EFI_SYSTEM_TABLE.h"
+#include "src/__support/common.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+namespace abort_utils {
+[[noreturn]] LIBC_INLINE void abort() {
+ // Hand control back to the firmware with an error status so that the
+ // loader reports the failure. Boot services are gone after
+ // ExitBootServices(), in which case all that is left is to trap.
+ if (app.system_table != nullptr && app.system_table->BootServices != nullptr)
+ app.system_table->BootServices->Exit(app.image_handle, EFI_ABORTED, 0,
+ nullptr);
+ __builtin_trap();
+}
+} // namespace abort_utils
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_STDLIB_UEFI_ABORT_UTILS_H
diff --git a/libc/src/stdlib/uefi/aligned_alloc.cpp b/libc/src/stdlib/uefi/aligned_alloc.cpp
new file mode 100644
index 0000000000000..6b534ca5a5a5d
--- /dev/null
+++ b/libc/src/stdlib/uefi/aligned_alloc.cpp
@@ -0,0 +1,25 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Implementation of aligned_alloc for UEFI.
+///
+//===----------------------------------------------------------------------===//
+
+#include "src/stdlib/aligned_alloc.h"
+#include "src/__support/OSUtil/uefi/pool_allocator.h"
+#include "src/__support/common.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(void *, aligned_alloc, (size_t alignment, size_t size)) {
+ return uefi::pool_allocate(alignment, size);
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/stdlib/uefi/calloc.cpp b/libc/src/stdlib/uefi/calloc.cpp
new file mode 100644
index 0000000000000..751c14e98ba66
--- /dev/null
+++ b/libc/src/stdlib/uefi/calloc.cpp
@@ -0,0 +1,25 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Implementation of calloc for UEFI.
+///
+//===----------------------------------------------------------------------===//
+
+#include "src/stdlib/calloc.h"
+#include "src/__support/OSUtil/uefi/pool_allocator.h"
+#include "src/__support/common.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(void *, calloc, (size_t num, size_t size)) {
+ return uefi::pool_calloc(num, size);
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/stdlib/uefi/free.cpp b/libc/src/stdlib/uefi/free.cpp
new file mode 100644
index 0000000000000..914b003f1f2de
--- /dev/null
+++ b/libc/src/stdlib/uefi/free.cpp
@@ -0,0 +1,23 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Implementation of free for UEFI.
+///
+//===----------------------------------------------------------------------===//
+
+#include "src/stdlib/free.h"
+#include "src/__support/OSUtil/uefi/pool_allocator.h"
+#include "src/__support/common.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(void, free, (void *ptr)) { uefi::pool_free(ptr); }
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/stdlib/uefi/malloc.cpp b/libc/src/stdlib/uefi/malloc.cpp
new file mode 100644
index 0000000000000..c878dad3be20e
--- /dev/null
+++ b/libc/src/stdlib/uefi/malloc.cpp
@@ -0,0 +1,25 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Implementation of malloc for UEFI.
+///
+//===----------------------------------------------------------------------===//
+
+#include "src/stdlib/malloc.h"
+#include "src/__support/OSUtil/uefi/pool_allocator.h"
+#include "src/__support/common.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(void *, malloc, (size_t size)) {
+ return uefi::pool_allocate(size);
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/stdlib/uefi/malloc_usable_size.cpp b/libc/src/stdlib/uefi/malloc_usable_size.cpp
new file mode 100644
index 0000000000000..03cde80486e66
--- /dev/null
+++ b/libc/src/stdlib/uefi/malloc_usable_size.cpp
@@ -0,0 +1,25 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Implementation of malloc_usable_size for UEFI.
+///
+//===----------------------------------------------------------------------===//
+
+#include "src/stdlib/malloc_usable_size.h"
+#include "src/__support/OSUtil/uefi/pool_allocator.h"
+#include "src/__support/common.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(size_t, malloc_usable_size, (void *ptr)) {
+ return uefi::pool_allocation_size(ptr);
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/stdlib/uefi/realloc.cpp b/libc/src/stdlib/uefi/realloc.cpp
new file mode 100644
index 0000000000000..3e647e7cc46f3
--- /dev/null
+++ b/libc/src/stdlib/uefi/realloc.cpp
@@ -0,0 +1,25 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Implementation of realloc for UEFI.
+///
+//===----------------------------------------------------------------------===//
+
+#include "src/stdlib/realloc.h"
+#include "src/__support/OSUtil/uefi/pool_allocator.h"
+#include "src/__support/common.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(void *, realloc, (void *ptr, size_t size)) {
+ return uefi::pool_realloc(ptr, size);
+}
+
+} // namespace LIBC_NAMESPACE_DECL
>From 64448649c4ff2f590bb34215a791135db3d0b6ea Mon Sep 17 00:00:00 2001
From: prabhukr <prabhukr at google.com>
Date: Thu, 12 Nov 2026 20:29:15 +0000
Subject: [PATCH 2/2] [libc][uefi] Avoid reinterpret_cast in the pool allocator
Use static_cast and byte-pointer arithmetic when aligning pool allocations and accessing allocation headers instead of casting pointers through uintptr_t with reinterpret_cast.
Assisted-by: Claude
---
libc/src/__support/OSUtil/uefi/pool_allocator.cpp | 11 ++++++-----
1 file changed, 6 insertions(+), 5 deletions(-)
diff --git a/libc/src/__support/OSUtil/uefi/pool_allocator.cpp b/libc/src/__support/OSUtil/uefi/pool_allocator.cpp
index 66823bc17651b..658ef8bd78e17 100644
--- a/libc/src/__support/OSUtil/uefi/pool_allocator.cpp
+++ b/libc/src/__support/OSUtil/uefi/pool_allocator.cpp
@@ -52,11 +52,11 @@ static_assert(sizeof(AllocationHeader) % alignof(AllocationHeader) == 0,
constexpr EFI_MEMORY_TYPE POOL_TYPE = EfiLoaderData;
LIBC_INLINE AllocationHeader *header_of(void *ptr) {
- return reinterpret_cast<AllocationHeader *>(ptr) - 1;
+ return static_cast<AllocationHeader *>(ptr) - 1;
}
LIBC_INLINE const AllocationHeader *header_of(const void *ptr) {
- return reinterpret_cast<const AllocationHeader *>(ptr) - 1;
+ return static_cast<const AllocationHeader *>(ptr) - 1;
}
} // namespace
@@ -78,9 +78,10 @@ void *pool_allocate(size_t alignment, size_t size) {
if (status != EFI_SUCCESS || base == nullptr)
return nullptr;
- uintptr_t user = reinterpret_cast<uintptr_t>(base) + sizeof(AllocationHeader);
- user = (user + alignment - 1) & ~(static_cast<uintptr_t>(alignment) - 1);
- void *ptr = reinterpret_cast<void *>(user);
+ cpp::byte *unaligned =
+ static_cast<cpp::byte *>(base) + sizeof(AllocationHeader);
+ size_t offset = (-cpp::bit_cast<uintptr_t>(unaligned)) & (alignment - 1);
+ void *ptr = unaligned + offset;
*header_of(ptr) = {base, size};
return ptr;
}
More information about the libc-commits
mailing list