brintos

brintos / llvm-project-archived public Read only

0
0
Text · 2.5 KiB · 8e71cae Raw
82 lines · plain
1; RUN: llc -mtriple=thumbv7-linux-gnueabi -filetype=obj < %s2; RUN: llc -mtriple=thumbv7-linux-gnueabi < %s | FileCheck %s3 4; This test verifies that KCFI instrumentation doesn't cause "out of range5; pc-relative fixup value" errors when generating object files.6;7; The test creates a scenario with enough KCFI-instrumented indirect calls8; (~32 bytes each) that would push a cbz/cbnz instruction out of its ±126 byte9; range if the KCFI_CHECK pseudo-instruction size is not properly accounted for.10;11; Without the fix (KCFI_CHECK returns size 0):12;   - Backend thinks KCFI checks take no space13;   - Generates cbz to branch over the code14;   - During assembly, cbz target is >126 bytes away15;   - Assembly fails with "error: out of range pc-relative fixup value"16;17; With the fix (KCFI_CHECK returns size 32 for Thumb2):18;   - Backend correctly accounts for KCFI check expansion19;   - Avoids cbz or uses longer-range branch instructions20;   - Assembly succeeds, object file is generated21 22declare void @external_function(i32)23 24; Test WITHOUT KCFI: should generate cbz since calls are small25; CHECK-LABEL: test_without_kcfi:26; CHECK: cbz27; CHECK-NOT: bic{{.*}}#128define i32 @test_without_kcfi(ptr %callback, i32 %x) {29entry:30  %cmp = icmp eq i32 %x, 031  br i1 %cmp, label %if_zero, label %if_nonzero32 33if_nonzero:34  ; Regular (non-KCFI) indirect calls - much smaller35  call void %callback()36  call void %callback()37  call void %callback()38  call void %callback()39  call void %callback()40  call void %callback()41 42  call void @external_function(i32 %x)43  %add1 = add i32 %x, 144  ret i32 %add145 46if_zero:47  call void @external_function(i32 0)48  ret i32 049}50 51; Test WITH KCFI: should NOT generate cbz due to large KCFI checks52; CHECK-LABEL: test_with_kcfi:53; CHECK-NOT: cbz54; CHECK: bic{{.*}}#155define i32 @test_with_kcfi(ptr %callback, i32 %x) !kcfi_type !1 {56entry:57  %cmp = icmp eq i32 %x, 058  br i1 %cmp, label %if_zero, label %if_nonzero59 60if_nonzero:61  ; Six KCFI-instrumented indirect calls (~192 bytes total, exceeds cbz range)62  call void %callback() [ "kcfi"(i32 12345678) ]63  call void %callback() [ "kcfi"(i32 12345678) ]64  call void %callback() [ "kcfi"(i32 12345678) ]65  call void %callback() [ "kcfi"(i32 12345678) ]66  call void %callback() [ "kcfi"(i32 12345678) ]67  call void %callback() [ "kcfi"(i32 12345678) ]68 69  ; Regular call to prevent optimization70  call void @external_function(i32 %x)71  %add1 = add i32 %x, 172  ret i32 %add173 74if_zero:75  call void @external_function(i32 0)76  ret i32 077}78 79!llvm.module.flags = !{!0}80!0 = !{i32 4, !"kcfi", i32 1}81!1 = !{i32 12345678}82