mirror of
https://github.com/official-stockfish/Stockfish.git
synced 2026-07-22 12:47:08 +00:00
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:
committed by
Joost VandeVondele
parent
3f2405bf4e
commit
229bd1e2e3
+13
-3
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
@@ -169,6 +169,7 @@ class Position {
|
||||
|
||||
// Position consistency check, for debugging
|
||||
bool pos_is_ok() const;
|
||||
bool material_key_is_ok() const;
|
||||
void flip();
|
||||
|
||||
StateInfo* state() const;
|
||||
@@ -180,6 +181,7 @@ class Position {
|
||||
private:
|
||||
// Initialization helpers (used while setting up a position)
|
||||
void set_castling_right(Color c, Square rfrom);
|
||||
Key compute_material_key() const;
|
||||
void set_state() const;
|
||||
void set_check_info() const;
|
||||
|
||||
|
||||
@@ -1214,6 +1214,8 @@ template<TBType Type>
|
||||
void* mapped(TBTable<Type>& e, const Position& pos) {
|
||||
|
||||
static std::mutex mutex;
|
||||
// Because TB is the only usage of materialKey, check it here in debug mode
|
||||
assert(pos.material_key_is_ok());
|
||||
|
||||
// Use 'acquire' to avoid a thread reading 'ready' == true while
|
||||
// another is still working. (compiler reordering may cause this).
|
||||
|
||||
Reference in New Issue
Block a user