Skip to content

Commit b7e16f5

Browse files
luisleo526ci-cx
andauthored
test: compare borrowed fd flags without Darwin's written bit (Part of CR-352) (#519)
* tests: say which fd flag changed and where in the three macOS-red borrowed-fd tests, plus an engine-free pipe control (CR-150 follow-up, diagnostics only) Engine main's macOS-26 Debug leg has been red since the CR-150 merge (test_report_nonfinite_equity_cabi_fd, test_report_nonfinite_equity_cpp_fd, test_report_sink_fd): each says only that a borrowed pipe fd "changed". A source read found no engine call that writes F_GETFL/F_GETFD on a borrowed fd, and the assertions cannot tell which query differs, by which bit, or at which step. This change only adds information; no comparison is weakened or removed: - tests/fd_flag_diagnostics.hpp: separate F_GETFL and F_GETFD queries (each with errno captured at once), fstat identity, boundary snapshots, and a failure message naming the baseline, final value, XOR and the first boundary that differs. - The three tests keep every original condition (baseline validity, raw equality of both queries, the destruction check) and now take the final values from the same real fcntl calls; intermediate snapshots are observations only. - tests/test_pipe_writer_fd_flags.cpp (new, UNIX): an engine-free plain and F_DUPFD_CLOEXEC writer pipe, reader non-blocking as in the nonfinite tests, writer flags compared strictly across one non-empty write, a drain and the reader's close. On macOS it shows whether Darwin pipe semantics or the engine change the flag. No src/ or include/ change. Not run on macOS or locally; the branch CI macOS-26 Debug leg is the evidence. * 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). --------- Co-authored-by: ci-cx <ci-cx@invalid>
1 parent 2561cfb commit b7e16f5

6 files changed

Lines changed: 354 additions & 21 deletions

‎tests/CMakeLists.txt‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3696,6 +3696,11 @@ list(REMOVE_ITEM TEST_SOURCES ${_pf_private_fixture_rows})
36963696
list(APPEND TEST_SOURCES test_metrics_overflow)
36973697
list(APPEND TEST_SOURCES test_native_c_host_report_failure)
36983698

3699+
# CR-150: engine-free pipe flag control beside the ordinary C++ unit rows.
3700+
if(UNIX)
3701+
list(APPEND TEST_SOURCES test_pipe_writer_fd_flags)
3702+
endif()
3703+
36993704
foreach(test_name ${TEST_SOURCES})
37003705
pineforge_add_unit_test(${test_name})
37013706
endforeach()

‎tests/fd_flag_diagnostics.hpp‎

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
#pragma once
2+
3+
#include <cerrno>
4+
#include <cstdint>
5+
#include <sstream>
6+
#include <string>
7+
#include <vector>
8+
9+
#include <fcntl.h>
10+
#include <sys/stat.h>
11+
12+
namespace fd_flag_diagnostics {
13+
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+
34+
struct Snapshot {
35+
const char* boundary;
36+
int flags;
37+
int flags_errno;
38+
int descriptor_flags;
39+
int descriptor_flags_errno;
40+
struct stat identity{};
41+
int stat_result;
42+
int stat_errno;
43+
};
44+
45+
inline Snapshot snapshot(int descriptor, const char* boundary) {
46+
const int saved_errno = errno;
47+
Snapshot result{};
48+
result.boundary = boundary;
49+
result.flags = ::fcntl(descriptor, F_GETFL);
50+
result.flags_errno = result.flags == -1 ? errno : 0;
51+
// Always query F_GETFD, including when F_GETFL failed or changed.
52+
result.descriptor_flags = ::fcntl(descriptor, F_GETFD);
53+
result.descriptor_flags_errno = result.descriptor_flags == -1 ? errno : 0;
54+
result.stat_result = ::fstat(descriptor, &result.identity);
55+
result.stat_errno = result.stat_result == -1 ? errno : 0;
56+
errno = saved_errno;
57+
return result;
58+
}
59+
60+
inline bool same_flags(const Snapshot& before, const Snapshot& after) {
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;
64+
}
65+
66+
inline void append_identity(std::ostream& output, const Snapshot& value) {
67+
output << "{result=" << value.stat_result << ",errno=" << value.stat_errno;
68+
if (value.stat_result == 0) {
69+
output << ",st_mode=0x" << std::hex << static_cast<std::uintmax_t>(value.identity.st_mode)
70+
<< ",st_dev=0x" << static_cast<std::uintmax_t>(value.identity.st_dev)
71+
<< ",st_ino=0x" << static_cast<std::uintmax_t>(value.identity.st_ino) << std::dec;
72+
} else {
73+
output << ",st_mode=unavailable,st_dev=unavailable,st_ino=unavailable";
74+
}
75+
output << '}';
76+
}
77+
78+
inline void append_query(std::ostream& output, const char* name, int before, int after,
79+
int before_errno, int after_errno) {
80+
output << name << "{baseline=0x" << std::hex << static_cast<unsigned int>(before)
81+
<< ",final=0x" << static_cast<unsigned int>(after)
82+
<< ",xor=0x" << (static_cast<unsigned int>(before) ^ static_cast<unsigned int>(after))
83+
<< std::dec << ",baseline_errno=" << before_errno << ",final_errno=" << after_errno << '}';
84+
}
85+
86+
class Trace {
87+
public:
88+
Trace(int descriptor, const char* baseline_boundary) : descriptor_(descriptor) {
89+
record(baseline_boundary);
90+
}
91+
92+
const Snapshot& baseline() const { return snapshots_.front(); }
93+
94+
Snapshot record(const char* boundary) {
95+
const int saved_errno = errno;
96+
const auto current = snapshot(descriptor_, boundary);
97+
snapshots_.push_back(current);
98+
errno = saved_errno;
99+
return current;
100+
}
101+
102+
// Observations never assert: existing callers retain their final checks.
103+
std::string message(const char* reason) const {
104+
const auto& before = snapshots_.front();
105+
const auto& after = snapshots_.back();
106+
const char* first_difference = "none";
107+
for (const auto& current : snapshots_) {
108+
if (!same_flags(before, current)) {
109+
first_difference = current.boundary;
110+
break;
111+
}
112+
}
113+
std::ostringstream output;
114+
output << reason << ": fd=" << descriptor_
115+
<< " baseline_boundary=\"" << before.boundary
116+
<< "\" final_boundary=\"" << after.boundary
117+
<< "\" first_difference=\"" << first_difference << "\" ";
118+
append_query(output, "F_GETFL", before.flags, after.flags, before.flags_errno, after.flags_errno);
119+
output << ' ';
120+
append_query(output, "F_GETFD", before.descriptor_flags, after.descriptor_flags,
121+
before.descriptor_flags_errno, after.descriptor_flags_errno);
122+
output << " fstat_before";
123+
append_identity(output, before);
124+
output << " fstat_after";
125+
append_identity(output, after);
126+
output << " snapshots=[";
127+
bool first = true;
128+
for (const auto& current : snapshots_) {
129+
if (!first) output << ',';
130+
first = false;
131+
output << "{boundary=\"" << current.boundary << "\",F_GETFL=0x"
132+
<< std::hex << static_cast<unsigned int>(current.flags) << std::dec
133+
<< ",F_GETFL_errno=" << current.flags_errno << ",F_GETFD=0x"
134+
<< std::hex << static_cast<unsigned int>(current.descriptor_flags) << std::dec
135+
<< ",F_GETFD_errno=" << current.descriptor_flags_errno << ",fstat";
136+
append_identity(output, current);
137+
output << '}';
138+
}
139+
output << ']';
140+
return output.str();
141+
}
142+
143+
private:
144+
int descriptor_;
145+
std::vector<Snapshot> snapshots_;
146+
};
147+
148+
} // namespace fd_flag_diagnostics
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
// CR-150: isolate pipe flag observations from every engine operation.
2+
#include "fd_flag_diagnostics.hpp"
3+
4+
#include <array>
5+
#include <cerrno>
6+
#include <cstdio>
7+
#include <stdexcept>
8+
#include <string>
9+
10+
#include <fcntl.h>
11+
#include <unistd.h>
12+
13+
namespace {
14+
void require(bool condition, const std::string& message) {
15+
if (!condition) throw std::runtime_error(message);
16+
}
17+
18+
struct Pipe {
19+
int descriptors[2]{-1, -1};
20+
Pipe() {
21+
require(::pipe(descriptors) == 0, "pipe failed");
22+
// Exactly the reader setup used by both nonfinite-equity fd tests.
23+
const int flags = ::fcntl(descriptors[0], F_GETFL);
24+
require(flags >= 0 && ::fcntl(descriptors[0], F_SETFL, flags | O_NONBLOCK) == 0,
25+
"nonblocking reader setup failed");
26+
}
27+
~Pipe() {
28+
for (const int fd : descriptors) if (fd >= 0) ::close(fd);
29+
}
30+
std::string drain() const {
31+
std::string bytes;
32+
std::array<char, 4096> buffer{};
33+
for (;;) {
34+
const auto count = ::read(descriptors[0], buffer.data(), buffer.size());
35+
if (count < 0 && errno == EINTR) continue;
36+
if (count < 0 && (errno == EAGAIN || errno == EWOULDBLOCK)) return bytes;
37+
require(count >= 0, "pipe read failed");
38+
if (count == 0) return bytes;
39+
bytes.append(buffer.data(), static_cast<std::size_t>(count));
40+
}
41+
}
42+
void duplicate_writer() {
43+
// Match L22A's inherited descriptor allocation and original close.
44+
const int duplicate = ::fcntl(descriptors[1], F_DUPFD_CLOEXEC, 17);
45+
require(duplicate >= 17, "cannot allocate a non-default inherited fd");
46+
const int original = descriptors[1];
47+
descriptors[1] = duplicate;
48+
require(::close(original) == 0, "original writer close failed");
49+
}
50+
void close_reader() {
51+
const int reader = descriptors[0];
52+
descriptors[0] = -1;
53+
require(::close(reader) == 0, "pipe reader close failed");
54+
}
55+
};
56+
57+
void run_case(bool duplicate_writer) {
58+
Pipe pipe;
59+
if (duplicate_writer) pipe.duplicate_writer();
60+
fd_flag_diagnostics::Trace fd_trace(pipe.descriptors[1], "before nonempty write");
61+
const int flags = fd_trace.baseline().flags;
62+
const int descriptor_flags = fd_trace.baseline().descriptor_flags;
63+
require(flags >= 0 && descriptor_flags >= 0, fd_trace.message("cannot read pipe writer flags"));
64+
65+
constexpr char payload[] = "borrowed-pipe-control";
66+
ssize_t written;
67+
do {
68+
written = ::write(pipe.descriptors[1], payload, sizeof payload - 1);
69+
} while (written < 0 && errno == EINTR);
70+
require(written == static_cast<ssize_t>(sizeof payload - 1), "nonempty pipe write failed");
71+
const auto after_write = fd_trace.record("after nonempty write");
72+
const auto bytes = pipe.drain();
73+
const auto after_drain = fd_trace.record("after draining reader");
74+
require(bytes == payload, "pipe control drain changed payload");
75+
pipe.close_reader();
76+
const auto after_close = fd_trace.record("after closing reader");
77+
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,
99+
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"));
121+
}
122+
} // namespace
123+
124+
int main() {
125+
const char* active_case = "plain writer";
126+
try {
127+
run_case(false);
128+
active_case = "F_DUPFD_CLOEXEC writer";
129+
run_case(true);
130+
return 0;
131+
} catch (const std::exception& error) {
132+
std::fprintf(stderr, "pipe writer fd control (%s): %s\n", active_case, error.what());
133+
return 1;
134+
}
135+
}

‎tests/test_report_nonfinite_equity_cabi_fd.cpp‎

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
#include <pineforge/pineforge.h>
55
#include <pineforge/report_sink.h>
66

7+
#include "fd_flag_diagnostics.hpp"
8+
79
#include <array>
810
#include <cerrno>
911
#include <cmath>
@@ -20,7 +22,7 @@ namespace {
2022
constexpr std::int64_t start = INT64_C(1704067200000);
2123
constexpr int bar_count = 4;
2224

23-
void require(bool condition, const char* message) {
25+
void require(bool condition, const std::string& message) {
2426
if (!condition) throw std::runtime_error(message);
2527
}
2628

@@ -124,8 +126,9 @@ void run_case(int field, double value, bool refuses) {
124126
"C host configuration failed");
125127
const pf_report_sink_fd_v1 sink{sizeof sink, 1, pipe.descriptors[1], "CR-289"};
126128
require(strategy_set_report_sink_fd_v1(host.handle, &sink) == 0, "C fd setter refused");
127-
const int flags = ::fcntl(pipe.descriptors[1], F_GETFL);
128-
const int fd_flags = ::fcntl(pipe.descriptors[1], F_GETFD);
129+
fd_flag_diagnostics::Trace fd_trace(pipe.descriptors[1], "after sink installation");
130+
const int flags = fd_trace.baseline().flags;
131+
const int fd_flags = fd_trace.baseline().descriptor_flags;
129132
std::array<pf_bar_t, bar_count> bars{};
130133
for (int i = 0; i < bar_count; ++i)
131134
bars[static_cast<std::size_t>(i)] =
@@ -135,6 +138,8 @@ void run_case(int field, double value, bool refuses) {
135138
~Report() { strategy_native_report_free_v1(&value); }
136139
} report;
137140
const int status = strategy_native_run_v1(host.handle, bars.data(), bar_count, &report.value);
141+
// This C API call combines the run and report capture.
142+
fd_trace.record("after strategy_native_run_v1 (run/capture)");
138143
require(host.injected, "native held point was never injected");
139144
const double actual = field == 0 ? host.injected_point.equity : host.injected_point.open_profit;
140145
require(std::isnan(value) ? std::isnan(actual) : actual == value, "native poison changed");
@@ -144,7 +149,9 @@ void run_case(int field, double value, bool refuses) {
144149
receipt.struct_size = sizeof receipt;
145150
receipt.version = 1;
146151
require(strategy_report_sink_receipt_v1(host.handle, &receipt) == 0, "receipt query failed");
152+
fd_trace.record("before draining reader");
147153
const auto bytes = pipe.drain();
154+
fd_trace.record("after draining reader");
148155
if (refuses) {
149156
// A failed run's partial report can itself refuse the poisoned scratch
150157
// point. Both public failure statuses must retain the typed cause.
@@ -171,8 +178,11 @@ void run_case(int field, double value, bool refuses) {
171178
&& receipt.count_e == bar_count && bytes.find("{\"trailer\":") != std::string::npos,
172179
"finite C fd stream did not complete");
173180
}
174-
require(flags >= 0 && fd_flags >= 0 && ::fcntl(pipe.descriptors[1], F_GETFL) == flags
175-
&& ::fcntl(pipe.descriptors[1], F_GETFD) == fd_flags, "borrowed fd changed");
181+
const auto final_flags = fd_trace.record("final assertion");
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)
185+
&& final_flags.descriptor_flags == fd_flags, fd_trace.message("borrowed fd changed"));
176186
}
177187
} // namespace
178188

0 commit comments

Comments
 (0)