Port to 3.1 - Fix out of range access in GetRecycleMemoryInfo (#27959)
authorJan Vorlicek <janvorli@microsoft.com>
Tue, 14 Jan 2020 18:34:51 +0000 (19:34 +0100)
committerAnirudh Agnihotry <anirudhagnihotry098@gmail.com>
Tue, 14 Jan 2020 18:34:51 +0000 (10:34 -0800)
Ports change #26873 to release 3.1 branch.

On OpenVZ virtualized linux, GetCurrentProcessorNumber which uses sched_getcpu()
can return a value greater than the number of processors reported by
sched_getaffinity with CPU_COUNT or sysconf(_SC_NPROCESSORS_ONLN).
For example, taskset -c 2,3 ./MyApp will make CPU_COUNT be 2 but
sched_getcpu() can return 2 or 3, and OpenVZ kernel can make
sysconf(_SC_NPROCESSORS_ONLN) return a limited cpu count but
sched_getcpu() still report the real processor number.

Example of affinity vs current CPU id on OpenVZ:
nproc: 8
nprocOnline: 1
affinity: 1, 0, 0, 0, 0, 0, 0, 0, cpuid: 2
affinity: 1, 0, 0, 0, 0, 0, 0, 0, cpuid: 2
affinity: 1, 0, 0, 0, 0, 0, 0, 0, cpuid: 2
affinity: 1, 0, 0, 0, 0, 0, 0, 0, cpuid: 2
affinity: 1, 0, 0, 0, 0, 0, 0, 0, cpuid: 2
affinity: 1, 0, 0, 0, 0, 0, 0, 0, cpuid: 5
affinity: 1, 0, 0, 0, 0, 0, 0, 0, cpuid: 5

src/vm/win32threadpool.h

index e05849e..f3fe62f 100644 (file)
@@ -749,7 +749,9 @@ public:
 #else // !FEATURE_PAL
             if (PAL_HasGetCurrentProcessorNumber())
             {
-                processorNumber = GetCurrentProcessorNumber();
+                // On linux, GetCurrentProcessorNumber which uses sched_getcpu() can return a value greater than the number
+                // of processors reported by sysconf(_SC_NPROCESSORS_ONLN) when using OpenVZ kernel.
+                processorNumber = GetCurrentProcessorNumber()%NumberOfProcessors;
             }
 #endif // !FEATURE_PAL
             return pRecycledListPerProcessor[processorNumber][memType];