Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit 795d9c0

Browse files
janvorlijkotas
authored andcommitted
Fix mismatch between new and free in numa.cpp (#15870)
* Fix mismatch between new and free in numa.cpp One of the allocations in the numa.cpp uses new to allocate an array, but it incorrectly uses free to free the memory. This change fixes it.
1 parent 8b8b3b9 commit 795d9c0

File tree

1 file changed

+47
-25
lines changed

1 file changed

+47
-25
lines changed

src/pal/src/numa/numa.cpp

Lines changed: 47 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ SET_DEFAULT_DEBUG_CHANNEL(NUMA);
3131

3232
#include <pthread.h>
3333
#include <dlfcn.h>
34+
#include <alloca.h>
3435

3536
#include <algorithm>
3637

@@ -85,26 +86,6 @@ FOR_ALL_NUMA_FUNCTIONS
8586
static const int MaxCpusPerGroup = 8 * sizeof(KAFFINITY);
8687
static const WORD NO_GROUP = 0xffff;
8788

88-
/*++
89-
Function:
90-
AllocateLookupArrays
91-
92-
Allocate CPU and group lookup arrays
93-
--*/
94-
VOID
95-
AllocateLookupArrays()
96-
{
97-
g_groupAndIndexToCpu = (short*)malloc(g_groupCount * MaxCpusPerGroup * sizeof(short));
98-
g_cpuToAffinity = (CpuAffinity*)malloc(g_possibleCpuCount * sizeof(CpuAffinity));
99-
g_groupToCpuMask = (KAFFINITY*)malloc(g_groupCount * sizeof(KAFFINITY));
100-
g_groupToCpuCount = (BYTE*)malloc(g_groupCount * sizeof(BYTE));
101-
102-
memset(g_groupAndIndexToCpu, 0xff, g_groupCount * MaxCpusPerGroup * sizeof(short));
103-
memset(g_cpuToAffinity, 0xff, g_possibleCpuCount * sizeof(CpuAffinity));
104-
memset(g_groupToCpuMask, 0, g_groupCount * sizeof(KAFFINITY));
105-
memset(g_groupToCpuCount, 0, g_groupCount * sizeof(BYTE));
106-
}
107-
10889
/*++
10990
Function:
11091
FreeLookupArrays
@@ -125,6 +106,42 @@ FreeLookupArrays()
125106
g_groupToCpuCount = NULL;
126107
}
127108

109+
/*++
110+
Function:
111+
AllocateLookupArrays
112+
113+
Allocate CPU and group lookup arrays
114+
Return TRUE if the allocation succeeded
115+
--*/
116+
BOOL
117+
AllocateLookupArrays()
118+
{
119+
g_groupAndIndexToCpu = (short*)malloc(g_groupCount * MaxCpusPerGroup * sizeof(short));
120+
if (g_groupAndIndexToCpu != NULL)
121+
{
122+
g_cpuToAffinity = (CpuAffinity*)malloc(g_possibleCpuCount * sizeof(CpuAffinity));
123+
if (g_cpuToAffinity != NULL)
124+
{
125+
g_groupToCpuMask = (KAFFINITY*)malloc(g_groupCount * sizeof(KAFFINITY));
126+
if (g_groupToCpuMask != NULL)
127+
{
128+
g_groupToCpuCount = (BYTE*)malloc(g_groupCount * sizeof(BYTE));
129+
memset(g_groupAndIndexToCpu, 0xff, g_groupCount * MaxCpusPerGroup * sizeof(short));
130+
memset(g_cpuToAffinity, 0xff, g_possibleCpuCount * sizeof(CpuAffinity));
131+
memset(g_groupToCpuMask, 0, g_groupCount * sizeof(KAFFINITY));
132+
memset(g_groupToCpuCount, 0, g_groupCount * sizeof(BYTE));
133+
134+
return TRUE;
135+
}
136+
}
137+
}
138+
139+
// One of the allocations have failed
140+
FreeLookupArrays();
141+
142+
return FALSE;
143+
}
144+
128145
/*++
129146
Function:
130147
GetFullAffinityMask
@@ -191,7 +208,11 @@ FOR_ALL_NUMA_FUNCTIONS
191208
g_groupCount += nodeGroupCount;
192209
}
193210

194-
AllocateLookupArrays();
211+
if (!AllocateLookupArrays())
212+
{
213+
dlclose(numaHandle);
214+
return FALSE;
215+
}
195216

196217
WORD currentGroup = 0;
197218
int currentGroupCpus = 0;
@@ -246,7 +267,10 @@ FOR_ALL_NUMA_FUNCTIONS
246267
g_groupCount = 1;
247268
g_highestNumaNode = 0;
248269

249-
AllocateLookupArrays();
270+
if (!AllocateLookupArrays())
271+
{
272+
return FALSE;
273+
}
250274

251275
for (int i = 0; i < g_possibleCpuCount; i++)
252276
{
@@ -836,8 +860,7 @@ VirtualAllocExNuma(
836860
if (result != NULL && g_numaAvailable)
837861
{
838862
int nodeMaskLength = (g_highestNumaNode + 1 + sizeof(unsigned long) - 1) / sizeof(unsigned long);
839-
unsigned long *nodeMask = new unsigned long[nodeMaskLength];
840-
863+
unsigned long *nodeMask = (unsigned long*)alloca(nodeMaskLength * sizeof(unsigned long));
841864
memset(nodeMask, 0, nodeMaskLength);
842865

843866
int index = nndPreferred / sizeof(unsigned long);
@@ -846,7 +869,6 @@ VirtualAllocExNuma(
846869

847870
int st = mbind(result, dwSize, MPOL_PREFERRED, nodeMask, g_highestNumaNode, 0);
848871

849-
free(nodeMask);
850872
_ASSERTE(st == 0);
851873
// If the mbind fails, we still return the allocated memory since the nndPreferred is just a hint
852874
}

0 commit comments

Comments
 (0)