Skip to content

Commit 522880c

Browse files
authored
[compiler-rt][RISCV] Avoid using __init_riscv_feature_bits as a direc… (#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.
1 parent 39e6dc0 commit 522880c

File tree

1 file changed

+7
-2
lines changed
  • compiler-rt/lib/builtins/cpu_model

1 file changed

+7
-2
lines changed

compiler-rt/lib/builtins/cpu_model/riscv.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -335,18 +335,23 @@ static void initRISCVFeature(struct riscv_hwprobe Hwprobes[]) {
335335

336336
static int FeaturesBitCached = 0;
337337

338-
void __init_riscv_feature_bits(void *) CONSTRUCTOR_ATTRIBUTE;
338+
void __init_riscv_feature_bits(void *);
339+
static void __init_riscv_feature_bits_ctor(void) CONSTRUCTOR_ATTRIBUTE;
339340

340341
// A constructor function that sets __riscv_feature_bits, and
341342
// __riscv_vendor_feature_bits to the right values. This needs to run
342343
// only once. This constructor is given the highest priority and it should
343344
// run before constructors without the priority set. However, it still runs
344345
// after ifunc initializers and needs to be called explicitly there.
345346

347+
static void CONSTRUCTOR_ATTRIBUTE __init_riscv_feature_bits_ctor(void) {
348+
__init_riscv_feature_bits(0);
349+
}
350+
346351
// PlatformArgs allows the platform to provide pre-computed data and access it
347352
// without extra effort. For example, Linux could pass the vDSO object to avoid
348353
// an extra system call.
349-
void CONSTRUCTOR_ATTRIBUTE __init_riscv_feature_bits(void *PlatformArgs) {
354+
void __init_riscv_feature_bits(void *PlatformArgs) {
350355

351356
if (FeaturesBitCached)
352357
return;

0 commit comments

Comments
 (0)