Skip to content

Commit 5bc283a

Browse files
committed
[Core][GPU][Hetero] Add compile-time guard to prevent string-literal conditions in OPENVINO_ASSERT
Add guard and update hetero/gpu code paths to use OPENVINO_THROW or explicit predicate asserts
1 parent a99a74e commit 5bc283a

File tree

10 files changed

+57
-26
lines changed

10 files changed

+57
-26
lines changed

src/common/low_precision_transformations/include/low_precision/layer_transformation.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ class LP_TRANSFORMATIONS_API DataPrecision {
131131
default:
132132
OPENVINO_ASSERT(false, "unexpected precision ", precision);
133133
}
134-
OPENVINO_ASSERT(false, "unexpected levels ", levels, " for precision ", precision);
134+
OPENVINO_THROW("unexpected levels ", levels, " for precision ", precision);
135135
}
136136

137137
static float getMaxValue(const element::Type precision, const size_t levels) {

src/common/transformations/tests/op_conversions/sdpa_to_paged_attention_test.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -80,18 +80,18 @@ namespace v13 = ov::op::v13;
8080
namespace {
8181

8282
// Constants and Parameters attributes:
83-
auto el_type_i64 = std::pair<std::string, detail::AttrAny>({"element_type", "i64"});
84-
auto el_type_i32 = std::pair<std::string, detail::AttrAny>({"element_type", "i32"});
85-
auto el_type_f32 = std::pair<std::string, detail::AttrAny>({"element_type", "f32"});
83+
auto el_type_i64 = std::pair<std::string, gen_pattern::detail::AttrAny>({"element_type", "i64"});
84+
auto el_type_i32 = std::pair<std::string, gen_pattern::detail::AttrAny>({"element_type", "i32"});
85+
auto el_type_f32 = std::pair<std::string, gen_pattern::detail::AttrAny>({"element_type", "f32"});
8686

8787
// Convert ops attributes:
88-
auto dest_type_i64 = std::pair<std::string, detail::AttrAny>({"destination_type", "i64"});
89-
auto dest_type_f32 = std::pair<std::string, detail::AttrAny>({"destination_type", "f32"});
90-
auto dest_type_f16 = std::pair<std::string, detail::AttrAny>({"destination_type", "f16"});
88+
auto dest_type_i64 = std::pair<std::string, gen_pattern::detail::AttrAny>({"destination_type", "i64"});
89+
auto dest_type_f32 = std::pair<std::string, gen_pattern::detail::AttrAny>({"destination_type", "f32"});
90+
auto dest_type_f16 = std::pair<std::string, gen_pattern::detail::AttrAny>({"destination_type", "f16"});
9191

9292
// Other attributes:
93-
auto numpy_broadcast = std::pair<std::string, detail::AttrAny>({"auto_broadcast", "numpy"});
94-
auto special_zero_true = std::pair<std::string, detail::AttrAny>({"special_zero", true});
93+
auto numpy_broadcast = std::pair<std::string, gen_pattern::detail::AttrAny>({"auto_broadcast", "numpy"});
94+
auto special_zero_true = std::pair<std::string, gen_pattern::detail::AttrAny>({"special_zero", true});
9595

9696
auto single_val = [](int rank, float val) {
9797
return makeConst(element::f32, ov::Shape{std::vector<size_t>(rank, 1)}, {val});

src/core/include/openvino/core/except.hpp

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,42 @@ class OPENVINO_API NotImplemented : public AssertFailure {
9191
protected:
9292
explicit NotImplemented(const std::string& what_arg) : ov::AssertFailure(what_arg) {}
9393
};
94+
95+
namespace detail {
96+
97+
template <class T>
98+
constexpr inline bool check_condition(T&& v) noexcept {
99+
return static_cast<bool>(std::forward<T>(v));
100+
}
101+
102+
template <std::size_t N>
103+
constexpr inline bool check_condition(const char (&)[N]) noexcept {
104+
static_assert(N == 0,
105+
"OPENVINO_ASSERT: string literal used as condition (always true). "
106+
"Did you mean to compare strings or check a pointer?");
107+
return false;
108+
}
109+
110+
template <std::size_t N>
111+
constexpr inline bool check_condition(const wchar_t (&)[N]) noexcept {
112+
static_assert(N == 0, "OPENVINO_ASSERT: wide string literal used as condition (always true).");
113+
return false;
114+
}
115+
116+
template <std::size_t N>
117+
constexpr inline bool check_condition(const char16_t (&)[N]) noexcept {
118+
static_assert(N == 0, "OPENVINO_ASSERT: UTF-16 string literal used as condition (always true).");
119+
return false;
120+
}
121+
122+
template <std::size_t N>
123+
constexpr inline bool check_condition(const char32_t (&)[N]) noexcept {
124+
static_assert(N == 0, "OPENVINO_ASSERT: UTF-32 string literal used as condition (always true).");
125+
return false;
126+
}
127+
128+
} // namespace detail
129+
94130
} // namespace ov
95131

96132
//
@@ -157,7 +193,7 @@ class OPENVINO_API NotImplemented : public AssertFailure {
157193
//
158194
#define OPENVINO_ASSERT_HELPER2(exc_class, ctx, check, ...) \
159195
do { \
160-
if (!static_cast<bool>(check)) { \
196+
if (!::ov::detail::check_condition(check)) { \
161197
::std::ostringstream ss___; \
162198
::ov::write_all_to_stream(ss___, __VA_ARGS__); \
163199
exc_class::create(__FILE__, __LINE__, (#check), (ctx), ss___.str()); \
@@ -166,7 +202,7 @@ class OPENVINO_API NotImplemented : public AssertFailure {
166202

167203
#define OPENVINO_ASSERT_HELPER1(exc_class, ctx, check) \
168204
do { \
169-
if (!static_cast<bool>(check)) { \
205+
if (!::ov::detail::check_condition(check)) { \
170206
exc_class::create(__FILE__, __LINE__, (#check), (ctx), exc_class::default_msg); \
171207
} \
172208
} while (0)

src/plugins/hetero/src/remote_context.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,7 @@ namespace hetero {
1414

1515
RemoteContext::RemoteContext(std::map<std::string, ov::SoPtr<ov::IRemoteContext>> contexts)
1616
: m_contexts(std::move(contexts)) {
17-
if (m_contexts.empty()) {
18-
OPENVINO_ASSERT("HETERO RemoteContext must have at least one underlying context");
19-
}
17+
OPENVINO_ASSERT(!m_contexts.empty(), "HETERO RemoteContext must have at least one underlying context");
2018
}
2119
const ov::AnyMap& RemoteContext::get_property() const {
2220
return m_contexts.begin()->second->get_property();

src/plugins/intel_cpu/src/nodes/region_yolo.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,7 @@ inline void RegionYolo::calculate_logistic(size_t start_index, int count, uint8_
417417
bf16_dst_data[i + start_index] = logistic_scalar(bf16_dst_data[i + start_index]);
418418
}
419419
} else {
420-
CPU_NODE_ASSERT("Unsupported precision configuration outPrc=", output_prec.get_type_name());
420+
CPU_NODE_THROW("Unsupported precision configuration outPrc=", output_prec.get_type_name());
421421
}
422422
}
423423
}

src/plugins/intel_gpu/src/graph/common_utils/jitter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ std::vector<ChannelName> get_data_channels_order(size_t rank) {
3232
case 8:
3333
return {channel::BATCH, channel::FEATURE, channel::V, channel::U, channel::W, channel::Z, channel::Y, channel::X};
3434
default:
35-
OPENVINO_ASSERT("[GPU] Unexpected rank ", rank, " in get_data_channels_order() func");
35+
OPENVINO_THROW("[GPU] Unexpected rank ", rank, " in get_data_channels_order() func");
3636
}
3737

3838
return {};

src/plugins/intel_gpu/src/graph/reshape.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -113,12 +113,9 @@ layout reshape_inst::calc_output_layout(reshape_node const& node, kernel_impl_pa
113113
auto input_layout = impl_param.get_non_padded_input_layout();
114114
auto desc = impl_param.typed_desc<reshape>();
115115
if (desc->output_shape.count() == 0) {
116-
if (desc->output_partial_shape.size() != 0) {
117-
format out_fmt = format::adjust_to_rank(input_layout.format, desc->output_partial_shape.rank().get_length());
118-
return layout{desc->output_partial_shape, input_layout.data_type, out_fmt};
119-
} else {
120-
OPENVINO_ASSERT("[GPU] Output shape is not provided");
121-
}
116+
OPENVINO_ASSERT(desc->output_partial_shape.size() != 0, "[GPU] Output shape is not provided");
117+
format out_fmt = format::adjust_to_rank(input_layout.format, desc->output_partial_shape.rank().get_length());
118+
return layout{desc->output_partial_shape, input_layout.data_type, out_fmt};
122119
}
123120

124121
auto sizes = desc->output_shape.sizes();

src/plugins/intel_gpu/src/kernel_selector/kernels/fully_connected/fully_connected_kernel_gemv.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,15 +162,15 @@ JitConstants FullyConnected_GEMV::GetJitConstants(const fully_connected_params&
162162
} else if (params.weights.GetLayout() == WeightsLayout::os_is_yx_osv64_isv2) {
163163
jit.AddConstant(MakeJitConstant("FILTER_LAYOUT_OS_IS_YX_TYPE", 2));
164164
} else {
165-
OPENVINO_ASSERT("GEMV doesn't support this weights layout: ", params.weights.GetLayout());
165+
OPENVINO_THROW("GEMV doesn't support this weights layout: ", params.weights.GetLayout());
166166
}
167167

168168
if (params.weights.GetDType() == WeightsType::UINT4) {
169169
jit.AddConstant(MakeJitConstant("WEI_UINT4", 1));
170170
} else if (params.weights.GetDType() == WeightsType::INT4) {
171171
jit.AddConstant(MakeJitConstant("WEI_UINT4", 0));
172172
} else {
173-
OPENVINO_ASSERT("GEMV only support INT4 and UINT4, doesn't support ", static_cast<size_t>(params.weights.GetDType()));
173+
OPENVINO_THROW("GEMV only support INT4 and UINT4, doesn't support ", static_cast<size_t>(params.weights.GetDType()));
174174
}
175175

176176
jit.AddConstant(MakeJitConstant("SIMD", simd));

src/plugins/intel_gpu/src/plugin/transformations/fc_horizontal_fusion.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ FullyConnectedHorizontalFusion::FullyConnectedHorizontalFusion(bool fuse_mlp_swi
256256
ov::as_type_ptr<ov::op::v0::Constant>(zp_convert->get_input_node_shared_ptr(0));
257257
cur_zp_val = zp_const->cast_vector<int32_t>()[0];
258258
} else {
259-
OPENVINO_ASSERT("Unsupported zp input node for FC horizontal fusion");
259+
OPENVINO_THROW("Unsupported zp input node for FC horizontal fusion");
260260
}
261261
if (cur_zp_val != scalar_zp_val)
262262
return false;

src/plugins/intel_gpu/tests/unit/test_cases/moe_gemm_gpu_test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ void get_reference(const std::vector<ov::float16>& input,
139139
acc += fa0 * input_r[2 * ki];
140140
acc += fa1 * input_r[2 * ki + 1];
141141
} else {
142-
OPENVINO_ASSERT("Not implemented dt");
142+
OPENVINO_THROW("Not implemented dt");
143143
}
144144
}
145145
scale_group++;

0 commit comments

Comments
 (0)