Skip to content

Commit bb843a0

Browse files
vondelesnicolet
authored andcommitted
Check tablebase files
This addresses partially issue #1911 in that it documents in our Readme the command that users can use to verifying the md5sum of their downloaded tablebase files. Additionally, a quick check of the file size (the size of each tablebase file modulo 64 is 16 as pointed out by @syzygy1) has been implemented at launch time in Stockfish. Closes #1927 and #1911 No functional change.
1 parent 3c576ef commit bb843a0

File tree

2 files changed

+29
-9
lines changed

2 files changed

+29
-9
lines changed

Readme.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,9 @@ Currently, Stockfish has the following UCI options:
8686
Example: `C:\tablebases\wdl345;C:\tablebases\wdl6;D:\tablebases\dtz345;D:\tablebases\dtz6`
8787

8888
It is recommended to store .rtbw files on an SSD. There is no loss in storing
89-
the .rtbz files on a regular HD.
89+
the .rtbz files on a regular HD. It is recommended to verify all md5 checksums
90+
of the downloaded tablebase files (`md5sum -c checksum.md5`) as corruption will
91+
lead to engine crashes.
9092

9193
* #### SyzygyProbeDepth
9294
Minimum remaining search depth for which a position is probed. Set this option

src/syzygy/tbprobe.cpp

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -214,14 +214,22 @@ class TBFile : public std::ifstream {
214214
return *baseAddress = nullptr, nullptr;
215215

216216
fstat(fd, &statbuf);
217+
218+
if (statbuf.st_size % 64 != 16)
219+
{
220+
std::cerr << "Corrupt tablebase file " << fname << std::endl;
221+
exit(EXIT_FAILURE);
222+
}
223+
217224
*mapping = statbuf.st_size;
218225
*baseAddress = mmap(nullptr, statbuf.st_size, PROT_READ, MAP_SHARED, fd, 0);
219226
madvise(*baseAddress, statbuf.st_size, MADV_RANDOM);
220227
::close(fd);
221228

222-
if (*baseAddress == MAP_FAILED) {
229+
if (*baseAddress == MAP_FAILED)
230+
{
223231
std::cerr << "Could not mmap() " << fname << std::endl;
224-
exit(1);
232+
exit(EXIT_FAILURE);
225233
}
226234
#else
227235
// Note FILE_FLAG_RANDOM_ACCESS is only a hint to Windows and as such may get ignored.
@@ -233,29 +241,39 @@ class TBFile : public std::ifstream {
233241

234242
DWORD size_high;
235243
DWORD size_low = GetFileSize(fd, &size_high);
244+
245+
if (size_low % 64 != 16)
246+
{
247+
std::cerr << "Corrupt tablebase file " << fname << std::endl;
248+
exit(EXIT_FAILURE);
249+
}
250+
236251
HANDLE mmap = CreateFileMapping(fd, nullptr, PAGE_READONLY, size_high, size_low, nullptr);
237252
CloseHandle(fd);
238253

239-
if (!mmap) {
254+
if (!mmap)
255+
{
240256
std::cerr << "CreateFileMapping() failed" << std::endl;
241-
exit(1);
257+
exit(EXIT_FAILURE);
242258
}
243259

244260
*mapping = (uint64_t)mmap;
245261
*baseAddress = MapViewOfFile(mmap, FILE_MAP_READ, 0, 0, 0);
246262

247-
if (!*baseAddress) {
263+
if (!*baseAddress)
264+
{
248265
std::cerr << "MapViewOfFile() failed, name = " << fname
249266
<< ", error = " << GetLastError() << std::endl;
250-
exit(1);
267+
exit(EXIT_FAILURE);
251268
}
252269
#endif
253270
uint8_t* data = (uint8_t*)*baseAddress;
254271

255272
constexpr uint8_t Magics[][4] = { { 0xD7, 0x66, 0x0C, 0xA5 },
256273
{ 0x71, 0xE8, 0x23, 0x5D } };
257274

258-
if (memcmp(data, Magics[type == WDL], 4)) {
275+
if (memcmp(data, Magics[type == WDL], 4))
276+
{
259277
std::cerr << "Corrupted table in file " << fname << std::endl;
260278
unmap(*baseAddress, *mapping);
261279
return *baseAddress = nullptr, nullptr;
@@ -416,7 +434,7 @@ class TBTables {
416434
}
417435
}
418436
std::cerr << "TB hash table size too low!" << std::endl;
419-
exit(1);
437+
exit(EXIT_FAILURE);
420438
}
421439

422440
public:

0 commit comments

Comments
 (0)