109 lines · c
1// RUN: %clang_dfsan -gmlt -mllvm -dfsan-track-origins=1 %s -o %t && \2// RUN: %run %t >%t.out 2>&13// RUN: FileCheck %s < %t.out4//5// RUN: %clang_dfsan -gmlt -mllvm -dfsan-track-origins=1 -mllvm -dfsan-instrument-with-call-threshold=0 %s -o %t && \6// RUN: %run %t >%t.out 2>&17// RUN: FileCheck %s < %t.out8 9#include <assert.h>10#include <malloc.h>11#include <sanitizer/dfsan_interface.h>12#include <stdio.h>13#include <string.h>14 15#define NOINLINE __attribute__((noinline))16 17NOINLINE int foo(int a, int b) { return a + b; }18 19NOINLINE void bar(int depth, void *addr, int size) {20 if (depth) {21 bar(depth - 1, addr, size);22 } else {23 dfsan_set_label(1, addr, size);24 }25}26 27NOINLINE void baz(int depth, void *addr, int size) { bar(depth, addr, size); }28 29int main(int argc, char *argv[]) {30 int a = 10;31 int b = 20;32 baz(8, &a, sizeof(a));33 int c = foo(a, b);34 dfsan_print_origin_trace(&c, NULL);35 // CHECK: Taint value 0x1 {{.*}} origin tracking ()36 // CHECK: Origin value: {{.*}}, Taint value was stored to memory at37 // CHECK: #0 {{.*}} in main {{.*}}origin_stack_trace.c:[[@LINE-4]]38 39 // CHECK: Origin value: {{.*}}, Taint value was created at40 // CHECK: #0 {{.*}} in bar.dfsan {{.*}}origin_stack_trace.c:[[@LINE-17]]41 // CHECK-COUNT-8: #{{[0-9]+}} {{.*}} in bar.dfsan {{.*}}origin_stack_trace.c:[[@LINE-20]]42 // CHECK: #9 {{.*}} in baz.dfsan {{.*}}origin_stack_trace.c:[[@LINE-15]]43 44 // Test logic expects this buffer to be large enough.45 // String contains current paths, which could vary in length.46 // Make this buffer much larger than necessary to accomodate variation.47 const size_t kBufSize = 8192;48 char *buf = (char *)malloc(kBufSize);49 size_t length = dfsan_sprint_origin_trace(&c, NULL, buf, kBufSize);50 assert(kBufSize > length);51 52 printf("==OUTPUT==\n\n%s==EOS==\n", buf);53 // CHECK: ==OUTPUT==54 // CHECK: Taint value 0x1 {{.*}} origin tracking ()55 // CHECK: Origin value: {{.*}}, Taint value was stored to memory at56 // CHECK: #0 {{.*}} in main {{.*}}origin_stack_trace.c:[[@LINE-23]]57 58 // CHECK: Origin value: {{.*}}, Taint value was created at59 // CHECK: #0 {{.*}} in bar.dfsan {{.*}}origin_stack_trace.c:[[@LINE-36]]60 // CHECK-COUNT-8: #{{[0-9]+}} {{.*}} in bar.dfsan {{.*}}origin_stack_trace.c:[[@LINE-39]]61 // CHECK: #9 {{.*}} in baz.dfsan {{.*}}origin_stack_trace.c:[[@LINE-34]]62 // CHECK: ==EOS==63 64 char tinybuf[18];65 size_t same_length =66 dfsan_sprint_origin_trace(&c, NULL, tinybuf, sizeof(tinybuf));67 68 printf("==TRUNCATED OUTPUT==\n\n%s==EOS==\n", tinybuf);69 // CHECK: ==TRUNCATED OUTPUT==70 // CHECK: Taint value 0x1==EOS==71 72 printf("Returned length: %zu\n", length);73 printf("Actual length: %zu\n", strlen(buf));74 printf("Returned length with truncation: %zu\n", same_length);75 76 // CHECK: Returned length: [[#LEN:]]77 // CHECK: Actual length: [[#LEN]]78 // CHECK: Returned length with truncation: [[#LEN]]79 80 size_t length_with_desc =81 dfsan_sprint_origin_trace(&c, "DESCRIPTION", buf, kBufSize);82 assert(kBufSize > length_with_desc);83 84 printf("==OUTPUT==\n\n%s==EOS==\n", buf);85 // CHECK: ==OUTPUT==86 // CHECK: Taint value 0x1 {{.*}} origin tracking (DESCRIPTION)87 // CHECK: Origin value: {{.*}}, Taint value was stored to memory at88 // CHECK: #0 {{.*}} in main {{.*}}origin_stack_trace.c:[[@LINE-55]]89 90 // CHECK: Origin value: {{.*}}, Taint value was created at91 // CHECK: #0 {{.*}} in bar.dfsan {{.*}}origin_stack_trace.c:[[@LINE-68]]92 // CHECK-COUNT-8: #{{[0-9]+}} {{.*}} in bar.dfsan {{.*}}origin_stack_trace.c:[[@LINE-71]]93 // CHECK: #9 {{.*}} in baz.dfsan {{.*}}origin_stack_trace.c:[[@LINE-66]]94 // CHECK: ==EOS==95 96 printf("Returned length: %zu\n", length_with_desc);97 // COMM: Message length is increased by 11: the length of "DESCRIPTION".98 // CHECK: Returned length: [[#LEN + 11]]99 100 buf[0] = '\0';101 length = dfsan_sprint_origin_trace(&c, NULL, buf, 0);102 printf("Output=\"%s\"\n", buf);103 printf("Returned length: %zu\n", length);104 // CHECK: Output=""105 // CHECK: Returned length: [[#LEN]]106 107 free(buf);108}109