Skip to content

Commit d8b4077

Browse files
authored
Update shared memory bound checking for infer requests (#565)
Echos Server PR: triton-inference-server/server#7083
1 parent 0c13079 commit d8b4077

File tree

3 files changed

+37
-8
lines changed

3 files changed

+37
-8
lines changed

src/c++/perf_analyzer/client_backend/triton_c_api/shared_memory_manager.cc

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,9 @@ SharedMemoryManager::RegisterSystemMemory(
8989

9090
Error
9191
SharedMemoryManager::GetMemoryInfo(
92-
const std::string& name, size_t offset, void** shm_mapped_addr,
93-
TRITONSERVER_MemoryType* memory_type, int64_t* device_id)
92+
const std::string& name, size_t offset, size_t byte_size,
93+
void** shm_mapped_addr, TRITONSERVER_MemoryType* memory_type,
94+
int64_t* device_id)
9495
{
9596
// protect shared_memory_map_ from concurrent access
9697
std::lock_guard<std::mutex> lock(mu_);
@@ -100,6 +101,29 @@ SharedMemoryManager::GetMemoryInfo(
100101
return Error(
101102
std::string("Unable to find shared memory region: '" + name + "'"));
102103
}
104+
105+
// validate offset
106+
size_t shm_region_end = 0;
107+
if (it->second->kind_ == TRITONSERVER_MEMORY_CPU) {
108+
shm_region_end = it->second->offset_;
109+
}
110+
if (it->second->byte_size_ > 0) {
111+
shm_region_end += it->second->byte_size_ - 1;
112+
}
113+
if (offset > shm_region_end) {
114+
return Error(
115+
std::string("Invalid offset for shared memory region: '" + name + "'")
116+
.c_str());
117+
}
118+
// validate byte_size + offset is within memory bounds
119+
size_t total_req_shm = offset + byte_size - 1;
120+
if (total_req_shm > shm_region_end) {
121+
return Error(std::string(
122+
"Invalid offset + byte size for shared memory region: '" +
123+
name + "'")
124+
.c_str());
125+
}
126+
103127
if (it->second->kind_ == TRITONSERVER_MEMORY_CPU) {
104128
*shm_mapped_addr = (void*)((uint8_t*)it->second->mapped_addr_ +
105129
it->second->offset_ + offset);

src/c++/perf_analyzer/client_backend/triton_c_api/shared_memory_manager.h

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,19 +70,22 @@ class SharedMemoryManager {
7070
Error RegisterSystemMemory(
7171
const std::string& name, void* ptr, const size_t byte_size);
7272

73-
/// Get the access information for the shared memory block with the specified
74-
/// name. Return an Error if named block doesn't exist.
73+
/// Get the access information for the shared memory block
74+
/// with the specified name. Return an Error
75+
/// if named block doesn't exist.
7576
/// \param name The name of the shared memory block to get.
7677
/// \param offset The offset in the block
78+
/// \param byte_size The byte size to request for the shm region
7779
/// \param shm_mapped_addr Returns the pointer to the shared
7880
/// memory block with the specified name and offset
7981
/// \param memory_type Returns the type of the memory
8082
/// \param device_id Returns the device id associated with the
8183
/// memory block
8284
/// \return an Error indicating success or failure.
8385
Error GetMemoryInfo(
84-
const std::string& name, size_t offset, void** shm_mapped_addr,
85-
TRITONSERVER_MemoryType* memory_type, int64_t* device_id);
86+
const std::string& name, size_t offset, size_t byte_size,
87+
void** shm_mapped_addr, TRITONSERVER_MemoryType* memory_type,
88+
int64_t* device_id);
8689

8790
/// Removes the named shared memory block of the specified type from
8891
/// the manager. Any future attempt to get the details of this block

src/c++/perf_analyzer/client_backend/triton_c_api/triton_loader.cc

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -945,7 +945,8 @@ TritonLoader::Infer(
945945
TRITONSERVER_MemoryType memory_type;
946946
int64_t memory_type_id;
947947
RETURN_IF_ERROR(shm_manager_->GetMemoryInfo(
948-
shm_name, offset, &buf, &memory_type, &memory_type_id));
948+
shm_name, offset, shm_byte_size, &buf, &memory_type,
949+
&memory_type_id));
949950

950951
alloc_payload.output_map_.emplace(
951952
std::piecewise_construct, std::forward_as_tuple(output->Name()),
@@ -1149,7 +1150,8 @@ TritonLoader::AddInputs(
11491150
TRITONSERVER_MemoryType memory_type;
11501151
int64_t memory_type_id;
11511152
RETURN_IF_ERROR(shm_manager_->GetMemoryInfo(
1152-
shm_name, offset, &buf, &memory_type, &memory_type_id));
1153+
shm_name, offset, shm_byte_size, &buf, &memory_type,
1154+
&memory_type_id));
11531155
RETURN_IF_TRITONSERVER_ERROR(
11541156
inference_request_append_input_data_fn_(
11551157
irequest, input_name, buf, byte_size,

0 commit comments

Comments
 (0)