1 //===-- tsan_string.cpp ---------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file is a part of ThreadSanitizer (TSan), a race detector.
10 //
11 //===----------------------------------------------------------------------===//
12 #include "tsan_test_util.h"
13 #include "gtest/gtest.h"
14 #include <string.h>
15
16 namespace __tsan {
17
TEST(ThreadSanitizer,Memcpy)18 TEST(ThreadSanitizer, Memcpy) {
19 char data0[7] = {1, 2, 3, 4, 5, 6, 7};
20 char data[7] = {42, 42, 42, 42, 42, 42, 42};
21 MainThread().Memcpy(data+1, data0+1, 5);
22 EXPECT_EQ(data[0], 42);
23 EXPECT_EQ(data[1], 2);
24 EXPECT_EQ(data[2], 3);
25 EXPECT_EQ(data[3], 4);
26 EXPECT_EQ(data[4], 5);
27 EXPECT_EQ(data[5], 6);
28 EXPECT_EQ(data[6], 42);
29 MainThread().Memset(data+1, 13, 5);
30 EXPECT_EQ(data[0], 42);
31 EXPECT_EQ(data[1], 13);
32 EXPECT_EQ(data[2], 13);
33 EXPECT_EQ(data[3], 13);
34 EXPECT_EQ(data[4], 13);
35 EXPECT_EQ(data[5], 13);
36 EXPECT_EQ(data[6], 42);
37 }
38
TEST(ThreadSanitizer,MemcpyRace1)39 TEST(ThreadSanitizer, MemcpyRace1) {
40 char *data = new char[10];
41 char *data1 = new char[10];
42 char *data2 = new char[10];
43 ScopedThread t1, t2;
44 t1.Memcpy(data, data1, 10);
45 t2.Memcpy(data, data2, 10, true);
46 }
47
TEST(ThreadSanitizer,MemcpyRace2)48 TEST(ThreadSanitizer, MemcpyRace2) {
49 char *data = new char[10];
50 char *data1 = new char[10];
51 char *data2 = new char[10];
52 ScopedThread t1, t2;
53 t1.Memcpy(data+5, data1, 1);
54 t2.Memcpy(data+3, data2, 4, true);
55 }
56
TEST(ThreadSanitizer,MemcpyRace3)57 TEST(ThreadSanitizer, MemcpyRace3) {
58 char *data = new char[10];
59 char *data1 = new char[10];
60 char *data2 = new char[10];
61 ScopedThread t1, t2;
62 t1.Memcpy(data, data1, 10);
63 t2.Memcpy(data1, data2, 10, true);
64 }
65
TEST(ThreadSanitizer,MemcpyStack)66 TEST(ThreadSanitizer, MemcpyStack) {
67 char *data = new char[10];
68 char *data1 = new char[10];
69 ScopedThread t1, t2;
70 t1.Memcpy(data, data1, 10);
71 t2.Memcpy(data, data1, 10, true);
72 }
73
TEST(ThreadSanitizer,MemsetRace1)74 TEST(ThreadSanitizer, MemsetRace1) {
75 char *data = new char[10];
76 ScopedThread t1, t2;
77 t1.Memset(data, 1, 10);
78 t2.Memset(data, 2, 10, true);
79 }
80
81 } // namespace __tsan
82