1 //===-- tsan_string.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_test_util.h"
14 #include "gtest/gtest.h"
15 #include <string.h>
16
17 namespace __tsan {
18
TEST(ThreadSanitizer,Memcpy)19 TEST(ThreadSanitizer, Memcpy) {
20 char data0[7] = {1, 2, 3, 4, 5, 6, 7};
21 char data[7] = {42, 42, 42, 42, 42, 42, 42};
22 MainThread().Memcpy(data+1, data0+1, 5);
23 EXPECT_EQ(data[0], 42);
24 EXPECT_EQ(data[1], 2);
25 EXPECT_EQ(data[2], 3);
26 EXPECT_EQ(data[3], 4);
27 EXPECT_EQ(data[4], 5);
28 EXPECT_EQ(data[5], 6);
29 EXPECT_EQ(data[6], 42);
30 MainThread().Memset(data+1, 13, 5);
31 EXPECT_EQ(data[0], 42);
32 EXPECT_EQ(data[1], 13);
33 EXPECT_EQ(data[2], 13);
34 EXPECT_EQ(data[3], 13);
35 EXPECT_EQ(data[4], 13);
36 EXPECT_EQ(data[5], 13);
37 EXPECT_EQ(data[6], 42);
38 }
39
TEST(ThreadSanitizer,MemcpyRace1)40 TEST(ThreadSanitizer, MemcpyRace1) {
41 char *data = new char[10];
42 char *data1 = new char[10];
43 char *data2 = new char[10];
44 ScopedThread t1, t2;
45 t1.Memcpy(data, data1, 10);
46 t2.Memcpy(data, data2, 10, true);
47 }
48
TEST(ThreadSanitizer,MemcpyRace2)49 TEST(ThreadSanitizer, MemcpyRace2) {
50 char *data = new char[10];
51 char *data1 = new char[10];
52 char *data2 = new char[10];
53 ScopedThread t1, t2;
54 t1.Memcpy(data+5, data1, 1);
55 t2.Memcpy(data+3, data2, 4, true);
56 }
57
TEST(ThreadSanitizer,MemcpyRace3)58 TEST(ThreadSanitizer, MemcpyRace3) {
59 char *data = new char[10];
60 char *data1 = new char[10];
61 char *data2 = new char[10];
62 ScopedThread t1, t2;
63 t1.Memcpy(data, data1, 10);
64 t2.Memcpy(data1, data2, 10, true);
65 }
66
TEST(ThreadSanitizer,MemcpyStack)67 TEST(ThreadSanitizer, MemcpyStack) {
68 char *data = new char[10];
69 char *data1 = new char[10];
70 ScopedThread t1, t2;
71 t1.Memcpy(data, data1, 10);
72 t2.Memcpy(data, data1, 10, true);
73 }
74
TEST(ThreadSanitizer,MemsetRace1)75 TEST(ThreadSanitizer, MemsetRace1) {
76 char *data = new char[10];
77 ScopedThread t1, t2;
78 t1.Memset(data, 1, 10);
79 t2.Memset(data, 2, 10, true);
80 }
81
82 } // namespace __tsan
83