Skip to content

Assert on out-of-range accesses #37

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 21 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
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
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ option(ENABLE_IMPLICIT_CONVERSIONS "Enable implicit conversions to-from raw poin
option(DISABLE_RM "Make ManagedArray a thin wrapper" Off)
mark_as_advanced(DISABLE_RM)
option(ENABLE_UM "Use CUDA unified (managed) memory" Off)
option(ENABLE_BOUNDS_CHECK "Enable bounds checking for chai::ManagedArray<T>::operator[]" Off)

set(ENABLE_TESTS On CACHE BOOL "")
set(ENABLE_EXAMPLES On CACHE BOOL "")
Expand Down
1 change: 1 addition & 0 deletions src/chai/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ set(CHAI_ENABLE_HIP ${ENABLE_HIP})
set(CHAI_ENABLE_IMPLICIT_CONVERSIONS ${ENABLE_IMPLICIT_CONVERSIONS})
set(CHAI_DISABLE_RM ${DISABLE_RM})
set(CHAI_ENABLE_UM ${ENABLE_UM})
set(CHAI_ENABLE_BOUNDS_CHECK ${ENABLE_BOUNDS_CHECK})

configure_file(
${PROJECT_SOURCE_DIR}/src/chai/config.hpp.in
Expand Down
14 changes: 14 additions & 0 deletions src/chai/ManagedArray.inl
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,16 @@
#include "ManagedArray.hpp"
#include "ArrayManager.hpp"

#if defined(CHAI_ENABLE_BOUNDS_CHECK)

#if defined(NDEBUG)
#undef NDEBUG // This makes sure assert is enabled even for release builds
#endif // defined(NDEBUG)

Copy link
Member

Choose a reason for hiding this comment

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

Could this have unintended consequences?

#include <cassert>

#endif // defined(CHAI_ENABLE_BOUNDS_CHECK)

namespace chai {

template<typename T>
Expand Down Expand Up @@ -389,6 +399,10 @@ template<typename T>
template<typename Idx>
CHAI_INLINE
CHAI_HOST_DEVICE T& ManagedArray<T>::operator[](const Idx i) const {
#if defined(CHAI_ENABLE_BOUNDS_CHECK)
assert(i >= 0 && static_cast<size_t>(i) < m_elems);
#endif

return m_active_pointer[i];
}

Expand Down
1 change: 1 addition & 0 deletions src/chai/config.hpp.in
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,6 @@
#cmakedefine CHAI_ENABLE_IMPLICIT_CONVERSIONS
#cmakedefine CHAI_DISABLE_RM
#cmakedefine CHAI_ENABLE_UM
#cmakedefine CHAI_ENABLE_BOUNDS_CHECK

#endif // CHAI_config_HPP
2 changes: 1 addition & 1 deletion src/util/forall.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ template <typename LOOP_BODY>
void forall_kernel_cpu(int begin, int end, LOOP_BODY body)
{
for (int i = 0; i < (end - begin); ++i) {
body(i);
body(begin+i);
}
}

Expand Down
49 changes: 48 additions & 1 deletion tests/integration/managed_array_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1394,4 +1394,51 @@ GPU_TEST(ManagedArray, CopyZero)

array.free();
}
#endif
#endif

#if defined(CHAI_ENABLE_BOUNDS_CHECK)

TEST(ManagedArray, UpperOutOfRangeAccess)
{
chai::ManagedArray<float> array(20);
array[19] = 0.0; // Should be fine
ASSERT_DEATH_IF_SUPPORTED( array[20] = 0.0, ".*" );
}

TEST(ManagedArray, LowerOutOfRangeAccess)
{
chai::ManagedArray<float> array(20);
array[0] = 0.0; // Should be fine
ASSERT_DEATH_IF_SUPPORTED( array[-1] = 0.0, ".*" );
}

#if defined(CHAI_ENABLE_CUDA)

GPU_TEST(ManagedArray, UpperOutOfRangeAccessGPU)
{
chai::ManagedArray<float> array(20);

forall(gpu(), 0, 1, [=] __device__ (int) {
array[20] = 0.0;
});

cudaError_t errorCode = cudaGetLastError();
ASSERT_EQ(cudaErrorAssert, errorCode);
ASSERT_EQ(cudaDeviceReset(), cudaSuccess);
}

GPU_TEST(ManagedArray, LowerOutOfRangeAccessGPU)
{
chai::ManagedArray<float> array(20);

forall(gpu(), 0, 1, [=] __device__ (int) {
array[-1] = 0.0;
});

cudaError_t errorCode = cudaGetLastError();
ASSERT_EQ(cudaErrorAssert, errorCode);
ASSERT_EQ(cudaDeviceReset(), cudaSuccess);
}

#endif // defined(CHAI_ENABLE_CUDA)
#endif // defined(CHAI_ENABLE_BOUNDS_CHECK)