• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

ossia / libossia / 45061331480

22 Nov 2026 07:32PM UTC coverage: 56.861% (+2.9%) from 54.011%
45061331480

Pull #988

github

web-flow
Merge 200fd19ef into 3ad59e1db
Pull Request #988: ossia-max: ossia.v8connector, parameters declared by a v8 script

2254 of 2484 new or added lines in 8 files covered. (90.74%)

780 existing lines in 17 files now uncovered.

37613 of 66149 relevant lines covered (56.86%)

189595.5 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

97.59
/tests/Network/SocketLifetimeTest.cpp
1
// This is an open source non-commercial project. Dear PVS-Studio, please check it.
2
// PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
3

4
#include "include_catch.hpp"
5

6
#include <ossia/detail/config.hpp>
7

8
#include <ossia/network/sockets/framing.hpp>
9
#include <ossia/network/sockets/line_framing.hpp>
10
#include <ossia/network/sockets/no_framing.hpp>
11
#include <ossia/network/sockets/serial_socket.hpp>
12
#include <ossia/network/sockets/tcp_socket.hpp>
13
#include <ossia/network/sockets/udp_socket.hpp>
14
#include <ossia/network/sockets/unix_socket.hpp>
15

16
#include <boost/asio/io_context.hpp>
17

18
#include <chrono>
19
#include <memory>
20
#include <optional>
21

22
/**
23
 * Destroying a socket that still has a read outstanding.
24
 *
25
 * Closing a descriptor does not make asio forget the operations pending on it:
26
 * they complete with operation_aborted on the next poll(), usually after the
27
 * owner is gone, and the handlers capture `this`.
28
 *
29
 * Each test arms a read, destroys the socket and pumps the context. Run them
30
 * under ASan - otherwise a use-after-free on a just-freed allocation goes
31
 * unnoticed and they pass either way.
32
 */
33

34
#if !defined(_WIN32)
35
#include <fcntl.h>
36
#include <stdlib.h>
37
#include <unistd.h>
38
#endif
39

40
namespace
41
{
42
//! Nano::Signal wants a callable object, not a lambda.
43
struct flag
44
{
45
  bool value{};
46
  void operator()() { value = true; }
1 ✔
47
};
48

49
//! Run the queued completions, including the aborted ones.
50
void drain(boost::asio::io_context& ctx)
23 ✔
51
{
52
  for(int i = 0; i < 8; i++)
207 ✔
53
  {
54
    ctx.poll();
184 ✔
55
    ctx.restart();
184 ✔
56
  }
57
}
23 ✔
58

59
#if !defined(_WIN32)
60
//! A pseudo-terminal is close enough to a serial port for asio to drive it,
61
//! which gives the test a device to open without any hardware.
62
struct pty_pair
63
{
64
  int master{-1};
65
  std::string slave;
66

67
  pty_pair()
7 ✔
68
  {
7 ✔
69
    master = ::posix_openpt(O_RDWR | O_NOCTTY);
7 ✔
70
    if(master < 0)
7 ✔
71
      return;
×
72
    if(::grantpt(master) != 0 || ::unlockpt(master) != 0)
7 ✔
UNCOV
73
      return;
×
74
    if(const char* name = ::ptsname(master))
7 ✔
75
      slave = name;
7 ✔
UNCOV
76
  }
×
77

78
  ~pty_pair()
7 ✔
79
  {
80
    if(master >= 0)
7 ✔
81
      ::close(master);
7 ✔
82
  }
7 ✔
83

84
  bool valid() const { return master >= 0 && !slave.empty(); }
7 ✔
85
};
86

87
//! Open a serial_socket on a pty, arm a read, destroy it, then pump.
88
template <typename Framing>
89
void serial_read_outlives_socket()
7 ✔
90
{
91
  pty_pair pty;
7 ✔
92
  if(!pty.valid())
7 ✔
UNCOV
93
    SKIP("could not allocate a pseudo-terminal");
×
94

95
  boost::asio::io_context ctx;
7 ✔
96

97
  ossia::net::serial_configuration conf;
7 ✔
98
  conf.port = pty.slave;
7 ✔
99

100
  // Heap-allocated on purpose: this is how the protocols own their socket, and
101
  // it is what makes the failure a plain heap-use-after-free for ASan.
102
  auto sock = std::make_unique<ossia::net::serial_socket<Framing>>(conf, ctx);
7 ✔
103
  sock->connect();
7 ✔
104

105
  // connect() posts on_open() with a bare `this`; run it now so that the test
106
  // is about the pending *read* and nothing else.
107
  drain(ctx);
7 ✔
108

109
  bool got_message = false;
7 ✔
110
  sock->receive([&](const unsigned char*, std::size_t) { got_message = true; });
7 ✔
111

112
  // Nothing is ever written to the master side, so the read is still pending.
113
  sock.reset();
7 ✔
114

115
  // Before the fix: the aborted completion runs here, calls
116
  // stream_processor::validate_stream, and that calls on_fail() on the freed
117
  // serial_socket.
118
  drain(ctx);
7 ✔
119

120
  REQUIRE(!got_message);
7 ✔
121
}
7 ✔
122
#endif
123
}
124

125
#if !defined(_WIN32)
126
TEST_CASE("test_serial_socket_destroyed_with_pending_read", "test_socket_lifetime")
1 ✔
127
{
128
  serial_read_outlives_socket<ossia::net::no_framing>();
1 ✔
129
}
1 ✔
130

131
TEST_CASE("test_serial_slip_destroyed_with_pending_read", "test_socket_lifetime")
1 ✔
132
{
133
  serial_read_outlives_socket<ossia::net::slip_framing>();
1 ✔
134
}
1 ✔
135

136
TEST_CASE("test_serial_cobs_destroyed_with_pending_read", "test_socket_lifetime")
1 ✔
137
{
138
  serial_read_outlives_socket<ossia::net::cobs_framing>();
1 ✔
139
}
1 ✔
140

141
TEST_CASE("test_serial_size_prefix_destroyed_with_pending_read", "test_socket_lifetime")
1 ✔
142
{
143
  serial_read_outlives_socket<ossia::net::size_prefix_framing>();
1 ✔
144
}
1 ✔
145

146
TEST_CASE("test_serial_stx_etx_destroyed_with_pending_read", "test_socket_lifetime")
1 ✔
147
{
148
  serial_read_outlives_socket<ossia::net::stx_etx_framing>();
1 ✔
149
}
1 ✔
150

151
TEST_CASE("test_serial_fixed_length_destroyed_with_pending_read", "test_socket_lifetime")
1 ✔
152
{
153
  serial_read_outlives_socket<ossia::net::fixed_length_framing>();
1 ✔
154
}
1 ✔
155

156
TEST_CASE("test_serial_line_destroyed_with_pending_read", "test_socket_lifetime")
1 ✔
157
{
158
  serial_read_outlives_socket<ossia::net::line_framing>();
1 ✔
159
}
1 ✔
160
#endif
161

162
TEST_CASE("test_udp_receive_socket_destroyed_with_pending_read", "test_socket_lifetime")
1 ✔
163
{
164
  boost::asio::io_context ctx;
1 ✔
165

166
  ossia::net::inbound_socket_configuration conf;
1 ✔
167
  conf.bind = "127.0.0.1";
1 ✔
168
  conf.port = 0; // ephemeral: nothing else needs to find us
1 ✔
169

170
  auto sock = std::make_unique<ossia::net::udp_receive_socket>(conf, ctx);
1 ✔
171
  sock->open();
1 ✔
172

173
  bool got_message = false;
1 ✔
174
  sock->receive([&](const char*, std::size_t) { got_message = true; });
1 ✔
175

176
  sock.reset();
1 ✔
177
  drain(ctx);
1 ✔
178

179
  REQUIRE(!got_message);
1 ✔
180
}
1 ✔
181

182
TEST_CASE("test_tcp_client_destroyed_with_pending_read", "test_socket_lifetime")
1 ✔
183
{
184
  boost::asio::io_context ctx;
1 ✔
185

186
  boost::asio::ip::tcp::acceptor acceptor{
187
      ctx,
188
      boost::asio::ip::tcp::endpoint{boost::asio::ip::make_address("127.0.0.1"), 0}};
1 ✔
189
  const auto port = acceptor.local_endpoint().port();
1 ✔
190

191
  // The accepted socket has to stay alive, otherwise the client's read would
192
  // complete with eof instead of staying pending.
193
  boost::asio::ip::tcp::socket peer{ctx};
1 ✔
194
  std::optional<boost::system::error_code> accept_result;
1 ✔
195
  acceptor.async_accept(peer, [&](boost::system::error_code ec) { accept_result = ec; });
2 ✔
196

197
  ossia::net::outbound_socket_configuration conf;
1 ✔
198
  conf.host = "127.0.0.1";
1 ✔
199
  conf.port = port;
1 ✔
200

201
  using client
202
      = ossia::net::framed_client<ossia::net::tcp_client, ossia::net::slip_framing>;
203
  auto sock = std::make_unique<client>(conf, ctx);
1 ✔
204

205
  flag connected;
1 ✔
206
  sock->on_open.connect(&connected);
1 ✔
207
  sock->connect();
1 ✔
208

209
  // Wait for the OS to complete both sides of the handshake before arming the read.
210
  ctx.run_for(std::chrono::seconds{5});
1 ✔
211
  REQUIRE(accept_result.has_value());
1 ✔
212
  REQUIRE(!*accept_result);
1 ✔
213
  REQUIRE(connected.value);
1 ✔
214
  ctx.restart();
1 ✔
215

216
  bool got_message = false;
1 ✔
217
  sock->receive([&](const unsigned char*, std::size_t) { got_message = true; });
1 ✔
218

219
  sock.reset();
1 ✔
220
  drain(ctx);
1 ✔
221

222
  REQUIRE(!got_message);
1 ✔
223
}
1 ✔
224

225
TEST_CASE("test_tcp_client_destroyed_with_pending_connect", "test_socket_lifetime")
1 ✔
226
{
227
  // Same defect, on the *connect* handler rather than the read one: async_connect
228
  // also captures `this`, and a socket destroyed before the connection settles
229
  // leaves that handler queued.
230
  boost::asio::io_context ctx;
1 ✔
231

232
  boost::asio::ip::tcp::acceptor acceptor{
233
      ctx,
234
      boost::asio::ip::tcp::endpoint{boost::asio::ip::make_address("127.0.0.1"), 0}};
1 ✔
235
  const auto port = acceptor.local_endpoint().port();
1 ✔
236

237
  ossia::net::outbound_socket_configuration conf;
1 ✔
238
  conf.host = "127.0.0.1";
1 ✔
239
  conf.port = port;
1 ✔
240

241
  auto sock = std::make_unique<ossia::net::tcp_client>(conf, ctx);
1 ✔
242
  sock->connect();
1 ✔
243

244
  // No drain(): the connect is still in flight.
245
  sock.reset();
1 ✔
246
  drain(ctx);
1 ✔
247
}
1 ✔
248

249
TEST_CASE("test_udp_receive_socket_destroyed_after_close", "test_socket_lifetime")
1 ✔
250
{
251
  // close() does not close anything itself, it *posts* a lambda that does. The
252
  // usual shutdown sequence is close() immediately followed by dropping the
253
  // owner, which leaves that lambda queued on a destroyed socket.
254
  boost::asio::io_context ctx;
1 ✔
255

256
  ossia::net::inbound_socket_configuration conf;
1 ✔
257
  conf.bind = "127.0.0.1";
1 ✔
258
  conf.port = 0;
1 ✔
259

260
  auto sock = std::make_unique<ossia::net::udp_receive_socket>(conf, ctx);
1 ✔
261
  sock->open();
1 ✔
262
  sock->close();
1 ✔
263
  sock.reset();
1 ✔
264

265
  drain(ctx);
1 ✔
266
}
1 ✔
267

268
TEST_CASE("test_tcp_client_destroyed_after_close", "test_socket_lifetime")
1 ✔
269
{
270
  boost::asio::io_context ctx;
1 ✔
271

272
  boost::asio::ip::tcp::acceptor acceptor{
273
      ctx,
274
      boost::asio::ip::tcp::endpoint{boost::asio::ip::make_address("127.0.0.1"), 0}};
1 ✔
275

276
  ossia::net::outbound_socket_configuration conf;
1 ✔
277
  conf.host = "127.0.0.1";
1 ✔
278
  conf.port = acceptor.local_endpoint().port();
1 ✔
279

280
  auto sock = std::make_unique<ossia::net::tcp_client>(conf, ctx);
1 ✔
281
  sock->connect();
1 ✔
282
  drain(ctx);
1 ✔
283

284
  sock->close();
1 ✔
285
  sock.reset();
1 ✔
286

287
  drain(ctx);
1 ✔
288
}
1 ✔
289

290
#if defined(BOOST_ASIO_HAS_LOCAL_SOCKETS)
291
TEST_CASE("test_unix_stream_client_destroyed_with_pending_read", "test_socket_lifetime")
1 ✔
292
{
293
  boost::asio::io_context ctx;
1 ✔
294

295
  const std::string path = "/tmp/ossia-socket-lifetime-test.sock";
1 ✔
296
  ::unlink(path.c_str());
1 ✔
297

298
  boost::asio::local::stream_protocol::acceptor acceptor{
299
      ctx, boost::asio::local::stream_protocol::endpoint{path}};
1 ✔
300

301
  boost::asio::local::stream_protocol::socket peer{ctx};
1 ✔
302
  acceptor.async_accept(peer, [](boost::system::error_code) { });
1 ✔
303

304
  ossia::net::fd_configuration conf;
1 ✔
305
  conf.fd = path;
1 ✔
306

307
  using client = ossia::net::framed_socket<
308
      ossia::net::unix_stream_client, ossia::net::size_prefix_framing>;
309
  auto sock = std::make_unique<client>(conf, ctx);
1 ✔
310
  sock->connect();
1 ✔
311

312
  drain(ctx);
1 ✔
313

314
  bool got_message = false;
1 ✔
315
  sock->receive([&](const unsigned char*, std::size_t) { got_message = true; });
1 ✔
316

317
  sock.reset();
1 ✔
318
  drain(ctx);
1 ✔
319
  ::unlink(path.c_str());
1 ✔
320

321
  REQUIRE(!got_message);
1 ✔
322
}
1 ✔
323

324
TEST_CASE(
1 ✔
325
    "test_unix_datagram_socket_destroyed_with_pending_read", "test_socket_lifetime")
326
{
327
  // This one is expected to have been safe all along: unix_datagram_socket
328
  // already cancels and drains in its destructor -- see the comment there, it
329
  // was added for a crash of exactly this family on macOS. The test is here so
330
  // that the property does not quietly regress.
331
  boost::asio::io_context ctx;
1 ✔
332

333
  const std::string path = "/tmp/ossia-socket-lifetime-test.dgram";
1 ✔
334
  ::unlink(path.c_str());
1 ✔
335

336
  ossia::net::fd_configuration conf;
1 ✔
337
  conf.fd = path;
1 ✔
338

339
  auto sock = std::make_unique<ossia::net::unix_datagram_socket>(conf, ctx);
1 ✔
340
  sock->open();
1 ✔
341

342
  bool got_message = false;
1 ✔
343
  sock->receive([&](const char*, std::size_t) { got_message = true; });
1 ✔
344

345
  sock.reset();
1 ✔
346
  drain(ctx);
1 ✔
347
  ::unlink(path.c_str());
1 ✔
348

349
  REQUIRE(!got_message);
1 ✔
350
}
1 ✔
351
#endif
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc