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 | |
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(dst: data+1, src: data0+1, size: 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(dst: data+1, val: 13, size: 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 | |
39 | TEST_F(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(dst: data, src: data1, size: 10); |
45 | t2.Memcpy(dst: data, src: data2, size: 10, expect_race: true); |
46 | } |
47 | |
48 | TEST_F(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(dst: data+5, src: data1, size: 1); |
54 | t2.Memcpy(dst: data+3, src: data2, size: 4, expect_race: true); |
55 | } |
56 | |
57 | TEST_F(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(dst: data, src: data1, size: 10); |
63 | t2.Memcpy(dst: data1, src: data2, size: 10, expect_race: true); |
64 | } |
65 | |
66 | TEST_F(ThreadSanitizer, MemcpyStack) { |
67 | char *data = new char[10]; |
68 | char *data1 = new char[10]; |
69 | ScopedThread t1, t2; |
70 | t1.Memcpy(dst: data, src: data1, size: 10); |
71 | t2.Memcpy(dst: data, src: data1, size: 10, expect_race: true); |
72 | } |
73 | |
74 | TEST_F(ThreadSanitizer, MemsetRace1) { |
75 | char *data = new char[10]; |
76 | ScopedThread t1, t2; |
77 | t1.Memset(dst: data, val: 1, size: 10); |
78 | t2.Memset(dst: data, val: 2, size: 10, expect_race: true); |
79 | } |
80 | |
81 | } // namespace __tsan |
82 | |