Merge pull request #22343 from poettering/assert-se-efi

efi: add/user assert_se()/ASSERT_SE_PTR() for OOM handling
This commit is contained in:
Luca Boccassi
2022-02-01 19:26:57 +00:00
committed by GitHub
4 changed files with 18 additions and 13 deletions
+1 -4
View File
@@ -1,9 +1,8 @@
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#ifndef NDEBUG
#include <efi.h>
#include <efilib.h>
#include "util.h"
void efi_assert(const char *expr, const char *file, unsigned line, const char *function) {
@@ -11,5 +10,3 @@ void efi_assert(const char *expr, const char *file, unsigned line, const char *f
for (;;)
BS->Stall(60 * 1000 * 1000);
}
#endif
+6 -7
View File
@@ -27,16 +27,15 @@
#define xnew_alloc(type, n, alloc) \
({ \
UINTN _alloc_size; \
if (__builtin_mul_overflow(sizeof(type), (n), &_alloc_size)) \
assert_not_reached(); \
assert_se(!__builtin_mul_overflow(sizeof(type), (n), &_alloc_size)); \
(type *) alloc(_alloc_size); \
})
#define xallocate_pool(size) ASSERT_PTR(AllocatePool(size))
#define xallocate_zero_pool(size) ASSERT_PTR(AllocateZeroPool(size))
#define xreallocate_pool(p, old_size, new_size) ASSERT_PTR(ReallocatePool((p), (old_size), (new_size)))
#define xpool_print(fmt, ...) ((CHAR16 *) ASSERT_PTR(PoolPrint((fmt), ##__VA_ARGS__)))
#define xstrdup(str) ((CHAR16 *) ASSERT_PTR(StrDuplicate(str)))
#define xallocate_pool(size) ASSERT_SE_PTR(AllocatePool(size))
#define xallocate_zero_pool(size) ASSERT_SE_PTR(AllocateZeroPool(size))
#define xreallocate_pool(p, old_size, new_size) ASSERT_SE_PTR(ReallocatePool((p), (old_size), (new_size)))
#define xpool_print(fmt, ...) ((CHAR16 *) ASSERT_SE_PTR(PoolPrint((fmt), ##__VA_ARGS__)))
#define xstrdup(str) ((CHAR16 *) ASSERT_SE_PTR(StrDuplicate(str)))
#define xnew(type, n) xnew_alloc(type, (n), xallocate_pool)
#define xnew0(type, n) xnew_alloc(type, (n), xallocate_zero_pool)
+1 -1
View File
@@ -226,7 +226,7 @@ static EFI_STATUS find_device(EFI_HANDLE *device, EFI_DEVICE_PATH **ret_device_p
}
/* Patch in the data we found */
EFI_DEVICE_PATH *xboot_path = ASSERT_PTR(DuplicateDevicePath(partition_path));
EFI_DEVICE_PATH *xboot_path = ASSERT_SE_PTR(DuplicateDevicePath(partition_path));
CopyMem((UINT8 *) xboot_path + ((UINT8 *) part_node - (UINT8 *) partition_path), &hd, sizeof(hd));
*ret_device_path = xboot_path;
return EFI_SUCCESS;
+10 -1
View File
@@ -53,14 +53,16 @@
#define CONCATENATE(x, y) XCONCATENATE(x, y)
#ifdef SD_BOOT
void efi_assert(const char *expr, const char *file, unsigned line, const char *function) _noreturn_;
#ifdef NDEBUG
#define assert(expr)
#define assert_not_reached() __builtin_unreachable()
#else
void efi_assert(const char *expr, const char *file, unsigned line, const char *function) _noreturn_;
#define assert(expr) ({ _likely_(expr) ? VOID_0 : efi_assert(#expr, __FILE__, __LINE__, __PRETTY_FUNCTION__); })
#define assert_not_reached() efi_assert("Code should not be reached", __FILE__, __LINE__, __PRETTY_FUNCTION__)
#endif
#define assert_se(expr) ({ _likely_(expr) ? VOID_0 : efi_assert(#expr, __FILE__, __LINE__, __PRETTY_FUNCTION__); })
#define memcpy(a, b, c) CopyMem((a), (b), (c))
#define free(a) FreePool(a)
@@ -74,6 +76,13 @@
_expr_; \
})
#define ASSERT_SE_PTR(expr) \
({ \
typeof(expr) _expr_ = (expr); \
assert_se(_expr_); \
_expr_; \
})
#if defined(static_assert)
#define assert_cc(expr) \
static_assert(expr, #expr)