From 58961ee1e631113260f391e3078cc3ef31c5b06c Mon Sep 17 00:00:00 2001 From: Lamakaio Date: Sat, 28 Sep 2024 19:56:26 +0200 Subject: [PATCH] fix aliasing issue in framework --- src/osp/framework/framework.h | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/osp/framework/framework.h b/src/osp/framework/framework.h index fdda6899..adbb0035 100644 --- a/src/osp/framework/framework.h +++ b/src/osp/framework/framework.h @@ -32,6 +32,7 @@ #include "../core/keyed_vector.h" #include "../tasks/tasks.h" +#include #include #include @@ -336,13 +337,17 @@ struct Framework // Access struct members of out.pl and out.di as if they're arrays, in order to write // DataIds and PipelineIds to them. + //Using this reference to avoid strict aliasing UB : https://gist.github.com/shafik/848ae25ee209f698763cffee272a58f8 + //Apparently the memcpy calls get optimized away. + FeatureInterface const &rInterface = m_fiinstData[fiId]; + if constexpr ( ! std::is_empty_v ) { auto const plMembers = arrayCast( arrayCast( arrayView(&out.pl, 1) ) ); for (std::size_t i = 0; i < plMembers.size(); ++i) { - plMembers[i].m_value = rInterface.pipelines[i]; + std::memcpy(&plMembers[i].m_value, &rInterface.pipelines[i], sizeof(PipelineId)); } } @@ -351,7 +356,7 @@ struct Framework auto const diMembers = arrayCast( arrayCast( arrayView(&out.di, 1) ) ); for (std::size_t i = 0; i < diMembers.size(); ++i) { - diMembers[i] = rInterface.data[i]; + std::memcpy(&diMembers[i], &rInterface.data[i], sizeof(DataId)); } } }