Skip to content

Commit 86c08b3

Browse files
[SYCL] Fix get_specialization_constant segmentation fault (#8542)
In certain cases get_specialization_constant would cause a segmentation fault, likely due to strict aliasing violations. This commit changes the implementation to use memcpy of the data into the resulting object, as this can be assumed to be valid due to specialization constants being device-copyable. --------- Signed-off-by: Larsen, Steffen <[email protected]>
1 parent 12a4566 commit 86c08b3

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed

sycl/include/sycl/kernel_bundle.hpp

+7-5
Original file line numberDiff line numberDiff line change
@@ -308,17 +308,19 @@ class kernel_bundle : public detail::kernel_bundle_plain,
308308
template <auto &SpecName>
309309
typename std::remove_reference_t<decltype(SpecName)>::value_type
310310
get_specialization_constant() const {
311-
const char *SpecSymName = detail::get_spec_constant_symbolic_ID<SpecName>();
312-
if (!is_specialization_constant_set(SpecSymName))
313-
return SpecName.getDefaultValue();
314-
315311
using SCType =
316312
typename std::remove_reference_t<decltype(SpecName)>::value_type;
317313

314+
const char *SpecSymName = detail::get_spec_constant_symbolic_ID<SpecName>();
315+
SCType Res{SpecName.getDefaultValue()};
316+
if (!is_specialization_constant_set(SpecSymName))
317+
return Res;
318+
318319
std::array<char, sizeof(SCType)> RetValue;
319320
get_specialization_constant_impl(SpecSymName, RetValue.data());
321+
std::memcpy(&Res, RetValue.data(), sizeof(SCType));
320322

321-
return *reinterpret_cast<SCType *>(RetValue.data());
323+
return Res;
322324
}
323325

324326
/// \returns an iterator to the first device image kernel_bundle contains

sycl/unittests/kernel-and-program/Cache.cpp

+5-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,11 @@ template <> const char *get_spec_constant_symbolic_ID<SpecConst1>() {
6868
static sycl::unittest::PiImage generateDefaultImage() {
6969
using namespace sycl::unittest;
7070

71+
std::vector<char> SpecConstData;
72+
PiProperty SC1 = makeSpecConstant<int>(SpecConstData, "SC1", {0}, {0}, {42});
73+
7174
PiPropertySet PropSet;
75+
addSpecConstants({SC1}, std::move(SpecConstData), PropSet);
7276

7377
std::vector<unsigned char> Bin{0, 1, 2, 3, 4, 5}; // Random data
7478

@@ -256,7 +260,7 @@ TEST_F(KernelAndProgramCacheTest, SpecConstantCacheNegative) {
256260
detail::KernelProgramCache::ProgramCache &Cache =
257261
CtxImpl->getKernelProgramCache().acquireCachedPrograms().get();
258262

259-
EXPECT_EQ(Cache.size(), 1U) << "Expect non-empty cache";
263+
EXPECT_EQ(Cache.size(), 2U) << "Expect an entry for each build in the cache.";
260264
}
261265

262266
// Check that kernel_bundle created through join() is not cached.

0 commit comments

Comments
 (0)