Skip to content

Commit 2eeacdf

Browse files
committed
Merge bitcoin#17527: Fix CPUID subleaf iteration
f93fc61 Put bounds on the number of CPUID leaves explored (Pieter Wuille) ba2c5fe Fix CPUID subleaf iteration (Pieter Wuille) Pull request description: This fixes bitcoin#17523. The code to determine which CPUID subleaves to explore was incorrect in bitcoin#17270. The new code here is based on Intel's reference documentation for CPUID (a document called "Intel® Processor Identification and the CPUID Instruction - Application Note 485", which I cannot actually find on their own website). ACKs for top commit: laanwj: ACK f93fc61 jonatack: ACK f93fc61 code review, tested rebased on current master bb862d7 with Debian 4.19 x86_64 mzumsande: ACK f93fc61, reviewed code and compared with the intel doc, tested on an AMD and an Intel processor. Tree-SHA512: 2790b326fa397b736c0f39f25807bea57de2752fdd58bf6693d044b8cb26df36c11cce165a334b471f8e33724f10e3b76edab5cc4e0e7776601aabda13277245
2 parents 9cbd87d + f93fc61 commit 2eeacdf

File tree

1 file changed

+18
-7
lines changed

1 file changed

+18
-7
lines changed

src/randomenv.cpp

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -197,19 +197,30 @@ void AddAllCPUID(CSHA512& hasher)
197197
// Iterate over all standard leaves
198198
AddCPUID(hasher, 0, 0, ax, bx, cx, dx); // Returns max leaf in ax
199199
uint32_t max = ax;
200-
for (uint32_t leaf = 1; leaf <= max; ++leaf) {
201-
for (uint32_t subleaf = 0;; ++subleaf) {
200+
for (uint32_t leaf = 1; leaf <= max && leaf <= 0xFF; ++leaf) {
201+
uint32_t maxsub = 0;
202+
for (uint32_t subleaf = 0; subleaf <= 0xFF; ++subleaf) {
202203
AddCPUID(hasher, leaf, subleaf, ax, bx, cx, dx);
203-
// Iterate over subleaves for leaf 4, 11, 13
204-
if (leaf != 4 && leaf != 11 && leaf != 13) break;
205-
if ((leaf == 4 || leaf == 13) && ax == 0) break;
206-
if (leaf == 11 && (cx & 0xFF00) == 0) break;
204+
// Iterate subleafs for leaf values 4, 7, 11, 13
205+
if (leaf == 4) {
206+
if ((ax & 0x1f) == 0) break;
207+
} else if (leaf == 7) {
208+
if (subleaf == 0) maxsub = ax;
209+
if (subleaf == maxsub) break;
210+
} else if (leaf == 11) {
211+
if ((cx & 0xff00) == 0) break;
212+
} else if (leaf == 13) {
213+
if (ax == 0 && bx == 0 && cx == 0 && dx == 0) break;
214+
} else {
215+
// For any other leaf, stop after subleaf 0.
216+
break;
217+
}
207218
}
208219
}
209220
// Iterate over all extended leaves
210221
AddCPUID(hasher, 0x80000000, 0, ax, bx, cx, dx); // Returns max extended leaf in ax
211222
uint32_t ext_max = ax;
212-
for (uint32_t leaf = 0x80000001; leaf <= ext_max; ++leaf) {
223+
for (uint32_t leaf = 0x80000001; leaf <= ext_max && leaf <= 0x800000FF; ++leaf) {
213224
AddCPUID(hasher, leaf, 0, ax, bx, cx, dx);
214225
}
215226
}

0 commit comments

Comments
 (0)