1 //===-- tsan_shadow_test.cc -----------------------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file is a part of ThreadSanitizer (TSan), a race detector.
11 //
12 //===----------------------------------------------------------------------===//
13 #include "tsan_platform.h"
14 #include "tsan_rtl.h"
15 #include "gtest/gtest.h"
16 
17 namespace __tsan {
18 
TEST(Shadow,FastState)19 TEST(Shadow, FastState) {
20   Shadow s(FastState(11, 22));
21   EXPECT_EQ(s.tid(), (u64)11);
22   EXPECT_EQ(s.epoch(), (u64)22);
23   EXPECT_EQ(s.GetIgnoreBit(), false);
24   EXPECT_EQ(s.GetFreedAndReset(), false);
25   EXPECT_EQ(s.GetHistorySize(), 0);
26   EXPECT_EQ(s.addr0(), (u64)0);
27   EXPECT_EQ(s.size(), (u64)1);
28   EXPECT_EQ(s.IsWrite(), true);
29 
30   s.IncrementEpoch();
31   EXPECT_EQ(s.epoch(), (u64)23);
32   s.IncrementEpoch();
33   EXPECT_EQ(s.epoch(), (u64)24);
34 
35   s.SetIgnoreBit();
36   EXPECT_EQ(s.GetIgnoreBit(), true);
37   s.ClearIgnoreBit();
38   EXPECT_EQ(s.GetIgnoreBit(), false);
39 
40   for (int i = 0; i < 8; i++) {
41     s.SetHistorySize(i);
42     EXPECT_EQ(s.GetHistorySize(), i);
43   }
44   s.SetHistorySize(2);
45   s.ClearHistorySize();
46   EXPECT_EQ(s.GetHistorySize(), 0);
47 }
48 
TEST(Shadow,Mapping)49 TEST(Shadow, Mapping) {
50   static int global;
51   int stack;
52   void *heap = malloc(0);
53   free(heap);
54 
55   CHECK(IsAppMem((uptr)&global));
56   CHECK(IsAppMem((uptr)&stack));
57   CHECK(IsAppMem((uptr)heap));
58 
59   CHECK(IsShadowMem(MemToShadow((uptr)&global)));
60   CHECK(IsShadowMem(MemToShadow((uptr)&stack)));
61   CHECK(IsShadowMem(MemToShadow((uptr)heap)));
62 }
63 
TEST(Shadow,Celling)64 TEST(Shadow, Celling) {
65   u64 aligned_data[4];
66   char *data = (char*)aligned_data;
67   CHECK_EQ((uptr)data % kShadowSize, 0);
68   uptr s0 = MemToShadow((uptr)&data[0]);
69   CHECK_EQ(s0 % kShadowSize, 0);
70   for (unsigned i = 1; i < kShadowCell; i++)
71     CHECK_EQ(s0, MemToShadow((uptr)&data[i]));
72   for (unsigned i = kShadowCell; i < 2*kShadowCell; i++)
73     CHECK_EQ(s0 + kShadowSize*kShadowCnt, MemToShadow((uptr)&data[i]));
74   for (unsigned i = 2*kShadowCell; i < 3*kShadowCell; i++)
75     CHECK_EQ(s0 + 2*kShadowSize*kShadowCnt, MemToShadow((uptr)&data[i]));
76 }
77 
78 }  // namespace __tsan
79