check for material key validity in tbprobe

During recent work on threat inputs, there was a hard-to-debug crash in tbprobe
caused by incorrectly computing st->materialKey. This PR adds correctness
checking to pos_is_ok and a faster check in tbprobe that will actually run in
CI (because pos_is_ok checking is disabled even in debug mode, for performance
purposes).

closes https://github.com/official-stockfish/Stockfish/pull/6410

No functional change
This commit is contained in:
Timothy Herchen
2025-11-13 22:42:10 +01:00
committed by Joost VandeVondele
parent 3f2405bf4e
commit 229bd1e2e3
3 changed files with 17 additions and 3 deletions
+13 -3
View File
@@ -336,8 +336,8 @@ void Position::set_check_info() const {
// The function is only used when a new position is set up
void Position::set_state() const {
st->key = st->materialKey = 0;
st->minorPieceKey = 0;
st->key = 0;
st->minorPieceKey = 0;
st->nonPawnKey[WHITE] = st->nonPawnKey[BLACK] = 0;
st->pawnKey = Zobrist::noPawns;
st->nonPawnMaterial[WHITE] = st->nonPawnMaterial[BLACK] = VALUE_ZERO;
@@ -375,10 +375,15 @@ void Position::set_state() const {
st->key ^= Zobrist::side;
st->key ^= Zobrist::castling[st->castlingRights];
st->materialKey = compute_material_key();
}
Key Position::compute_material_key() const {
Key k = 0;
for (Piece pc : Pieces)
for (int cnt = 0; cnt < pieceCount[pc]; ++cnt)
st->materialKey ^= Zobrist::psq[pc][8 + cnt];
k ^= Zobrist::psq[pc][8 + cnt];
return k;
}
@@ -1447,6 +1452,9 @@ void Position::flip() {
}
bool Position::material_key_is_ok() const { return compute_material_key() == st->materialKey; }
// Performs some consistency checks for the position object
// and raise an assert if something wrong is detected.
// This is meant to be helpful when debugging.
@@ -1496,6 +1504,8 @@ bool Position::pos_is_ok() const {
assert(0 && "pos_is_ok: Castling");
}
assert(material_key_is_ok() && "pos_is_ok: materialKey");
return true;
}