syd 3.57.0

rock-solid application kernel
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
//
// Syd: rock-solid application kernel
// src/kcov/mod.rs: KCOV userspace ABI shim for syzkaller
//
// Copyright (c) 2025, 2026 Ali Polatel <alip@chesswob.org>
// SPDX-License-Identifier: GPL-3.0

use std::{
    fmt,
    sync::{Arc, OnceLock, RwLock},
};

use nix::{errno::Errno, unistd::Pid};
use serde::{Serialize, Serializer};

use crate::hash::SydHashMap;

// KCOV ABI handlers
pub(crate) mod abi;

// KCOV API utilities
pub(crate) mod api;

// Thread-local sink describing where the live writer should send records.
#[derive(Clone, Copy, Debug)]
pub(crate) struct TlsSink {
    pub(crate) id: KcovId,
}

thread_local! {
    static TLS_SINK: RwLock<Option<TlsSink>> = const { RwLock::new(None) };
    static RECURSION_GUARD: RwLock<bool> = const { RwLock::new(false) };
}

pub(crate) fn get_tls_sink() -> Option<KcovId> {
    // Check recursion guard first.
    let guard = match RECURSION_GUARD.try_with(|g| *g.read().unwrap_or_else(|e| e.into_inner())) {
        Ok(g) => g,
        Err(_) => return None,
    };
    if guard {
        return None;
    }

    // Try TLS.
    if let Some(id) = TLS_SINK
        .try_with(|s| {
            s.read()
                .unwrap_or_else(|e| e.into_inner())
                .map(|sink| sink.id)
        })
        .ok()
        .flatten()
    {
        return Some(id);
    }

    None
}

pub(crate) fn set_tls_sink(id: KcovId) {
    let _ =
        TLS_SINK.try_with(|s| *s.write().unwrap_or_else(|e| e.into_inner()) = Some(TlsSink { id }));
}

pub(crate) fn clear_tls_sink() {
    let _ = TLS_SINK.try_with(|s| *s.write().unwrap_or_else(|e| e.into_inner()) = None);
}

#[derive(Clone, Copy, Debug, Default)]
struct KcovBind {
    local: Option<KcovId>,
    remote: Option<KcovId>,
}

// Global TID map: TID -> KcovBind
static KCOV_TID_MAP: OnceLock<RwLock<SydHashMap<Pid, KcovBind>>> = OnceLock::new();

fn kcov_tid_map() -> &'static RwLock<SydHashMap<Pid, KcovBind>> {
    KCOV_TID_MAP.get_or_init(|| RwLock::new(SydHashMap::default()))
}

pub(crate) fn set_kcov_tid(tid: Pid, id: KcovId, is_remote: bool) {
    let mut map = kcov_tid_map().write().unwrap_or_else(|e| e.into_inner());
    let mut bind = map.get(&tid).copied().unwrap_or_default();
    if is_remote {
        bind.remote = Some(id);
    } else {
        bind.local = Some(id);
    }
    map.insert(tid, bind);
}

pub(crate) fn get_kcov_tid(tid: Pid) -> Option<KcovId> {
    let map = kcov_tid_map().read().unwrap_or_else(|e| e.into_inner());
    map.get(&tid).and_then(|b| b.local.or(b.remote))
}

pub(crate) fn get_kcov_tid_local(tid: Pid) -> Option<KcovId> {
    let map = kcov_tid_map().read().unwrap_or_else(|e| e.into_inner());
    map.get(&tid).and_then(|b| b.local)
}

pub(crate) fn get_kcov_tid_remote(tid: Pid) -> Option<KcovId> {
    let map = kcov_tid_map().read().unwrap_or_else(|e| e.into_inner());
    map.get(&tid).and_then(|b| b.remote)
}

pub(crate) fn remove_kcov_tid(tid: Pid) {
    let (local, remote) = {
        let map = kcov_tid_map().read().unwrap_or_else(|e| e.into_inner());
        map.get(&tid).map_or((None, None), |b| (b.local, b.remote))
    };
    if let Some(id) = local {
        abi::kcov_mgr().exit_reset(id);
        abi::kcov_clear_mode(id);
    }
    if let Some(id) = remote {
        abi::kcov_mgr().exit_reset(id);
        abi::kcov_clear_mode(id);
        abi::kcov_handle_drop(id);
    }

    let (drop_local, drop_remote) = {
        let mut map = kcov_tid_map().write().unwrap_or_else(|e| e.into_inner());
        map.remove(&tid);
        let dl = local.filter(|id| {
            !map.values()
                .any(|b| b.local == Some(*id) || b.remote == Some(*id))
        });
        let dr = remote.filter(|id| {
            Some(*id) != local
                && !map
                    .values()
                    .any(|b| b.local == Some(*id) || b.remote == Some(*id))
        });
        (dl, dr)
    };
    if let Some(id) = drop_local {
        abi::kcov_mgr().close(id);
        abi::kcov_reg_remove(id);
    }
    if let Some(id) = drop_remote {
        abi::kcov_mgr().close(id);
        abi::kcov_reg_remove(id);
    }
}

pub(crate) fn unbind_kcov_tid(tid: Pid, id: KcovId) {
    let mut map = kcov_tid_map().write().unwrap_or_else(|e| e.into_inner());
    if let Some(bind) = map.get_mut(&tid) {
        if bind.local == Some(id) {
            bind.local = None;
        }
        if bind.remote == Some(id) {
            bind.remote = None;
        }
        if bind.local.is_none() && bind.remote.is_none() {
            map.remove(&tid);
        }
    }
}

pub(crate) fn inherit_kcov_tid(parent_tid: Pid, child_tid: Pid) {
    let remote = {
        let map = kcov_tid_map().read().unwrap_or_else(|e| e.into_inner());
        map.get(&parent_tid).and_then(|b| b.remote)
    };
    if let Some(id) = remote {
        set_kcov_tid(child_tid, id, true);
    }
}

//
// Public API
//

// KCOV modes (pc/cmp).
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
pub(crate) enum TraceMode {
    Pc,
    Cmp,
}

impl fmt::Display for TraceMode {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Pc => write!(f, "pc"),
            Self::Cmp => write!(f, "cmp"),
        }
    }
}

impl Serialize for TraceMode {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        serializer.serialize_str(&self.to_string())
    }
}

// /sys/kernel/debug/kcov handle.
#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
pub(crate) struct KcovId(u64);

impl KcovId {
    // Create a new KcovId.
    pub(crate) const fn new(id: u64) -> Self {
        Self(id)
    }
}

pub(crate) struct Kcov {
    map: RwLock<SydHashMap<KcovId, Arc<State>>>,
}

impl Kcov {
    pub(crate) fn new() -> Self {
        Self {
            map: RwLock::new(SydHashMap::default()),
        }
    }

    pub(crate) fn open(&self, kcov_id: u64) -> Result<(), Errno> {
        let kcov_id = KcovId(kcov_id);
        let state_arc = Arc::new(State::new());

        let mut map = self.map.write().unwrap_or_else(|e| e.into_inner());
        map.insert(kcov_id, state_arc);

        Ok(())
    }

    pub(crate) fn init_trace(
        &self,
        kcov_id: KcovId,
        words: u64,
        wordsize: u8,
    ) -> Result<(), Errno> {
        self.get(kcov_id)?.init_trace(words, wordsize)
    }

    pub(crate) fn reset(&self, id: KcovId) {
        if let Ok(st) = self.get(id) {
            let mut core = st.core.write().unwrap_or_else(|e| e.into_inner());
            core.phase = Phase::Disabled;
            core.mode = None;
        }
    }

    pub(crate) fn enable(&self, id: KcovId, mode: TraceMode) -> Result<(), Errno> {
        let st = self.get(id)?;
        st.enable(mode)?;

        set_tls_sink(id);

        Ok(())
    }

    pub(crate) fn disable(&self, id: KcovId) -> Result<(), Errno> {
        let st = self.get(id)?;
        st.disable()?;

        clear_tls_sink();

        Ok(())
    }

    pub(crate) fn exit_reset(&self, id: KcovId) {
        if let Ok(st) = self.get(id) {
            let mut core = st.core.write().unwrap_or_else(|e| e.into_inner());
            if core.phase == Phase::Enabled {
                core.phase = Phase::Init;
                core.mode = None;
            }
        }
    }

    pub(crate) fn close(&self, id: KcovId) {
        let mut map = self.map.write().unwrap_or_else(|e| e.into_inner());
        map.remove(&id);
    }

    fn get(&self, kcov_id: KcovId) -> Result<Arc<State>, Errno> {
        let read_guard = self.map.read().unwrap_or_else(|e| e.into_inner());
        read_guard.get(&kcov_id).cloned().ok_or(Errno::EBADF)
    }
}

//
// Internals
//

#[derive(Copy, Clone, Debug, Eq, PartialEq)]
enum Phase {
    Disabled,
    Init,
    Enabled,
}

impl fmt::Display for Phase {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let s = match self {
            Self::Disabled => "disabled",
            Self::Init => "init",
            Self::Enabled => "enabled",
        };
        f.write_str(s)
    }
}

impl Serialize for Phase {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        serializer.serialize_str(&self.to_string())
    }
}

struct State {
    core: RwLock<Core>,
}

struct Core {
    mode: Option<TraceMode>,
    phase: Phase,
}

impl State {
    fn new() -> Self {
        Self {
            core: RwLock::new(Core {
                mode: None,
                phase: Phase::Disabled,
            }),
        }
    }

    fn init_trace(&self, words: u64, wordsize: u8) -> Result<(), Errno> {
        if words < 2 || words > (i32::MAX as u64) / u64::from(wordsize) {
            return Err(Errno::EINVAL);
        }

        let mut core = self.core.write().unwrap_or_else(|e| e.into_inner());
        if core.phase != Phase::Disabled {
            return Err(Errno::EBUSY);
        }

        core.mode = None;
        core.phase = Phase::Init;

        Ok(())
    }

    fn enable(&self, mode: TraceMode) -> Result<(), Errno> {
        let mut core = self.core.write().unwrap_or_else(|e| e.into_inner());

        match core.phase {
            Phase::Init => {
                core.mode = Some(mode);
                core.phase = Phase::Enabled;
                Ok(())
            }

            Phase::Enabled | Phase::Disabled => Err(Errno::EINVAL),
        }
    }

    fn disable(&self) -> Result<(), Errno> {
        let mut core = self.core.write().unwrap_or_else(|e| e.into_inner());

        if core.phase != Phase::Enabled {
            return Err(Errno::EINVAL);
        }

        core.phase = Phase::Init;
        core.mode = None;
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_kcov_id_new_1() {
        let id = KcovId::new(42);
        assert_eq!(id, KcovId(42));
    }

    #[test]
    fn test_kcov_id_eq_1() {
        assert_eq!(KcovId::new(1), KcovId::new(1));
    }

    #[test]
    fn test_kcov_id_ne_1() {
        assert_ne!(KcovId::new(1), KcovId::new(2));
    }

    #[test]
    fn test_tls_sink_none_by_default_1() {
        clear_tls_sink();
        assert!(get_tls_sink().is_none());
    }

    #[test]
    fn test_tls_sink_set_get_1() {
        let id = KcovId::new(99);
        set_tls_sink(id);
        assert_eq!(get_tls_sink(), Some(id));
        clear_tls_sink();
    }

    #[test]
    fn test_tls_sink_clear_1() {
        let id = KcovId::new(77);
        set_tls_sink(id);
        clear_tls_sink();
        assert!(get_tls_sink().is_none());
    }
}