Peter Collingbourne | 82437bf | 2015-06-15 21:07:11 +0000 | [diff] [blame] | 1 | ; RUN: opt -safe-stack -S -mtriple=i386-pc-linux-gnu < %s -o - | FileCheck %s |
Evgeniy Stepanov | 8827f2d | 2015-12-22 00:13:11 +0000 | [diff] [blame] | 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 |
Peter Collingbourne | 82437bf | 2015-06-15 21:07:11 +0000 | [diff] [blame] | 3 | ; RUN: opt -safe-stack -S -mtriple=x86_64-pc-linux-gnu < %s -o - | FileCheck %s |
Evgeniy Stepanov | 8827f2d | 2015-12-22 00:13:11 +0000 | [diff] [blame] | 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 |
Peter Collingbourne | 82437bf | 2015-06-15 21:07:11 +0000 | [diff] [blame] | 5 | |
| 6 | ; array [4 x i8] |
| 7 | ; Requires protector. |
| 8 | |
Evgeniy Stepanov | 8827f2d | 2015-12-22 00:13:11 +0000 | [diff] [blame] | 9 | ; CHECK: @__safestack_unsafe_stack_ptr = external thread_local(initialexec) global i8* |
| 10 | ; SINGLE-THREAD: @__safestack_unsafe_stack_ptr = external global i8* |
| 11 | |
Peter Collingbourne | 82437bf | 2015-06-15 21:07:11 +0000 | [diff] [blame] | 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 | |
Peter Collingbourne | 82437bf | 2015-06-15 21:07:11 +0000 | [diff] [blame] | 20 | %a.addr = alloca i8*, align 8 |
Peter Collingbourne | 82437bf | 2015-06-15 21:07:11 +0000 | [diff] [blame] | 21 | %buf = alloca [4 x i8], align 1 |
| 22 | |
Evgeniy Stepanov | 45fa0fd | 2016-06-16 22:34:04 +0000 | [diff] [blame^] | 23 | ; CHECK: %[[AADDR:.*]] = alloca i8*, align 8 |
Peter Collingbourne | 82437bf | 2015-06-15 21:07:11 +0000 | [diff] [blame] | 24 | ; CHECK: store i8* {{.*}}, i8** %[[AADDR]], align 8 |
| 25 | store i8* %a, i8** %a.addr, align 8 |
| 26 | |
Evgeniy Stepanov | 45fa0fd | 2016-06-16 22:34:04 +0000 | [diff] [blame^] | 27 | ; CHECK: %[[BUFPTR:.*]] = getelementptr i8, i8* %[[USP]], i32 -4 |
| 28 | ; CHECK: %[[BUFPTR2:.*]] = bitcast i8* %[[BUFPTR]] to [4 x i8]* |
Peter Collingbourne | 82437bf | 2015-06-15 21:07:11 +0000 | [diff] [blame] | 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 | |
Evgeniy Stepanov | a4ac3f4 | 2015-12-01 00:06:13 +0000 | [diff] [blame] | 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 | |
Peter Collingbourne | 82437bf | 2015-06-15 21:07:11 +0000 | [diff] [blame] | 90 | declare i8* @strcpy(i8*, i8*) |