gz / rust-cpuid

cpuid library in rust.
https://docs.rs/raw-cpuid/
MIT License
147 stars 43 forks source link

cpuid dies on overflow on AMD 7713P #133

Closed bcantrill closed 1 year ago

bcantrill commented 1 year ago

Thanks for this useful crate! Running the cpuid binary on an AMD 7713P with overflow detection enabled (either via the debug profile or explicitly enabled) results in a panic:

...
Processor Brand String = "AMD EPYC 7713P 64-Core Processor"
L1 TLB 2/4 MiB entries (0x8000_0005/eax):
┌──────────────────┬─────────────────┐
│     iTLB #entries│               64│
│iTLB associativity│Fully associative│
│     dTLB #entries│               64│
│dTLB associativity│Fully associative│
└──────────────────┴─────────────────┘
L1 TLB 4 KiB entries (0x8000_0005/ebx):
┌──────────────────┬─────────────────┐
│     iTLB #entries│               64│
│iTLB associativity│Fully associative│
│     dTLB #entries│               64│
│dTLB associativity│Fully associative│
└──────────────────┴─────────────────┘
L1 dCache (0x8000_0005/ecx):
┌─────────────────┬───────┐
│line size [Bytes]│     64│
│    lines per tag│      1│
│    associativity│NWay(8)│
│       size [KiB]│     32│
└─────────────────┴───────┘
L1 iCache (0x8000_0005/edx):
┌─────────────────┬───────┐
│line size [Bytes]│     64│
│    lines per tag│      1│
│    associativity│NWay(8)│
│       size [KiB]│     32│
└─────────────────┴───────┘
L2 TLB 2/4 MiB entries (0x8000_0006/eax):
┌──────────────────┬───────┐
│     iTLB #entries│    512│
│iTLB associativity│NWay(2)│
│     dTLB #entries│   2048│
│dTLB associativity│NWay(4)│
└──────────────────┴───────┘
L2 TLB 4 KiB entries (0x8000_0006/ebx):
┌──────────────────┬───────┐
│     iTLB #entries│    512│
│iTLB associativity│NWay(4)│
│     dTLB #entries│   2048│
│dTLB associativity│NWay(8)│
└──────────────────┴───────┘
L2 Cache (0x8000_0006/ecx):
┌─────────────────┬───────┐
│line size [Bytes]│     64│
│    lines per tag│      1│
│    associativity│NWay(8)│
│       size [KiB]│    512│
└─────────────────┴───────┘
L3 Cache (0x8000_0006/edx):
thread 'main' panicked at 'attempt to multiply with overflow', src/bin/cpuid.rs:1300:45
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

The problem is that l3cache_size() returns a u16 -- and the cache size here (512) times 512 exceeds a 16-bit quantity; it should be promoted to a u32 before being multiplied:

diff --git a/src/bin/cpuid.rs b/src/bin/cpuid.rs
index e58ce16..f27d11f 100644
--- a/src/bin/cpuid.rs
+++ b/src/bin/cpuid.rs
@@ -1297,7 +1297,7 @@ fn markdown(_opts: Opts) {
                 RowGen::tuple("line size [Bytes]", info.l3cache_line_size()),
                 RowGen::tuple("lines per tag", info.l3cache_lines_per_tag()),
                 RowGen::tuple("associativity", info.l3cache_associativity()),
-                RowGen::tuple("size [KiB]", info.l3cache_size() * 512),
+                RowGen::tuple("size [KiB]", info.l3cache_size() as u32 * 512),
             ],
         );
     }

Thanks again for the crate!

gz commented 1 year ago

fixed in 10.6.1 thanks a lot for reporting this!