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 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
|
// Copyright 2015 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/40285824): Remove this and convert code to safer constructs.
#pragma allow_unsafe_buffers
#endif
#include "components/exo/wayland/server.h"
#include <stdlib.h>
#include <memory>
#include <wayland-client-core.h>
#include <wayland-server-core.h>
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/functional/bind.h"
#include "base/functional/callback_forward.h"
#include "base/run_loop.h"
#include "base/synchronization/lock.h"
#include "base/test/bind.h"
#include "base/threading/thread.h"
#include "components/exo/display.h"
#include "components/exo/test/test_security_delegate.h"
#include "components/exo/wayland/server_util.h"
#include "components/exo/wayland/test/wayland_server_test.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace exo::wayland {
using ServerTest = test::WaylandServerTestBase;
struct TestListener {
public:
TestListener();
wl_listener listener;
bool notified = false;
};
TestListener::TestListener() {
listener.notify = [](wl_listener* listener_ptr, void* data) {
TestListener* test_listener = wl_container_of(
listener_ptr, /*sample=*/test_listener, /*member=*/listener);
test_listener->notified = true;
};
}
TEST_F(ServerTest, Open) {
auto server = CreateServer();
// Check that calling Open() succeeds.
bool rv = server->Open();
EXPECT_TRUE(rv);
}
TEST_F(ServerTest, GetFileDescriptor) {
auto server = CreateServer();
bool rv = server->Open();
EXPECT_TRUE(rv);
// Check that the returned file descriptor is valid.
int fd = server->GetFileDescriptor();
DCHECK_GE(fd, 0);
}
TEST_F(ServerTest, SecurityDelegateAssociation) {
auto security_delegate =
std::make_unique<::exo::test::TestSecurityDelegate>();
SecurityDelegate* security_delegate_ptr = security_delegate.get();
auto server = CreateServer(std::move(security_delegate));
EXPECT_EQ(GetSecurityDelegate(server->GetWaylandDisplay()),
security_delegate_ptr);
}
TEST_F(ServerTest, StartFd) {
ScopedTempSocket sock;
auto server = CreateServer();
base::RunLoop start_loop;
server->StartWithFdAsync(sock.TakeFd(),
base::BindLambdaForTesting([&](bool success) {
EXPECT_TRUE(success);
start_loop.Quit();
}));
start_loop.Run();
base::Thread client_thread("client");
client_thread.Start();
wl_display* client_display = nullptr;
base::RunLoop connect_loop;
client_thread.task_runner()->PostTaskAndReply(
FROM_HERE, base::BindLambdaForTesting([&]() {
client_display =
wl_display_connect(sock.server_path().MaybeAsASCII().c_str());
int events = wl_display_roundtrip(client_display);
EXPECT_GE(events, 0);
}),
connect_loop.QuitClosure());
connect_loop.Run();
EXPECT_NE(client_display, nullptr);
wl_list* all_clients =
wl_display_get_client_list(server->GetWaylandDisplay());
ASSERT_FALSE(wl_list_empty(all_clients));
wl_client* client = wl_client_from_link(all_clients->next);
TestListener client_destruction_listener;
wl_client_add_destroy_listener(client, &client_destruction_listener.listener);
client_thread.task_runner()->PostTask(
FROM_HERE, base::BindLambdaForTesting(
[&]() { wl_display_disconnect(client_display); }));
while (!client_destruction_listener.notified) {
server->Dispatch(base::Milliseconds(10));
}
}
TEST_F(ServerTest, Dispatch) {
auto server = CreateServer();
bool rv = server->Open();
EXPECT_TRUE(rv);
base::Thread client_thread("client");
client_thread.Start();
TestListener client_creation_listener;
wl_display_add_client_created_listener(server->GetWaylandDisplay(),
&client_creation_listener.listener);
base::Lock lock;
wl_display* client_display = nullptr;
bool connected_to_server = false;
client_thread.task_runner()->PostTask(
FROM_HERE, base::BindLambdaForTesting([&]() {
// As soon as wl_display_connect() is executed, the server side could
// notify client creation and exit the while-loop. Therefore, the lock
// is required to ensure `connected_to_server` is set before it is
// accessed on the main thread.
base::AutoLock locker(lock);
client_display = wl_display_connect(nullptr);
connected_to_server = !!client_display;
}));
while (!client_creation_listener.notified) {
server->Dispatch(base::Milliseconds(10));
}
// Remove the listener from the display's client creation signal.
wl_list_remove(&client_creation_listener.listener.link);
{
base::AutoLock locker(lock);
EXPECT_TRUE(connected_to_server);
}
wl_list* all_clients =
wl_display_get_client_list(server->GetWaylandDisplay());
ASSERT_FALSE(wl_list_empty(all_clients));
wl_client* client = wl_client_from_link(all_clients->next);
TestListener client_destruction_listener;
wl_client_add_destroy_listener(client, &client_destruction_listener.listener);
client_thread.task_runner()->PostTask(
FROM_HERE, base::BindLambdaForTesting(
[&]() { wl_display_disconnect(client_display); }));
while (!client_destruction_listener.notified) {
server->Dispatch(base::Milliseconds(10));
}
// Remove the listener from the client's destroy signal.
wl_list_remove(&client_destruction_listener.listener.link);
}
using WaylandServerFlushTest = test::WaylandServerTest;
// Calls Server::Flush() to check that it doesn't have any bad side-effects.
TEST_F(WaylandServerFlushTest, Flush) {
EXPECT_TRUE(server_);
server_->Flush();
}
// Regression test for crbug.com/1508130. Asserts that flushing the client
// buffer does not result in a UAF crash.
TEST_F(WaylandServerFlushTest, FlushDoesNotCrashDuringClientDisconnect) {
// Store a reference to the client resource.
wl_client* client = client_resource_;
// The client should not yet have undergone destruction.
EXPECT_FALSE(IsClientDestroyed(client));
// Create a wl_resource associated with the client.
wl_resource* output_resource =
wl_resource_create(client, &wl_output_interface, 3, 0);
// Add user-data on the newly created wl_resource. This will be cleared during
// client destruction.
SetImplementation(output_resource, /*implementation=*/nullptr,
std::make_unique<base::ScopedClosureRunner>(
base::BindLambdaForTesting([&]() {
EXPECT_TRUE(IsClientDestroyed(client));
server_->Flush();
})));
// Push an event to the output client's event buffer so it is non-empty.
wl_output_send_name(output_resource, "test_output");
// Simulate the client closing its connection and disconnecting from Exo. Exo
// should handle this without crashing.
DisconnectClientAndWait();
}
} // namespace exo::wayland
|