Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
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
121 changes: 100 additions & 21 deletions src/api/cpp/nixl_descriptors.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
#include <vector>
#include <string>
#include <algorithm>
#include <cassert>
#include <stdexcept>
#include "nixl_types.h"

/**
Expand Down Expand Up @@ -260,20 +262,63 @@ operator==(const nixlRemoteDesc &lhs, const nixlRemoteDesc &rhs);
*/
template<class T>
class nixlDescList {
protected:
private:
/** @var NIXL memory type */
nixl_mem_t type;
/** @var Vector for storing nixlDescs */
std::vector<T> descs;
/** @var Whether the descriptor list is a shallow copy */
bool isShallowCopy_;

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.

I wonder whether "shallow copy" is the right term, how about something like non-owning view, or read-only view?

/** @var Common view of the descriptor list, both for shallow copy and own data */
const T *view_;

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.

Suggest naming this data_ as a view consists of data (a pointer) and size (a size_t).

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.

Could we also use std::variant to hold either the "view" type (data + size) or a std::vector<T>?
It might not be as simple but it would avoid any bugs related to using descs while _isShallowCopy is true

/** @var Size of the descriptor list */
size_t size_;

/**
* @brief Synchronize the view of the descriptor list with the data in the vector.
* Can be called only in owner mode (not shallow copy), after vector is modified
*/
inline void
syncView() {

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.

The inline is superfluous, a noexcept could be added.

view_ = descs.data();
size_ = descs.size();
}

/**
* @brief Check that the descriptor list is not a shallow copy.
* Throws std::logic_error if it is.
*/
void
checkModifiable() const {
if (isShallowCopy_) {
throw std::logic_error("Descriptor list is a non-modifiable shallow copy");
}
}

/**
* @brief Swap two nixlDescList objects.
*/
static void
swap(nixlDescList<T> &first, nixlDescList<T> &second) noexcept;

/**
* @brief Private constructor to create a shallow copy of a nixlDescList.
* Used internally by the makeShallowCopy to return only a const reference
* to the shallow copy.
*/
nixlDescList(const nixl_mem_t &type, const T *view, size_t size);

public:
using iterator_t = typename std::vector<T>::iterator;

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.

The inconsistency between the two iterator types looks a bit weird to me. I'd use pointers for both cases and then remove the type aliases to make things clearer below. I see the iterator is used for an insert() later.

using const_iterator_t = const T *;

/**
* @brief Parametrized Constructor for nixlDescList
*
* @param type NIXL memory type of descriptor list
* @param init_size initial size for descriptor list (default = 0)
*/
nixlDescList(const nixl_mem_t &type, const int &init_size = 0);
nixlDescList(const nixl_mem_t &type, size_t init_size = 0);

/**
* @brief Deserializer constructor for nixlDescList from nixlSerDes object
Expand All @@ -289,15 +334,36 @@ class nixlDescList {
*
* @param d_list other nixlDescList object of the same type
*/
nixlDescList(const nixlDescList<T> &d_list) = default;
nixlDescList(const nixlDescList<T> &d_list);

/**
* @brief Move constructor for creating nixlDescList from another object
* of the same type.
*
* @param d_list other nixlDescList object of the same type
*/
nixlDescList(nixlDescList<T> &&d_list) noexcept;

/**
* @brief Operator = overloading constructor for nixlDescList
*
* @param d_list nixlDescList object
*/
nixlDescList &
operator=(const nixlDescList<T> &d_list) = default;
operator=(nixlDescList<T> d_list) noexcept;

/**
* @brief Create a shallow copy of a nixlDescList.
*
* @param type NIXL memory type of descriptor list
* @param view View of the descriptor list
* @param size Size of the descriptor list
* @return Shallow copy of the descriptor list
*/
static const nixlDescList<T>

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.

const does nothing when returning by value.

makeShallowCopy(const nixl_mem_t &type, const T *view, size_t size) {
return nixlDescList<T>(type, view, size);
}

/**
* @brief nixlDescList Destructor
Expand All @@ -315,49 +381,57 @@ class nixlDescList {
/**
* @brief Get count of descriptors
*/
inline int
int
descCount() const {
return descs.size();
return size_;
}
Comment on lines +384 to +387

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.

⚠️ Potential issue | 🟡 Minor

Potential narrowing conversion from size_t to int.

descCount() returns int but size_ is size_t. For lists exceeding INT_MAX elements, this silently overflows. Consider returning size_t or adding a narrowing check. The size() method correctly returns size_t.

Suggested fix
-    int
+    size_t
     descCount() const {
         return size_;
     }

Or, if int return is required for API compatibility:

     int
     descCount() const {
+        assert(size_ <= static_cast<size_t>(std::numeric_limits<int>::max()));
         return size_;
     }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
int
descCount() const {
return descs.size();
return size_;
}
size_t
descCount() const {
return size_;
}
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/api/cpp/nixl_descriptors.h` around lines 384 - 387, The descCount()
method currently returns int while reading size_ (a size_t), risking silent
narrowing; change descCount() to return size_t to match size(), or if int is
required for API compatibility, add an explicit bounds check in descCount() that
caps values > INT_MAX (using size_ and INT_MAX) and handles overflow (e.g.,
throw or return INT_MAX) to avoid undefined behavior—update the signature and
any callers of descCount() accordingly (refer to descCount() and size_).


size_t
size() const {
return size_;
}

/**
* @brief Check if nixlDescList is empty or not
*/
bool
isEmpty() const noexcept {
return descs.empty();
inline bool
isEmpty() const {
return (size_ == 0);

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.

Brackets are superfluous (as is the inline while noexcept is missing).

}

const T &
inline const T &

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.

Same thing about inline and noexcept for this and the following functions.

operator[](size_t index) const {
return descs[index];
return view_[index];
}

T &
inline T &
operator[](size_t index) {

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.

Maybe add an at() function to this class like std::vector's that checks the index bounds?

assert(!isShallowCopy_);
return descs[index];
}

/**
* @brief Vector like iterators for const and non-const elements
*/
inline typename std::vector<T>::const_iterator
const_iterator_t
begin() const {
return descs.begin();
return view_;
}

inline typename std::vector<T>::const_iterator
const_iterator_t
end() const {
return descs.end();
return view_ + size_;
}

inline typename std::vector<T>::iterator
iterator_t
begin() {
assert(!isShallowCopy_);
return descs.begin();
}

inline typename std::vector<T>::iterator
iterator_t
end() {
assert(!isShallowCopy_);
return descs.end();
}

Expand All @@ -378,28 +452,33 @@ class nixlDescList {
* @param count Number of elements after resizing DescList object
*/
virtual void
resize(const size_t &count);
resize(size_t count);

/**
* @brief Empty the descriptors list
*/
inline void
clear() {
checkModifiable();
descs.clear();
syncView();
}

/**
* @brief Add Descriptors to descriptor list
*/
virtual void
addDesc(const T &desc);
addDesc(T desc);

void
addDesc(T desc, iterator_t it);

/**
* @brief Remove descriptor from list at index
* Can throw std::out_of_range exception.
*/
void
remDesc(const int &index);
remDesc(size_t index);
Comment on lines 480 to +481

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.

⚠️ Potential issue | 🔴 Critical

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
# Verify callers of remDesc that may pass negative values

echo "=== Checking remDesc callers with int index ==="
rg -n -B2 -A2 'remDesc\(' --type cpp

echo ""
echo "=== Checking getIndex return type and usage ==="
rg -n 'int.*getIndex|getIndex.*int' --type cpp

Repository: ai-dynamo/nixl

Length of output: 4346


🏁 Script executed:

# Check remDesc implementation
sed -n '259,270p' src/infra/nixl_descriptors.cpp

echo ""
echo "=== Check Rust wrapper signatures ==="
sed -n '1080,1100p' src/bindings/rust/wrapper.cpp
sed -n '1275,1295p' src/bindings/rust/wrapper.cpp

echo ""
echo "=== Check NIXL_ERR_NOT_FOUND definition ==="
rg -n 'NIXL_ERR_NOT_FOUND|define.*NOT_FOUND' --type cpp --type h | head -20

echo ""
echo "=== Check getIndex implementation ==="
sed -n '298,320p' src/infra/nixl_descriptors.cpp

Repository: ai-dynamo/nixl

Length of output: 4056


🏁 Script executed:

rg -n 'define NIXL_ERR_NOT_FOUND|NIXL_ERR_NOT_FOUND\s*=' --type h

Repository: ai-dynamo/nixl

Length of output: 114


Critical: remDesc(size_t) silently converts negative indices to large values.

The signature change from int to size_t introduces a dangerous implicit conversion. Callers pass int values directly from getIndex() (which returns -4 for not-found), including in src/infra/nixl_memory_section.cpp and C API wrappers in src/bindings/rust/wrapper.cpp.

When a negative int is passed, it converts to a very large size_t value, and the bounds check (index < 0) in the implementation is always false for an unsigned type, allowing out-of-bounds access.

Suggested fix: Keep int signature with proper validation
     void
-    remDesc(size_t index);
+    remDesc(int index);

Or add explicit validation in the implementation:

void
nixlDescList<T>::remDesc(int index) {
    checkModifiable();
    if ((index >= (int)descs.size()) || (index < 0)) {
        throw std::out_of_range("Index is out of range");
    }
    descs.erase(descs.begin() + index);
    syncView();
}

Note: The current check (index < 0) when index is size_t is always false and does not prevent the conversion of negative values to large unsigned numbers.

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
void
remDesc(const int &index);
remDesc(size_t index);
void
remDesc(int index);
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/api/cpp/nixl_descriptors.h` around lines 480 - 481, The remDesc(size_t)
declaration causes negative ints (e.g. from getIndex()) to wrap to huge unsigned
values and bypass the (index < 0) check; fix by reverting remDesc to accept a
signed int (e.g. remDesc(int index)) or, if you must keep size_t, add explicit
validation at the top of the implementation: checkModifiable(); verify index is
within bounds and not negative by using a signed comparison (cast descs.size()
to int or check the source int before conversion) and throw std::out_of_range on
failure before calling descs.erase or syncView; update call sites (e.g. where
getIndex() is used in src/infra/nixl_memory_section.cpp and the Rust C API
wrappers) if you change the signature.


/**
* @brief Convert a nixlDescList with metadata by trimming it to a
Expand Down
6 changes: 3 additions & 3 deletions src/infra/mem_section.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,15 @@ class nixlSecDescList : public nixlDescList<nixlSectionDesc> {
using nixlDescList<nixlSectionDesc>::operator[]; // bring in const overload

void
addDesc(const nixlSectionDesc &desc) override;
addDesc(nixlSectionDesc desc) override;

bool
verifySorted() const;

nixlSectionDesc &
operator[](size_t index) {
assert(verifySorted());
return descs[index];
return nixlDescList::operator[](index);
}

int
Expand All @@ -85,7 +85,7 @@ class nixlSecDescList : public nixlDescList<nixlSectionDesc> {
getCoveringIndex(const nixlBasicDesc &query) const;

void
resize(const size_t &count) override;
resize(size_t count) override;

// Disable parent's convenience constructors that allow pre-sizing
nixlSecDescList(const nixlSecDescList &) = default;
Expand Down
Loading
Loading