1 ; RUN: opt -safe-stack -S -mtriple=i386-pc-linux-gnu < %s -o - | FileCheck %s 2 ; RUN: opt -safe-stack -safe-stack-usp-storage=single-thread -S -mtriple=i386-pc-linux-gnu < %s -o - | FileCheck -check-prefix=SINGLE-THREAD %s 3 ; RUN: opt -safe-stack -S -mtriple=x86_64-pc-linux-gnu < %s -o - | FileCheck %s 4 ; RUN: opt -safe-stack -safe-stack-usp-storage=single-thread -S -mtriple=x86_64-pc-linux-gnu < %s -o - | FileCheck -check-prefix=SINGLE-THREAD %s 5 6 ; array [4 x i8] 7 ; Requires protector. 8 9 ; CHECK: @__safestack_unsafe_stack_ptr = external thread_local(initialexec) global i8* 10 ; SINGLE-THREAD: @__safestack_unsafe_stack_ptr = external global i8* 11 12 define void @foo(i8* %a) nounwind uwtable safestack { 13 entry: 14 ; CHECK: %[[USP:.*]] = load i8*, i8** @__safestack_unsafe_stack_ptr 15 16 ; CHECK: %[[USST:.*]] = getelementptr i8, i8* %[[USP]], i32 -16 17 18 ; CHECK: store i8* %[[USST]], i8** @__safestack_unsafe_stack_ptr 19 20 %a.addr = alloca i8*, align 8 21 %buf = alloca [4 x i8], align 1 22 23 ; CHECK: %[[AADDR:.*]] = alloca i8*, align 8 24 ; CHECK: store i8* {{.*}}, i8** %[[AADDR]], align 8 25 store i8* %a, i8** %a.addr, align 8 26 27 ; CHECK: %[[BUFPTR:.*]] = getelementptr i8, i8* %[[USP]], i32 -4 28 ; CHECK: %[[BUFPTR2:.*]] = bitcast i8* %[[BUFPTR]] to [4 x i8]* 29 ; CHECK: %[[GEP:.*]] = getelementptr inbounds [4 x i8], [4 x i8]* %[[BUFPTR2]], i32 0, i32 0 30 %gep = getelementptr inbounds [4 x i8], [4 x i8]* %buf, i32 0, i32 0 31 32 ; CHECK: %[[A2:.*]] = load i8*, i8** %[[AADDR]], align 8 33 %a2 = load i8*, i8** %a.addr, align 8 34 35 ; CHECK: call i8* @strcpy(i8* %[[GEP]], i8* %[[A2]]) 36 %call = call i8* @strcpy(i8* %gep, i8* %a2) 37 38 ; CHECK: store i8* %[[USP]], i8** @__safestack_unsafe_stack_ptr 39 ret void 40 } 41 42 ; Load from an array at a fixed offset, no overflow. 43 define i8 @StaticArrayFixedSafe() nounwind uwtable safestack { 44 entry: 45 ; CHECK-LABEL: define i8 @StaticArrayFixedSafe( 46 ; CHECK-NOT: __safestack_unsafe_stack_ptr 47 ; CHECK: ret i8 48 %buf = alloca i8, i32 4, align 1 49 %gep = getelementptr inbounds i8, i8* %buf, i32 2 50 %x = load i8, i8* %gep, align 1 51 ret i8 %x 52 } 53 54 ; Load from an array at a fixed offset with overflow. 55 define i8 @StaticArrayFixedUnsafe() nounwind uwtable safestack { 56 entry: 57 ; CHECK-LABEL: define i8 @StaticArrayFixedUnsafe( 58 ; CHECK: __safestack_unsafe_stack_ptr 59 ; CHECK: ret i8 60 %buf = alloca i8, i32 4, align 1 61 %gep = getelementptr inbounds i8, i8* %buf, i32 5 62 %x = load i8, i8* %gep, align 1 63 ret i8 %x 64 } 65 66 ; Load from an array at an unknown offset. 67 define i8 @StaticArrayVariableUnsafe(i32 %ofs) nounwind uwtable safestack { 68 entry: 69 ; CHECK-LABEL: define i8 @StaticArrayVariableUnsafe( 70 ; CHECK: __safestack_unsafe_stack_ptr 71 ; CHECK: ret i8 72 %buf = alloca i8, i32 4, align 1 73 %gep = getelementptr inbounds i8, i8* %buf, i32 %ofs 74 %x = load i8, i8* %gep, align 1 75 ret i8 %x 76 } 77 78 ; Load from an array of an unknown size. 79 define i8 @DynamicArrayUnsafe(i32 %sz) nounwind uwtable safestack { 80 entry: 81 ; CHECK-LABEL: define i8 @DynamicArrayUnsafe( 82 ; CHECK: __safestack_unsafe_stack_ptr 83 ; CHECK: ret i8 84 %buf = alloca i8, i32 %sz, align 1 85 %gep = getelementptr inbounds i8, i8* %buf, i32 2 86 %x = load i8, i8* %gep, align 1 87 ret i8 %x 88 } 89 90 declare i8* @strcpy(i8*, i8*) 91