Home | History | Annotate | Download | only in SafeStack
      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   ; CHECK: %[[AADDR:.*]] = alloca i8*, align 8
     21   %a.addr = alloca i8*, align 8
     22 
     23   ; CHECK: %[[BUFPTR:.*]] = getelementptr i8, i8* %[[USP]], i32 -4
     24   ; CHECK: %[[BUFPTR2:.*]] = bitcast i8* %[[BUFPTR]] to [4 x i8]*
     25   %buf = alloca [4 x i8], align 1
     26 
     27   ; CHECK: store i8* {{.*}}, i8** %[[AADDR]], align 8
     28   store i8* %a, i8** %a.addr, align 8
     29 
     30   ; CHECK: %[[GEP:.*]] = getelementptr inbounds [4 x i8], [4 x i8]* %[[BUFPTR2]], i32 0, i32 0
     31   %gep = getelementptr inbounds [4 x i8], [4 x i8]* %buf, i32 0, i32 0
     32 
     33   ; CHECK: %[[A2:.*]] = load i8*, i8** %[[AADDR]], align 8
     34   %a2 = load i8*, i8** %a.addr, align 8
     35 
     36   ; CHECK: call i8* @strcpy(i8* %[[GEP]], i8* %[[A2]])
     37   %call = call i8* @strcpy(i8* %gep, i8* %a2)
     38 
     39   ; CHECK: store i8* %[[USP]], i8** @__safestack_unsafe_stack_ptr
     40   ret void
     41 }
     42 
     43 ; Load from an array at a fixed offset, no overflow.
     44 define i8 @StaticArrayFixedSafe() nounwind uwtable safestack {
     45 entry:
     46   ; CHECK-LABEL: define i8 @StaticArrayFixedSafe(
     47   ; CHECK-NOT: __safestack_unsafe_stack_ptr
     48   ; CHECK: ret i8
     49   %buf = alloca i8, i32 4, align 1
     50   %gep = getelementptr inbounds i8, i8* %buf, i32 2
     51   %x = load i8, i8* %gep, align 1
     52   ret i8 %x
     53 }
     54 
     55 ; Load from an array at a fixed offset with overflow.
     56 define i8 @StaticArrayFixedUnsafe() nounwind uwtable safestack {
     57 entry:
     58   ; CHECK-LABEL: define i8 @StaticArrayFixedUnsafe(
     59   ; CHECK: __safestack_unsafe_stack_ptr
     60   ; CHECK: ret i8
     61   %buf = alloca i8, i32 4, align 1
     62   %gep = getelementptr inbounds i8, i8* %buf, i32 5
     63   %x = load i8, i8* %gep, align 1
     64   ret i8 %x
     65 }
     66 
     67 ; Load from an array at an unknown offset.
     68 define i8 @StaticArrayVariableUnsafe(i32 %ofs) nounwind uwtable safestack {
     69 entry:
     70   ; CHECK-LABEL: define i8 @StaticArrayVariableUnsafe(
     71   ; CHECK: __safestack_unsafe_stack_ptr
     72   ; CHECK: ret i8
     73   %buf = alloca i8, i32 4, align 1
     74   %gep = getelementptr inbounds i8, i8* %buf, i32 %ofs
     75   %x = load i8, i8* %gep, align 1
     76   ret i8 %x
     77 }
     78 
     79 ; Load from an array of an unknown size.
     80 define i8 @DynamicArrayUnsafe(i32 %sz) nounwind uwtable safestack {
     81 entry:
     82   ; CHECK-LABEL: define i8 @DynamicArrayUnsafe(
     83   ; CHECK: __safestack_unsafe_stack_ptr
     84   ; CHECK: ret i8
     85   %buf = alloca i8, i32 %sz, align 1
     86   %gep = getelementptr inbounds i8, i8* %buf, i32 2
     87   %x = load i8, i8* %gep, align 1
     88   ret i8 %x
     89 }
     90 
     91 declare i8* @strcpy(i8*, i8*)
     92