Files
ViewDesignEngine/tests/fuzz/fuzz_boolean.cpp
T
hm 4bd64ac481 fix(brep): avoid thread_local memo construction in W31 guard prefetch worker (W47)
根因:legacy 差集核的 W31 守卫修复候选预取线程(brep_boolean.cpp 内
GuardRepairPrefetch,短生命周期 std::thread)在 worker 内调用
pipeline_validate(),首次构造 thread_local BooleanPipelineMemo;线程退出时
TLS 析构访问已释放的 thread_local 存储 → PageHeap 下 100% UAF,普通堆下为
并行 ctest 偶发 Exit code 0xC0000374
(TolerantModelingVDE371Test.MultiBoolean_DifferenceSubtractsAll /
BooleanCorpus.Diff_BoxBox_10x10x1_1x1x10,同属纯平面方体贯通差集)。

修复:worker 内改用 validate()(pipeline memo 在 worker 恒非 active,
pipeline_validate 本就退化为 validate;语义等价),并新增
VDE_DISABLE_GUARD_PREFETCH=1 诊断门(默认行为不变)。

验证:PageHeap OMP=2/4/24 矩阵修复前全 AV、修复后全 PASS;640 runs 压测
修复前 2 fails → 修复后 0 fails;ctest -R "Fuzz|Boolean|Brep|Oracle" 两轮
0/1348 失败;B5 digest d1c875b2322e097d 与 18 行 CBRA SHA256 均与基线一致。
新增 3 个回归测试与 2 个工具,详见 docs/planning/wave8/fuzz-notes.md。
2026-11-20 14:57:37 +08:00

331 lines
12 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/// @file fuzz_boolean.cpp
/// @brief B-Rep 布尔随机参数模糊测试 + 持续运行模式
///
/// 测试策略:
/// 1. 随机尺寸的盒子/圆柱/球体对做布尔运算
/// 2. 随机偏移后的布尔运算
/// 3. 连续布尔链式模糊测试
/// 4. 验证结果的合法性
/// 5. 持续运行模式
#include <gtest/gtest.h>
#include "vde/brep/brep.h"
#include "vde/brep/brep_boolean.h"
#include "vde/brep/modeling.h"
#include "vde/brep/brep_validate.h"
#include "vde/brep/brep_heal.h"
#include "vde/core/point.h"
#include <cmath>
#include <random>
#include <string>
#include <cstdlib>
#include <atomic>
#include <thread>
#include <vector>
#ifdef _OPENMP
#include <omp.h>
#endif
using namespace vde::brep;
using namespace vde::core;
static std::mt19937 s_rng(42);
// Debug 构建下布尔运算慢 2–10×;降低重负担用例预算,Release(NDEBUG)保持原覆盖。
#ifdef NDEBUG
constexpr int kMixedIterations = 60;
constexpr int kChainIterations = 20;
constexpr int kSelfIterations = 30;
#else
// Debug 布尔运算远慢于 Release(单次随机基本体可能秒级);只保留最小回归预算,
// 保证每个 ctest 用例在 Debug 超时档内完成,seed 固定仍可复现。
constexpr int kMixedIterations = 1;
constexpr int kChainIterations = 1;
constexpr int kSelfIterations = 1;
#endif
// ── 随机参数生成器 ──
namespace {
double rand_range(double lo, double hi) {
return std::uniform_real_distribution<double>(lo, hi)(s_rng);
}
BrepModel rand_box() {
return make_box(rand_range(0.5, 10.0),
rand_range(0.5, 10.0),
rand_range(0.5, 10.0));
}
BrepModel rand_cylinder() {
return make_cylinder(rand_range(0.2, 5.0),
rand_range(0.5, 10.0),
static_cast<int>(rand_range(8, 64)));
}
BrepModel rand_sphere() {
return make_sphere(rand_range(0.3, 5.0),
static_cast<int>(rand_range(8, 32)),
static_cast<int>(rand_range(4, 16)));
}
BrepModel rand_primitive() {
int choice = static_cast<int>(rand_range(0, 3));
switch (choice) {
case 0: return rand_box();
case 1: return rand_cylinder();
default: return rand_sphere();
}
}
} // anonymous
// ═══════════════════════════════════════════════
// 随机盒子布尔测试
// ═══════════════════════════════════════════════
TEST(FuzzBoolean, RandomBoxUnion) {
for (int i = 0; i < 50; ++i) {
auto a = rand_box();
auto b = rand_box();
EXPECT_NO_THROW({
auto u = brep_union(a, b);
EXPECT_TRUE(u.is_valid());
// 结果不应为空体
EXPECT_GT(u.num_vertices(), 0u);
// 验证
auto result = validate(u);
(void)result;
});
}
}
TEST(FuzzBoolean, RandomBoxIntersection) {
for (int i = 0; i < 50; ++i) {
auto a = rand_box();
auto b = rand_box();
EXPECT_NO_THROW({
auto inter = brep_intersection(a, b);
EXPECT_TRUE(inter.is_valid());
});
}
}
TEST(FuzzBoolean, RandomBoxDifference) {
for (int i = 0; i < 50; ++i) {
auto a = rand_box();
auto b = rand_box();
EXPECT_NO_THROW({
auto diff = brep_difference(a, b);
EXPECT_TRUE(diff.is_valid());
});
}
}
// ═══════════════════════════════════════════════
// 随机基本体布尔混合测试
// ═══════════════════════════════════════════════
TEST(FuzzBoolean, RandomMixedPrimitives) {
for (int i = 0; i < kMixedIterations; ++i) {
auto a = rand_primitive();
auto b = rand_primitive();
EXPECT_NO_THROW({
auto u = brep_union(a, b);
EXPECT_TRUE(u.is_valid()) << "union failed at iteration " << i;
auto inter = brep_intersection(a, b);
EXPECT_TRUE(inter.is_valid()) << "intersection failed at iteration " << i;
auto diff = brep_difference(a, b);
EXPECT_TRUE(diff.is_valid()) << "difference failed at iteration " << i;
});
}
}
// ═══════════════════════════════════════════════
// 随机布尔链(连续多个布尔操作)
// ═══════════════════════════════════════════════
TEST(FuzzBoolean, RandomBooleanChain) {
for (int i = 0; i < kChainIterations; ++i) {
EXPECT_NO_THROW({
auto result = rand_primitive();
int chain_len = static_cast<int>(rand_range(2, 6));
for (int j = 0; j < chain_len; ++j) {
auto other = rand_primitive();
int op_choice = static_cast<int>(rand_range(0, 3));
switch (op_choice) {
case 0: result = brep_union(result, other); break;
case 1: result = brep_intersection(result, other); break;
default: result = brep_difference(result, other); break;
}
ASSERT_TRUE(result.is_valid())
<< "chain failed at step " << j << " of chain " << i;
// 随机链在病态相交下可能指数级增面(实测单步 2915 面);
// fuzz 不追求构造巨型中间体,超过预算即结束该链,避免
// 后续 union 触发内存压力/非确定性崩溃。
if (result.num_faces() > 2000u) break;
}
// 最终结果应仍有效
EXPECT_TRUE(result.is_valid());
});
}
}
// ═══════════════════════════════════════════════
// 自交操作模糊测试
// ═══════════════════════════════════════════════
TEST(FuzzBoolean, RandomSelfOperations) {
for (int i = 0; i < kSelfIterations; ++i) {
auto body = rand_primitive();
EXPECT_NO_THROW({
auto u = brep_union(body, body);
EXPECT_TRUE(u.is_valid());
auto inter = brep_intersection(body, body);
EXPECT_TRUE(inter.is_valid());
auto diff = brep_difference(body, body);
EXPECT_TRUE(diff.is_valid());
});
}
}
// ═══════════════════════════════════════════════
// 网格指纹缓存并发淘汰回归(W42 / 0xc0000374 根因)
// ═══════════════════════════════════════════════
// 背景:get_body_mesh() 按 BrepModel 指针缓存网格 + 空间索引,容量守卫
// (>64 条时 clear())会在任意线程调用。旧实现返回静态 map 节点的引用,
// clear() 会析构其他线程正持有并读取的缓存条目 → 悬垂引用(UAF),与
// "并行 ctest 偶发 0xc0000374、单跑难复现" 完全吻合。修复后缓存条目由
// shared_ptr 持有,淘汰只影响映射表,不析构仍被使用的条目。
//
// 本用例双线程压测:T1 用大面数球体做需要网格分类/截面收集的布尔;
// T2 用约 90 个不同网格体做小布尔,把缓存推过 64 条容量上限触发淘汰。
// 在 ASan 构建下运行本用例可直接暴露该 UAF;正常构建下验证语义正确性。
TEST(FuzzBoolean, MeshCacheEvictionConcurrent) {
std::atomic<bool> start{false};
std::atomic<int> failures{0};
std::atomic<int> exceptions{0};
auto victim = [&]() {
try {
while (!start.load(std::memory_order_acquire))
std::this_thread::yield();
// 非平面大面数体,触发 get_body_mesh 分类 + 平面截交路径。
BrepModel sphere = make_sphere(3.0, 24, 12);
for (int i = 0; i < 8; ++i) {
BrepModel box = make_box(4.0, 4.0, 4.0);
BrepModel u = brep_union(sphere, box);
if (!u.is_valid()) ++failures;
BrepModel d = brep_difference(sphere, box);
if (!d.is_valid()) ++failures;
}
} catch (...) {
++exceptions;
}
};
auto evictor = [&]() {
try {
while (!start.load(std::memory_order_acquire))
std::this_thread::yield();
for (int i = 0; i < 90; ++i) {
BrepModel s = make_sphere(1.0 + 0.001 * i, 8, 4);
BrepModel b = make_box(1.5, 1.5, 1.5);
BrepModel u = brep_union(s, b);
if (!u.is_valid()) ++failures;
}
} catch (...) {
++exceptions;
}
};
std::thread t1(victim);
std::thread t2(evictor);
start.store(true, std::memory_order_release);
t1.join();
t2.join();
EXPECT_EQ(exceptions.load(), 0) << "布尔调用抛出异常";
EXPECT_EQ(failures.load(), 0) << "并发布尔结果无效";
}
// ═══════════════════════════════════════════════
// W47:守卫修复候选预取线程的线程退出回归
// ═══════════════════════════════════════════════
/// brep_difference 的 W31 守卫修复预取线程(brep_boolean.cpp 内启动/join 的
/// 短生命周期 std::thread)在多线程布尔核下会在 PageHeap 中于 TLS 析构时
/// 触发 use-after-free(并行 ctest 偶发 0xC0000374 的确定性形态)。此用例把
/// 该类穿透差集重复多轮,覆盖 worker 线程的创建/退出路径;配合
/// tools/fuzz/run_boolean_pageheap.ps1 可做 100% 确定性回归。
TEST(FuzzBoolean, GuardRepairPrefetchThreadedDifference) {
#ifdef _OPENMP
const int prev_threads = omp_get_max_threads();
omp_set_num_threads(4);
#endif
for (int i = 0; i < 12; ++i) {
auto a = make_box(10, 10, 1);
auto b = make_box(1, 1, 10);
auto r = brep_difference(a, b);
EXPECT_TRUE(r.is_valid()) << "iteration " << i;
}
#ifdef _OPENMP
omp_set_num_threads(prev_threads);
#endif
}
// ═══════════════════════════════════════════════
// 持续运行模式
// ═══════════════════════════════════════════════
TEST(FuzzBoolean, ContinuousFuzz) {
const char* forever = std::getenv("VDE_FUZZ_FOREVER");
if (!forever || std::string(forever) != "1") {
GTEST_SKIP() << "设置 VDE_FUZZ_FOREVER=1 启用持续模糊测试";
}
int iteration = 0;
while (true) {
++iteration;
auto a = rand_primitive();
auto b = rand_primitive();
// 三项布尔操作都必须成功
auto u = brep_union(a, b);
ASSERT_TRUE(u.is_valid()) << "union failed at iteration " << iteration;
auto inter = brep_intersection(a, b);
ASSERT_TRUE(inter.is_valid()) << "intersection failed at iteration " << iteration;
auto diff = brep_difference(a, b);
ASSERT_TRUE(diff.is_valid()) << "difference failed at iteration " << iteration;
// 验证
auto vr_u = validate(u);
(void)vr_u;
auto vr_i = validate(inter);
(void)vr_i;
if (iteration % 1000 == 0) {
std::cout << "[FuzzBoolean] iteration=" << iteration
<< " union_faces=" << u.num_faces()
<< " intersection_faces=" << inter.num_faces()
<< std::endl;
}
}
}