Skip to content

Checking for uninitialized memory #85

@LTLA

Description

@LTLA

pybind11::array_t constructors don't zero their allocated memory, which can expose subtle bugs in the underlying libraries that (incorrectly) assumed that the input arrays were zeroed. To flush out these bugs, we can just modify the pybind11 code to fill the allocated arrays with some obvious nonsense:

    explicit array_t(ShapeContainer shape, const T *ptr = nullptr, handle base = handle())
        : array_t(private_ctor{},
                  std::move(shape),
                  (ExtraFlags & f_style) != 0 ? detail::f_strides(*shape, itemsize())
                                              : detail::c_strides(*shape, itemsize()),
                  ptr,
                  base) {

        // Aaron's custom crap to smoke out uninitialized values.
        if (ptr) {
            auto buff = request();
            std::fill_n(reinterpret_cast<T*>(buff.ptr), buff.size, []{
                if constexpr(std::is_integral<T>::value) {
                    return -1;
                } else {
                    return std::numeric_limits<T>::quiet_NaN();
                }
            }());
        }
    }

    explicit array_t(ssize_t count, const T *ptr = nullptr, handle base = handle())
        : array({count}, {}, ptr, base) {
        // Aaron's custom crap to smoke out uninitialized values.
        if (!ptr) {
            auto buff = request();
            std::fill_n(reinterpret_cast<T*>(buff.ptr), buff.size, []{
                if constexpr(std::is_integral<T>::value) {
                    return 100;
                } else {
                    return std::numeric_limits<T>::quiet_NaN();
                }
            }());
        }
    }

and then check that the tests still work correctly. (Most tests involve an equality check that should fail if it encounters NaNs, or bounds checks for integers.)

MSan might also catch this but it seems tedious to get it to work with Python.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions