[llvm] [BOLT][RISCV] Add RISCV tests for external branch target validation (PR #223991)

via llvm-commits llvm-commits at lists.llvm.org
Wed Sep 16 05:27:50 PDT 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-backend-risc-v

Author: Thrrreeee (Thrrreeee)

<details>
<summary>Changes</summary>

Add RISC-V regression tests to complement #<!-- -->221927, covering two external branch target validation cases.

---
Full diff: https://github.com/llvm/llvm-project/pull/223991.diff


2 Files Affected:

- (added) bolt/test/RISCV/constant-island-branch-rescan.s (+43) 
- (added) bolt/test/RISCV/validate-multiple-branch-targets.s (+55) 


``````````diff
diff --git a/bolt/test/RISCV/constant-island-branch-rescan.s b/bolt/test/RISCV/constant-island-branch-rescan.s
new file mode 100644
index 0000000000000..f8d0694306710
--- /dev/null
+++ b/bolt/test/RISCV/constant-island-branch-rescan.s
@@ -0,0 +1,43 @@
+## Reject a data-island target again when ignoring the source rescans its
+## references. The data word is an instruction encoding, as in assembly that
+## emits instructions as data; its mapping symbol still identifies data.
+## Both the source and target must retain their original addresses and bytes.
+
+# RUN: llvm-mc -triple=riscv64 -filetype=obj %s -o %t.o
+# RUN: ld.lld --emit-relocs -Ttext=0x10000 %t.o -o %t.exe
+# RUN: llvm-objcopy --dump-section=.text=%t.original.text %t.exe
+# RUN: llvm-bolt %t.exe -o %t.bolt 2>&1 | FileCheck %s --check-prefix=DIAG
+# RUN: llvm-nm --defined-only %t.bolt | FileCheck %s --check-prefix=ADDR --implicit-check-not=__ENTRY_
+# RUN: llvm-objcopy --dump-section=.bolt.org.text=%t.output.text %t.bolt
+# RUN: cmp %t.original.text %t.output.text
+# RUN: llvm-bolt %t.exe -o %t.skip.bolt --skip-funcs=_start 2>&1 | FileCheck %s --check-prefix=DIAG
+# RUN: llvm-nm --defined-only %t.skip.bolt | FileCheck %s --check-prefix=ADDR --implicit-check-not=__ENTRY_
+# RUN: llvm-objcopy --dump-section=.bolt.org.text=%t.skip.text %t.skip.bolt
+# RUN: cmp %t.original.text %t.skip.text
+
+## The same invalid reference must not be diagnosed repeatedly during rescans.
+# DIAG: BOLT-WARNING: ignoring entry point at address 0x1000c in constant island of function target
+# DIAG-NOT: ignoring entry point
+# DIAG-NOT: BOLT-ERROR
+# ADDR-DAG: 0000000000010000 T _start
+# ADDR-DAG: 0000000000010008 T target
+
+  .text
+  .option norvc
+  .globl _start
+  .type _start, at function
+_start:
+  beq t0, t1, .Ldata
+  ret
+  .size _start, .-_start
+
+  .globl target
+  .type target, at function
+target:
+  nop
+.Ldata:
+  .word 0xcd027057
+  ret
+  .size target, .-target
+
+  .reloc 0, R_RISCV_NONE
diff --git a/bolt/test/RISCV/validate-multiple-branch-targets.s b/bolt/test/RISCV/validate-multiple-branch-targets.s
new file mode 100644
index 0000000000000..e5a115ce0e958
--- /dev/null
+++ b/bolt/test/RISCV/validate-multiple-branch-targets.s
@@ -0,0 +1,55 @@
+## Reject every invalid target from a source, including an instruction-middle
+## target after a data-island target. Deferring setIgnored() until validation
+## completes must preserve the instruction boundaries needed for later checks.
+
+# RUN: llvm-mc -triple=riscv64 -filetype=obj %s -o %t.o
+# RUN: ld.lld --emit-relocs -Ttext=0x10000 %t.o -o %t.exe
+# RUN: llvm-objcopy --dump-section=.text=%t.original.text %t.exe
+# RUN: llvm-bolt %t.exe -o %t.bolt 2>&1 | FileCheck %s --check-prefix=DIAG
+# RUN: llvm-nm --defined-only %t.bolt | FileCheck %s --check-prefix=ADDR --implicit-check-not=__ENTRY_
+# RUN: llvm-objcopy --dump-section=.bolt.org.text=%t.output.text %t.bolt
+# RUN: cmp %t.original.text %t.output.text
+# RUN: llvm-bolt %t.exe -o %t.skip.bolt --skip-funcs=_start 2>&1 | FileCheck %s --check-prefix=DIAG
+# RUN: llvm-nm --defined-only %t.skip.bolt | FileCheck %s --check-prefix=ADDR --implicit-check-not=__ENTRY_
+# RUN: llvm-objcopy --dump-section=.bolt.org.text=%t.skip.text %t.skip.bolt
+# RUN: cmp %t.original.text %t.skip.text
+
+# DIAG: BOLT-WARNING: corrupted control flow detected in function _start: an external branch/call targets an invalid instruction in function data_target at address 0x10004; ignoring both functions
+# DIAG-NEXT: BOLT-WARNING: ignoring entry point at address 0x10004 in constant island of function data_target
+# DIAG-NEXT: BOLT-WARNING: corrupted control flow detected in function _start: an external branch/call targets an invalid instruction in function instruction_target at address 0x1000e; ignoring both functions
+# DIAG-NOT: corrupted control flow detected
+# DIAG-NOT: ignoring entry point
+# DIAG-NOT: BOLT-ERROR
+# ADDR-DAG: 0000000000010000 T data_target
+# ADDR-DAG: 000000000001000c T instruction_target
+# ADDR-DAG: 0000000000010014 T _start
+
+  .text
+  .option norvc
+## Targets precede the source so their instruction boundaries are available
+## when the explicit --skip-funcs run scans the source's external references.
+  .globl data_target
+  .type data_target, at function
+data_target:
+  nop
+.Ldata:
+  .word 0xcd027057
+  ret
+  .size data_target, .-data_target
+
+  .globl instruction_target
+  .type instruction_target, at function
+instruction_target:
+  addi a0, a0, 1
+  ret
+  .size instruction_target, .-instruction_target
+
+  .globl _start
+  .type _start, at function
+_start:
+  beq a0, a1, .Ldata
+  beq a2, a3, instruction_target + 2
+  ret
+  .size _start, .-_start
+
+  .reloc 0, R_RISCV_NONE

``````````

</details>


https://github.com/llvm/llvm-project/pull/223991


More information about the llvm-commits mailing list