From 719bfcc82f2dd2e27125f834540bff8b35f94894 Mon Sep 17 00:00:00 2001 From: Jan Vorlicek Date: Tue, 14 Jan 2020 19:34:51 +0100 Subject: [PATCH] Port to 3.1 - Fix out of range access in GetRecycleMemoryInfo (#27959) 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 | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/vm/win32threadpool.h b/src/vm/win32threadpool.h index e05849e..f3fe62f 100644 --- a/src/vm/win32threadpool.h +++ b/src/vm/win32threadpool.h @@ -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]; -- 2.7.4