ondas 2.0.0

Read-only, format-independent waveform analysis library
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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
use std::ops::ControlFlow;

use super::{
    types::{Storage, Types},
    wire::Wire,
};
use crate::{Result, Time, TimeSpan};

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(super) enum Scalar {
    Ordinal(u8),
    Integer(i64),
    Real(u64),
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(super) enum Event {
    Scalar {
        id: u32,
        rank: usize,
        time: Time,
        offset: u64,
        value: Scalar,
    },
    ListEnd(Time),
    Tick(Time),
}

// Keep checked buffered decoding inside the caller's scalar loop.
#[inline(always)]
pub(super) fn scalar(wire: &mut Wire, types: &Types, id: usize) -> Result<Scalar> {
    let (storage, literals, _) = types
        .scalar(id)
        .ok_or_else(|| wire.malformed("non-scalar body type"))?;
    Ok(match storage {
        Storage::B2 | Storage::E8 => {
            let ordinal = wire.byte()?;
            if ordinal as usize >= literals.len() {
                return Err(wire.malformed("body enum ordinal exceeds base literals"));
            }
            Scalar::Ordinal(ordinal)
        }
        Storage::I32 | Storage::P32 => Scalar::Integer(wire.sleb(32)?),
        Storage::I64 | Storage::P64 => Scalar::Integer(wire.sleb(64)?),
        Storage::F64 => Scalar::Real(wire.real_bits()?),
    })
}

#[derive(Default)]
struct Clock {
    current: Option<i64>,
    first: Option<i64>,
}

impl Clock {
    fn span(&self) -> Option<TimeSpan> {
        Some(TimeSpan::new(
            Time::from_ticks(self.first? as u64),
            Time::from_ticks(self.current? as u64),
        ))
    }

    // Some(flow) ends traversal; None permits decoding the new time's values.
    fn enter<B>(
        &mut self,
        next: i64,
        end: Time,
        wire: &Wire,
        visitor: &mut impl FnMut(Event) -> ControlFlow<B>,
    ) -> Result<Option<ControlFlow<B>>> {
        if next < 0 || self.current.is_some_and(|previous| next < previous) {
            return Err(wire.malformed("negative or backward body time"));
        }
        if let Some(previous) = self.current
            && next > previous
            && let ControlFlow::Break(value) =
                visitor(Event::Tick(Time::from_ticks(previous as u64)))
        {
            return Ok(Some(ControlFlow::Break(value)));
        }
        if next as u64 > end.ticks() {
            return Ok(Some(ControlFlow::Continue(())));
        }
        self.first.get_or_insert(next);
        self.current = Some(next);
        Ok(None)
    }
}

fn descriptor(wire: &mut Wire) -> Result<()> {
    let expected = [if wire.big_endian { 2 } else { 1 }, 4, 1, 0];
    if wire.fixed::<4>()? != expected {
        return Err(wire.unsupported("footer size/order layout"));
    }
    Ok(())
}

/// One sequential state machine validates opening and replays selected queries.
/// It keeps no source values or timestamp table. Scalar consumers own assembly.
pub(super) fn walk<B>(
    wire: &mut Wire,
    types: &Types,
    active: &[(u32, usize)],
    end: Time,
    mut visitor: impl FnMut(Event) -> ControlFlow<B>,
) -> Result<(ControlFlow<B>, Option<TimeSpan>)> {
    let mut clock = Clock::default();
    let mut footer = false;
    loop {
        // Reading through fill_buf preserves actual I/O failures. A clean EOF is
        // permitted here only; all unfinished fields use Wire's checked reads.
        if wire.at_eof()? {
            break;
        }
        let tag = wire.fixed::<4>()?;
        match &tag {
            b"SNP\0" | b"CYC\0" if footer => {
                return Err(wire.malformed("body section after footer"));
            }
            b"SNP\0" => {
                wire.zeros()?;
                let time = wire.i64()?;
                if let Some(flow) = clock.enter(time, end, wire, &mut visitor)? {
                    return Ok((flow, clock.span()));
                }
                for (rank, &(id, ty)) in active.iter().enumerate() {
                    let offset = wire.offset;
                    let value = scalar(wire, types, ty)?;
                    if let ControlFlow::Break(value) = visitor(Event::Scalar {
                        id,
                        rank,
                        time: Time::from_ticks(time as u64),
                        offset,
                        value,
                    }) {
                        return Ok((ControlFlow::Break(value), clock.span()));
                    }
                }
                wire.marker(b"ESN\0")?;
                if let ControlFlow::Break(value) =
                    visitor(Event::ListEnd(Time::from_ticks(time as u64)))
                {
                    return Ok((ControlFlow::Break(value), clock.span()));
                }
            }
            b"CYC\0" => {
                let mut time = wire.i64()?;
                if let Some(flow) = clock.enter(time, end, wire, &mut visitor)? {
                    return Ok((flow, clock.span()));
                }
                loop {
                    let mut rank = 0usize;
                    loop {
                        let delta = wire.uleb()? as usize;
                        if delta == 0 {
                            break;
                        }
                        rank = rank
                            .checked_add(delta)
                            .ok_or_else(|| wire.malformed("cycle scalar rank overflow"))?;
                        let &(id, ty) = active.get(rank - 1).ok_or_else(|| {
                            wire.malformed("cycle scalar rank exceeds active table")
                        })?;
                        let offset = wire.offset;
                        let value = scalar(wire, types, ty)?;
                        if let ControlFlow::Break(value) = visitor(Event::Scalar {
                            id,
                            rank: rank - 1,
                            time: Time::from_ticks(time as u64),
                            offset,
                            value,
                        }) {
                            return Ok((ControlFlow::Break(value), clock.span()));
                        }
                    }
                    if let ControlFlow::Break(value) =
                        visitor(Event::ListEnd(Time::from_ticks(time as u64)))
                    {
                        return Ok((ControlFlow::Break(value), clock.span()));
                    }
                    let delta = wire.sleb(64)?;
                    if delta == -1 {
                        wire.marker(b"ECY\0")?;
                        break;
                    }
                    if delta < 0 {
                        return Err(wire.malformed("negative cycle time increment"));
                    }
                    time = time
                        .checked_add(delta)
                        .ok_or_else(|| wire.malformed("cycle time overflow"))?;
                    if let Some(flow) = clock.enter(time, end, wire, &mut visitor)? {
                        return Ok((flow, clock.span()));
                    }
                }
            }
            b"DIR\0" => {
                if footer {
                    return Err(wire.malformed("duplicate directory"));
                }
                // A footer ends the value stream; no subsequent same-time list
                // is permitted. Deliver the completed tick before footer work.
                if let Some(time) = clock.current
                    && let ControlFlow::Break(value) =
                        visitor(Event::Tick(Time::from_ticks(time as u64)))
                {
                    return Ok((ControlFlow::Break(value), clock.span()));
                }
                footer = true;
                descriptor(wire)?;
                let count = wire.count("directory entry")?;
                if count as u64 > wire.len.saturating_sub(wire.offset) / 8 {
                    return Err(wire.malformed("directory count exceeds remaining input"));
                }
                for _ in 0..count {
                    let name = wire.fixed::<4>()?;
                    if name[3] != 0 || !name[..3].iter().all(u8::is_ascii_uppercase) {
                        return Err(wire.malformed("invalid directory section name"));
                    }
                    let _offset = wire.u32()?;
                }
                wire.marker(b"EOD\0")?;
            }
            b"TAI\0" => {
                if !footer
                    && let Some(time) = clock.current
                    && let ControlFlow::Break(value) =
                        visitor(Event::Tick(Time::from_ticks(time as u64)))
                {
                    return Ok((ControlFlow::Break(value), clock.span()));
                }
                footer = true;
                descriptor(wire)?;
                let _directory = wire.u32()?;
                if !wire.at_eof()? {
                    return Err(wire.malformed("data after tailer"));
                }
                break;
            }
            _ if tag[3] != 0 => return Err(wire.malformed("invalid body section marker")),
            _ => {
                return Err(wire.unsupported(format_args!(
                    "body section {}",
                    String::from_utf8_lossy(&tag[..3])
                )));
            }
        }
    }
    if !footer
        && let Some(time) = clock.current
        && let ControlFlow::Break(value) = visitor(Event::Tick(Time::from_ticks(time as u64)))
    {
        return Ok((ControlFlow::Break(value), clock.span()));
    }
    Ok((ControlFlow::Continue(()), clock.span()))
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::io::{BufReader, Cursor};

    fn logic_types() -> Types {
        let bytes =
            b"TYP\0\0\0\0\0\x01\0\0\0\x17\0\x09\x01\x02\x03\x04\x05\x06\x07\x08\x09\0".to_vec();
        let mut wire = Wire::new(Box::new(Cursor::new(bytes))).unwrap();
        Types::read(
            &mut wire,
            [
                "", "'U'", "'X'", "'0'", "'1'", "'Z'", "'W'", "'L'", "'H'", "'-'",
            ]
            .into_iter()
            .map(str::to_owned)
            .collect(),
        )
        .unwrap()
    }

    fn snapshot(time: i64, values: &[u8], bytes: &mut Vec<u8>) {
        bytes.extend(b"SNP\0\0\0\0\0");
        bytes.extend(time.to_le_bytes());
        bytes.extend(values);
        bytes.extend(b"ESN\0");
    }

    #[test]
    fn active_ranks_and_equal_times_across_sections() {
        let types = logic_types();
        let active = [(1, 0), (3, 0)];
        let mut bytes = Vec::new();
        snapshot(0, &[3, 2], &mut bytes);
        bytes.extend(b"CYC\0");
        bytes.extend(0i64.to_le_bytes());
        bytes.extend([2, 3, 0, 0, 1, 2, 0, 127]);
        bytes.extend(b"ECY\0");
        snapshot(0, &[2, 2], &mut bytes);
        bytes.extend(b"CYC\0");
        bytes.extend(1i64.to_le_bytes());
        bytes.extend([0, 127]);
        bytes.extend(b"ECY\0");
        for capacity in [1, 2, 4096] {
            let mut wire = Wire::new(Box::new(BufReader::with_capacity(
                capacity,
                Cursor::new(bytes.clone()),
            )))
            .unwrap()
            .with_buffer_capacity(capacity);
            let mut events = Vec::new();
            let (flow, span) = walk(
                &mut wire,
                &types,
                &active,
                Time::from_ticks(u64::MAX),
                |event| {
                    events.push(event);
                    ControlFlow::<()>::Continue(())
                },
            )
            .unwrap();
            assert!(flow.is_continue());
            assert_eq!(
                span,
                Some(TimeSpan::new(Time::from_ticks(0), Time::from_ticks(1)))
            );
            assert_eq!(
                events
                    .iter()
                    .filter_map(|e| if let Event::Tick(t) = e {
                        Some(t.ticks())
                    } else {
                        None
                    })
                    .collect::<Vec<_>>(),
                [0, 1]
            );
            let scalars = events
                .iter()
                .filter_map(|e| {
                    if let Event::Scalar { id, value, .. } = e {
                        Some((*id, *value))
                    } else {
                        None
                    }
                })
                .collect::<Vec<_>>();
            assert_eq!(
                scalars,
                [
                    (1, Scalar::Ordinal(3)),
                    (3, Scalar::Ordinal(2)),
                    (3, Scalar::Ordinal(3)),
                    (1, Scalar::Ordinal(2)),
                    (1, Scalar::Ordinal(2)),
                    (3, Scalar::Ordinal(2))
                ]
            );
        }
    }

    #[test]
    fn tick_break_and_end_bound_do_not_decode_the_next_values() {
        let types = logic_types();
        let active = [(1, 0)];
        let mut bytes = Vec::new();
        snapshot(0, &[3], &mut bytes);
        bytes.extend(b"CYC\0");
        bytes.extend(1i64.to_le_bytes());
        let boundary = bytes.len();
        bytes.extend([1, 255, 0, 127]);
        bytes.extend(b"ECY\0");
        let mut wire = Wire::new(Box::new(Cursor::new(bytes.clone()))).unwrap();
        let (flow, _) = walk(
            &mut wire,
            &types,
            &active,
            Time::from_ticks(u64::MAX),
            |event| {
                if matches!(event, Event::Tick(_)) {
                    ControlFlow::Break("done")
                } else {
                    ControlFlow::Continue(())
                }
            },
        )
        .unwrap();
        assert_eq!(flow, ControlFlow::Break("done"));
        assert_eq!(wire.offset, boundary as u64);
        let mut wire = Wire::new(Box::new(Cursor::new(bytes.clone()))).unwrap();
        assert!(
            walk(&mut wire, &types, &active, Time::from_ticks(0), |_| {
                ControlFlow::<()>::Continue(())
            })
            .unwrap()
            .0
            .is_continue()
        );
        assert_eq!(wire.offset, boundary as u64);
        let mut wire = Wire::new(Box::new(Cursor::new(bytes))).unwrap();
        assert!(matches!(
            walk(
                &mut wire,
                &types,
                &active,
                Time::from_ticks(u64::MAX),
                |_| ControlFlow::<()>::Continue(())
            ),
            Err(crate::Error::Malformed { .. })
        ));
    }

    #[test]
    fn truncation_bad_ranks_and_time_overflow_fail_before_tick_delivery() {
        let types = logic_types();
        let active = [(1, 0)];
        for values in [
            vec![2, 2, 0, 127],
            vec![1, 255, 0, 127],
            vec![0, 126],
            vec![1],
            vec![0, 127, b'E', b'C', b'Y'],
        ] {
            let mut bytes = b"CYC\0".to_vec();
            bytes.extend(0i64.to_le_bytes());
            bytes.extend(values);
            let mut wire = Wire::new(Box::new(Cursor::new(bytes))).unwrap();
            let mut ticks = 0;
            assert!(
                walk(
                    &mut wire,
                    &types,
                    &active,
                    Time::from_ticks(u64::MAX),
                    |event| {
                        ticks += usize::from(matches!(event, Event::Tick(_)));
                        ControlFlow::<()>::Continue(())
                    }
                )
                .is_err()
            );
            assert_eq!(ticks, 0);
        }
        let mut bytes = b"CYC\0".to_vec();
        bytes.extend(i64::MAX.to_le_bytes());
        bytes.extend([0, 1]);
        let mut wire = Wire::new(Box::new(Cursor::new(bytes))).unwrap();
        assert!(
            walk(
                &mut wire,
                &types,
                &active,
                Time::from_ticks(u64::MAX),
                |_| ControlFlow::<()>::Continue(())
            )
            .is_err()
        );
        let mut wire = Wire::new(Box::new(Cursor::new(Vec::new()))).unwrap();
        assert_eq!(
            walk(&mut wire, &types, &active, Time::from_ticks(0), |_| {
                ControlFlow::<()>::Continue(())
            })
            .unwrap()
            .1,
            None
        );
    }

    #[test]
    fn known_footer_layout_and_optional_absence() {
        let types = logic_types();
        let active = [(1, 0)];
        let mut bytes = Vec::new();
        snapshot(0, &[2], &mut bytes);
        bytes.extend(b"DIR\0\x01\x04\x01\0\0\0\0\0EOD\0TAI\0\x01\x04\x01\0\0\0\0\0");
        let mut wire = Wire::new(Box::new(BufReader::with_capacity(1, Cursor::new(bytes))))
            .unwrap()
            .with_buffer_capacity(1);
        let mut ticks = 0;
        let (flow, _) = walk(
            &mut wire,
            &types,
            &active,
            Time::from_ticks(u64::MAX),
            |event| {
                ticks += usize::from(matches!(event, Event::Tick(_)));
                ControlFlow::<()>::Continue(())
            },
        )
        .unwrap();
        assert!(flow.is_continue());
        assert_eq!(ticks, 1);
    }

    #[test]
    #[ignore = "requires the installed GHW corpus"]
    fn source_body_validation_classifies_the_complete_corpus() {
        use std::fs::{self, File};
        let root = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("fixtures/ghw");
        let mut paths = fs::read_dir(root)
            .unwrap()
            .map(|e| e.unwrap().path())
            .filter(|p| p.is_dir())
            .collect::<Vec<_>>();
        paths.sort();
        let mut passed = 0;
        for path in &paths {
            let mut wire = Wire::new(Box::new(BufReader::new(
                File::open(path.join("waveform.ghw")).unwrap(),
            )))
            .unwrap();
            wire.header().unwrap();
            let strings = wire.strings().unwrap();
            let parsed = Types::read(&mut wire, strings);
            if path.file_name().unwrap() == "ghw0002-ghdl-issue-538" {
                assert!(matches!(parsed, Err(crate::Error::Malformed { .. })));
                continue;
            }
            let mut types = parsed.unwrap_or_else(|e| panic!("{}: {e}", path.display()));
            let mut tag = wire.fixed::<4>().unwrap();
            if &tag == b"WKT\0" {
                types.wkt(&mut wire).unwrap();
                tag = wire.fixed().unwrap();
            }
            assert_eq!(&tag, b"HIE\0");
            let declarations = super::super::hierarchy::read(&mut wire, &types)
                .unwrap_or_else(|e| panic!("{}: {e}", path.display()));
            wire.marker(b"EOH\0").unwrap();
            let result = walk(
                &mut wire,
                &types,
                &declarations.active,
                Time::from_ticks(u64::MAX),
                |_| ControlFlow::<()>::Continue(()),
            );
            if matches!(
                path.file_name().unwrap().to_str().unwrap(),
                "ghw0044-cpu-uppercase" | "ghw0050-cpu-lowercase" | "ghw0059-toplevel-tb"
            ) {
                assert!(
                    matches!(result, Err(crate::Error::Malformed { .. })),
                    "{}",
                    path.display()
                );
            } else {
                let (flow, span) = result.unwrap_or_else(|e| panic!("{}: {e}", path.display()));
                assert!(flow.is_continue());
                assert!(span.is_some());
                passed += 1;
            }
        }
        assert_eq!(passed + 4, paths.len());
        eprintln!(
            "GHW source body probe: {passed} complete, 3 malformed cycles, 1 malformed missing nested range"
        );
    }
}