summaryrefslogtreecommitdiff
path: root/lib/tsan/tests/unit/tsan_stack_test.cc
blob: d5392959c48c24b49c12cee35ec39ba2fccfc0fd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
//===-- tsan_stack_test.cc ------------------------------------------------===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file is a part of ThreadSanitizer (TSan), a race detector.
//
//===----------------------------------------------------------------------===//
#include "tsan_sync.h"
#include "tsan_rtl.h"
#include "gtest/gtest.h"
#include <string.h>

namespace __tsan {

static void TestStackTrace(StackTrace *trace) {
  ThreadState thr(0, 0, 0, 0, 0, 0, 0, 0);

  trace->ObtainCurrent(&thr, 0);
  EXPECT_EQ(trace->Size(), (uptr)0);

  trace->ObtainCurrent(&thr, 42);
  EXPECT_EQ(trace->Size(), (uptr)1);
  EXPECT_EQ(trace->Get(0), (uptr)42);

  *thr.shadow_stack_pos++ = 100;
  *thr.shadow_stack_pos++ = 101;
  trace->ObtainCurrent(&thr, 0);
  EXPECT_EQ(trace->Size(), (uptr)2);
  EXPECT_EQ(trace->Get(0), (uptr)100);
  EXPECT_EQ(trace->Get(1), (uptr)101);

  trace->ObtainCurrent(&thr, 42);
  EXPECT_EQ(trace->Size(), (uptr)3);
  EXPECT_EQ(trace->Get(0), (uptr)100);
  EXPECT_EQ(trace->Get(1), (uptr)101);
  EXPECT_EQ(trace->Get(2), (uptr)42);
}

TEST(StackTrace, Basic) {
  ScopedInRtl in_rtl;
  StackTrace trace;
  TestStackTrace(&trace);
}

TEST(StackTrace, StaticBasic) {
  ScopedInRtl in_rtl;
  uptr buf[10];
  StackTrace trace1(buf, 10);
  TestStackTrace(&trace1);
  StackTrace trace2(buf, 3);
  TestStackTrace(&trace2);
}

TEST(StackTrace, StaticTrim) {
  ScopedInRtl in_rtl;
  uptr buf[2];
  StackTrace trace(buf, 2);
  ThreadState thr(0, 0, 0, 0, 0, 0, 0, 0);

  *thr.shadow_stack_pos++ = 100;
  *thr.shadow_stack_pos++ = 101;
  *thr.shadow_stack_pos++ = 102;
  trace.ObtainCurrent(&thr, 0);
  EXPECT_EQ(trace.Size(), (uptr)2);
  EXPECT_EQ(trace.Get(0), (uptr)101);
  EXPECT_EQ(trace.Get(1), (uptr)102);

  trace.ObtainCurrent(&thr, 42);
  EXPECT_EQ(trace.Size(), (uptr)2);
  EXPECT_EQ(trace.Get(0), (uptr)102);
  EXPECT_EQ(trace.Get(1), (uptr)42);
}


}  // namespace __tsan