-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtls.C
136 lines (118 loc) · 3.89 KB
/
tls.C
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
/* This Library implements thread_local for NT5.1/NT5.2
* Copyright 2023, Dibyamartanda Samanta <[email protected]>
* License: GPL 2.1/ Apache 2.0
*/
include <ntos.h> // It just include all header their in ROS SDK & NDK
DWORD WINAPI TlsAlloc( void )
{
PEB * const peb = NtCurrentTeb()->ProcessEnvironmentBlock;
RtlAcquirePebLock();
DWORD index = RtlFindClearBitsAndSet( peb->TlsBitmap, 1, 1 );
if (index != ~0U)
{
NtCurrentTeb()->TlsSlots[index] = 0;
}
else
{
index = RtlFindClearBitsAndSet( peb->TlsExpansionBitmap, 1, 0 );
if (index != ~0U)
{
if (!NtCurrentTeb()->TlsExpansionSlots &&
!(NtCurrentTeb()->TlsExpansionSlots = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
8 * sizeof(peb->TlsExpansionBitmapBits) * sizeof(void*) )))
{
RtlClearBits( peb->TlsExpansionBitmap, index, 1 );
index = ~0U;
SetLastError( ERROR_NOT_ENOUGH_MEMORY );
}
else
{
NtCurrentTeb()->TlsExpansionSlots[index] = 0; /* clear the value */
index = index+ TLS_MINIMUM_AVAILABLE;
}
}
else SetLastError( ERROR_NO_MORE_ITEMS );
}
RtlReleasePebLock();
return index;
}
BOOL WINAPI TlsFree( DWORD index )
{
BOOL result;
RtlAcquirePebLock();
if(index < TLS_MINIMUM_AVAILABLE)
{
result = RtlAreBitsSet( NtCurrentTeb()->ProcessEnvironmentBlock->TlsBitmap, index, 1 );
if (result ! =0)
RtlClearBits( NtCurrentTeb()->ProcessEnvironmentBlock->TlsBitmap, index, 1 );
}
if (index >= TLS_MINIMUM_AVAILABLE)
{
result = RtlAreBitsSet( NtCurrentTeb()->ProcessEnvironmentBlock->TlsExpansionBitmap, index - TLS_MINIMUM_AVAILABLE, 1 );
if (result != 0)
RtlClearBits( NtCurrentTeb()->ProcessEnvironmentBlock->TlsExpansionBitmap, index - TLS_MINIMUM_AVAILABLE, 1 );
}
if (result != 0)
{
NtSetInformationThread( GetCurrentThread(), ThreadZeroTlsCell, &index, sizeof(index) );
}
else
{
SetLastError( ERROR_INVALID_PARAMETER );
RtlReleasePebLock();
}
return result;
}
LPVOID WINAPI TlsGetValue( DWORD index )
{
LPVOID result;
if (index < TLS_MINIMUM_AVAILABLE)
{
result = NtCurrentTeb()->TlsSlots[index];
}
if(index >= TLS_MINIMUM_AVAILABLE
{
index = index - TLS_MINIMUM_AVAILABLE;
if (index >= 8 * sizeof(NtCurrentTeb()->ProcessEnvironmentBlock->TlsExpansionBitmapBits))
{
SetLastError( ERROR_INVALID_PARAMETER );
result = NULL;
}
if (!NtCurrentTeb()->TlsExpansionSlots)
{
result = NULL;
}
else
result = NtCurrentTeb()->TlsExpansionSlots[index];
}
SetLastError( ERROR_SUCCESS );
return result;
}
// might have bugs
BOOL WINAPI TlsSetValue( DWORD index, LPVOID value )
{
bool result;
if (index < TLS_MINIMUM_AVAILABLE)
{
NtCurrentTeb()->TlsSlots[index] = value;
}
if(index >= TLS_MINIMUM_AVAILABLE)
{
index = index - TLS_MINIMUM_AVAILABLE;
if (index >= 8 * sizeof(NtCurrentTeb()->ProcessEnvironmentBlock->TlsExpansionBitmapBits))
{
SetLastError( ERROR_INVALID_PARAMETER );
result = FALSE;
}
if (!NtCurrentTeb()->TlsExpansionSlots &&
!(NtCurrentTeb()->TlsExpansionSlots = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
8 * sizeof(NtCurrentTeb()->ProcessEnvironmentBlock->TlsExpansionBitmapBits) * sizeof(void*) )))
{
SetLastError( ERROR_NOT_ENOUGH_MEMORY );
result = FALSE;
}
NtCurrentTeb()->TlsExpansionSlots[index] = value;
return result;
}
return TRUE;
}