1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
|
//===-- CommunicationTest.cpp ---------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "lldb/Core/Communication.h"
#include "TestingSupport/SubsystemRAII.h"
#include "lldb/Core/ThreadedCommunication.h"
#include "lldb/Host/Config.h"
#include "lldb/Host/ConnectionFileDescriptor.h"
#include "lldb/Host/Pipe.h"
#include "lldb/Host/Socket.h"
#include "llvm/Testing/Support/Error.h"
#include "gtest/gtest.h"
#include <chrono>
#include <thread>
#if LLDB_ENABLE_POSIX
#include <fcntl.h>
#endif
using namespace lldb_private;
class CommunicationTest : public testing::Test {
private:
SubsystemRAII<Socket> m_subsystems;
};
static void CommunicationReadTest(bool use_read_thread) {
llvm::Expected<Socket::Pair> pair = Socket::CreatePair();
ASSERT_THAT_EXPECTED(pair, llvm::Succeeded());
Socket &a = *pair->first;
size_t num_bytes = 4;
ASSERT_THAT_ERROR(a.Write("test", num_bytes).ToError(), llvm::Succeeded());
ASSERT_EQ(num_bytes, 4U);
ThreadedCommunication comm("test");
comm.SetConnection(
std::make_unique<ConnectionFileDescriptor>(std::move(pair->second)));
comm.SetCloseOnEOF(true);
if (use_read_thread) {
ASSERT_TRUE(comm.StartReadThread());
}
// This read should wait for the data to become available and return it.
lldb::ConnectionStatus status = lldb::eConnectionStatusSuccess;
char buf[16];
Status error;
EXPECT_EQ(
comm.Read(buf, sizeof(buf), std::chrono::seconds(5), status, &error), 4U);
EXPECT_EQ(status, lldb::eConnectionStatusSuccess);
EXPECT_THAT_ERROR(error.ToError(), llvm::Succeeded());
buf[4] = 0;
EXPECT_STREQ(buf, "test");
// These reads should time out as there is no more data.
error.Clear();
EXPECT_EQ(comm.Read(buf, sizeof(buf), std::chrono::microseconds(10), status,
&error),
0U);
EXPECT_EQ(status, lldb::eConnectionStatusTimedOut);
EXPECT_THAT_ERROR(error.ToError(), llvm::Failed());
// 0 is special-cased, so we test it separately.
error.Clear();
EXPECT_EQ(
comm.Read(buf, sizeof(buf), std::chrono::seconds(0), status, &error), 0U);
EXPECT_EQ(status, lldb::eConnectionStatusTimedOut);
EXPECT_THAT_ERROR(error.ToError(), llvm::Failed());
// This read should return EOF.
ASSERT_THAT_ERROR(a.Close().ToError(), llvm::Succeeded());
error.Clear();
EXPECT_EQ(
comm.Read(buf, sizeof(buf), std::chrono::seconds(5), status, &error), 0U);
EXPECT_EQ(status, lldb::eConnectionStatusEndOfFile);
EXPECT_THAT_ERROR(error.ToError(), llvm::Succeeded());
// JoinReadThread() should just return immediately if there was no read
// thread started.
EXPECT_TRUE(comm.JoinReadThread());
// Test using Communication that is disconnected.
ASSERT_EQ(comm.Disconnect(), lldb::eConnectionStatusSuccess);
if (use_read_thread) {
ASSERT_TRUE(comm.StartReadThread());
}
error.Clear();
EXPECT_EQ(
comm.Read(buf, sizeof(buf), std::chrono::seconds(5), status, &error), 0U);
EXPECT_EQ(status, lldb::eConnectionStatusLostConnection);
EXPECT_THAT_ERROR(error.ToError(), llvm::Failed());
EXPECT_TRUE(comm.JoinReadThread());
// Test using Communication without a connection.
comm.SetConnection(nullptr);
if (use_read_thread) {
ASSERT_TRUE(comm.StartReadThread());
}
error.Clear();
EXPECT_EQ(
comm.Read(buf, sizeof(buf), std::chrono::seconds(5), status, &error), 0U);
EXPECT_EQ(status, lldb::eConnectionStatusNoConnection);
EXPECT_THAT_ERROR(error.ToError(), llvm::Failed());
EXPECT_TRUE(comm.JoinReadThread());
}
TEST_F(CommunicationTest, Read) {
CommunicationReadTest(/*use_thread=*/false);
}
TEST_F(CommunicationTest, ReadThread) {
CommunicationReadTest(/*use_thread=*/true);
}
TEST_F(CommunicationTest, SynchronizeWhileClosing) {
llvm::Expected<Socket::Pair> pair = Socket::CreatePair();
ASSERT_THAT_EXPECTED(pair, llvm::Succeeded());
Socket &a = *pair->first;
ThreadedCommunication comm("test");
comm.SetConnection(
std::make_unique<ConnectionFileDescriptor>(std::move(pair->second)));
comm.SetCloseOnEOF(true);
ASSERT_TRUE(comm.StartReadThread());
// Ensure that we can safely synchronize with the read thread while it is
// closing the read end (in response to us closing the write end).
ASSERT_THAT_ERROR(a.Close().ToError(), llvm::Succeeded());
comm.SynchronizeWithReadThread();
ASSERT_TRUE(comm.StopReadThread());
}
#if LLDB_ENABLE_POSIX
TEST_F(CommunicationTest, WriteAll) {
Pipe pipe;
ASSERT_THAT_ERROR(pipe.CreateNew().ToError(), llvm::Succeeded());
// Make the write end non-blocking in order to easily reproduce a partial
// write.
int write_fd = pipe.ReleaseWriteFileDescriptor();
int flags = fcntl(write_fd, F_GETFL);
ASSERT_NE(flags, -1);
ASSERT_NE(fcntl(write_fd, F_SETFL, flags | O_NONBLOCK), -1);
ConnectionFileDescriptor read_conn{pipe.ReleaseReadFileDescriptor(),
/*owns_fd=*/true};
Communication write_comm;
write_comm.SetConnection(
std::make_unique<ConnectionFileDescriptor>(write_fd, /*owns_fd=*/true));
std::thread read_thread{[&read_conn]() {
// Read using a smaller buffer to increase chances of partial write.
char buf[128 * 1024];
lldb::ConnectionStatus conn_status;
do {
read_conn.Read(buf, sizeof(buf), std::chrono::seconds(1), conn_status,
nullptr);
} while (conn_status != lldb::eConnectionStatusEndOfFile);
}};
// Write 1 MiB of data into the pipe.
lldb::ConnectionStatus conn_status;
Status error;
std::vector<uint8_t> data(1024 * 1024, 0x80);
EXPECT_EQ(write_comm.WriteAll(data.data(), data.size(), conn_status, &error),
data.size());
EXPECT_EQ(conn_status, lldb::eConnectionStatusSuccess);
EXPECT_FALSE(error.Fail());
// Close the write end in order to trigger EOF.
write_comm.Disconnect();
read_thread.join();
}
#endif
|