Skip to content

Commit b883edb

Browse files
author
ci-cx
committed
tests: compare a borrowed fd's F_GETFL without Darwin's "descriptor was written" bit (CR-150 follow-up, fixes the three macOS-red tests)
macOS-26 Debug has been red on main since the CR-150 merge because test_report_nonfinite_equity_cabi_fd, test_report_nonfinite_equity_cpp_fd and test_report_sink_fd compare the raw F_GETFL of a borrowed pipe fd across a run. The diagnostics run (engine-free control test_pipe_writer_fd_flags, no engine in the process) showed that on macOS the first successful write to any pipe sets bit 0x00010000 in the writer's F_GETFL (0x1 -> 0x10001), F_GETFD unchanged. The engine does not change a caller-controlled flag; the assumption in the tests is wrong on Darwin. - tests/fd_flag_diagnostics.hpp: comparable_status_flags() clears exactly that one bit on __APPLE__ (FWASWRITTEN, static_asserted to 0x00010000) and is the identity everywhere else; a failed query stays negative. same_flags() now also refuses a failed F_GETFL on either side. - The three tests (all raw F_GETFL equalities across a run, including the rejected-replacement comparison in test_report_sink_fd that the macOS run never reached) compare the masked value on both sides; F_GETFD stays strict; the failure messages still print the raw hex values. - test_pipe_writer_fd_flags: on Apple the raw difference after the first write must be 0 or exactly that bit, on Linux the raw values must be equal; added Linux-running mutation controls showing the helper does not hide a change of O_NONBLOCK, O_APPEND, the access mode or FD_CLOEXEC, nor a failed fcntl. No src/ or include/ change. Verified only by the branch CI (macOS-26 Debug).
1 parent bf2f667 commit b883edb

5 files changed

Lines changed: 76 additions & 8 deletions

‎tests/fd_flag_diagnostics.hpp‎

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,26 @@
1111

1212
namespace fd_flag_diagnostics {
1313

14+
#if defined(__APPLE__)
15+
// Darwin FWASWRITTEN is kernel bookkeeping: "descriptor was written".
16+
// The engine-free test_pipe_writer_fd_flags macOS run observed 0x00010000
17+
// appear on the first successful pipe write without any engine involved.
18+
#if defined(FWASWRITTEN)
19+
inline constexpr int darwin_was_written = FWASWRITTEN;
20+
#else
21+
inline constexpr int darwin_was_written = 0x00010000;
22+
#endif
23+
static_assert(darwin_was_written == 0x00010000, "unexpected Darwin FWASWRITTEN bit");
24+
#endif
25+
26+
inline int comparable_status_flags(int flags) {
27+
#if defined(__APPLE__)
28+
return flags < 0 ? flags : flags & ~darwin_was_written;
29+
#else
30+
return flags;
31+
#endif
32+
}
33+
1434
struct Snapshot {
1535
const char* boundary;
1636
int flags;
@@ -38,7 +58,9 @@ inline Snapshot snapshot(int descriptor, const char* boundary) {
3858
}
3959

4060
inline bool same_flags(const Snapshot& before, const Snapshot& after) {
41-
return before.flags == after.flags && before.descriptor_flags == after.descriptor_flags;
61+
return before.flags >= 0 && after.flags >= 0
62+
&& comparable_status_flags(before.flags) == comparable_status_flags(after.flags)
63+
&& before.descriptor_flags == after.descriptor_flags;
4264
}
4365

4466
inline void append_identity(std::ostream& output, const Snapshot& value) {

‎tests/test_pipe_writer_fd_flags.cpp‎

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,49 @@ void run_case(bool duplicate_writer) {
7575
pipe.close_reader();
7676
const auto after_close = fd_trace.record("after closing reader");
7777

78-
require(after_write.flags == flags && after_write.descriptor_flags == descriptor_flags
79-
&& after_drain.flags == flags && after_drain.descriptor_flags == descriptor_flags
80-
&& after_close.flags == flags && after_close.descriptor_flags == descriptor_flags,
78+
#if defined(__APPLE__)
79+
const int raw_difference = flags ^ after_write.flags;
80+
require(raw_difference == 0 || raw_difference == fd_flag_diagnostics::darwin_was_written,
81+
fd_trace.message("pipe write changed more than Darwin's written bit"));
82+
#else
83+
require(after_write.flags == flags && after_drain.flags == flags && after_close.flags == flags,
84+
fd_trace.message("pipe write changed raw status flags"));
85+
#endif
86+
require(fd_flag_diagnostics::same_flags(fd_trace.baseline(), after_write)
87+
&& fd_flag_diagnostics::same_flags(fd_trace.baseline(), after_drain)
88+
&& fd_flag_diagnostics::same_flags(fd_trace.baseline(), after_close),
89+
fd_trace.message("comparable pipe writer flags changed"));
90+
require(fd_flag_diagnostics::comparable_status_flags(after_write.flags)
91+
== fd_flag_diagnostics::comparable_status_flags(flags)
92+
&& after_write.descriptor_flags == descriptor_flags
93+
&& fd_flag_diagnostics::comparable_status_flags(after_drain.flags)
94+
== fd_flag_diagnostics::comparable_status_flags(flags)
95+
&& after_drain.descriptor_flags == descriptor_flags
96+
&& fd_flag_diagnostics::comparable_status_flags(after_close.flags)
97+
== fd_flag_diagnostics::comparable_status_flags(flags)
98+
&& after_close.descriptor_flags == descriptor_flags,
8199
fd_trace.message("pipe writer flags changed"));
100+
101+
// Mutate observations of the real pipe without involving an engine.
102+
for (const int bit : {O_NONBLOCK, O_APPEND, O_ACCMODE}) {
103+
auto changed = fd_trace.baseline();
104+
changed.flags ^= bit;
105+
require(fd_flag_diagnostics::comparable_status_flags(changed.flags)
106+
!= fd_flag_diagnostics::comparable_status_flags(flags)
107+
&& !fd_flag_diagnostics::same_flags(fd_trace.baseline(), changed),
108+
fd_trace.message("comparison hid a caller-controlled status flag change"));
109+
}
110+
auto changed_descriptor = fd_trace.baseline();
111+
changed_descriptor.descriptor_flags ^= FD_CLOEXEC;
112+
require(!fd_flag_diagnostics::same_flags(fd_trace.baseline(), changed_descriptor),
113+
fd_trace.message("comparison hid an FD_CLOEXEC change"));
114+
auto failed_query = fd_trace.baseline();
115+
failed_query.flags = -1;
116+
require(fd_flag_diagnostics::comparable_status_flags(failed_query.flags) == failed_query.flags
117+
&& !fd_flag_diagnostics::same_flags(fd_trace.baseline(), failed_query)
118+
&& !fd_flag_diagnostics::same_flags(failed_query, fd_trace.baseline())
119+
&& !fd_flag_diagnostics::same_flags(failed_query, failed_query),
120+
fd_trace.message("failed F_GETFL query compared equal"));
82121
}
83122
} // namespace
84123

‎tests/test_report_nonfinite_equity_cabi_fd.cpp‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,9 @@ void run_case(int field, double value, bool refuses) {
179179
"finite C fd stream did not complete");
180180
}
181181
const auto final_flags = fd_trace.record("final assertion");
182-
require(flags >= 0 && fd_flags >= 0 && final_flags.flags == flags
182+
require(flags >= 0 && fd_flags >= 0
183+
&& fd_flag_diagnostics::comparable_status_flags(final_flags.flags)
184+
== fd_flag_diagnostics::comparable_status_flags(flags)
183185
&& final_flags.descriptor_flags == fd_flags, fd_trace.message("borrowed fd changed"));
184186
}
185187
} // namespace

‎tests/test_report_nonfinite_equity_cpp_fd.cpp‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,9 @@ void fd_run(int field, double value, bool refuses) {
160160
"finite C++ fd stream did not complete");
161161
}
162162
const auto final_flags = fd_trace.record("final assertion");
163-
require(flags >= 0 && fd_flags >= 0 && final_flags.flags == flags
163+
require(flags >= 0 && fd_flags >= 0
164+
&& fd_flag_diagnostics::comparable_status_flags(final_flags.flags)
165+
== fd_flag_diagnostics::comparable_status_flags(flags)
164166
&& final_flags.descriptor_flags == fd_flags, fd_trace.message("borrowed fd changed"));
165167
}
166168
} // namespace

‎tests/test_report_sink_fd.cpp‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -451,7 +451,9 @@ void completed_fd_removal(const fs::path& directory, const std::vector<Bar>& bar
451451
fd_trace.record("after second removal checks/fill_report");
452452
unchanged_storage(host, storage, scratch);
453453
const auto final_flags = fd_trace.record("final NULL removal assertion");
454-
require(final_flags.flags == flags && final_flags.descriptor_flags == descriptor_flags,
454+
require(fd_flag_diagnostics::comparable_status_flags(final_flags.flags)
455+
== fd_flag_diagnostics::comparable_status_flags(flags)
456+
&& final_flags.descriptor_flags == descriptor_flags,
455457
fd_trace.message("NULL removal closed the borrowed fd or changed its flags"));
456458

457459
// A retained receipt must not keep the sink installed for the next
@@ -516,7 +518,8 @@ std::string fd_run(const fs::path& directory, const std::vector<Bar>& bars,
516518
require(strategy_set_report_sink_fd_v1(host.handle(), &invalid) == -1,
517519
"bad replacement version accepted");
518520
require(receipt(host).complete == 1, "bad replacement erased completed receipt");
519-
require(::fcntl(output->fd(), F_GETFL) == flags
521+
require(fd_flag_diagnostics::comparable_status_flags(::fcntl(output->fd(), F_GETFL))
522+
== fd_flag_diagnostics::comparable_status_flags(flags)
520523
&& ::fcntl(output->fd(), F_GETFD) == descriptor_flags,
521524
"writer changed caller fd flags");
522525
require(strategy_set_report_sink_v1(host.handle(), nullptr) == 0,

0 commit comments

Comments
 (0)