/
listing_0124_32bit_pointer_main.cpp
162 lines (126 loc) · 4.54 KB
/
listing_0124_32bit_pointer_main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/* ========================================================================
(C) Copyright 2023 by Molly Rocket, Inc., All Rights Reserved.
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Please see https://computerenhance.com for more information
======================================================================== */
/* ========================================================================
LISTING 124
======================================================================== */
/* NOTE(casey): _CRT_SECURE_NO_WARNINGS is here because otherwise we cannot
call fopen(). If we replace fopen() with fopen_s() to avoid the warning,
then the code doesn't compile on Linux anymore, since fopen_s() does not
exist there.
What exactly the CRT maintainers were thinking when they made this choice,
I have no idea. */
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdint.h>
#include <windows.h>
typedef uint8_t u8;
typedef uint16_t u16;
typedef uint32_t u32;
typedef uint64_t u64;
typedef int32_t s32;
typedef int32_t b32;
typedef float f32;
typedef double f64;
#define ArrayCount(Array) (sizeof(Array)/sizeof((Array)[0]))
#define KERNEL_MEMORY_DLL_PATH L"kernelbase.dll"
//
// NOTE(casey): The following definitions are required if you do not have an up-to-date version of the Windows SDK
//
#ifndef MEM_REPLACE_PLACEHOLDER
#define MemExtendedParameterAddressRequirements 1
struct MEM_EXTENDED_PARAMETER
{
DWORD64 Type;
union
{
DWORD64 ULong64;
PVOID Pointer;
SIZE_T Size;
HANDLE Handle;
DWORD ULong;
};
};
struct MEM_ADDRESS_REQUIREMENTS
{
PVOID LowestStartingAddress;
PVOID HighestEndingAddress;
SIZE_T Alignment;
};
#endif
typedef PVOID virtual_alloc_2(HANDLE, PVOID, SIZE_T, ULONG, ULONG, MEM_EXTENDED_PARAMETER *, ULONG);
template<class type> struct p32
{
operator p32<void>() {p32<void> Result = {Value}; return Result;}
operator type *() {return (type *)(u64)Value;}
type *operator->() {return (type *)(u64)Value;}
u32 Value;
};
template<class type> p32<type> Cast32BP(p32<void> Input)
{
p32<type> Result = {};
Result.Value = Input.Value;
return Result;
}
static p32<void> Allocate32BPCompatible(u32 MinimumSize)
{
p32<void> Result = {};
HMODULE Kernel = LoadLibraryW(KERNEL_MEMORY_DLL_PATH);
virtual_alloc_2 *VirtualAlloc2 = (virtual_alloc_2 *)GetProcAddress(Kernel, "VirtualAlloc2");
if(VirtualAlloc2)
{
MEM_ADDRESS_REQUIREMENTS Addr = {};
Addr.LowestStartingAddress = 0;
Addr.HighestEndingAddress = (PVOID)0xffffffffull;
Addr.Alignment = 0;
MEM_EXTENDED_PARAMETER Param = {};
Param.Type = MemExtendedParameterAddressRequirements;
Param.Pointer = &Addr;
Result.Value = (u32)(u64)VirtualAlloc2(0, 0, MinimumSize, MEM_RESERVE|MEM_COMMIT, PAGE_READWRITE, &Param, 1);
}
else
{
/* NOTE(casey): I've included this code for reference, just to show that it can work without
the new APIs. However, it is not code I would ever consider shipping. It just hunts for addresses
under 4GB to try to allocate, and makes no attempt to intelligently map that space or track what it has
already used. So it is unnecessarily quite slow, and is very limited in that it has a much
larger granularity than the Windows' own 64k restriction.
*/
for(u32 BasePage = 64; !Result.Value && (BasePage < 65536); BasePage += 64)
{
Result.Value = (u32)(u64)VirtualAlloc((PVOID)(u64)(BasePage*64*1024), MinimumSize, MEM_RESERVE|MEM_COMMIT, PAGE_READWRITE);
}
}
return Result;
}
static void Deallocate32BCompatible(p32<void> Memory)
{
if(Memory)
{
VirtualFree(Memory, 0, MEM_RELEASE);
}
}
int main(void)
{
printf("\n32-bit pointer test:\n");
p32<u32> SmallPointer = Cast32BP<u32>(Allocate32BPCompatible(256*1024));
if(SmallPointer)
{
SmallPointer[0] = 123;
SmallPointer[1] = 256;
u32 *RegularPointer = SmallPointer;
printf(" Ptr: %llu bytes (%llu bits)\n", sizeof(SmallPointer), 8*sizeof(SmallPointer));
printf(" %u: %u\n", 0, RegularPointer[0]);
printf(" %u: %u\n", 1, RegularPointer[1]);
Deallocate32BCompatible(SmallPointer);
}
else
{
printf(" FAILED\n");
}
return 0;
}