Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions mooncake-integration/transfer_engine/transfer_engine_py.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,13 @@ static void (*freeMemory)(void*) = nullptr;
static std::string g_protocol;

// Handle allocateMemory function pointer based on protocol
void initMemoryAllocator(const char* protocol) {
bool initMemoryAllocator(const char* protocol) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

static bool?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, done in d5a8be3. It is only called from this translation unit, so it is static now.

if (allocateMemory != nullptr) {
LOG(WARNING) << "Memory allocator already initialized with: "
<< g_protocol;
return;
return true;
}
g_protocol = protocol;
if (protocol == nullptr) protocol = "";
if (strcmp(protocol, "nvlink") == 0) {
#ifdef USE_MNNVL
allocateMemory = [](size_t s) -> void* {
Expand All @@ -68,6 +68,7 @@ void initMemoryAllocator(const char* protocol) {
LOG(INFO) << "Selected MNNVL (NVLink) memory allocator";
#else
LOG(ERROR) << "Protocol 'nvlink' requires -DUSE_MNNVL=ON";
return false;
#endif
} else if (strcmp(protocol, "hip") == 0) {
#ifdef USE_HIP
Expand All @@ -80,6 +81,7 @@ void initMemoryAllocator(const char* protocol) {
LOG(INFO) << "Selected HIP memory allocator";
#else
LOG(ERROR) << "Protocol 'hip' requires -DUSE_HIP=ON";
return false;
#endif
} else if (strcmp(protocol, "nvlink_intra") == 0) {
#ifdef USE_INTRA_NVLINK
Expand All @@ -93,12 +95,15 @@ void initMemoryAllocator(const char* protocol) {
LOG(INFO) << "Selected Intra-NVLink memory allocator";
#else
LOG(ERROR) << "Protocol 'nvlink_intra' requires -DUSE_INTRA_NVLINK=ON";
return false;
#endif
} else {
allocateMemory = malloc;
freeMemory = free;
LOG(WARNING) << "Using default malloc/free for protocol: " << protocol;
}
g_protocol = protocol;
return true;
}

TransferEnginePy::TransferEnginePy() {
Expand Down Expand Up @@ -169,8 +174,6 @@ int TransferEnginePy::initialize(const char* local_hostname,
const char* metadata_server,
const char* protocol,
const char* device_name) {
initMemoryAllocator(protocol);

auto conn_string = parseConnectionString(metadata_server);
return initializeExt(local_hostname, conn_string.second.c_str(), protocol,
device_name, conn_string.first.c_str());
Expand All @@ -181,11 +184,12 @@ int TransferEnginePy::initializeExt(const char* local_hostname,
const char* protocol,
const char* device_name,
const char* metadata_type) {
if (strcmp(protocol, "xgmi") == 0) {
if (protocol != nullptr && strcmp(protocol, "xgmi") == 0) {
LOG(ERROR) << "Protocol 'xgmi' is not exposed in the Python API. "
<< "Use 'hip' instead.";
return -1;
}
if (!initMemoryAllocator(protocol)) return -1;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

There is a potential segmentation fault at line 186: strcmp(protocol, "xgmi"). If protocol is nullptr (which is possible and handled on line 193), strcmp will dereference a null pointer and crash. Please ensure protocol is checked for nullptr before calling strcmp(protocol, "xgmi").

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in d545628. initializeExt now guards the xgmi comparison when protocol is null, then lets allocator initialization normalize it to the default protocol.


std::string proto = protocol ? std::string(protocol) : "";
std::string conn_string = buildConnString(metadata_type, metadata_server);
Expand Down Expand Up @@ -256,6 +260,10 @@ int TransferEnginePy::initializeExt(const char* local_hostname,
int TransferEnginePy::getRpcPort() { return engine_->getRpcPort(); }

char* TransferEnginePy::allocateRawBuffer(size_t capacity) {
if (allocateMemory == nullptr || freeMemory == nullptr) {
LOG(ERROR) << "Memory allocator is not initialized";
return nullptr;
}
auto buffer = allocateMemory(capacity);
if (!buffer) return nullptr;
int ret = engine_->registerLocalMemory(buffer, capacity, kWildcardLocation);
Expand Down
Loading