vcl-normalizer 0.1.0

VCL 4.1 functional-equivalence comparator
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
//! VMOD introspection: load JSON specs from .so binaries.
//!
//! Every vmod .so embeds a JSON spec between markers VMOD_JSON_SPEC\x02 and \x03.
//! This module extracts and parses that spec to validate vmod calls.

use std::collections::BTreeMap;
use std::path::PathBuf;

/// Complete VMOD specification extracted from a .so binary.
#[derive(Debug, Clone)]
pub struct VmodSpec {
    pub funcs: BTreeMap<String, Sig>,
    pub objects: BTreeMap<String, ObjSpec>,
}

/// Specification of a VMOD object (with init and methods).
#[derive(Debug, Clone)]
pub struct ObjSpec {
    pub init: Sig,
    pub methods: BTreeMap<String, Sig>,
}

/// Function signature: list of argument specifications.
#[derive(Debug, Clone)]
pub struct Sig {
    pub args: Vec<ArgSpec>,
}

/// Specification of a function/method argument.
#[derive(Debug, Clone)]
pub struct ArgSpec {
    pub name: Option<String>,    // None = positional-only
    pub default: Option<String>, // literal default value as string
    pub optional: bool,
    /// For an `ENUM`-typed argument: its legal literal values (e.g.
    /// `["IDENTITY", "BASE64", "HEX", ...]` for blob.encode's `encoding`
    /// arg), taken verbatim from the vmod's JSON spec. `None` for any
    /// non-ENUM argument.
    pub enum_values: Option<Vec<String>>,
}

const MARKER_START: &[u8] = b"VMOD_JSON_SPEC\x02";
const MARKER_END: u8 = b'\x03';

/// Get the default VMOD search paths from pkg-config.
/// Returns empty vec on failure (no warning emitted here; let the caller decide).
pub fn default_vmod_paths() -> Vec<PathBuf> {
    match std::process::Command::new("pkg-config")
        .arg("--variable=vmoddir")
        .arg("varnishapi")
        .output()
    {
        Ok(output) if output.status.success() => {
            let path_str = String::from_utf8_lossy(&output.stdout);
            vec![PathBuf::from(path_str.trim())]
        }
        _ => vec![],
    }
}

/// Load and parse a VMOD specification from a .so file.
///
/// For `import foo;`: searches for `libvmod_foo.so` in `vmod_paths` (in order).
/// For `import foo from "path";`: uses that path directly.
///
/// Returns `None` on any error (missing file, no marker, bad JSON, etc.),
/// and emits a warning to stderr.
pub fn load_vmod(name: &str, from: Option<&str>, vmod_paths: &[PathBuf]) -> Option<VmodSpec> {
    let so_path = if let Some(path) = from {
        PathBuf::from(path)
    } else {
        let so_name = format!("libvmod_{}.so", name);
        let mut found = None;
        for base_path in vmod_paths {
            let candidate = base_path.join(&so_name);
            if candidate.exists() {
                found = Some(candidate);
                break;
            }
        }
        found.unwrap_or_else(|| {
            eprintln!("warning: vmod '{}' not found in vmod paths", name);
            PathBuf::new()
        })
    };

    if so_path.as_os_str().is_empty() {
        return None;
    }

    // Read the file.
    let data = match std::fs::read(&so_path) {
        Ok(d) => d,
        Err(e) => {
            eprintln!(
                "warning: failed to read vmod '{}' from {:?}: {}",
                name, so_path, e
            );
            return None;
        }
    };

    // Find the marker.
    let start_idx = match find_bytes(&data, MARKER_START) {
        Some(idx) => idx + MARKER_START.len(),
        None => {
            eprintln!("warning: vmod '{}' has no VMOD_JSON_SPEC marker", name);
            return None;
        }
    };

    // Find the end marker.
    let end_idx = match data[start_idx..].iter().position(|&b| b == MARKER_END) {
        Some(pos) => start_idx + pos,
        None => {
            eprintln!(
                "warning: vmod '{}' has truncated VMOD_JSON_SPEC (no end marker)",
                name
            );
            return None;
        }
    };

    let json_bytes = &data[start_idx..end_idx];

    // Parse JSON.
    let spec_json: serde_json::Value = match serde_json::from_slice(json_bytes) {
        Ok(j) => j,
        Err(e) => {
            eprintln!("warning: vmod '{}' has invalid JSON spec: {}", name, e);
            return None;
        }
    };

    // The spec is an array of arrays.
    let entries = match spec_json.as_array() {
        Some(arr) => arr,
        None => {
            eprintln!("warning: vmod '{}' spec is not a JSON array", name);
            return None;
        }
    };

    let mut funcs = BTreeMap::new();
    let mut objects = BTreeMap::new();

    for entry in entries {
        let entry_arr = match entry.as_array() {
            Some(arr) if !arr.is_empty() => arr,
            _ => continue,
        };

        match entry_arr[0].as_str() {
            Some("$VMOD") => {
                // Check version: entry_arr[1] should be a version string starting with "2"
                if let Some(version) = entry_arr.get(1).and_then(|v| v.as_str()) {
                    if !version.starts_with("2") {
                        eprintln!(
                            "warning: vmod '{}' has unsupported spec version '{}'",
                            name, version
                        );
                        return None;
                    }
                } else {
                    eprintln!("warning: vmod '{}' has no version in $VMOD entry", name);
                    return None;
                }
            }
            Some("$FUNC") => {
                // ["$FUNC", fname, signature]
                if let (Some(fname), Some(sig)) = (
                    entry_arr.get(1).and_then(|v| v.as_str()),
                    entry_arr.get(2).and_then(|v| v.as_array()),
                ) {
                    if let Some(spec) = parse_signature(sig) {
                        funcs.insert(fname.into(), spec);
                    }
                }
            }
            Some("$OBJ") => {
                // ["$OBJ", oname, {flags}, ctype, ["$INIT", signature], ["$FINI", …], ["$METHOD", mname, signature]*]
                if let Some(oname) = entry_arr.get(1).and_then(|v| v.as_str()) {
                    let mut init_sig = None;
                    let mut methods = BTreeMap::new();

                    for item in &entry_arr[2..] {
                        if let Some(item_arr) = item.as_array() {
                            if let Some(item_type) = item_arr.first().and_then(|v| v.as_str()) {
                                match item_type {
                                    "$INIT" => {
                                        if let Some(sig) =
                                            item_arr.get(1).and_then(|v| v.as_array())
                                        {
                                            if let Some(spec) = parse_signature(sig) {
                                                init_sig = Some(spec);
                                            }
                                        }
                                    }
                                    "$METHOD" => {
                                        // ["$METHOD", mname, signature]
                                        if let (Some(mname), Some(sig)) = (
                                            item_arr.get(1).and_then(|v| v.as_str()),
                                            item_arr.get(2).and_then(|v| v.as_array()),
                                        ) {
                                            if let Some(spec) = parse_signature(sig) {
                                                methods.insert(mname.into(), spec);
                                            }
                                        }
                                    }
                                    "$FINI" | "$CPROTO" | "$EVENT" => {
                                        // Ignored.
                                    }
                                    _ => {}
                                }
                            }
                        }
                    }

                    if let Some(init) = init_sig {
                        objects.insert(oname.into(), ObjSpec { init, methods });
                    }
                }
            }
            Some("$CPROTO") | Some("$EVENT") => {
                // Ignored.
            }
            _ => {
                // Unknown entry type, skip.
            }
        }
    }

    Some(VmodSpec { funcs, objects })
}

/// Parse a signature array: [[RETTYPE, …], cfunc_name, cproto_string, arg*]
fn parse_signature(sig: &[serde_json::Value]) -> Option<Sig> {
    // We only care about the args, which come at index 3 and beyond.
    let mut args = Vec::new();

    // Skip the first 3 elements (return type, cfunc name, cproto).
    for arg_val in &sig[3..] {
        if let Some(arg_arr) = arg_val.as_array() {
            if arg_arr.is_empty() {
                continue;
            }

            // arg := [TYPE, name|null, internal_name, default?, spec?, is_optional?]
            let name = arg_arr
                .get(1)
                .and_then(|v| v.as_str())
                .map(|s| s.to_string());

            // For an ENUM-typed arg, index 4 ("spec?") is the list of legal
            // literal values, e.g. ["IDENTITY", "BASE64", "HEX", ...].
            let enum_values = if arg_arr.first().and_then(|v| v.as_str()) == Some("ENUM") {
                arg_arr.get(4).and_then(|v| v.as_array()).map(|arr| {
                    arr.iter()
                        .filter_map(|v| v.as_str().map(|s| s.to_string()))
                        .collect()
                })
            } else {
                None
            };

            let default = arg_arr.get(3).map(|v| {
                // Default can be any JSON value; convert to string for storage.
                match v {
                    serde_json::Value::String(s) => s.clone(),
                    serde_json::Value::Number(n) => n.to_string(),
                    serde_json::Value::Bool(b) => b.to_string(),
                    serde_json::Value::Null => "null".to_string(),
                    _ => v.to_string(),
                }
            });

            // An arg is optional if:
            // - It has a 4th element (default) (index 3)
            // - Or it has an explicit 6th element (is_optional) (index 5) that is true
            let optional = arg_arr.get(3).is_some()
                || arg_arr.get(5).and_then(|v| v.as_bool()).unwrap_or(false);

            args.push(ArgSpec {
                name,
                default,
                optional,
                enum_values,
            });
        }
    }

    Some(Sig { args })
}

/// Helper: find a byte sequence in a slice.
fn find_bytes(haystack: &[u8], needle: &[u8]) -> Option<usize> {
    haystack
        .windows(needle.len())
        .position(|window| window == needle)
}

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

    /// Helper to create a test fixture .so file with embedded JSON spec.
    fn create_test_vmod(json_spec: &str) -> Vec<u8> {
        let mut data = Vec::new();
        // Arbitrary padding before marker.
        data.extend_from_slice(b"ELF\x7f\x00\x00\x00\x00padding");
        // The marker + JSON.
        data.extend_from_slice(MARKER_START);
        data.extend_from_slice(json_spec.as_bytes());
        data.push(MARKER_END);
        // Arbitrary padding after marker.
        data.extend_from_slice(b"more padding");
        data
    }

    #[test]
    fn v1_extract_spec_from_fixture() {
        // V1: Extract spec from fixture byte blob
        let spec_json = r#"[["$VMOD","2.0","foo"],["$FUNC","tolower",[["STRING"],null,"VCP_void",["STRING","s",null]]]]"#;
        let data = create_test_vmod(spec_json);

        // Write to a temp file and load.
        let temp_file = std::env::temp_dir().join("test_vmod_v1.so");
        std::fs::write(&temp_file, &data).unwrap();

        let spec = load_vmod("test_vmod_v1", Some(temp_file.to_str().unwrap()), &[]).unwrap();

        assert!(spec.funcs.contains_key("tolower"));
        let tolower_sig = &spec.funcs["tolower"];
        assert_eq!(tolower_sig.args.len(), 1);

        let _ = std::fs::remove_file(&temp_file);
    }

    #[test]
    fn v2_missing_marker_truncated_json_bad_version() {
        // V2: Missing marker / truncated JSON / $VMOD version "3.0" → warning + None
        let temp_dir = std::env::temp_dir();

        // Test 1: Missing marker
        let data = b"ELF\x7f\x00\x00padding only, no marker";
        let file1 = temp_dir.join("test_vmod_v2_no_marker.so");
        std::fs::write(&file1, data).unwrap();
        let result = load_vmod("test_vmod_v2_no_marker", Some(file1.to_str().unwrap()), &[]);
        assert!(result.is_none());

        // Test 2: Version "3.0" (unsupported)
        let spec_json = r#"[["$VMOD","3.0","foo"]]"#;
        let data = create_test_vmod(spec_json);
        let file2 = temp_dir.join("test_vmod_v2_bad_version.so");
        std::fs::write(&file2, &data).unwrap();
        let result = load_vmod(
            "test_vmod_v2_bad_version",
            Some(file2.to_str().unwrap()),
            &[],
        );
        assert!(result.is_none());

        // Test 3: Truncated JSON (no end marker)
        let mut data = Vec::new();
        data.extend_from_slice(b"ELF\x7f");
        data.extend_from_slice(MARKER_START);
        data.extend_from_slice(b"[incomplete json...");
        // No end marker
        let file3 = temp_dir.join("test_vmod_v2_truncated.so");
        std::fs::write(&file3, &data).unwrap();
        let result = load_vmod("test_vmod_v2_truncated", Some(file3.to_str().unwrap()), &[]);
        assert!(result.is_none());

        let _ = std::fs::remove_file(&file1);
        let _ = std::fs::remove_file(&file2);
        let _ = std::fs::remove_file(&file3);
    }

    #[test]
    fn v3_func_parsing_optional_args() {
        // V3: $FUNC parsing: names, arg count, optional-arg detection
        let spec_json = r#"[
            ["$VMOD","2.0","foo"],
            ["$FUNC","func_with_optional",[["STRING"],null,"VCP_void",
                ["STRING","required",null],
                ["STRING","optional_with_default","opt_internal","default_value"],
                ["INT",null,"positional_only_internal"]
            ]]
        ]"#;
        let data = create_test_vmod(spec_json);
        let temp_file = std::env::temp_dir().join("test_vmod_v3.so");
        std::fs::write(&temp_file, &data).unwrap();

        let spec = load_vmod("test_vmod_v3", Some(temp_file.to_str().unwrap()), &[]).unwrap();

        let func = &spec.funcs["func_with_optional"];
        assert_eq!(func.args.len(), 3);

        // First arg: required (name="required", no default)
        assert_eq!(func.args[0].name.as_deref(), Some("required"));
        assert_eq!(func.args[0].default, None);
        assert!(!func.args[0].optional);

        // Second arg: optional with default
        assert_eq!(func.args[1].name.as_deref(), Some("optional_with_default"));
        assert_eq!(func.args[1].default, Some("default_value".to_string()));
        assert!(func.args[1].optional);

        // Third arg: positional-only (name=null)
        assert_eq!(func.args[2].name, None);
        assert!(!func.args[2].optional);

        let _ = std::fs::remove_file(&temp_file);
    }

    #[test]
    fn v4_obj_parsing_init_and_methods() {
        // V4: $OBJ parsing: $INIT signature + $METHOD list; $CPROTO/$EVENT ignored
        let spec_json = r#"[
            ["$VMOD","2.0","foo"],
            ["$OBJ","myobj",{},"struct obj *",
                ["$INIT",[["VOID"],null,"myobj_init",["STRING","arg1",null]]],
                ["$CPROTO","..."],
                ["$EVENT","..."],
                ["$METHOD","method1",[["STRING"],null,"myobj_method1"]],
                ["$METHOD","method2",[["INT"],null,"myobj_method2",["STRING","param",null]]]
            ]
        ]"#;
        let data = create_test_vmod(spec_json);
        let temp_file = std::env::temp_dir().join("test_vmod_v4.so");
        std::fs::write(&temp_file, &data).unwrap();

        let spec = load_vmod("test_vmod_v4", Some(temp_file.to_str().unwrap()), &[]).unwrap();

        let obj = &spec.objects["myobj"];
        assert_eq!(obj.init.args.len(), 1);
        assert_eq!(obj.init.args[0].name.as_deref(), Some("arg1"));

        assert_eq!(obj.methods.len(), 2);
        assert!(obj.methods.contains_key("method1"));
        assert!(obj.methods.contains_key("method2"));
        assert_eq!(obj.methods["method2"].args.len(), 1);

        let _ = std::fs::remove_file(&temp_file);
    }

    #[test]
    fn v5_missing_so_file() {
        // V5: .so not found on vmod paths → warning, returns None
        let nonexistent_path = std::env::temp_dir().join("nonexistent_vmod_v5.so");
        let result = load_vmod("v5_missing", Some(nonexistent_path.to_str().unwrap()), &[]);
        assert!(result.is_none());
    }

    #[test]
    fn v6_system_vmod_if_available() {
        // V6: (integration, auto-skip) system libvmod_std.so via pkg-config
        // This test auto-skips if pkg-config is unavailable.
        let default_paths = default_vmod_paths();
        if default_paths.is_empty() {
            eprintln!("pkg-config not available; skipping V6 test");
            return;
        }

        // Try to load the system std vmod.
        if let Some(spec) = load_vmod("std", None, &default_paths) {
            // Should have tolower function (or similar, tolower is standard in vmod_std).
            assert!(!spec.funcs.is_empty(), "std vmod should contain functions");
        }
        // If loading fails, that's okay too (might not be installed).
    }
}