[compiler-rt] [compiler-rt][xray] porting to sunos platforms. (PR #90362)

David CARLIER via llvm-commits llvm-commits at lists.llvm.org
Sat Apr 27 14:39:40 PDT 2024


https://github.com/devnexen created https://github.com/llvm/llvm-project/pull/90362

None

>From 3d4670784bae759e4517a88ec73d4ef3e03efe28 Mon Sep 17 00:00:00 2001
From: David Carlier <devnexen at gmail.com>
Date: Sat, 27 Apr 2024 23:37:45 +0100
Subject: [PATCH] [compiler-rt][xray] porting to sunos platforms.

---
 compiler-rt/cmake/config-ix.cmake             |  2 +-
 compiler-rt/lib/xray/CMakeLists.txt           |  4 +++
 compiler-rt/lib/xray/xray_x86_64.cpp          | 32 +++++++++++++++++++
 .../TestCases/Posix/fork_basic_logging.cpp    |  4 +++
 compiler-rt/test/xray/lit.cfg.py              |  2 +-
 5 files changed, 42 insertions(+), 2 deletions(-)

diff --git a/compiler-rt/cmake/config-ix.cmake b/compiler-rt/cmake/config-ix.cmake
index ba740af9e1d60f..a891e23b02acf0 100644
--- a/compiler-rt/cmake/config-ix.cmake
+++ b/compiler-rt/cmake/config-ix.cmake
@@ -871,7 +871,7 @@ else()
 endif()
 
 if (COMPILER_RT_HAS_SANITIZER_COMMON AND XRAY_SUPPORTED_ARCH AND
-    OS_NAME MATCHES "Darwin|Linux|FreeBSD|NetBSD|Fuchsia")
+    OS_NAME MATCHES "Darwin|Linux|FreeBSD|NetBSD|Fuchsia|SunOS")
   set(COMPILER_RT_HAS_XRAY TRUE)
 else()
   set(COMPILER_RT_HAS_XRAY FALSE)
diff --git a/compiler-rt/lib/xray/CMakeLists.txt b/compiler-rt/lib/xray/CMakeLists.txt
index cf7b5062aae32d..551997d8d14684 100644
--- a/compiler-rt/lib/xray/CMakeLists.txt
+++ b/compiler-rt/lib/xray/CMakeLists.txt
@@ -189,6 +189,10 @@ if (TARGET cxx-headers OR HAVE_LIBCXX)
   set(XRAY_DEPS cxx-headers)
 endif()
 
+if (CMAKE_SYSTEM_NAME STREQUAL "SunOs")
+  set(XRAY_LINK_LIBS ${XRAY_LINK_LIBS} kstat)
+endif()
+
 if (APPLE)
   add_weak_symbols("sanitizer_common" WEAK_SYMBOL_LINK_FLAGS)
   add_weak_symbols("xray" WEAK_SYMBOL_LINK_FLAGS)
diff --git a/compiler-rt/lib/xray/xray_x86_64.cpp b/compiler-rt/lib/xray/xray_x86_64.cpp
index b9666a40861d48..6ae93426c622e1 100644
--- a/compiler-rt/lib/xray/xray_x86_64.cpp
+++ b/compiler-rt/lib/xray/xray_x86_64.cpp
@@ -9,6 +9,8 @@
 #if SANITIZER_FREEBSD || SANITIZER_NETBSD || SANITIZER_APPLE
 #include <sys/types.h>
 #include <sys/sysctl.h>
+#elif SANITIZER_SOLARIS
+#include <kstat.h>
 #elif SANITIZER_FUCHSIA
 #include <zircon/syscalls.h>
 #endif
@@ -101,6 +103,36 @@ uint64_t getTSCFrequency() XRAY_NEVER_INSTRUMENT {
 
     return 0;
 }
+#elif SANITIZER_SOLARIS
+uint64_t getTSCFrequency() XRAY_NEVER_INSTRUMENT {
+    kstat_named_t *nm;
+    kstat_ctl_t *ctl;
+    kstat_t *s;
+    uint64_t TSCFrequency;
+
+    ctl = kstat_open();
+    if (!ctl) {
+      return 0;
+    }
+    s = kstat_lookup(ctl, "cpu_info", 0, NULL);
+    if (!s) {
+      kstat_close(ctl);
+      return 0;
+    }
+
+    if (kstat_read(ctl, s, NULL) == -1) {
+      kstat_close(ctl);
+      return 0;
+    }
+
+    nm = reinterpret_cast<kstat_named_t *>(kstat_data_lookup(s, "clock_MHz"));
+
+    auto clock = nm->data_type == KSTAT_DATA_INT32 ? nm->value.i32 : nm->value.i64;
+    TSCFrequency = static_cast<uint64_t>(clock) * 1000000;
+    kstat_close(ctl);
+
+    return TSCFrequency;
+}
 #elif !SANITIZER_FUCHSIA
 uint64_t getTSCFrequency() XRAY_NEVER_INSTRUMENT {
     /* Not supported */
diff --git a/compiler-rt/test/xray/TestCases/Posix/fork_basic_logging.cpp b/compiler-rt/test/xray/TestCases/Posix/fork_basic_logging.cpp
index 58f310e3a1083f..8c4095466354a9 100644
--- a/compiler-rt/test/xray/TestCases/Posix/fork_basic_logging.cpp
+++ b/compiler-rt/test/xray/TestCases/Posix/fork_basic_logging.cpp
@@ -13,6 +13,10 @@
 
 // Not ported.
 // UNSUPPORTED: target={{.*netbsd.*}}
+// Not ported.
+// UNSUPPORTED: target={{.*solaris.*}}
+// Not ported.
+// UNSUPPORTED: target={{.*illumos.*}}
 
 #include "xray/xray_log_interface.h"
 #include <stdio.h>
diff --git a/compiler-rt/test/xray/lit.cfg.py b/compiler-rt/test/xray/lit.cfg.py
index f73ae3acd77154..491d5c1ff4e57c 100644
--- a/compiler-rt/test/xray/lit.cfg.py
+++ b/compiler-rt/test/xray/lit.cfg.py
@@ -56,7 +56,7 @@ def build_invocation(compile_flags):
 # Default test suffixes.
 config.suffixes = [".c", ".cpp"]
 
-if config.host_os not in ["FreeBSD", "Linux", "NetBSD", "OpenBSD"]:
+if config.host_os not in ["FreeBSD", "Linux", "NetBSD", "OpenBSD", "SunOS"]:
     config.unsupported = True
 elif "64" not in config.host_arch:
     if "arm" in config.host_arch:



More information about the llvm-commits mailing list