26
26
27
27
#include " pb_memory.h"
28
28
29
+ #include < sstream>
30
+
29
31
namespace triton { namespace backend { namespace python {
30
32
31
33
std::unique_ptr<PbMemory>
@@ -225,12 +227,6 @@ PbMemory::LoadFromSharedMemory(
225
227
{
226
228
MemoryShm* memory_shm_ptr = reinterpret_cast <MemoryShm*>(data_shm);
227
229
char * memory_data_shm = data_shm + sizeof (MemoryShm);
228
-
229
- if (memory_data_shm + memory_shm_ptr->byte_size >
230
- (char *)shm_pool->GetBaseAddress () + shm_pool->GetCurrentCapacity ()) {
231
- throw PythonBackendException (" Attempted to access out of bounds memory." );
232
- }
233
-
234
230
char * data_ptr = nullptr ;
235
231
bool opened_cuda_ipc_handle = false ;
236
232
if (memory_shm_ptr->memory_type == TRITONSERVER_MEMORY_GPU &&
@@ -265,6 +261,19 @@ PbMemory::LoadFromSharedMemory(
265
261
} else {
266
262
data_ptr = memory_data_shm;
267
263
}
264
+
265
+ // This check only validates CPU shared memory access.
266
+ if (memory_shm_ptr->memory_type != TRITONSERVER_MEMORY_GPU &&
267
+ (data_ptr + memory_shm_ptr->byte_size >
268
+ (char *)shm_pool->GetBaseAddress () + shm_pool->GetCurrentCapacity ())) {
269
+ std::ostringstream oss;
270
+ oss << " 0x" << std::hex
271
+ << (reinterpret_cast <uintptr_t >(data_ptr) + memory_shm_ptr->byte_size );
272
+ throw PythonBackendException (
273
+ std::string (" Attempted to access out of bounds memory address " ) +
274
+ oss.str ());
275
+ }
276
+
268
277
return std::unique_ptr<PbMemory>(new PbMemory (
269
278
data_shm, data_ptr, handle,
270
279
opened_cuda_ipc_handle /* opened_cuda_ipc_handle */ ));
@@ -280,11 +289,6 @@ PbMemory::LoadFromSharedMemory(
280
289
reinterpret_cast <MemoryShm*>(memory_shm.data_ .get ());
281
290
char * memory_data_shm = memory_shm.data_ .get () + sizeof (MemoryShm);
282
291
283
- if (memory_data_shm + memory_shm_ptr->byte_size >
284
- (char *)shm_pool->GetBaseAddress () + shm_pool->GetCurrentCapacity ()) {
285
- throw PythonBackendException (" Attempted to access out of bounds memory." );
286
- }
287
-
288
292
char * data_ptr = nullptr ;
289
293
bool opened_cuda_ipc_handle = false ;
290
294
if (memory_shm_ptr->memory_type == TRITONSERVER_MEMORY_GPU) {
@@ -319,6 +323,19 @@ PbMemory::LoadFromSharedMemory(
319
323
} else {
320
324
data_ptr = memory_data_shm;
321
325
}
326
+
327
+ // This check only validates CPU shared memory access.
328
+ if (memory_shm_ptr->memory_type != TRITONSERVER_MEMORY_GPU &&
329
+ (data_ptr + memory_shm_ptr->byte_size >
330
+ (char *)shm_pool->GetBaseAddress () + shm_pool->GetCurrentCapacity ())) {
331
+ std::ostringstream oss;
332
+ oss << " 0x" << std::hex
333
+ << (reinterpret_cast <uintptr_t >(data_ptr) + memory_shm_ptr->byte_size );
334
+ throw PythonBackendException (
335
+ std::string (" Attempted to access out of bounds memory address " ) +
336
+ oss.str ());
337
+ }
338
+
322
339
return std::unique_ptr<PbMemory>(new PbMemory (
323
340
memory_shm, data_ptr,
324
341
opened_cuda_ipc_handle /* opened_cuda_ipc_handle */ ));
0 commit comments