Skip to content

Commit e75ab64

Browse files
authored
Fixes for runtime errors reported in id_decode.hpp:set_dim_in_rec() by Mi300 UndefinedBehaviorSanitizer job (#114)
* Initial fix for runtime error in id_decode.hpp:set_dim_in_rec() * actual fix: corrected the handling of case where dim==1 (ROCPROFILER_DIMENSION_NONE) * removing magic numbers * minor fix * fix for invalid bool value at runtime * clang format * build fix --------- Co-authored-by: Welton, Benjamin <Benjamin.Welton@amd.com> Co-authored-by: Benjamin Welton <bewelton@amd.com> [ROCm/rocprofiler-sdk commit: cffda33]
1 parent 2e3191b commit e75ab64

File tree

1 file changed

+8
-6
lines changed
  • projects/rocprofiler-sdk/source/lib/rocprofiler-sdk/counters

1 file changed

+8
-6
lines changed

projects/rocprofiler-sdk/source/lib/rocprofiler-sdk/counters/id_decode.hpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ namespace counters
3737
constexpr uint64_t COUNTER_BIT_LENGTH = 16;
3838
constexpr uint64_t DIM_BIT_LENGTH = 48;
3939
constexpr uint64_t MAX_64 = std::numeric_limits<uint64_t>::max();
40+
constexpr uint64_t BITS_IN_UINT64 = std::numeric_limits<uint64_t>::digits;
4041
enum rocprofiler_profile_counter_instance_types
4142
{
4243
ROCPROFILER_DIMENSION_NONE = 0, ///< No dimension data, returns/sets 48 bit value as is
@@ -86,7 +87,7 @@ rocprofiler::counters::set_dim_in_rec(rocprofiler_counter_instance_id_t&
8687
rocprofiler_profile_counter_instance_types dim,
8788
size_t value)
8889
{
89-
size_t bit_length = DIM_BIT_LENGTH / ROCPROFILER_DIMENSION_LAST;
90+
uint64_t bit_length = DIM_BIT_LENGTH / ROCPROFILER_DIMENSION_LAST;
9091

9192
if(dim == ROCPROFILER_DIMENSION_NONE)
9293
{
@@ -96,16 +97,17 @@ rocprofiler::counters::set_dim_in_rec(rocprofiler_counter_instance_id_t&
9697
}
9798
else
9899
{
100+
uint64_t mask = (MAX_64 >> (BITS_IN_UINT64 - bit_length)) << ((dim - 1) * bit_length);
99101
// Reset bits to 0 for dimension. Does so by getting the bit length as F's then
100102
// shifiting that into the position of dim. Not's that value and then and's it
101103
// with id.
102-
uint64_t mask = (MAX_64 >> (64 - bit_length)) << ((dim - 1) * bit_length);
103-
id = (id & ~(mask));
104+
id = (id & ~(mask));
104105
// Set the value for the dimenstion
105106
id = id | (value << ((dim - 1) * bit_length));
106107
}
107108

108-
CHECK(value <= (MAX_64 >> (64 - bit_length))) << "Dimension value exceeds max allowed";
109+
CHECK(value <= (MAX_64 >> (BITS_IN_UINT64 - bit_length)))
110+
<< "Dimension value exceeds max allowed";
109111
}
110112

111113
inline void
@@ -115,7 +117,7 @@ rocprofiler::counters::set_counter_in_rec(rocprofiler_counter_instance_id_t& id,
115117
// Maximum counter value given the current setup.
116118
CHECK(value.handle <= 0xffff) << "Counter id exceeds max allowed";
117119
// Reset bits to 0 for counter id
118-
id = id & ~((MAX_64 >> (64 - DIM_BIT_LENGTH)) << (DIM_BIT_LENGTH));
120+
id = id & ~((MAX_64 >> (BITS_IN_UINT64 - DIM_BIT_LENGTH)) << (DIM_BIT_LENGTH));
119121
// Set the value for the dimenstion
120122
id = id | (value.handle << (DIM_BIT_LENGTH));
121123
}
@@ -131,6 +133,6 @@ rocprofiler::counters::rec_to_dim_pos(rocprofiler_counter_instance_id_t
131133
}
132134

133135
size_t bit_length = DIM_BIT_LENGTH / ROCPROFILER_DIMENSION_LAST;
134-
id = id & ((MAX_64 >> (64 - bit_length)) << ((dim - 1) * bit_length));
136+
id = id & ((MAX_64 >> (BITS_IN_UINT64 - bit_length)) << ((dim - 1) * bit_length));
135137
return id >> ((dim - 1) * bit_length);
136138
}

0 commit comments

Comments
 (0)