[compiler-rt] [compiler-rt][RISCV] Avoid using __init_riscv_feature_bits as a direc… (PR #115316)

Kito Cheng via llvm-commits llvm-commits at lists.llvm.org
Thu Nov 7 05:33:02 PST 2024


https://github.com/kito-cheng created https://github.com/llvm/llvm-project/pull/115316

…t constructor

`__init_riscv_feature_bits` takes an argument that can be platform-specific, potentially pointing to the VDSO address of the hwprobe system call for Linux. However, marking it as a constructor does not guarantee that 0/NULL will always be passed to this argument, which may result in treating an uninitialized or garbage value as a pointer to hwprobe, leading to a crash.

The simplest solution is to introduce a small constructor function to ensure that the platform-specific argument is set to 0/NULL.

>From 188868e390ad17cba0218458c43081ecb161a0da Mon Sep 17 00:00:00 2001
From: Kito Cheng <kito.cheng at sifive.com>
Date: Thu, 7 Nov 2024 21:24:59 +0800
Subject: [PATCH] [compiler-rt][RISCV] Avoid using __init_riscv_feature_bits as
 a direct constructor

`__init_riscv_feature_bits` takes an argument that can be platform-specific,
potentially pointing to the VDSO address of the hwprobe system call for Linux.
However, marking it as a constructor does not guarantee that 0/NULL will always
be passed to this argument, which may result in treating an uninitialized or
garbage value as a pointer to hwprobe, leading to a crash.

The simplest solution is to introduce a small constructor function to ensure
that the platform-specific argument is set to 0/NULL.
---
 compiler-rt/lib/builtins/cpu_model/riscv.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)

diff --git a/compiler-rt/lib/builtins/cpu_model/riscv.c b/compiler-rt/lib/builtins/cpu_model/riscv.c
index 052124fdde447e..d2dffb45ec0347 100644
--- a/compiler-rt/lib/builtins/cpu_model/riscv.c
+++ b/compiler-rt/lib/builtins/cpu_model/riscv.c
@@ -335,7 +335,8 @@ static void initRISCVFeature(struct riscv_hwprobe Hwprobes[]) {
 
 static int FeaturesBitCached = 0;
 
-void __init_riscv_feature_bits(void *) CONSTRUCTOR_ATTRIBUTE;
+void __init_riscv_feature_bits(void *);
+static void __init_riscv_feature_bits_ctor(void) CONSTRUCTOR_ATTRIBUTE;
 
 // A constructor function that sets __riscv_feature_bits, and
 // __riscv_vendor_feature_bits to the right values.  This needs to run
@@ -343,10 +344,14 @@ void __init_riscv_feature_bits(void *) CONSTRUCTOR_ATTRIBUTE;
 // run before constructors without the priority set.  However, it still runs
 // after ifunc initializers and needs to be called explicitly there.
 
+static void CONSTRUCTOR_ATTRIBUTE __init_riscv_feature_bits_ctor (void) {
+  __init_riscv_feature_bits(0);
+}
+
 // PlatformArgs allows the platform to provide pre-computed data and access it
 // without extra effort. For example, Linux could pass the vDSO object to avoid
 // an extra system call.
-void CONSTRUCTOR_ATTRIBUTE __init_riscv_feature_bits(void *PlatformArgs) {
+void __init_riscv_feature_bits(void *PlatformArgs) {
 
   if (FeaturesBitCached)
     return;



More information about the llvm-commits mailing list