Skip to content

Commit 595fc34

Browse files
committed
Fix a possible overflow in TT resize
On platforms where size_t is 32 bit, we can have an overflow in this expression: (mbSize * 1024 * 1024) Fix it setting max hash size of 2GB on platforms where size_t is 32 bit. A small rename while there: now struct Cluster is definied inside class TranspositionTable so we should drop the redundant TT prefix. No functional change.
1 parent 58fdb84 commit 595fc34

File tree

3 files changed

+10
-8
lines changed

3 files changed

+10
-8
lines changed

src/tt.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,17 @@ TranspositionTable TT; // Our global transposition table
3232

3333
void TranspositionTable::resize(size_t mbSize) {
3434

35-
assert(sizeof(TTCluster) == CacheLineSize / 2);
35+
assert(sizeof(Cluster) == CacheLineSize / 2);
3636

37-
size_t newClusterCount = size_t(1) << msb((mbSize * 1024 * 1024) / sizeof(TTCluster));
37+
size_t newClusterCount = size_t(1) << msb((mbSize * 1024 * 1024) / sizeof(Cluster));
3838

3939
if (newClusterCount == clusterCount)
4040
return;
4141

4242
clusterCount = newClusterCount;
4343

4444
free(mem);
45-
mem = calloc(clusterCount * sizeof(TTCluster) + CacheLineSize - 1, 1);
45+
mem = calloc(clusterCount * sizeof(Cluster) + CacheLineSize - 1, 1);
4646

4747
if (!mem)
4848
{
@@ -51,7 +51,7 @@ void TranspositionTable::resize(size_t mbSize) {
5151
exit(EXIT_FAILURE);
5252
}
5353

54-
table = (TTCluster*)((uintptr_t(mem) + CacheLineSize - 1) & ~(CacheLineSize - 1));
54+
table = (Cluster*)((uintptr_t(mem) + CacheLineSize - 1) & ~(CacheLineSize - 1));
5555
}
5656

5757

@@ -61,7 +61,7 @@ void TranspositionTable::resize(size_t mbSize) {
6161

6262
void TranspositionTable::clear() {
6363

64-
std::memset(table, 0, clusterCount * sizeof(TTCluster));
64+
std::memset(table, 0, clusterCount * sizeof(Cluster));
6565
}
6666

6767

src/tt.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ class TranspositionTable {
7676
static const int CacheLineSize = 64;
7777
static const int TTClusterSize = 3;
7878

79-
struct TTCluster {
79+
struct Cluster {
8080
TTEntry entry[TTClusterSize];
8181
char padding[2]; // Align to the cache line size
8282
};
@@ -96,7 +96,7 @@ class TranspositionTable {
9696

9797
private:
9898
size_t clusterCount;
99-
TTCluster* table;
99+
Cluster* table;
100100
void* mem;
101101
uint8_t generation8; // Size must be not bigger than TTEntry::genBound8
102102
};

src/ucioption.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,13 @@ bool CaseInsensitiveLess::operator() (const string& s1, const string& s2) const
5454

5555
void init(OptionsMap& o) {
5656

57+
const int MaxHashMB = Is64Bit ? 1024 * 1024 : 2048;
58+
5759
o["Write Debug Log"] << Option(false, on_logger);
5860
o["Contempt"] << Option(0, -100, 100);
5961
o["Min Split Depth"] << Option(0, 0, 12, on_threads);
6062
o["Threads"] << Option(1, 1, MAX_THREADS, on_threads);
61-
o["Hash"] << Option(16, 1, 1024 * 1024, on_hash_size);
63+
o["Hash"] << Option(16, 1, MaxHashMB, on_hash_size);
6264
o["Clear Hash"] << Option(on_clear_hash);
6365
o["Ponder"] << Option(true);
6466
o["MultiPV"] << Option(1, 1, 500);

0 commit comments

Comments
 (0)