runtime: require explicit GC layouts

This commit is contained in:
Jake Bailey
2026-08-07 10:21:19 -07:00
committed by Ron Evans
parent 1a4cb2032e
commit a7360d5ad3
28 changed files with 279 additions and 108 deletions
+13 -13
View File
@@ -7,7 +7,7 @@ declare nonnull ptr @runtime.alloc(i32, ptr)
; Test allocating a single int (i32) that should be allocated on the stack.
define void @testInt() {
%alloc = call align 4 ptr @runtime.alloc(i32 4, ptr null)
%alloc = call align 4 ptr @runtime.alloc(i32 4, ptr inttoptr (i32 3 to ptr))
store i32 5, ptr %alloc
ret void
}
@@ -15,7 +15,7 @@ define void @testInt() {
; Test allocating an array of 3 i16 values that should be allocated on the
; stack.
define i16 @testArray() {
%alloc = call align 2 ptr @runtime.alloc(i32 6, ptr null)
%alloc = call align 2 ptr @runtime.alloc(i32 6, ptr inttoptr (i32 3 to ptr))
%alloc.1 = getelementptr i16, ptr %alloc, i32 1
store i16 5, ptr %alloc.1
%alloc.2 = getelementptr i16, ptr %alloc, i32 2
@@ -25,15 +25,15 @@ define i16 @testArray() {
; Test allocating objects with an unknown alignment.
define void @testUnknownAlign() {
%alloc32 = call ptr @runtime.alloc(i32 32, ptr null)
%alloc32 = call ptr @runtime.alloc(i32 32, ptr inttoptr (i32 3 to ptr))
store i8 5, ptr %alloc32
%alloc24 = call ptr @runtime.alloc(i32 24, ptr null)
%alloc24 = call ptr @runtime.alloc(i32 24, ptr inttoptr (i32 3 to ptr))
store i16 5, ptr %alloc24
%alloc12 = call ptr @runtime.alloc(i32 12, ptr null)
%alloc12 = call ptr @runtime.alloc(i32 12, ptr inttoptr (i32 3 to ptr))
store i16 5, ptr %alloc12
%alloc6 = call ptr @runtime.alloc(i32 6, ptr null)
%alloc6 = call ptr @runtime.alloc(i32 6, ptr inttoptr (i32 3 to ptr))
store i16 5, ptr %alloc6
%alloc3 = call ptr @runtime.alloc(i32 3, ptr null)
%alloc3 = call ptr @runtime.alloc(i32 3, ptr inttoptr (i32 3 to ptr))
store i16 5, ptr %alloc3
ret void
}
@@ -41,27 +41,27 @@ define void @testUnknownAlign() {
; Call a function that will let the pointer escape, so the heap-to-stack
; transform shouldn't be applied.
define void @testEscapingCall() {
%alloc = call align 4 ptr @runtime.alloc(i32 4, ptr null)
%alloc = call align 4 ptr @runtime.alloc(i32 4, ptr inttoptr (i32 3 to ptr))
%val = call ptr @escapeIntPtr(ptr %alloc)
ret void
}
define void @testEscapingCall2() {
%alloc = call align 4 ptr @runtime.alloc(i32 4, ptr null)
%alloc = call align 4 ptr @runtime.alloc(i32 4, ptr inttoptr (i32 3 to ptr))
%val = call ptr @escapeIntPtrSometimes(ptr %alloc, ptr %alloc)
ret void
}
; Call a function that doesn't let the pointer escape.
define void @testNonEscapingCall() {
%alloc = call align 4 ptr @runtime.alloc(i32 4, ptr null)
%alloc = call align 4 ptr @runtime.alloc(i32 4, ptr inttoptr (i32 3 to ptr))
%val = call ptr @noescapeIntPtr(ptr %alloc)
ret void
}
; Return the allocated value, which lets it escape.
define ptr @testEscapingReturn() {
%alloc = call align 4 ptr @runtime.alloc(i32 4, ptr null)
%alloc = call align 4 ptr @runtime.alloc(i32 4, ptr inttoptr (i32 3 to ptr))
ret ptr %alloc
}
@@ -70,7 +70,7 @@ define void @testNonEscapingLoop() {
entry:
br label %loop
loop:
%alloc = call align 4 ptr @runtime.alloc(i32 4, ptr null)
%alloc = call align 4 ptr @runtime.alloc(i32 4, ptr inttoptr (i32 3 to ptr))
%ptr = call ptr @noescapeIntPtr(ptr %alloc)
%result = icmp eq ptr null, %ptr
br i1 %result, label %loop, label %end
@@ -80,7 +80,7 @@ end:
; Test a zero-sized allocation.
define void @testZeroSizedAlloc() {
%alloc = call align 1 ptr @runtime.alloc(i32 0, ptr null)
%alloc = call align 1 ptr @runtime.alloc(i32 0, ptr inttoptr (i32 3 to ptr))
%ptr = call ptr @noescapeIntPtr(ptr %alloc)
ret void
}
+3 -3
View File
@@ -42,13 +42,13 @@ define void @testUnknownAlign() {
}
define void @testEscapingCall() {
%alloc = call align 4 ptr @runtime.alloc(i32 4, ptr null)
%alloc = call align 4 ptr @runtime.alloc(i32 4, ptr inttoptr (i32 3 to ptr))
%val = call ptr @escapeIntPtr(ptr %alloc)
ret void
}
define void @testEscapingCall2() {
%alloc = call align 4 ptr @runtime.alloc(i32 4, ptr null)
%alloc = call align 4 ptr @runtime.alloc(i32 4, ptr inttoptr (i32 3 to ptr))
%val = call ptr @escapeIntPtrSometimes(ptr %alloc, ptr %alloc)
ret void
}
@@ -61,7 +61,7 @@ define void @testNonEscapingCall() {
}
define ptr @testEscapingReturn() {
%alloc = call align 4 ptr @runtime.alloc(i32 4, ptr null)
%alloc = call align 4 ptr @runtime.alloc(i32 4, ptr inttoptr (i32 3 to ptr))
ret ptr %alloc
}
+8 -8
View File
@@ -18,7 +18,7 @@ define ptr @getPointer() {
define ptr @needsStackSlots() {
; Tracked pointer. Although, in this case the value is immediately returned
; so tracking it is not really necessary.
%ptr = call ptr @runtime.alloc(i32 4, ptr null)
%ptr = call ptr @runtime.alloc(i32 4, ptr inttoptr (i32 3 to ptr))
call void @runtime.trackPointer(ptr %ptr)
call void @someArbitraryFunction()
%val = load i8, ptr @someGlobal
@@ -39,7 +39,7 @@ define ptr @needsStackSlots2() {
call void @runtime.trackPointer(ptr %ptr2)
; Here is finally the point where an allocation happens.
%unused = call ptr @runtime.alloc(i32 4, ptr null)
%unused = call ptr @runtime.alloc(i32 4, ptr inttoptr (i32 3 to ptr))
call void @runtime.trackPointer(ptr %unused)
ret ptr %ptr1
@@ -57,7 +57,7 @@ define ptr @fibNext(ptr %x, ptr %y) {
%x.val = load i8, ptr %x
%y.val = load i8, ptr %y
%out.val = add i8 %x.val, %y.val
%out.alloc = call ptr @runtime.alloc(i32 1, ptr null)
%out.alloc = call ptr @runtime.alloc(i32 1, ptr inttoptr (i32 3 to ptr))
call void @runtime.trackPointer(ptr %out.alloc)
store i8 %out.val, ptr %out.alloc
ret ptr %out.alloc
@@ -65,9 +65,9 @@ define ptr @fibNext(ptr %x, ptr %y) {
define ptr @allocLoop() {
entry:
%entry.x = call ptr @runtime.alloc(i32 1, ptr null)
%entry.x = call ptr @runtime.alloc(i32 1, ptr inttoptr (i32 3 to ptr))
call void @runtime.trackPointer(ptr %entry.x)
%entry.y = call ptr @runtime.alloc(i32 1, ptr null)
%entry.y = call ptr @runtime.alloc(i32 1, ptr inttoptr (i32 3 to ptr))
call void @runtime.trackPointer(ptr %entry.y)
store i8 1, ptr %entry.y
br label %loop
@@ -93,7 +93,7 @@ define void @testGEPBitcast() {
%arr = call ptr @arrayAlloc()
%arr.bitcast = getelementptr [32 x i8], ptr %arr, i32 0, i32 0
call void @runtime.trackPointer(ptr %arr.bitcast)
%other = call ptr @runtime.alloc(i32 1, ptr null)
%other = call ptr @runtime.alloc(i32 1, ptr inttoptr (i32 3 to ptr))
call void @runtime.trackPointer(ptr %other)
ret void
}
@@ -103,7 +103,7 @@ define void @someArbitraryFunction() {
}
define void @earlyPopRegression() {
%x.alloc = call ptr @runtime.alloc(i32 4, ptr null)
%x.alloc = call ptr @runtime.alloc(i32 4, ptr inttoptr (i32 3 to ptr))
call void @runtime.trackPointer(ptr %x.alloc)
; At this point the pass used to pop the stack chain, resulting in a potential use-after-free during allocAndSave.
musttail call void @allocAndSave(ptr %x.alloc)
@@ -111,7 +111,7 @@ define void @earlyPopRegression() {
}
define void @allocAndSave(ptr %x) {
%y = call ptr @runtime.alloc(i32 4, ptr null)
%y = call ptr @runtime.alloc(i32 4, ptr inttoptr (i32 3 to ptr))
call void @runtime.trackPointer(ptr %y)
store ptr %y, ptr %x
store ptr %x, ptr @ptrGlobal
+8 -8
View File
@@ -21,7 +21,7 @@ define ptr @needsStackSlots() {
%2 = getelementptr { ptr, i32, ptr }, ptr %gc.stackobject, i32 0, i32 0
store ptr %1, ptr %2, align 4
store ptr %gc.stackobject, ptr @runtime.stackChainStart, align 4
%ptr = call ptr @runtime.alloc(i32 4, ptr null)
%ptr = call ptr @runtime.alloc(i32 4, ptr inttoptr (i32 3 to ptr))
%3 = getelementptr { ptr, i32, ptr }, ptr %gc.stackobject, i32 0, i32 2
store ptr %ptr, ptr %3, align 4
call void @someArbitraryFunction()
@@ -47,7 +47,7 @@ define ptr @needsStackSlots2() {
%ptr2 = getelementptr i8, ptr @someGlobal, i32 0
%6 = getelementptr { ptr, i32, ptr, ptr, ptr, ptr, ptr }, ptr %gc.stackobject, i32 0, i32 5
store ptr %ptr2, ptr %6, align 4
%unused = call ptr @runtime.alloc(i32 4, ptr null)
%unused = call ptr @runtime.alloc(i32 4, ptr inttoptr (i32 3 to ptr))
%7 = getelementptr { ptr, i32, ptr, ptr, ptr, ptr, ptr }, ptr %gc.stackobject, i32 0, i32 6
store ptr %unused, ptr %7, align 4
store ptr %1, ptr @runtime.stackChainStart, align 4
@@ -69,7 +69,7 @@ define ptr @fibNext(ptr %x, ptr %y) {
%x.val = load i8, ptr %x, align 1
%y.val = load i8, ptr %y, align 1
%out.val = add i8 %x.val, %y.val
%out.alloc = call ptr @runtime.alloc(i32 1, ptr null)
%out.alloc = call ptr @runtime.alloc(i32 1, ptr inttoptr (i32 3 to ptr))
%3 = getelementptr { ptr, i32, ptr }, ptr %gc.stackobject, i32 0, i32 2
store ptr %out.alloc, ptr %3, align 4
store i8 %out.val, ptr %out.alloc, align 1
@@ -85,10 +85,10 @@ entry:
%1 = getelementptr { ptr, i32, ptr, ptr, ptr, ptr, ptr }, ptr %gc.stackobject, i32 0, i32 0
store ptr %0, ptr %1, align 4
store ptr %gc.stackobject, ptr @runtime.stackChainStart, align 4
%entry.x = call ptr @runtime.alloc(i32 1, ptr null)
%entry.x = call ptr @runtime.alloc(i32 1, ptr inttoptr (i32 3 to ptr))
%2 = getelementptr { ptr, i32, ptr, ptr, ptr, ptr, ptr }, ptr %gc.stackobject, i32 0, i32 2
store ptr %entry.x, ptr %2, align 4
%entry.y = call ptr @runtime.alloc(i32 1, ptr null)
%entry.y = call ptr @runtime.alloc(i32 1, ptr inttoptr (i32 3 to ptr))
%3 = getelementptr { ptr, i32, ptr, ptr, ptr, ptr, ptr }, ptr %gc.stackobject, i32 0, i32 3
store ptr %entry.y, ptr %3, align 4
store i8 1, ptr %entry.y, align 1
@@ -126,7 +126,7 @@ define void @testGEPBitcast() {
%arr.bitcast = getelementptr [32 x i8], ptr %arr, i32 0, i32 0
%3 = getelementptr { ptr, i32, ptr, ptr }, ptr %gc.stackobject, i32 0, i32 2
store ptr %arr.bitcast, ptr %3, align 4
%other = call ptr @runtime.alloc(i32 1, ptr null)
%other = call ptr @runtime.alloc(i32 1, ptr inttoptr (i32 3 to ptr))
%4 = getelementptr { ptr, i32, ptr, ptr }, ptr %gc.stackobject, i32 0, i32 3
store ptr %other, ptr %4, align 4
store ptr %1, ptr @runtime.stackChainStart, align 4
@@ -144,7 +144,7 @@ define void @earlyPopRegression() {
%2 = getelementptr { ptr, i32, ptr }, ptr %gc.stackobject, i32 0, i32 0
store ptr %1, ptr %2, align 4
store ptr %gc.stackobject, ptr @runtime.stackChainStart, align 4
%x.alloc = call ptr @runtime.alloc(i32 4, ptr null)
%x.alloc = call ptr @runtime.alloc(i32 4, ptr inttoptr (i32 3 to ptr))
%3 = getelementptr { ptr, i32, ptr }, ptr %gc.stackobject, i32 0, i32 2
store ptr %x.alloc, ptr %3, align 4
call void @allocAndSave(ptr %x.alloc)
@@ -159,7 +159,7 @@ define void @allocAndSave(ptr %x) {
%2 = getelementptr { ptr, i32, ptr }, ptr %gc.stackobject, i32 0, i32 0
store ptr %1, ptr %2, align 4
store ptr %gc.stackobject, ptr @runtime.stackChainStart, align 4
%y = call ptr @runtime.alloc(i32 4, ptr null)
%y = call ptr @runtime.alloc(i32 4, ptr inttoptr (i32 3 to ptr))
%3 = getelementptr { ptr, i32, ptr }, ptr %gc.stackobject, i32 0, i32 2
store ptr %y, ptr %3, align 4
store ptr %y, ptr %x, align 4