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 |
David L Kreitzer | d5c6755 | 2016-10-14 17:56:00 +0000 | [diff] [blame] | 2 | ; RUN: opt -safe-stack -S -mtriple=i386-pc-contiki-unknown < %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 |
| 4 | |
| 5 | ; array [4 x i8] |
| 6 | ; Requires protector. |
| 7 | |
Evgeniy Stepanov | 8827f2d | 2015-12-22 00:13:11 +0000 | [diff] [blame] | 8 | ; CHECK: @__safestack_unsafe_stack_ptr = external thread_local(initialexec) global i8* |
| 9 | ; SINGLE-THREAD: @__safestack_unsafe_stack_ptr = external global i8* |
| 10 | |
Peter Collingbourne | 82437bf | 2015-06-15 21:07:11 +0000 | [diff] [blame] | 11 | define void @foo(i8* %a) nounwind uwtable safestack { |
| 12 | entry: |
| 13 | ; CHECK: %[[USP:.*]] = load i8*, i8** @__safestack_unsafe_stack_ptr |
| 14 | |
| 15 | ; CHECK: %[[USST:.*]] = getelementptr i8, i8* %[[USP]], i32 -16 |
| 16 | |
| 17 | ; CHECK: store i8* %[[USST]], i8** @__safestack_unsafe_stack_ptr |
| 18 | |
Peter Collingbourne | 82437bf | 2015-06-15 21:07:11 +0000 | [diff] [blame] | 19 | %a.addr = alloca i8*, align 8 |
Peter Collingbourne | 82437bf | 2015-06-15 21:07:11 +0000 | [diff] [blame] | 20 | %buf = alloca [4 x i8], align 1 |
| 21 | |
Evgeniy Stepanov | 45fa0fd | 2016-06-16 22:34:04 +0000 | [diff] [blame] | 22 | ; CHECK: %[[AADDR:.*]] = alloca i8*, align 8 |
Peter Collingbourne | 82437bf | 2015-06-15 21:07:11 +0000 | [diff] [blame] | 23 | ; CHECK: store i8* {{.*}}, i8** %[[AADDR]], align 8 |
| 24 | store i8* %a, i8** %a.addr, align 8 |
| 25 | |
Evgeniy Stepanov | 45fa0fd | 2016-06-16 22:34:04 +0000 | [diff] [blame] | 26 | ; CHECK: %[[BUFPTR:.*]] = getelementptr i8, i8* %[[USP]], i32 -4 |
| 27 | ; CHECK: %[[BUFPTR2:.*]] = bitcast i8* %[[BUFPTR]] to [4 x i8]* |
Peter Collingbourne | 82437bf | 2015-06-15 21:07:11 +0000 | [diff] [blame] | 28 | ; CHECK: %[[GEP:.*]] = getelementptr inbounds [4 x i8], [4 x i8]* %[[BUFPTR2]], i32 0, i32 0 |
| 29 | %gep = getelementptr inbounds [4 x i8], [4 x i8]* %buf, i32 0, i32 0 |
| 30 | |
| 31 | ; CHECK: %[[A2:.*]] = load i8*, i8** %[[AADDR]], align 8 |
| 32 | %a2 = load i8*, i8** %a.addr, align 8 |
| 33 | |
| 34 | ; CHECK: call i8* @strcpy(i8* %[[GEP]], i8* %[[A2]]) |
| 35 | %call = call i8* @strcpy(i8* %gep, i8* %a2) |
| 36 | |
| 37 | ; CHECK: store i8* %[[USP]], i8** @__safestack_unsafe_stack_ptr |
| 38 | ret void |
| 39 | } |
| 40 | |
Evgeniy Stepanov | a4ac3f4 | 2015-12-01 00:06:13 +0000 | [diff] [blame] | 41 | ; Load from an array at a fixed offset, no overflow. |
| 42 | define i8 @StaticArrayFixedSafe() nounwind uwtable safestack { |
| 43 | entry: |
| 44 | ; CHECK-LABEL: define i8 @StaticArrayFixedSafe( |
| 45 | ; CHECK-NOT: __safestack_unsafe_stack_ptr |
| 46 | ; CHECK: ret i8 |
| 47 | %buf = alloca i8, i32 4, align 1 |
| 48 | %gep = getelementptr inbounds i8, i8* %buf, i32 2 |
| 49 | %x = load i8, i8* %gep, align 1 |
| 50 | ret i8 %x |
| 51 | } |
| 52 | |
| 53 | ; Load from an array at a fixed offset with overflow. |
| 54 | define i8 @StaticArrayFixedUnsafe() nounwind uwtable safestack { |
| 55 | entry: |
| 56 | ; CHECK-LABEL: define i8 @StaticArrayFixedUnsafe( |
| 57 | ; CHECK: __safestack_unsafe_stack_ptr |
| 58 | ; CHECK: ret i8 |
| 59 | %buf = alloca i8, i32 4, align 1 |
| 60 | %gep = getelementptr inbounds i8, i8* %buf, i32 5 |
| 61 | %x = load i8, i8* %gep, align 1 |
| 62 | ret i8 %x |
| 63 | } |
| 64 | |
| 65 | ; Load from an array at an unknown offset. |
| 66 | define i8 @StaticArrayVariableUnsafe(i32 %ofs) nounwind uwtable safestack { |
| 67 | entry: |
| 68 | ; CHECK-LABEL: define i8 @StaticArrayVariableUnsafe( |
| 69 | ; CHECK: __safestack_unsafe_stack_ptr |
| 70 | ; CHECK: ret i8 |
| 71 | %buf = alloca i8, i32 4, align 1 |
| 72 | %gep = getelementptr inbounds i8, i8* %buf, i32 %ofs |
| 73 | %x = load i8, i8* %gep, align 1 |
| 74 | ret i8 %x |
| 75 | } |
| 76 | |
| 77 | ; Load from an array of an unknown size. |
| 78 | define i8 @DynamicArrayUnsafe(i32 %sz) nounwind uwtable safestack { |
| 79 | entry: |
| 80 | ; CHECK-LABEL: define i8 @DynamicArrayUnsafe( |
| 81 | ; CHECK: __safestack_unsafe_stack_ptr |
| 82 | ; CHECK: ret i8 |
| 83 | %buf = alloca i8, i32 %sz, align 1 |
| 84 | %gep = getelementptr inbounds i8, i8* %buf, i32 2 |
| 85 | %x = load i8, i8* %gep, align 1 |
| 86 | ret i8 %x |
| 87 | } |
| 88 | |
Peter Collingbourne | 82437bf | 2015-06-15 21:07:11 +0000 | [diff] [blame] | 89 | declare i8* @strcpy(i8*, i8*) |