Skip to content

Commit bd677b9

Browse files
committed
[CIR][Test] Add divergence test for vtable constant issue
This test documents a divergence between CIR and CodeGen where CIR incorrectly emits vtables as mutable globals instead of immutable constants. **Issue:** CIR emits vtables with 'global' linkage: @_ZTV4Base = linkonce_odr global { [3 x ptr] } ... CodeGen correctly emits them as 'constant': @_ZTV4Base = linkonce_odr unnamed_addr constant { [3 x ptr] } ... comdat **Root Cause:** In CIRGenModule.cpp:4081, createOrReplaceCXXRuntimeVariable() calls createGlobalOp() without passing isConstant=true parameter. **Impact:** 1. Vtables placed in .data instead of .rodata section 2. Security vulnerability - writable vtables enable exploits 3. Missed optimization opportunities 4. Diverges from CodeGen without justification **Additional Issues:** CIR also misses 'unnamed_addr' and 'comdat' attributes that CodeGen emits. The test is marked XFAIL until the fix is implemented. Test Plan: Build and verify test fails as expected (documenting divergence): bin/llvm-lit -v ../../clang/test/CIR/CodeGen/vtable-constant-divergence.cpp
1 parent 98a979e commit bd677b9

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -emit-llvm %s -o %t-cir.ll
2+
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -emit-llvm %s -o %t-codegen.ll
3+
// RUN: diff %t-cir.ll %t-codegen.ll
4+
5+
// XFAIL: *
6+
7+
// This test documents a divergence between CIR and CodeGen:
8+
// CIR emits vtables as 'global' (mutable) instead of 'constant' (immutable).
9+
// This is a bug that needs to be fixed.
10+
//
11+
// Expected (CodeGen):
12+
// @_ZTV4Base = linkonce_odr unnamed_addr constant { [3 x ptr] } ...
13+
//
14+
// Actual (CIR):
15+
// @_ZTV4Base = linkonce_odr global { [3 x ptr] } ...
16+
//
17+
// The vtable should be marked as 'constant' because:
18+
// 1. Vtables are never modified at runtime
19+
// 2. They should be placed in .rodata (read-only data section)
20+
// 3. Writable vtables are a security vulnerability
21+
// 4. CodeGen has always emitted them as constant
22+
23+
class Base {
24+
public:
25+
virtual void foo() {}
26+
};
27+
28+
void test() {
29+
Base b;
30+
b.foo();
31+
}

0 commit comments

Comments
 (0)