padlock-output 0.10.5

Terminal, JSON, SARIF, and diff output formatters for the padlock struct layout analyzer
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
// padlock-output/src/explain.rs
//
// Renders a visual field-by-field memory layout table for a single struct.
// Shows each field's offset, size, alignment, and padding gaps inline.

use padlock_core::analysis::impact::estimate_impact;
use padlock_core::ir::{StructLayout, TypeInfo, find_padding};

/// Render a visual layout table for one struct.
///
/// Example output:
///
/// ```text
/// ReadyEvent  24 bytes  align=4
/// ┌──────────────────────────────────────────────────────────────────┐
/// │ offset │ size │ align │ CL │ field                              │
/// ├──────────────────────────────────────────────────────────────────┤
/// │      0 │    1 │     1 │  0 │ tick: u8                          │
/// │      1 │    3 │     — │  0 │ <padding>                         │
/// │      4 │    4 │     4 │  0 │ ready: Ready                      │
/// │      8 │    1 │     1 │  0 │ is_shutdown: bool                 │
/// │      9 │   15 │     — │  0 │ <padding> (trailing)              │
/// └──────────────────────────────────────────────────────────────────┘
/// 14 bytes wasted (58%) — reorder: ready, tick, is_shutdown → 8 bytes
/// ```
///
/// The `CL` column shows the zero-indexed cache-line number for each field.
/// A cache-line separator row (`╞══ cache line N ══╡`) is also emitted
/// whenever the layout crosses a cache-line boundary.
pub fn render_explain(layout: &StructLayout) -> String {
    use padlock_core::analysis::reorder;

    let mut out = String::new();

    // Header
    let loc = match (&layout.source_file, layout.source_line) {
        (Some(f), Some(l)) => format!("  ({}:{})", f, l),
        (Some(f), None) => format!("  ({})", f),
        _ => String::new(),
    };
    out.push_str(&format!("{}{}\n", layout.name, loc));
    out.push_str(&format!(
        "{} bytes  align={}  fields={}{}{}\n",
        layout.total_size,
        layout.align,
        layout.fields.len(),
        if layout.is_packed { "  [packed]" } else { "" },
        if layout.is_repr_rust {
            "  [repr(Rust) — compiler may reorder]"
        } else {
            ""
        },
    ));

    // Table — columns: offset(6) | size(4) | align(5) | CL(2) | field(36)
    let col_field = 36usize;
    let divider = format!(
        "{:─<8}{:─<6}{:─<7}{:─<4}┼{:─<col_field$}┤",
        "", "", "", "", ""
    );
    let top = format!(
        "{:─<8}{:─<6}{:─<7}{:─<4}┬{:─<col_field$}┐",
        "", "", "", "", ""
    );
    let bot = format!(
        "{:─<8}{:─<6}{:─<7}{:─<4}┴{:─<col_field$}┘",
        "", "", "", "", ""
    );
    let header = format!(
        "{:>6}{:>4}{:>5}{:>2} │ {:<col_field$}│",
        "offset", "size", "align", "CL", "field"
    );

    out.push_str(&top);
    out.push('\n');
    out.push_str(&header);
    out.push('\n');
    out.push_str(&divider);
    out.push('\n');

    // Build rows: interleave fields with padding gaps and cache-line markers
    #[derive(Debug)]
    enum Row {
        Field {
            offset: usize,
            size: usize,
            align: usize,
            name: String,
            ty: String,
        },
        Pad {
            offset: usize,
            size: usize,
            trailing: bool,
        },
        CacheLine {
            line_number: usize,
            offset: usize,
        },
    }

    let cache_line = layout.arch.cache_line_size;
    let mut rows: Vec<Row> = Vec::new();
    let gaps = find_padding(layout);

    let last_field_name = layout.fields.last().map(|f| f.name.as_str()).unwrap_or("");

    // Track which cache lines have been crossed so we can insert markers.
    let mut last_cache_line: Option<usize> = None;

    for field in &layout.fields {
        let field_cache_line = field.offset / cache_line;

        // Insert a cache-line boundary marker when entering a new cache line.
        if last_cache_line.is_none_or(|prev| field_cache_line > prev) {
            if last_cache_line.is_some() {
                // Not the first cache line: insert a separator row.
                rows.push(Row::CacheLine {
                    line_number: field_cache_line,
                    offset: field_cache_line * cache_line,
                });
            }
            last_cache_line = Some(field_cache_line);
        }

        let ty_name = type_name(&field.ty);
        rows.push(Row::Field {
            offset: field.offset,
            size: field.size,
            align: field.align,
            name: field.name.clone(),
            ty: ty_name,
        });
        if let Some(gap) = gaps.iter().find(|g| g.after_field == field.name) {
            let pad_offset = field.offset + field.size;
            let is_trailing = field.name == last_field_name;
            rows.push(Row::Pad {
                offset: pad_offset,
                size: gap.bytes,
                trailing: is_trailing,
            });
        }
    }

    // Cache-line separator row width matches the table inner width.
    // Inner width = 8(offset) + 1(┼) + 6(size) + 1(┼) + 7(align) + 1(┼) + 4(CL) + 1(┼) + col_field + 3
    let cache_sep_inner = 8 + 1 + 6 + 1 + 7 + 1 + 4 + 1 + col_field + 3; // ─ count between outer │
    for row in &rows {
        match row {
            Row::Field {
                offset,
                size,
                align,
                name,
                ty,
            } => {
                let cl = offset / cache_line;
                let label = format!("{}: {}", name, ty);
                let label = if label.len() > col_field {
                    format!("{}", &label[..col_field - 1])
                } else {
                    label
                };
                out.push_str(&format!(
                    "{:>6}{:>4}{:>5}{:>2} │ {:<col_field$}│\n",
                    offset, size, align, cl, label
                ));
            }
            Row::Pad {
                offset,
                size,
                trailing,
            } => {
                let cl = offset / cache_line;
                let label = if *trailing {
                    "<padding> (trailing)".to_string()
                } else {
                    "<padding>".to_string()
                };
                out.push_str(&format!(
                    "{:>6}{:>4}{:>5}{:>2} │ {:<col_field$}│\n",
                    offset, size, "", cl, label
                ));
            }
            Row::CacheLine {
                line_number,
                offset,
            } => {
                let label = format!("── cache line {line_number} (offset {offset}) ");
                // Pad to fill the inner width with '═' characters.
                let used = label.len();
                let pad = if cache_sep_inner > used + 4 {
                    "".repeat(cache_sep_inner - used - 4)
                } else {
                    String::new()
                };
                out.push_str(&format!("{label}{pad}\n"));
            }
        }
    }

    out.push_str(&bot);
    out.push('\n');

    // Summary line — gaps already includes trailing padding from find_padding.
    let wasted: usize = gaps.iter().map(|g| g.bytes).sum();

    if wasted > 0 && !layout.is_packed && !layout.is_union {
        let pct = wasted as f64 / layout.total_size as f64 * 100.0;
        let (opt_size, savings) = reorder::reorder_savings(layout);
        if savings > 0 {
            let opt_order: Vec<String> = reorder::optimal_order(layout)
                .iter()
                .map(|f| f.name.clone())
                .collect();
            out.push_str(&format!(
                "{} bytes wasted ({:.0}%) — reorder: {}{} bytes\n",
                wasted,
                pct,
                opt_order.join(", "),
                opt_size
            ));

            // Impact block: show concrete memory/cache effects at scale.
            // Cache line size: default 64 bytes (x86-64 / aarch64).
            const CACHE_LINE: usize = 64;
            let impact = estimate_impact(savings, layout.total_size, opt_size, CACHE_LINE);
            out.push_str(&format!(
                "  ~{savings} KB extra per 1K instances · ~{savings} MB per 1M \
                 instances · ~{cl_1m} extra cache lines/1M (seq. scan)\n",
                cl_1m = fmt_count(impact.extra_cache_lines_1m),
            ));
            if impact.reduces_cache_line_crossings() {
                out.push_str(&format!(
                    "  Spans {} cache line(s); optimal spans {}\n",
                    impact.current_cache_lines, impact.optimal_cache_lines,
                ));
            }
        } else {
            out.push_str(&format!(
                "{} bytes wasted ({:.0}%) — already in optimal order\n",
                wasted, pct
            ));
        }
    } else if layout.is_packed {
        out.push_str("packed — no padding\n");
    } else {
        out.push_str("no layout issues — struct is already optimally laid out\n");
    }

    out
}

/// Format a large count with K/M suffix for readability.
fn fmt_count(n: usize) -> String {
    if n >= 1_000_000 {
        format!("{}M", n / 1_000_000)
    } else if n >= 1_000 {
        format!("{}K", n / 1_000)
    } else {
        n.to_string()
    }
}

fn type_name(ty: &TypeInfo) -> String {
    match ty {
        TypeInfo::Primitive { name, .. } => name.clone(),
        TypeInfo::Pointer { .. } => "*ptr".to_string(),
        TypeInfo::Array { element, count, .. } => format!("[{}; {}]", type_name(element), count),
        TypeInfo::Struct(inner) => inner.name.clone(),
        TypeInfo::Opaque { name, .. } => name.clone(),
    }
}

// ── tests ─────────────────────────────────────────────────────────────────────

#[cfg(test)]
mod tests {
    use super::*;
    use padlock_core::ir::test_fixtures::connection_layout;

    #[test]
    fn explain_contains_field_names() {
        let layout = connection_layout();
        let out = render_explain(&layout);
        assert!(out.contains("timeout"));
        assert!(out.contains("port"));
        assert!(out.contains("is_active"));
        assert!(out.contains("is_tls"));
    }

    #[test]
    fn explain_shows_padding_rows() {
        let layout = connection_layout();
        let out = render_explain(&layout);
        assert!(out.contains("<padding>"));
    }

    #[test]
    fn explain_shows_struct_size() {
        let layout = connection_layout();
        let out = render_explain(&layout);
        assert!(out.contains("24 bytes"));
    }

    #[test]
    fn explain_shows_reorder_suggestion() {
        let layout = connection_layout();
        let out = render_explain(&layout);
        assert!(out.contains("reorder"));
        assert!(out.contains(""));
    }

    #[test]
    fn explain_shows_impact_scale_line() {
        let layout = connection_layout();
        let out = render_explain(&layout);
        // Connection saves 8B → should show ~8 KB per 1K and ~8 MB per 1M
        assert!(out.contains("~8 KB extra per 1K instances"));
        assert!(out.contains("~8 MB per 1M instances"));
        assert!(out.contains("extra cache lines/1M"));
    }

    #[test]
    fn explain_no_impact_line_when_no_savings() {
        let layout = padlock_core::ir::test_fixtures::packed_layout();
        let out = render_explain(&layout);
        assert!(!out.contains("KB extra per 1K"));
        assert!(!out.contains("MB per 1M"));
    }

    #[test]
    fn explain_shows_cache_line_separator_when_struct_spans_multiple_lines() {
        use padlock_core::arch::X86_64_SYSV;
        use padlock_core::ir::{AccessPattern, Field, StructLayout, TypeInfo};
        // Build a struct that spans two 64-byte cache lines
        let big = StructLayout {
            name: "Big".to_string(),
            total_size: 128,
            align: 8,
            fields: vec![
                Field {
                    name: "a".to_string(),
                    ty: TypeInfo::Primitive {
                        name: "u8[60]".to_string(),
                        size: 60,
                        align: 1,
                    },
                    offset: 0,
                    size: 60,
                    align: 1,
                    source_file: None,
                    source_line: None,
                    access: AccessPattern::Unknown,
                },
                Field {
                    name: "b".to_string(),
                    ty: TypeInfo::Primitive {
                        name: "u64".to_string(),
                        size: 8,
                        align: 8,
                    },
                    offset: 64,
                    size: 8,
                    align: 8,
                    source_file: None,
                    source_line: None,
                    access: AccessPattern::Unknown,
                },
            ],
            source_file: None,
            source_line: None,
            arch: &X86_64_SYSV,
            is_packed: false,
            is_union: false,
            is_repr_rust: false,
            suppressed_findings: Vec::new(),
            uncertain_fields: Vec::new(),
        };
        let out = render_explain(&big);
        assert!(
            out.contains("cache line 1"),
            "must show cache line 1 separator: {out}"
        );
    }

    #[test]
    fn explain_shows_cl_column_header() {
        let layout = connection_layout();
        let out = render_explain(&layout);
        assert!(out.contains("CL"), "CL column header must appear");
    }

    #[test]
    fn explain_cl_column_shows_zero_for_small_struct() {
        // connection_layout is 24 bytes — all fields on cache line 0
        let layout = connection_layout();
        let out = render_explain(&layout);
        // Every data row should have │  0 │ (CL 0) and none should show │  1 │
        assert!(out.contains("│  0 │"), "all fields must be on cache line 0");
        assert!(
            !out.contains("│  1 │"),
            "no field should be on cache line 1"
        );
    }

    #[test]
    fn explain_cl_column_shows_nonzero_for_large_struct() {
        use padlock_core::arch::X86_64_SYSV;
        use padlock_core::ir::{AccessPattern, Field, StructLayout, TypeInfo};
        let big = StructLayout {
            name: "Big".to_string(),
            total_size: 128,
            align: 8,
            fields: vec![
                Field {
                    name: "a".to_string(),
                    ty: TypeInfo::Primitive {
                        name: "u8[64]".to_string(),
                        size: 64,
                        align: 1,
                    },
                    offset: 0,
                    size: 64,
                    align: 1,
                    source_file: None,
                    source_line: None,
                    access: AccessPattern::Unknown,
                },
                Field {
                    name: "b".to_string(),
                    ty: TypeInfo::Primitive {
                        name: "u64".to_string(),
                        size: 8,
                        align: 8,
                    },
                    offset: 64,
                    size: 8,
                    align: 8,
                    source_file: None,
                    source_line: None,
                    access: AccessPattern::Unknown,
                },
            ],
            source_file: None,
            source_line: None,
            arch: &X86_64_SYSV,
            is_packed: false,
            is_union: false,
            is_repr_rust: false,
            suppressed_findings: Vec::new(),
            uncertain_fields: Vec::new(),
        };
        let out = render_explain(&big);
        // field 'b' starts at offset 64 → cache line 1
        assert!(out.contains("│  1 │"), "field b must show CL 1");
    }

    #[test]
    fn explain_no_cache_line_separator_for_small_struct() {
        // Connection (24 bytes) fits in one cache line — no separator expected
        let layout = connection_layout();
        let out = render_explain(&layout);
        assert!(
            !out.contains("cache line 1"),
            "single-cache-line struct must not show separator"
        );
    }

    #[test]
    fn fmt_count_formats_correctly() {
        assert_eq!(fmt_count(999), "999");
        assert_eq!(fmt_count(1_000), "1K");
        assert_eq!(fmt_count(125_000), "125K");
        assert_eq!(fmt_count(1_000_000), "1M");
        assert_eq!(fmt_count(2_500_000), "2M");
    }
}