From 6dea5c5e3d3f19f3d14c8274e509f50b45f425fa Mon Sep 17 00:00:00 2001 From: Laurent Morichetti Date: Thu, 15 Sep 2022 10:33:38 -0700 Subject: [PATCH] Fix an array subscript out-of-bounds error Starting with gcc-11 (verified with gcc-12 as well), an array out-of-bounds subscript error is reported for accessing the registration table element at the operation ID index. Validating the index in the function calling Register/Unregister does not quiet the warning/error in release builds, so, for gcc-11 and gcc-12, we disable that warning just for the RegistrationTable class. Change-Id: I6bc4a02aa072cfa8905ecde5e3960aebf32fc912 [ROCm/roctracer commit: 67ce5fae13c28f7a6f904482c101e9f6584f6cfe] --- .../roctracer/src/roctracer/registration_table.h | 16 ++++++++++++++++ projects/roctracer/src/roctracer/roctracer.cpp | 12 ++++++++++++ 2 files changed, 28 insertions(+) diff --git a/projects/roctracer/src/roctracer/registration_table.h b/projects/roctracer/src/roctracer/registration_table.h index aa25779bd6..f9f6efd478 100644 --- a/projects/roctracer/src/roctracer/registration_table.h +++ b/projects/roctracer/src/roctracer/registration_table.h @@ -32,6 +32,19 @@ namespace roctracer::util { +#if __GNUC__ == 11 || __GNUCC__ == 12 +// Starting with gcc-11 (verified with gcc-12 as well), an array out-of-bounds subscript error is +// reported for accessing the registration table element at the operation ID index. Validating the +// index in the function calling Register/Unregister does not quiet the warning/error in release +// builds, so, for gcc-11 and gcc-12, we disable that warning just for this class. +#define IGNORE_GCC_ARRAY_BOUNDS_ERROR 1 +#endif // __GNUC__ == 11 || __GNUCC__ == 12 + +#if IGNORE_GCC_ARRAY_BOUNDS_ERROR +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Warray-bounds" +#endif // IGNORE_GCC_ARRAY_BOUNDS_ERROR + namespace detail { struct False { constexpr bool operator()() { return false; } @@ -78,6 +91,9 @@ template class Regi } table_[N]{}; }; +#if IGNORE_GCC_ARRAY_BOUNDS_ERROR +#pragma GCC diagnostic pop +#endif // IGNORE_GCC_ARRAY_BOUNDS_ERROR } // namespace roctracer::util diff --git a/projects/roctracer/src/roctracer/roctracer.cpp b/projects/roctracer/src/roctracer/roctracer.cpp index 1e0c401e77..3be42de65d 100644 --- a/projects/roctracer/src/roctracer/roctracer.cpp +++ b/projects/roctracer/src/roctracer/roctracer.cpp @@ -489,6 +489,9 @@ static void roctracer_enable_callback_impl(roctracer_domain_t domain, uint32_t o roctracer_rtapi_callback_t callback, void* user_data) { std::lock_guard lock(registration_mutex); + if (operation_id >= get_op_end(domain) || callback == nullptr) + throw ApiError(ROCTRACER_STATUS_ERROR_INVALID_ARGUMENT, "invalid argument"); + switch (domain) { case ACTIVITY_DOMAIN_HSA_EVT: HSA_registration_group.Register(hsa_evt_callback_table, operation_id, callback, user_data); @@ -538,6 +541,9 @@ ROCTRACER_API roctracer_status_t roctracer_enable_domain_callback( static void roctracer_disable_callback_impl(roctracer_domain_t domain, uint32_t operation_id) { std::lock_guard lock(registration_mutex); + if (operation_id >= get_op_end(domain)) + throw ApiError(ROCTRACER_STATUS_ERROR_INVALID_ARGUMENT, "invalid argument"); + switch (domain) { case ACTIVITY_DOMAIN_HSA_EVT: HSA_registration_group.Unregister(hsa_evt_callback_table, operation_id); @@ -635,6 +641,9 @@ static void roctracer_enable_activity_impl(roctracer_domain_t domain, uint32_t o if (memory_pool == nullptr) EXC_RAISING(ROCTRACER_STATUS_ERROR_DEFAULT_POOL_UNDEFINED, "no default pool"); + if (op >= get_op_end(domain)) + throw ApiError(ROCTRACER_STATUS_ERROR_INVALID_ARGUMENT, "invalid argument"); + switch (domain) { case ACTIVITY_DOMAIN_HSA_EVT: break; @@ -701,6 +710,9 @@ ROCTRACER_API roctracer_status_t roctracer_enable_domain_activity(activity_domai static void roctracer_disable_activity_impl(roctracer_domain_t domain, uint32_t op) { std::lock_guard lock(registration_mutex); + if (op >= get_op_end(domain)) + throw ApiError(ROCTRACER_STATUS_ERROR_INVALID_ARGUMENT, "invalid argument"); + switch (domain) { case ACTIVITY_DOMAIN_HSA_EVT: break;