Skip to content

Commit e2e2ae5

Browse files
mc-nvpskiran1
andauthored
fix: Improve validation for system shared memory register (#8273) (#8310)
Co-authored-by: Sai Kiran Polisetty <spolisetty@nvidia.com>
1 parent c70ea03 commit e2e2ae5

4 files changed

Lines changed: 34 additions & 1 deletion

File tree

qa/L0_shared_memory/shared_memory_test.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,24 @@ def test_python_client_leak(self):
509509
"client memory usage is increasing",
510510
)
511511

512+
def test_register_reserved_names(self):
513+
"""
514+
Test that registration fails if attempting to use a reserved
515+
prefix for the shm key.
516+
"""
517+
# This matches kTritonSharedMemoryRegionPrefix in the server code.
518+
reserved_prefix = "triton_python_backend_shm_region_"
519+
520+
# The shared memory key cannot start with the reserved prefix.
521+
shm_name = "my_test_shm_name"
522+
shm_key = f"{reserved_prefix}_my_test_shm_key"
523+
524+
with self.assertRaisesRegex(
525+
utils.InferenceServerException,
526+
f"cannot register shared memory region '{shm_name}' with key '{shm_key}' as the key contains the reserved prefix '{reserved_prefix}'",
527+
) as e:
528+
self.triton_client.register_system_shared_memory(shm_name, shm_key, 10000)
529+
512530

513531
def callback(user_data, result, error):
514532
if error:

qa/L0_shared_memory/test.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ for i in \
5555
test_infer_byte_size_out_of_bound \
5656
test_infer_integer_overflow \
5757
test_register_out_of_bound \
58+
test_register_reserved_names \
5859
test_python_client_leak; do
5960
for client_type in http grpc; do
6061
SERVER_ARGS="--model-repository=`pwd`/models --log-verbose=1 ${SERVER_ARGS_EXTRA}"

src/common.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ constexpr char kContentEncodingHTTPHeader[] = "Content-Encoding";
4545
constexpr char kContentTypeHeader[] = "Content-Type";
4646
constexpr char kContentLengthHeader[] = "Content-Length";
4747

48+
// This prefix is reserved for shm regions created internally by Triton
49+
constexpr char kTritonSharedMemoryRegionPrefix[] =
50+
"triton_python_backend_shm_region_";
51+
4852
constexpr int MAX_GRPC_MESSAGE_SIZE = INT32_MAX;
4953

5054
/// The value for a dimension in a shape that indicates that that

src/shared_memory_manager.cc

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,17 @@ SharedMemoryManager::RegisterSystemSharedMemory(
354354
const std::string& name, const std::string& shm_key, const size_t offset,
355355
const size_t byte_size)
356356
{
357+
// Check if the shared memory key starts with the reserved prefix
358+
if (shm_key.rfind(kTritonSharedMemoryRegionPrefix, 0) == 0) {
359+
return TRITONSERVER_ErrorNew(
360+
TRITONSERVER_ERROR_INVALID_ARG,
361+
std::string(
362+
"cannot register shared memory region '" + name + "' with key '" +
363+
shm_key + "' as the key contains the reserved prefix '" +
364+
kTritonSharedMemoryRegionPrefix + "'")
365+
.c_str());
366+
}
367+
357368
std::lock_guard<std::mutex> lock(mu_);
358369

359370
if (shared_memory_map_.find(name) != shared_memory_map_.end()) {
@@ -670,7 +681,6 @@ SharedMemoryManager::UnregisterAll(TRITONSERVER_MemoryType memory_type)
670681
++next_it;
671682
if (it->second->kind_ == TRITONSERVER_MEMORY_GPU) {
672683
TRITONSERVER_Error* err = UnregisterHelper(it->first, memory_type);
673-
;
674684
if (err != nullptr) {
675685
unregister_fails.push_back(it->first);
676686
LOG_VERBOSE(1) << TRITONSERVER_ErrorMessage(err);

0 commit comments

Comments
 (0)