Skip to content

Commit

Permalink
Use GEN_CHECK macro, moved some tests to MmFreeSystemMemory
Browse files Browse the repository at this point in the history
  • Loading branch information
ergo720 committed Nov 25, 2023
1 parent 6920a43 commit e36bd35
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 76 deletions.
110 changes: 35 additions & 75 deletions src/tests/mm/MmAllocateSystemMemory.c
Original file line number Diff line number Diff line change
@@ -1,18 +1,7 @@
#include <xboxkrnl/xboxkrnl.h>

#include "util/output.h"

#define PAGE_SIZE 4096

#define PAGE_NOACCESS 0x01
#define PAGE_READONLY 0x02
#define PAGE_READWRITE 0x04
#define PAGE_EXECUTE 0x10
#define PAGE_EXECUTE_READ 0x20
#define PAGE_EXECUTE_READWRITE 0x40
#define PAGE_GUARD 0x100
#define PAGE_NOCACHE 0x200
#define PAGE_WRITECOMBINE 0x400
#include "assertions/defines.h"

#define SYSTEM_MEMORY_BASE 0xD0000000
#define SYSTEM_MEMORY_SIZE (512 * 1024 * 1024) // = 0x20000000
Expand All @@ -25,74 +14,45 @@ void test_MmAllocateSystemMemory()
{
const char* func_num = "0x00A7";
const char* func_name = "MmAllocateSystemMemory";
BOOL tests_passed = 1;
BOOL test_passed = 1;
print_test_header(func_num, func_name);

ULONG NumberOfPages;
PVOID Addr = MmAllocateSystemMemory(100, PAGE_READWRITE | PAGE_NOCACHE);
{
if (Addr == NULL) {
print("Failed to allocate system memory; %u\n", __LINE__);
goto NextTest1;
}
if (IS_SYSTEM_ADDRESS(Addr) == FALSE) {
print("Allocated system memory in unexpected region, Addr was %p; %u\n", Addr, __LINE__);
tests_passed = 0;
goto Free;
}
if (!CHECK_ALIGNMENT((ULONG)Addr, PAGE_SIZE)) {
print("Addr is not aligned to a page boundary, Addr was %p; %u\n", Addr, __LINE__);
tests_passed = 0;
goto Free;
}
}
Free:
NumberOfPages = MmFreeSystemMemory(Addr, 100);
if (NumberOfPages != 1) {
print("Unexpected number of pages freed, NumberOfPages was %u; %u\n", NumberOfPages, __LINE__);
tests_passed = 0;
}
NextTest1:
Addr = MmAllocateSystemMemory(PAGE_SIZE, PAGE_READONLY);
if (Addr == NULL) {
print("Failed to allocate system memory; %u\n", __LINE__);
goto NextTest2;
}
NumberOfPages = MmFreeSystemMemory(Addr, 0);
if (NumberOfPages != 1) {
print("Unexpected number of pages freed, NumberOfPages was %u; %u\n", NumberOfPages, __LINE__);
tests_passed = 0;
}
NextTest2:
Addr = MmAllocateSystemMemory(PAGE_SIZE + 1, PAGE_READONLY);
if (Addr == NULL) {
print("Failed to allocate system memory; %u\n", __LINE__);
goto NextTest3;
}
NumberOfPages = MmFreeSystemMemory(Addr, 0);
if (NumberOfPages != 2) {
print("Unexpected number of pages freed, NumberOfPages was %u; %u\n", NumberOfPages, __LINE__);
tests_passed = 0;
// NOTE: these tests will be marked as failed if not enough memory can be allocated for them

ULONG num_of_pages;
BOOL is_system_address;
BOOL is_page_alignment;

// Test #1 - RW and no cache
PVOID sys_addr = MmAllocateSystemMemory(100, PAGE_READWRITE | PAGE_NOCACHE);
GEN_CHECK(sys_addr != NULL, TRUE, "SystemMemoryAddress (RW, No Cache)");
if (sys_addr) {
is_system_address = IS_SYSTEM_ADDRESS(sys_addr);
GEN_CHECK(is_system_address, TRUE, "is_system_address (RW, No Cache)");

is_page_alignment = CHECK_ALIGNMENT((ULONG)sys_addr, PAGE_SIZE);
GEN_CHECK(is_page_alignment, TRUE, "is_page_alignment (RW, No Cache)");

num_of_pages = MmFreeSystemMemory(sys_addr, 100);
GEN_CHECK(num_of_pages, 1, "num_of_pages (RW, No Cache)");
}
NextTest3:
Addr = MmAllocateSystemMemory(1, PAGE_READWRITE | PAGE_READONLY);
if (Addr != NULL) {
print("Allocation unexpectedly succeeded; %u\n", __LINE__);
MmFreeSystemMemory(Addr, 0);
tests_passed = 0;

// Test #2 - expected failure to allocate memory
sys_addr = MmAllocateSystemMemory(1, PAGE_READWRITE | PAGE_READONLY);
GEN_CHECK(sys_addr != NULL, FALSE, "SystemMemoryAddress (RO - invalid)");
if (sys_addr) {
MmFreeSystemMemory(sys_addr, 0);
}
Addr = MmAllocateSystemMemory(1, PAGE_READWRITE | PAGE_EXECUTE);
if (Addr != NULL) {
print("Allocation unexpectedly succeeded; %u\n", __LINE__);
MmFreeSystemMemory(Addr, 0);
tests_passed = 0;
sys_addr = MmAllocateSystemMemory(1, PAGE_READWRITE | PAGE_EXECUTE);
GEN_CHECK(sys_addr != NULL, FALSE, "SystemMemoryAddress (E - invalid)");
if (sys_addr) {
MmFreeSystemMemory(sys_addr, 0);
}
Addr = MmAllocateSystemMemory(1, PAGE_READWRITE | PAGE_NOCACHE | PAGE_WRITECOMBINE);
if (Addr != NULL) {
print("Allocation unexpectedly succeeded; %u\n", __LINE__);
MmFreeSystemMemory(Addr, 0);
tests_passed = 0;
sys_addr = MmAllocateSystemMemory(1, PAGE_READWRITE | PAGE_NOCACHE | PAGE_WRITECOMBINE);
GEN_CHECK(sys_addr != NULL, FALSE, "SystemMemoryAddress (RW, No Cache, Write Combine - invalid)");
if (sys_addr) {
MmFreeSystemMemory(sys_addr, 0);
}

print_test_footer(func_num, func_name, tests_passed);
print_test_footer(func_num, func_name, test_passed);
}
28 changes: 27 additions & 1 deletion src/tests/mm/MmFreeSystemMemory.c
Original file line number Diff line number Diff line change
@@ -1,8 +1,34 @@
#include <xboxkrnl/xboxkrnl.h>

#include "util/output.h"
#include "assertions/defines.h"

void test_MmFreeSystemMemory()
{
/* FIXME: This is a stub! implement this function! */
const char* func_num = "0x00AC";
const char* func_name = "MmFreeSystemMemory";
BOOL test_passed = 1;
print_test_header(func_num, func_name);

// NOTE: these tests will be marked as failed if not enough memory can be allocated for them

ULONG num_of_pages;

// Test #1 - free one page
PVOID sys_addr = MmAllocateSystemMemory(PAGE_SIZE, PAGE_READONLY);
GEN_CHECK(sys_addr != NULL, TRUE, "SystemMemoryAddress (RO - PAGE_SIZE)");
if (sys_addr) {
num_of_pages = MmFreeSystemMemory(sys_addr, 0);
GEN_CHECK(num_of_pages, 1, "num_of_pages (RO - PAGE_SIZE)");
}

// Test #1 - free two pages
sys_addr = MmAllocateSystemMemory(PAGE_SIZE + 1, PAGE_READONLY);
GEN_CHECK(sys_addr != NULL, TRUE, "SystemMemoryAddress (RO - PAGE_SIZE + 1)");
if (sys_addr) {
num_of_pages = MmFreeSystemMemory(sys_addr, 0);
GEN_CHECK(num_of_pages, 2, "num_of_pages (RO - PAGE_SIZE + 1)");
}

print_test_footer(func_num, func_name, test_passed);
}

0 comments on commit e36bd35

Please sign in to comment.