Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pe: Add IS_PAGE_ALIGNED macro #541

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions include/peimage.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
#define ALIGN_VALUE(Value, Alignment) ((Value) + (((Alignment) - (Value)) & ((Alignment) - 1)))
#define ALIGN_POINTER(Pointer, Alignment) ((VOID *) (ALIGN_VALUE ((UINTN)(Pointer), (Alignment))))

// Check if `val` is evenly aligned to the page size.
#define IS_PAGE_ALIGNED(val) (!((val) & 0xfff))

//
// PE32+ Subsystem type for EFI images
//
Expand Down
4 changes: 2 additions & 2 deletions pe.c
Original file line number Diff line number Diff line change
Expand Up @@ -937,7 +937,7 @@ get_mem_attrs (uintptr_t addr, size_t size, uint64_t *attrs)
if (EFI_ERROR(efi_status) || !proto)
return efi_status;

if (physaddr & 0xfff || size & 0xfff || size == 0 || attrs == NULL) {
if (!IS_PAGE_ALIGNED(physaddr) || !IS_PAGE_ALIGNED(size) || size == 0 || attrs == NULL) {
dprint(L"%a called on 0x%llx-0x%llx and attrs 0x%llx\n",
__func__, (unsigned long long)physaddr,
(unsigned long long)(physaddr+size-1),
Expand Down Expand Up @@ -971,7 +971,7 @@ update_mem_attrs(uintptr_t addr, uint64_t size,
(unsigned long long)addr, (unsigned long long)size,
&before, efi_status);

if (physaddr & 0xfff || size & 0xfff || size == 0) {
if (!IS_PAGE_ALIGNED(physaddr) || !IS_PAGE_ALIGNED(size) || size == 0) {
dprint(L"%a called on 0x%llx-0x%llx (size 0x%llx) +%a%a%a -%a%a%a\n",
__func__, (unsigned long long)physaddr,
(unsigned long long)(physaddr + size - 1),
Expand Down
30 changes: 30 additions & 0 deletions test-pe-util.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// SPDX-License-Identifier: BSD-2-Clause-Patent
/*
* test-pe-util.c - test PE utilities
*/

#ifndef SHIM_UNIT_TEST
#define SHIM_UNIT_TEST
#endif
#include "shim.h"

static int
test_is_page_aligned(void)
{
assert_true_return(IS_PAGE_ALIGNED(0), -1, "\n");
assert_false_return(IS_PAGE_ALIGNED(1), -1, "\n");
assert_false_return(IS_PAGE_ALIGNED(4095), -1, "\n");
assert_true_return(IS_PAGE_ALIGNED(4096), -1, "\n");
assert_false_return(IS_PAGE_ALIGNED(4097), -1, "\n");

return 0;
}

int
main(void)
{
int status = 0;
test(test_is_page_aligned);

return status;
}