Skip to content

Commit db8aab4

Browse files
committed
GPU: Add check that struct was read correctly from file
1 parent 5d4d956 commit db8aab4

File tree

3 files changed

+34
-38
lines changed

3 files changed

+34
-38
lines changed

GPU/GPUTracking/Base/GPUReconstruction.cxx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1237,7 +1237,8 @@ int32_t GPUReconstruction::ReadSettings(const char* dir)
12371237
f = dir;
12381238
f += "settings.dump";
12391239
new (mGRPSettings.get()) GPUSettingsGRP;
1240-
if (ReadStructFromFile(f.c_str(), mGRPSettings.get())) {
1240+
bool error;
1241+
if (ReadStructFromFile(f.c_str(), mGRPSettings.get(), &error) && error) {
12411242
return 1;
12421243
}
12431244
param().UpdateSettings(mGRPSettings.get());

GPU/GPUTracking/Base/GPUReconstruction.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -302,9 +302,7 @@ class GPUReconstruction
302302
template <class T>
303303
void DumpDynamicStructToFile(const T* obj, size_t dynamicSize, const char* file);
304304
template <class T>
305-
std::unique_ptr<T> ReadStructFromFile(const char* file);
306-
template <class T>
307-
int32_t ReadStructFromFile(const char* file, T* obj);
305+
std::unique_ptr<T> ReadStructFromFile(const char* file, T* obj = nullptr, bool* errorOnMissing = nullptr);
308306
template <class T, auto F>
309307
aligned_unique_buffer_ptr<T> ReadDynamicStructFromFile(const char* file);
310308

GPU/GPUTracking/Base/GPUReconstructionIO.h

Lines changed: 31 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ inline T* GPUReconstruction::AllocateIOMemoryHelper(size_t n, const T*& ptr, std
4141
retVal = u.get();
4242
if (GetProcessingSettings().registerStandaloneInputMemory) {
4343
if (registerMemoryForGPU(u.get(), n * sizeof(T))) {
44-
GPUError("Error registering memory for GPU: %p - %ld bytes\n", (void*)u.get(), (int64_t)(n * sizeof(T)));
44+
GPUError("Error registering memory for GPU: %p - %zu bytes\n", (void*)u.get(), n * sizeof(T));
4545
throw std::bad_alloc();
4646
}
4747
}
@@ -69,7 +69,7 @@ inline uint32_t GPUReconstruction::DumpData(FILE* fp, const T* const* entries, c
6969
}
7070
}
7171
if (GetProcessingSettings().debugLevel >= 2) {
72-
GPUInfo("Dumped %ld %s", (int64_t)numTotal, IOTYPENAMES[type]);
72+
GPUInfo("Dumped %zu %s", numTotal, IOTYPENAMES[type]);
7373
}
7474
return numTotal;
7575
}
@@ -103,7 +103,7 @@ inline size_t GPUReconstruction::ReadData(FILE* fp, const T** entries, S* num, s
103103
}
104104
(void)r;
105105
if (GetProcessingSettings().debugLevel >= 2) {
106-
GPUInfo("Read %ld %s", (int64_t)numTotal, IOTYPENAMES[type]);
106+
GPUInfo("Read %zu %s", numTotal, IOTYPENAMES[type]);
107107
}
108108
return numTotal;
109109
}
@@ -133,7 +133,7 @@ inline std::unique_ptr<T> GPUReconstruction::ReadFlatObjectFromFile(const char*
133133
r = fread(size, sizeof(size[0]), 2, fp);
134134
if (r == 0 || size[0] != sizeof(T)) {
135135
fclose(fp);
136-
GPUError("ERROR reading %s, invalid size: %ld (%ld expected)", file, (int64_t)size[0], (int64_t)sizeof(T));
136+
GPUError("ERROR reading %s, invalid size: %zu (%zu expected)", file, size[0], sizeof(T));
137137
throw std::runtime_error("invalid size");
138138
}
139139
std::unique_ptr<T> retVal(new T);
@@ -143,7 +143,7 @@ inline std::unique_ptr<T> GPUReconstruction::ReadFlatObjectFromFile(const char*
143143
r = fread(buf, 1, size[1], fp);
144144
fclose(fp);
145145
if (GetProcessingSettings().debugLevel >= 2) {
146-
GPUInfo("Read %ld bytes from %s", (int64_t)r, file);
146+
GPUInfo("Read %zu bytes from %s", r, file);
147147
}
148148
retVal->clearInternalBufferPtr();
149149
retVal->setActualBufferAddress(buf);
@@ -165,47 +165,40 @@ inline void GPUReconstruction::DumpStructToFile(const T* obj, const char* file)
165165
}
166166

167167
template <class T>
168-
inline std::unique_ptr<T> GPUReconstruction::ReadStructFromFile(const char* file)
168+
inline std::unique_ptr<T> GPUReconstruction::ReadStructFromFile(const char* file, T* obj, bool* errorOnMissing)
169169
{
170170
FILE* fp = fopen(file, "rb");
171171
if (fp == nullptr) {
172+
if (errorOnMissing) {
173+
*errorOnMissing = true;
174+
}
172175
return nullptr;
173176
}
174177
size_t size, r;
175178
r = fread(&size, sizeof(size), 1, fp);
176179
if (r == 0 || size != sizeof(T)) {
177180
fclose(fp);
178-
GPUError("ERROR reading %s, invalid size: %ld (%ld expected)", file, (int64_t)size, (int64_t)sizeof(T));
181+
GPUError("ERROR reading %s, invalid size: %zu (%zu expected)", file, size, sizeof(T));
179182
throw std::runtime_error("invalid size");
180183
}
181-
std::unique_ptr<T> newObj(new T);
182-
r = fread(newObj.get(), 1, size, fp);
183-
fclose(fp);
184-
if (GetProcessingSettings().debugLevel >= 2) {
185-
GPUInfo("Read %ld bytes from %s", (int64_t)r, file);
186-
}
187-
return newObj;
188-
}
189-
190-
template <class T>
191-
inline int32_t GPUReconstruction::ReadStructFromFile(const char* file, T* obj)
192-
{
193-
FILE* fp = fopen(file, "rb");
194-
if (fp == nullptr) {
195-
return 1;
196-
}
197-
size_t size, r;
198-
r = fread(&size, sizeof(size), 1, fp);
199-
if (r == 0) {
200-
fclose(fp);
201-
return 1;
184+
std::unique_ptr<T> retVal(nullptr);
185+
if (obj == nullptr) {
186+
retVal = std::make_unique<T>();
187+
obj = retVal.get();
202188
}
203189
r = fread(obj, 1, size, fp);
204190
fclose(fp);
191+
if (r != size) {
192+
GPUError("ERROR reading %s, read %zu (%zu expected)", file, r, size);
193+
throw std::runtime_error("invalid size");
194+
}
205195
if (GetProcessingSettings().debugLevel >= 2) {
206-
GPUInfo("Read %ld bytes from %s", (int64_t)r, file);
196+
GPUInfo("Read %zu bytes from %s", r, file);
197+
}
198+
if (errorOnMissing) {
199+
*errorOnMissing = false;
207200
}
208-
return 0;
201+
return retVal;
209202
}
210203

211204
template <class T>
@@ -234,27 +227,31 @@ inline aligned_unique_buffer_ptr<T> GPUReconstruction::ReadDynamicStructFromFile
234227
r2 = fread(&dynsize, sizeof(dynsize), 1, fp);
235228
if (r == 0 || r2 == 0 || size != sizeof(T) || dynsize < size) {
236229
fclose(fp);
237-
GPUError("ERROR reading %s, invalid size: %ld (%ld buffer size, %ld object size expected)", file, (int64_t)size, (int64_t)dynsize, (int64_t)sizeof(T));
230+
GPUError("ERROR reading %s, invalid size: %zu (%zu buffer size, %zu object size expected)", file, size, dynsize, sizeof(T));
238231
throw std::runtime_error("invalid size");
239232
}
240233
std::unique_ptr<T> tmp = std::make_unique<T>();
241234
r = fread(tmp.get(), sizeof(T), 1, fp);
242235
if (r == 0) {
243236
fclose(fp);
244-
GPUError("ERROR reading %s", file, (int64_t)size, (int64_t)sizeof(T));
237+
GPUError("ERROR reading %s %zu (%zu expected)", file, size, sizeof(T));
245238
throw std::runtime_error("read error");
246239
}
247240
if ((tmp.get()->*F)() != dynsize) {
248241
fclose(fp);
249-
GPUError("ERROR: invalid size: %ld (%ld expected)", file, (int64_t)dynsize, (int64_t)(tmp.get()->*F)());
242+
GPUError("ERROR in %s: invalid size: %zu (%zu expected)", file, dynsize, (tmp.get()->*F)());
250243
throw std::runtime_error("invalid size");
251244
}
252245
aligned_unique_buffer_ptr<T> newObj(dynsize);
253246
memcpy(newObj.get(), tmp.get(), sizeof(T));
254247
r = fread(newObj.getraw() + sizeof(T), 1, dynsize - sizeof(T), fp);
255248
fclose(fp);
249+
if (r != dynsize - sizeof(T)) {
250+
GPUError("ERROR in %s: File Read error in %s: %zu (%zu expected)", file, r, dynsize);
251+
throw std::runtime_error("invalid size");
252+
}
256253
if (GetProcessingSettings().debugLevel >= 2) {
257-
GPUInfo("Read %ld bytes from %s", (int64_t)r, file);
254+
GPUInfo("Read %zu bytes from %s", r + dynsize, file);
258255
}
259256
return newObj;
260257
}

0 commit comments

Comments
 (0)