Process Hacker
handlep.h
Go to the documentation of this file.
1 #ifndef _PH_HANDLEP_H
2 #define _PH_HANDLEP_H
3 
4 #define PH_HANDLE_TABLE_ENTRY_TYPE 0x1
5 #define PH_HANDLE_TABLE_ENTRY_IN_USE 0x0
6 #define PH_HANDLE_TABLE_ENTRY_FREE 0x1
7 
8 // Locked actually means Not Locked. This means
9 // that an in use, locked handle table entry can
10 // be used as-is.
11 #define PH_HANDLE_TABLE_ENTRY_LOCKED 0x2
12 #define PH_HANDLE_TABLE_ENTRY_LOCKED_SHIFT 1
13 
14 // There is initially one handle table level, with
15 // 256 entries. When the handle table is expanded,
16 // the table is replaced with a level 1 table, which
17 // contains 256 pointers to level 0 tables (the first
18 // entry already points to the initial level 0 table).
19 // Similarly, when the handle table is expanded a
20 // second time, the table is replaced with a level 2
21 // table, which contains 256 pointers to level 1 tables.
22 //
23 // This provides a maximum of 16,777,216 handles.
24 
25 #define PH_HANDLE_TABLE_LEVEL_ENTRIES 256
26 #define PH_HANDLE_TABLE_LEVEL_MASK 0x3
27 
28 #define PH_HANDLE_TABLE_LOCKS 8
29 #define PH_HANDLE_TABLE_LOCK_INDEX(HandleValue) ((HandleValue) % PH_HANDLE_TABLE_LOCKS)
30 
31 typedef struct _PH_HANDLE_TABLE
32 {
35 
36  ULONG Count;
37  ULONG_PTR TableValue;
38  ULONG FreeValue;
39  ULONG NextValue;
40  ULONG FreeValueAlt;
41 
42  ULONG Flags;
43 
46 
48  _Inout_ PPH_HANDLE_TABLE HandleTable,
49  _In_ ULONG Index
50  )
51 {
52  PhAcquireQueuedLockShared(&HandleTable->Locks[Index]);
53 }
54 
56  _Inout_ PPH_HANDLE_TABLE HandleTable,
57  _In_ ULONG Index
58  )
59 {
60  PhReleaseQueuedLockShared(&HandleTable->Locks[Index]);
61 }
62 
63 // Handle values work by specifying indicies into each
64 // level.
65 //
66 // Bits 0-7: level 0
67 // Bits 8-15: level 1
68 // Bits 16-23: level 2
69 // Bits 24-31: reserved
70 
71 #define PH_HANDLE_VALUE_INVALID ((ULONG)-1)
72 #define PH_HANDLE_VALUE_SHIFT 2
73 #define PH_HANDLE_VALUE_BIAS 4
74 
75 #define PH_HANDLE_VALUE_LEVEL0(HandleValue) ((HandleValue) & 0xff)
76 #define PH_HANDLE_VALUE_LEVEL1_U(HandleValue) ((HandleValue) >> 8)
77 #define PH_HANDLE_VALUE_LEVEL1(HandleValue) (PH_HANDLE_VALUE_LEVEL1_U(HandleValue) & 0xff)
78 #define PH_HANDLE_VALUE_LEVEL2_U(HandleValue) ((HandleValue) >> 16)
79 #define PH_HANDLE_VALUE_LEVEL2(HandleValue) (PH_HANDLE_VALUE_LEVEL2_U(HandleValue) & 0xff)
80 #define PH_HANDLE_VALUE_IS_INVALID(HandleValue) (((HandleValue) >> 24) != 0)
81 
82 FORCEINLINE HANDLE PhpEncodeHandle(
83  _In_ ULONG HandleValue
84  )
85 {
86  return (HANDLE)((HandleValue << PH_HANDLE_VALUE_SHIFT) + PH_HANDLE_VALUE_BIAS);
87 }
88 
89 FORCEINLINE ULONG PhpDecodeHandle(
90  _In_ HANDLE Handle
91  )
92 {
93  return ((ULONG)Handle - PH_HANDLE_VALUE_BIAS) >> PH_HANDLE_VALUE_SHIFT;
94 }
95 
97  _Inout_ PPH_HANDLE_TABLE HandleTable,
98  _In_ PPH_HANDLE_TABLE_ENTRY HandleTableEntry
99  );
100 
102  _Inout_ PPH_HANDLE_TABLE HandleTable,
103  _Out_ PULONG HandleValue
104  );
105 
107  _Inout_ PPH_HANDLE_TABLE HandleTable,
108  _In_ ULONG HandleValue,
109  _Inout_ PPH_HANDLE_TABLE_ENTRY HandleTableEntry
110  );
111 
113  _In_ PPH_HANDLE_TABLE HandleTable,
114  _In_ BOOLEAN Initialize
115  );
116 
118  _In_ PPH_HANDLE_TABLE HandleTable,
119  _In_ ULONG HandleValue
120  );
121 
123  _Inout_ PPH_HANDLE_TABLE HandleTable
124  );
125 
127  _In_ PPH_HANDLE_TABLE HandleTable,
128  _In_ BOOLEAN Initialize
129  );
130 
132  _In_ PPH_HANDLE_TABLE_ENTRY Table
133  );
134 
136  _In_ PPH_HANDLE_TABLE HandleTable
137  );
138 
140  _In_ PPH_HANDLE_TABLE_ENTRY *Table
141  );
142 
144  _In_ PPH_HANDLE_TABLE HandleTable
145  );
146 
148  _In_ PPH_HANDLE_TABLE_ENTRY **Table
149  );
150 
151 #endif