PR#14: the mask read once had an unbraced `else` that bound to the INNER `if (CPU_ISSET(...))`, so every CPU id outside the mask re-appended all hardware_concurrency() ids: a k-of-CPU_SETSIZE mask returned k + (CPU_SETSIZE - k) * hardware_concurrency() - 1 entries (issue #13: 16143 CPUs on a 16-thread box). Guard: pin this process to the first few allowed CPUs and require the list read to equal th
| 264 | // exactly - the blow-up shape named, computed and rejected - and everything derived from it (physical_cores, |
| 265 | // detect_cpu_topology) to stay inside the mask. No root, no cgroups, no quota: pure affinity. |
| 266 | static bool dangling_else_guard(const std::vector<int>& allowed) { |
| 267 | if (allowed.size() < 2) { |
| 268 | std::puts("SKIP dangling-else guard: fewer than two allowed CPUs"); |
| 269 | return true; |
| 270 | } |
| 271 | const size_t k = (std::min)(size_t(4), allowed.size()); |
| 272 | const std::vector<int> restricted(allowed.begin(), allowed.begin() + k); |
| 273 | if (!set_affinity(restricted)) return false; |
| 274 | bool ok = true; |
| 275 | std::vector<unsigned long> mask; |
| 276 | std::vector<int> cpus; |
| 277 | if (cpu::detail::get_thread_affinity(mask, &cpus) != 0) { |
| 278 | std::fprintf(stderr, "dangling-else guard: get_thread_affinity failed on the restricted mask\n"); |
| 279 | ok = false; |
| 280 | } |
| 281 | if (ok) { |
| 282 | const size_t bits = mask_popcount(mask); |
| 283 | // the exact list AND the two size identities; a re-introduced dangling else fails all three at once |
| 284 | ok = cpus == restricted && cpus.size() == bits && bits == k; |
| 285 | // the old bug's shape, stated as the number it produced and refused as such (CPU_SETSIZE == 1024 on glibc) |
| 286 | const long long blow_up = (long long) k + (long long) (CPU_SETSIZE - k) * |
| 287 | (long long) std::thread::hardware_concurrency() - 1; |
| 288 | if (!ok) |
| 289 | std::fprintf(stderr, |
| 290 | "dangling-else guard: mask read gave %zu CPUs, mask holds %lu bits, set wanted %zu (the dangling else gave %lld)\n", |
| 291 | cpus.size(), (unsigned long) bits, k, blow_up); |
| 292 | ok = ok && (long long) cpus.size() != blow_up; |
| 293 | } |
| 294 | if (ok) ok = topology_respects_current_mask(); |
| 295 | if (ok) { |
| 296 | const auto topo = cpu::detect_cpu_topology(false); |
| 297 | const auto cores = cpu::physical_cores(false); |
| 298 | // logical processors seen == the mask's size; the worker-core list is a non-empty subset of it, never the |
| 299 | // (CPU_SETSIZE - k) * h blow-up (physical_cores == topo.worker_cores, both checked) |
| 300 | ok = topo.logical == (int) k && !cores.empty() && cores.size() <= k && topo.worker_cores.size() <= k; |
| 301 | if (!ok) |
| 302 | std::fprintf(stderr, "dangling-else guard: topology %d logical, %zu worker cores for a %zu-CPU mask\n", |
| 303 | topo.logical, topo.worker_cores.size(), k); |
| 304 | } |
| 305 | const bool restored = set_affinity(allowed) && expect_affinity(allowed, "restore the full mask after the dangling-else guard"); |
| 306 | if (ok && restored) std::printf("Dangling-else guard: %zu-CPU self-affinity mask read exactly (%zu CPUs), topology stayed inside it\n", k, k); |
| 307 | return ok && restored; |
| 308 | } |
| 309 | |
| 310 | int main(int argc, char** argv) { |
| 311 | bool require_high = false; |
no test coverage detected