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 -S -mtriple=x86_64-pc-linux-gnu < %s -o - | FileCheck %s
      3 
      4 @.str = private unnamed_addr constant [4 x i8] c"%s\0A\00", align 1
      5 
      6 ; no arrays / no nested arrays
      7 ; Requires no protector.
      8 
      9 define void @foo(i8* %a) nounwind uwtable safestack {
     10 entry:
     11   ; CHECK-LABEL: define void @foo(
     12   ; CHECK-NOT: __safestack_unsafe_stack_ptr
     13   ; CHECK: ret void
     14   %a.addr = alloca i8*, align 8
     15   store i8* %a, i8** %a.addr, align 8
     16   %0 = load i8*, i8** %a.addr, align 8
     17   %call = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i32 0, i32 0), i8* %0)
     18   ret void
     19 }
     20 
     21 declare i32 @printf(i8*, ...)
     22 
     23 target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
     24 target triple = "x86_64-unknown-linux-gnu"
     25 
     26 define void @call_memset(i64 %len) safestack {
     27 entry:
     28   ; CHECK-LABEL: define void @call_memset
     29   ; CHECK: @__safestack_unsafe_stack_ptr
     30   ; CHECK: ret void
     31   %q = alloca [10 x i8], align 1
     32   %arraydecay = getelementptr inbounds [10 x i8], [10 x i8]* %q, i32 0, i32 0
     33   call void @llvm.memset.p0i8.i64(i8* %arraydecay, i8 1, i64 %len, i32 1, i1 false)
     34   ret void
     35 }
     36 
     37 define void @call_constant_memset() safestack {
     38 entry:
     39   ; CHECK-LABEL: define void @call_constant_memset
     40   ; CHECK-NOT: @__safestack_unsafe_stack_ptr
     41   ; CHECK: ret void
     42   %q = alloca [10 x i8], align 1
     43   %arraydecay = getelementptr inbounds [10 x i8], [10 x i8]* %q, i32 0, i32 2
     44   call void @llvm.memset.p0i8.i64(i8* %arraydecay, i8 1, i64 7, i32 1, i1 false)
     45   ret void
     46 }
     47 
     48 define void @call_constant_overflow_memset() safestack {
     49 entry:
     50   ; CHECK-LABEL: define void @call_constant_overflow_memset
     51   ; CHECK: @__safestack_unsafe_stack_ptr
     52   ; CHECK: ret void
     53   %q = alloca [10 x i8], align 1
     54   %arraydecay = getelementptr inbounds [10 x i8], [10 x i8]* %q, i32 0, i32 7
     55   call void @llvm.memset.p0i8.i64(i8* %arraydecay, i8 1, i64 5, i32 1, i1 false)
     56   ret void
     57 }
     58 
     59 define void @call_constant_underflow_memset() safestack {
     60 entry:
     61   ; CHECK-LABEL: define void @call_constant_underflow_memset
     62   ; CHECK: @__safestack_unsafe_stack_ptr
     63   ; CHECK: ret void
     64   %q = alloca [10 x i8], align 1
     65   %arraydecay = getelementptr [10 x i8], [10 x i8]* %q, i32 0, i32 -1
     66   call void @llvm.memset.p0i8.i64(i8* %arraydecay, i8 1, i64 3, i32 1, i1 false)
     67   ret void
     68 }
     69 
     70 ; Readnone nocapture -> safe
     71 define void @call_readnone(i64 %len) safestack {
     72 entry:
     73   ; CHECK-LABEL: define void @call_readnone
     74   ; CHECK-NOT: @__safestack_unsafe_stack_ptr
     75   ; CHECK: ret void
     76   %q = alloca [10 x i8], align 1
     77   %arraydecay = getelementptr inbounds [10 x i8], [10 x i8]* %q, i32 0, i32 0
     78   call void @readnone(i8* %arraydecay)
     79   ret void
     80 }
     81 
     82 ; Arg0 is readnone, arg1 is not. Pass alloca ptr as arg0 -> safe
     83 define void @call_readnone0_0(i64 %len) safestack {
     84 entry:
     85   ; CHECK-LABEL: define void @call_readnone0_0
     86   ; CHECK-NOT: @__safestack_unsafe_stack_ptr
     87   ; CHECK: ret void
     88   %q = alloca [10 x i8], align 1
     89   %arraydecay = getelementptr inbounds [10 x i8], [10 x i8]* %q, i32 0, i32 0
     90   call void @readnone0(i8* %arraydecay, i8* zeroinitializer)
     91   ret void
     92 }
     93 
     94 ; Arg0 is readnone, arg1 is not. Pass alloca ptr as arg1 -> unsafe
     95 define void @call_readnone0_1(i64 %len) safestack {
     96 entry:
     97   ; CHECK-LABEL: define void @call_readnone0_1
     98   ; CHECK: @__safestack_unsafe_stack_ptr
     99   ; CHECK: ret void
    100   %q = alloca [10 x i8], align 1
    101   %arraydecay = getelementptr inbounds [10 x i8], [10 x i8]* %q, i32 0, i32 0
    102   call void @readnone0(i8 *zeroinitializer, i8* %arraydecay)
    103   ret void
    104 }
    105 
    106 ; Readonly nocapture -> unsafe
    107 define void @call_readonly(i64 %len) safestack {
    108 entry:
    109   ; CHECK-LABEL: define void @call_readonly
    110   ; CHECK: @__safestack_unsafe_stack_ptr
    111   ; CHECK: ret void
    112   %q = alloca [10 x i8], align 1
    113   %arraydecay = getelementptr inbounds [10 x i8], [10 x i8]* %q, i32 0, i32 0
    114   call void @readonly(i8* %arraydecay)
    115   ret void
    116 }
    117 
    118 ; Readonly nocapture -> unsafe
    119 define void @call_arg_readonly(i64 %len) safestack {
    120 entry:
    121   ; CHECK-LABEL: define void @call_arg_readonly
    122   ; CHECK: @__safestack_unsafe_stack_ptr
    123   ; CHECK: ret void
    124   %q = alloca [10 x i8], align 1
    125   %arraydecay = getelementptr inbounds [10 x i8], [10 x i8]* %q, i32 0, i32 0
    126   call void @arg_readonly(i8* %arraydecay)
    127   ret void
    128 }
    129 
    130 ; Readwrite nocapture -> unsafe
    131 define void @call_readwrite(i64 %len) safestack {
    132 entry:
    133   ; CHECK-LABEL: define void @call_readwrite
    134   ; CHECK: @__safestack_unsafe_stack_ptr
    135   ; CHECK: ret void
    136   %q = alloca [10 x i8], align 1
    137   %arraydecay = getelementptr inbounds [10 x i8], [10 x i8]* %q, i32 0, i32 0
    138   call void @readwrite(i8* %arraydecay)
    139   ret void
    140 }
    141 
    142 ; Captures the argument -> unsafe
    143 define void @call_capture(i64 %len) safestack {
    144 entry:
    145   ; CHECK-LABEL: define void @call_capture
    146   ; CHECK: @__safestack_unsafe_stack_ptr
    147   ; CHECK: ret void
    148   %q = alloca [10 x i8], align 1
    149   %arraydecay = getelementptr inbounds [10 x i8], [10 x i8]* %q, i32 0, i32 0
    150   call void @capture(i8* %arraydecay)
    151   ret void
    152 }
    153 
    154 ; Lifetime intrinsics are always safe.
    155 define void @call_lifetime(i32* %p) {
    156   ; CHECK-LABEL: define void @call_lifetime
    157   ; CHECK-NOT: @__safestack_unsafe_stack_ptr
    158   ; CHECK: ret void
    159 entry:
    160   %q = alloca [100 x i8], align 16
    161   %0 = bitcast [100 x i8]* %q to i8*
    162   call void @llvm.lifetime.start(i64 100, i8* %0)
    163   call void @llvm.lifetime.end(i64 100, i8* %0)
    164   ret void
    165 }
    166 
    167 declare void @readonly(i8* nocapture) readonly
    168 declare void @arg_readonly(i8* readonly nocapture)
    169 declare void @readwrite(i8* nocapture)
    170 declare void @capture(i8* readnone) readnone
    171 
    172 declare void @readnone(i8* nocapture) readnone
    173 declare void @readnone0(i8* nocapture readnone, i8* nocapture)
    174 
    175 declare void @llvm.memset.p0i8.i64(i8* nocapture, i8, i64, i32, i1) nounwind argmemonly
    176 
    177 declare void @llvm.lifetime.start(i64, i8* nocapture) nounwind argmemonly
    178 declare void @llvm.lifetime.end(i64, i8* nocapture) nounwind argmemonly
    179