git: 78f8ebe0c343 - stable/13 - hwpmc_intel: assert for correct nclasses value

From: Alexander Motin <mav_at_FreeBSD.org>
Date: Mon, 04 Jul 2022 18:09:09 UTC
The branch stable/13 has been updated by mav:

URL: https://cgit.FreeBSD.org/src/commit/?id=78f8ebe0c343eb43f832adb6061d610b777c6a76

commit 78f8ebe0c343eb43f832adb6061d610b777c6a76
Author:     Mitchell Horne <mhorne@FreeBSD.org>
AuthorDate: 2021-08-04 17:37:05 +0000
Commit:     Alexander Motin <mav@FreeBSD.org>
CommitDate: 2022-07-04 17:48:08 +0000

    hwpmc_intel: assert for correct nclasses value
    
    This variable is set based on the exact CPU model detected. If this
    value is set too small, it could lead to a NULL-dereference from an
    improperly initialized pmc_rowindex_to_classdep array.
    
    Though it has been fixed, this was previously the case for Broadwell.
    Add two asserts to catch this in DEBUG kernels, as it represents a
    configuration error that may be hard to uncover otherwise.
    
    PR:             253687
    Reported by:    Zhenlei Huang <zlei.huang@gmail.com>
    Sponsored by:   The FreeBSD Foundation
    
    (cherry picked from commit 8399d923a5697b7c194dbd44c33018c94ec42c4e)
---
 sys/dev/hwpmc/hwpmc_intel.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/sys/dev/hwpmc/hwpmc_intel.c b/sys/dev/hwpmc/hwpmc_intel.c
index 1f05489023c6..2d8377e1a838 100644
--- a/sys/dev/hwpmc/hwpmc_intel.c
+++ b/sys/dev/hwpmc/hwpmc_intel.c
@@ -276,6 +276,7 @@ pmc_intel_initialize(void)
 	case PMC_CPU_INTEL_IVYBRIDGE_XEON:
 	case PMC_CPU_INTEL_HASWELL:
 	case PMC_CPU_INTEL_HASWELL_XEON:
+		MPASS(nclasses >= PMC_MDEP_CLASS_INDEX_IAF);
 		error = pmc_core_initialize(pmc_mdep, ncpus, verov);
 		break;
 
@@ -317,6 +318,7 @@ pmc_intel_initialize(void)
 	case PMC_CPU_INTEL_BROADWELL:
 	case PMC_CPU_INTEL_SANDYBRIDGE:
 #endif
+		MPASS(nclasses >= PMC_MDEP_CLASS_INDEX_UCF);
 		error = pmc_uncore_initialize(pmc_mdep, ncpus);
 		break;
 	default: