| Issue |
237912
|
| Summary |
[Driver][RISCV][LoongArch] -fsanitize=function code-model check compares the raw -mcmodel= spelling, rejecting -mcmodel=medlow / -mcmodel=normal
|
| Labels |
new issue
|
| Assignees |
|
| Reporter |
zjy-dev
|
`SanitizerArgs` only accepts `-fsanitize=function` when the code model is small, but it compares the
unnormalized `-mcmodel=` value with the string `"small"`. `tools::addMCModel` maps RISC-V `medlow` and
LoongArch `normal` to `small` before passing the value to cc1. As a result, explicitly requesting the
small code model with these target-specific names is rejected. On LoongArch, `small` itself is not
an accepted spelling, so no explicit small-model spelling works with `-fsanitize=function`.
```c
extern void target_fn(void);
void call_indirect(void (*fp)(void)) { fp(); }
void take(void) { call_indirect(target_fn); }
```
Compile flag: `clang --target=<triple> -fsanitize=function -mcmodel=<cm> -O1 -c t.c`
```
== --target=riscv64-unknown-linux-gnu -fsanitize=function -mcmodel=small
accepted
== --target=riscv64-unknown-linux-gnu -fsanitize=function -mcmodel=medlow
clang: error: invalid argument '-fsanitize=function' only allowed with '-mcmodel=small'
cc1 code model without -fsanitize=function: "-mcmodel=small" # same model as above
== --target=loongarch64-unknown-linux-gnu -fsanitize=function -mcmodel=normal
clang: error: invalid argument '-fsanitize=function' only allowed with '-mcmodel=small'
cc1 code model without -fsanitize=function: "-mcmodel=small"
== --target=loongarch64-unknown-linux-gnu -fsanitize=function -mcmodel=small
clang: error: unsupported argument 'small' to option '-mcmodel=' for target 'loongarch64-unknown-linux-gnu'
```
The check is at `clang/lib/Driver/SanitizerArgs.cpp:531-541` (`StringRef CM = A->getValue(); if (CM != "small" && ...)`).
The mapping is in `clang/lib/Driver/ToolChains/CommonArgs.cpp:3246-3252` (LoongArch `normal`->`small`) and
`:3260-3263` (RISC-V `medlow`->`small`, `medany`->`medium`). The check fails closed by issuing an error
and dropping `Function`, so it does not cause an unsafe build. However, the documented default code
model cannot be named explicitly together with `-fsanitize=function`.
This still reproduces with clang trunk (Compiler Explorer, 2026-11-01).
**Suggested fix:** before comparing, normalize the value with the same target-specific mapping as
`addMCModel`, for example by factoring that mapping into a helper. Then add RUN lines for
`riscv64 -mcmodel=medlow` and `loongarch64 -mcmodel=normal` to `clang/test/Driver/fsanitize-undefined.c`.
Environment: clang 23.1.2 (llvmorg-23.1.2), targets riscv64-unknown-linux-gnu, loongarch64-unknown-linux-gnu.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs