[Openmp-commits] [openmp] [ompd] Fix strict aliasing violation in TargetValue::getValue() (PR #97739)

Nikita Popov via Openmp-commits openmp-commits at lists.llvm.org
Thu Jul 4 08:23:33 PDT 2024


https://github.com/nikic created https://github.com/llvm/llvm-project/pull/97739

For the case where baseTypeSize does not match the size of T, read the value into a separate char buffer first. This avoids accessing buf using two different types.

Fixes https://github.com/llvm/llvm-project/issues/94616.

>From 7df36bc5a6634d60e7aafb9c82938e1c193958ce Mon Sep 17 00:00:00 2001
From: Nikita Popov <npopov at redhat.com>
Date: Thu, 4 Jul 2024 17:21:15 +0200
Subject: [PATCH] [ompd] Fix strict aliasing violation in
 TargetValue::getValue()

For the case where baseTypeSize does not match the size of T,
read the value into a separate char buffer first. This avoids
accessing buf using two different types.
---
 openmp/libompd/src/TargetValue.h | 37 ++++++++++++++++++--------------
 1 file changed, 21 insertions(+), 16 deletions(-)

diff --git a/openmp/libompd/src/TargetValue.h b/openmp/libompd/src/TargetValue.h
index 87e4575885e7da..d7429f7705dfdf 100644
--- a/openmp/libompd/src/TargetValue.h
+++ b/openmp/libompd/src/TargetValue.h
@@ -231,22 +231,27 @@ class TBaseValue : public TValue {
 
 template <typename T> ompd_rc_t TBaseValue::getValue(T &buf) {
   assert(sizeof(T) >= baseTypeSize);
-  ompd_rc_t ret = getValue(&buf, 1);
-  if (sizeof(T) > baseTypeSize) {
-    switch (baseTypeSize) {
-    case 1:
-      buf = (T) * ((int8_t *)&buf);
-      break;
-    case 2:
-      buf = (T) * ((int16_t *)&buf);
-      break;
-    case 4:
-      buf = (T) * ((int32_t *)&buf);
-      break;
-    case 8:
-      buf = (T) * ((int64_t *)&buf);
-      break;
-    }
+  if (sizeof(T) == baseTypeSize)
+    return getValue(&buf, 1);
+
+  char tmp[sizeof(T)];
+  ompd_rc_t ret = getValue(tmp, 1);
+  switch (baseTypeSize) {
+  case 1:
+    buf = (T) * ((int8_t *)tmp);
+    break;
+  case 2:
+    buf = (T) * ((int16_t *)tmp);
+    break;
+  case 4:
+    buf = (T) * ((int32_t *)tmp);
+    break;
+  case 8:
+    buf = (T) * ((int64_t *)tmp);
+    break;
+  default:
+    assert(0 && "Invalid baseTypeSize");
+    break;
   }
   return ret;
 }



More information about the Openmp-commits mailing list