1 //===-- tsan_string.cpp ---------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // This file is a part of ThreadSanitizer (TSan), a race detector.
11 //===----------------------------------------------------------------------===//
12 #include "tsan_test_util.h"
13 #include "gtest/gtest.h"
18 TEST_F(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);
39 TEST_F(ThreadSanitizer
, MemcpyRace1
) {
40 char *data
= new char[10];
41 char *data1
= new char[10];
42 char *data2
= new char[10];
44 t1
.Memcpy(data
, data1
, 10);
45 t2
.Memcpy(data
, data2
, 10, true);
48 TEST_F(ThreadSanitizer
, MemcpyRace2
) {
49 char *data
= new char[10];
50 char *data1
= new char[10];
51 char *data2
= new char[10];
53 t1
.Memcpy(data
+5, data1
, 1);
54 t2
.Memcpy(data
+3, data2
, 4, true);
57 TEST_F(ThreadSanitizer
, MemcpyRace3
) {
58 char *data
= new char[10];
59 char *data1
= new char[10];
60 char *data2
= new char[10];
62 t1
.Memcpy(data
, data1
, 10);
63 t2
.Memcpy(data1
, data2
, 10, true);
66 TEST_F(ThreadSanitizer
, MemcpyStack
) {
67 char *data
= new char[10];
68 char *data1
= new char[10];
70 t1
.Memcpy(data
, data1
, 10);
71 t2
.Memcpy(data
, data1
, 10, true);
74 TEST_F(ThreadSanitizer
, MemsetRace1
) {
75 char *data
= new char[10];
77 t1
.Memset(data
, 1, 10);
78 t2
.Memset(data
, 2, 10, true);