mesh-llm-native-runtime 0.75.0

Native runtime manifest, selection, and cache policy for Mesh LLM
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
use crate::NativeRuntimeBackend;
use anyhow::{Context, Result, bail};
use serde::{Deserialize, Serialize};
use sha2::{Digest, Sha256};
use std::{
    collections::BTreeMap,
    fs::{self, File},
    io::Read,
    path::{Component, Path, PathBuf},
};

pub const NATIVE_RUNTIME_MANIFEST_FILE: &str = "manifest.json";

#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
pub struct NativeRuntimePlatform {
    pub os: String,
    pub arch: String,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub target: Option<String>,
}

#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
pub struct NativeRuntimeArtifact {
    pub id: String,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub mesh_version: Option<String>,
    pub skippy_abi: String,
    pub platform: NativeRuntimePlatform,
    pub backend: NativeRuntimeBackend,
    #[serde(default)]
    pub rank: i64,
    pub libraries: Vec<String>,
    #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
    pub files: BTreeMap<String, String>,
    #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
    pub tools: BTreeMap<String, String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub url: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub sha256: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub signature: Option<String>,
}

#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
pub struct NativeRuntimeManifest {
    pub runtime: NativeRuntimeArtifact,
}

#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
pub struct NativeRuntimeReleaseManifest {
    pub mesh_version: String,
    pub skippy_abi: String,
    #[serde(default)]
    pub artifacts: Vec<NativeRuntimeArtifact>,
}

impl NativeRuntimeArtifact {
    pub fn native_runtime_id(&self) -> &str {
        &self.id
    }

    pub fn mesh_version_or<'a>(&'a self, fallback: &'a str) -> &'a str {
        self.mesh_version.as_deref().unwrap_or(fallback)
    }
}

impl NativeRuntimeManifest {
    pub fn read_from_dir(dir: &Path) -> Result<Self> {
        let path = dir.join(NATIVE_RUNTIME_MANIFEST_FILE);
        let text = fs::read_to_string(&path)
            .with_context(|| format!("read native runtime manifest {}", path.display()))?;
        let manifest: Self = serde_json::from_str(&text)
            .with_context(|| format!("parse native runtime manifest {}", path.display()))?;
        manifest.validate()?;
        manifest.verify_contents(dir)?;
        Ok(manifest)
    }

    pub fn write_to_dir(&self, dir: &Path) -> Result<()> {
        fs::create_dir_all(dir)
            .with_context(|| format!("create native runtime dir {}", dir.display()))?;
        let mut manifest = self.clone();
        if manifest.runtime.files.is_empty() {
            for library in &manifest.runtime.libraries {
                let path = checked_runtime_path(dir, library)?;
                manifest
                    .runtime
                    .files
                    .insert(library.clone(), sha256_file(&path)?);
            }
        }
        manifest.validate()?;
        let path = dir.join(NATIVE_RUNTIME_MANIFEST_FILE);
        let text = serde_json::to_string_pretty(&manifest)?;
        fs::write(&path, format!("{text}\n"))
            .with_context(|| format!("write native runtime manifest {}", path.display()))
    }

    pub fn validate(&self) -> Result<()> {
        validate_artifact(&self.runtime)
    }

    fn verify_contents(&self, dir: &Path) -> Result<()> {
        if self.runtime.files.is_empty() {
            bail!(
                "native runtime artifact {} does not declare file checksums",
                self.runtime.id
            );
        }
        for library in &self.runtime.libraries {
            if !self.runtime.files.contains_key(library) {
                bail!(
                    "native runtime artifact {} library {} is missing a file checksum",
                    self.runtime.id,
                    library
                );
            }
        }
        verify_file_checksums(dir, "file", &self.runtime.files)?;
        verify_file_checksums(dir, "tool", &self.runtime.tools)
    }
}

impl NativeRuntimeReleaseManifest {
    pub fn read_from_path(path: &Path) -> Result<Self> {
        let text = fs::read_to_string(path)
            .with_context(|| format!("read native runtime release manifest {}", path.display()))?;
        Self::from_json_str(&text)
            .with_context(|| format!("parse native runtime release manifest {}", path.display()))
    }

    pub fn from_json_str(text: &str) -> Result<Self> {
        let manifest: Self =
            serde_json::from_str(text).context("parse native runtime release manifest")?;
        manifest.validate()?;
        Ok(manifest)
    }

    pub fn validate(&self) -> Result<()> {
        if self.mesh_version.trim().is_empty() {
            bail!("native runtime release manifest mesh_version is empty");
        }
        if self.skippy_abi.trim().is_empty() {
            bail!("native runtime release manifest skippy_abi is empty");
        }
        for artifact in &self.artifacts {
            validate_artifact(artifact)?;
            if artifact.skippy_abi != self.skippy_abi {
                bail!(
                    "native runtime artifact {} has skippy_abi {}, expected {}",
                    artifact.id,
                    artifact.skippy_abi,
                    self.skippy_abi
                );
            }
        }
        Ok(())
    }
}

fn validate_artifact(artifact: &NativeRuntimeArtifact) -> Result<()> {
    if artifact.id.trim().is_empty() {
        bail!("native runtime artifact id is empty");
    }
    if artifact.skippy_abi.trim().is_empty() {
        bail!(
            "native runtime artifact {} skippy_abi is empty",
            artifact.id
        );
    }
    if artifact.platform.os.trim().is_empty() || artifact.platform.arch.trim().is_empty() {
        bail!(
            "native runtime artifact {} must declare platform os and arch",
            artifact.id
        );
    }
    if artifact.libraries.is_empty() {
        bail!(
            "native runtime artifact {} must declare at least one library",
            artifact.id
        );
    }
    for (path, checksum) in artifact.files.iter().chain(&artifact.tools) {
        validate_runtime_path(path)?;
        normalize_sha256(checksum).with_context(|| {
            format!(
                "native runtime artifact {} has invalid checksum for {}",
                artifact.id, path
            )
        })?;
    }
    Ok(())
}

fn verify_file_checksums(
    root: &Path,
    kind: &str,
    checksums: &BTreeMap<String, String>,
) -> Result<()> {
    for (relative, expected) in checksums {
        let path = checked_runtime_path(root, relative)?;
        let actual = sha256_file(&path)?;
        let expected = normalize_sha256(expected)?;
        if actual != expected {
            bail!(
                "native runtime {kind} checksum mismatch for {relative}: expected {expected}, got {actual}"
            );
        }
    }
    Ok(())
}

fn checked_runtime_path(root: &Path, relative: &str) -> Result<PathBuf> {
    validate_runtime_path(relative)?;
    let root = root
        .canonicalize()
        .with_context(|| format!("canonicalize native runtime root {}", root.display()))?;
    let path = root.join(relative);
    let path = path
        .canonicalize()
        .with_context(|| format!("canonicalize native runtime file {}", path.display()))?;
    if !path.starts_with(&root) {
        bail!("native runtime path escapes its bundle: {relative}");
    }
    if !path.is_file() {
        bail!("native runtime path is not a file: {}", path.display());
    }
    Ok(path)
}

fn validate_runtime_path(relative: &str) -> Result<()> {
    let path = Path::new(relative);
    if relative.trim().is_empty()
        || path.is_absolute()
        || path
            .components()
            .any(|component| !matches!(component, Component::Normal(_)))
    {
        bail!("native runtime path must be a safe relative file path: {relative}");
    }
    Ok(())
}

fn normalize_sha256(value: &str) -> Result<String> {
    let value = value
        .trim()
        .strip_prefix("sha256:")
        .unwrap_or(value.trim())
        .to_ascii_lowercase();
    if value.len() != 64 || !value.bytes().all(|byte| byte.is_ascii_hexdigit()) {
        bail!("expected a 64-character SHA-256 digest");
    }
    Ok(value)
}

fn sha256_file(path: &Path) -> Result<String> {
    let mut file =
        File::open(path).with_context(|| format!("open native runtime file {}", path.display()))?;
    let mut digest = Sha256::new();
    // This is reached through the Kotlin/UniFFI SDK while running on a JVM
    // native thread. JVM-created native threads can have a 1 MiB stack, so a
    // 1 MiB local array can overflow that stack before checksum verification
    // even begins. Keep the read buffer on the heap instead.
    let mut buffer = vec![0_u8; 1024 * 1024];
    loop {
        let count = file
            .read(&mut buffer)
            .with_context(|| format!("read native runtime file {}", path.display()))?;
        if count == 0 {
            break;
        }
        digest.update(&buffer[..count]);
    }
    Ok(format!("{:x}", digest.finalize()))
}

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

    #[test]
    fn reads_native_runtime_manifest_shape() {
        let temp = tempfile::tempdir().unwrap();
        let library = temp.path().join("lib/libllama.so");
        fs::create_dir_all(library.parent().unwrap()).unwrap();
        fs::write(&library, b"native runtime").unwrap();
        let checksum = sha256_file(&library).unwrap();
        fs::write(
            temp.path().join(NATIVE_RUNTIME_MANIFEST_FILE),
            r#"{
  "runtime": {
    "id": "meshllm-runtime-linux-x86_64-cuda12",
    "mesh_version": "0.68.0",
    "skippy_abi": "0.1.25",
    "platform": {
      "os": "linux",
      "arch": "x86_64",
      "target": "x86_64-unknown-linux-gnu"
    },
    "backend": {
      "kind": "cuda",
      "cuda": {
        "toolkit_major": 12,
        "gpu_arches": ["sm_90"]
      }
    },
    "rank": 650,
    "libraries": ["lib/libllama.so"],
    "files": {"lib/libllama.so": "__CHECKSUM__"},
    "tools": {}
  }
}"#
            .replace("__CHECKSUM__", &checksum),
        )
        .unwrap();

        let manifest = NativeRuntimeManifest::read_from_dir(temp.path()).unwrap();

        assert_eq!(manifest.runtime.id, "meshllm-runtime-linux-x86_64-cuda12");
        assert_eq!(manifest.runtime.skippy_abi, "0.1.25");
        assert_eq!(manifest.runtime.backend.kind.as_str(), "cuda");
    }

    #[test]
    fn reads_release_manifest() {
        let manifest = NativeRuntimeReleaseManifest::from_json_str(
            r#"{
  "mesh_version": "0.68.0",
  "skippy_abi": "0.1.25",
  "artifacts": [
    {
      "id": "meshllm-runtime-linux-x86_64-cpu",
      "mesh_version": "0.68.0",
      "skippy_abi": "0.1.25",
      "platform": { "os": "linux", "arch": "x86_64" },
      "backend": { "kind": "cpu" },
      "rank": 100,
      "libraries": ["lib/libllama.so"]
    }
  ]
}"#,
        )
        .unwrap();

        assert_eq!(manifest.artifacts.len(), 1);
        assert_eq!(manifest.artifacts[0].backend, NativeRuntimeBackend::cpu());
    }

    #[test]
    fn rejects_tampered_runtime_file() {
        let temp = tempfile::tempdir().unwrap();
        let library = temp.path().join("lib/libllama.so");
        fs::create_dir_all(library.parent().unwrap()).unwrap();
        fs::write(&library, b"native runtime").unwrap();
        let manifest = NativeRuntimeManifest {
            runtime: NativeRuntimeArtifact {
                id: "meshllm-runtime-linux-x86_64-cpu".to_string(),
                mesh_version: Some("0.68.0".to_string()),
                skippy_abi: "0.1.25".to_string(),
                platform: NativeRuntimePlatform {
                    os: "linux".to_string(),
                    arch: "x86_64".to_string(),
                    target: None,
                },
                backend: NativeRuntimeBackend::cpu(),
                rank: 0,
                libraries: vec!["lib/libllama.so".to_string()],
                files: Default::default(),
                tools: Default::default(),
                url: None,
                sha256: None,
                signature: None,
            },
        };
        manifest.write_to_dir(temp.path()).unwrap();
        fs::write(&library, b"tampered runtime").unwrap();

        let error = NativeRuntimeManifest::read_from_dir(temp.path()).unwrap_err();

        assert!(error.to_string().contains("checksum mismatch"));
        assert!(error.to_string().contains("lib/libllama.so"));
    }

    #[test]
    fn rejects_tampered_runtime_tool() {
        let temp = tempfile::tempdir().unwrap();
        let library = temp.path().join("lib/libllama.so");
        let tool = temp.path().join("tools/mesh-llm-gpu-benchmark");
        fs::create_dir_all(library.parent().unwrap()).unwrap();
        fs::create_dir_all(tool.parent().unwrap()).unwrap();
        fs::write(&library, b"native runtime").unwrap();
        fs::write(&tool, b"benchmark tool").unwrap();
        let manifest = NativeRuntimeManifest {
            runtime: NativeRuntimeArtifact {
                id: "meshllm-runtime-linux-x86_64-cuda12".to_string(),
                mesh_version: Some("0.68.0".to_string()),
                skippy_abi: "0.1.25".to_string(),
                platform: NativeRuntimePlatform {
                    os: "linux".to_string(),
                    arch: "x86_64".to_string(),
                    target: None,
                },
                backend: NativeRuntimeBackend::cuda(12, vec!["sm_90".to_string()]),
                rank: 0,
                libraries: vec!["lib/libllama.so".to_string()],
                files: BTreeMap::from([(
                    "lib/libllama.so".to_string(),
                    sha256_file(&library).unwrap(),
                )]),
                tools: BTreeMap::from([(
                    "tools/mesh-llm-gpu-benchmark".to_string(),
                    sha256_file(&tool).unwrap(),
                )]),
                url: None,
                sha256: None,
                signature: None,
            },
        };
        manifest.write_to_dir(temp.path()).unwrap();
        fs::write(&tool, b"tampered benchmark tool").unwrap();

        let error = NativeRuntimeManifest::read_from_dir(temp.path()).unwrap_err();

        assert!(error.to_string().contains("checksum mismatch"));
        assert!(error.to_string().contains("tools/mesh-llm-gpu-benchmark"));
    }

    #[test]
    fn checksum_verification_runs_on_a_small_foreign_thread_stack() {
        let file = tempfile::NamedTempFile::new().unwrap();
        fs::write(file.path(), b"native runtime").unwrap();
        let path = file.path().to_path_buf();

        let checksum = std::thread::Builder::new()
            .name("native-runtime-checksum-test".to_string())
            .stack_size(256 * 1024)
            .spawn(move || sha256_file(&path))
            .unwrap()
            .join()
            .unwrap()
            .unwrap();

        assert_eq!(checksum, sha256_file(file.path()).unwrap());
    }

    #[test]
    fn rejects_runtime_manifest_without_file_checksums() {
        let temp = tempfile::tempdir().unwrap();
        fs::create_dir_all(temp.path().join("lib")).unwrap();
        fs::write(temp.path().join("lib/libllama.so"), b"native runtime").unwrap();
        fs::write(
            temp.path().join(NATIVE_RUNTIME_MANIFEST_FILE),
            r#"{
  "runtime": {
    "id": "meshllm-runtime-linux-x86_64-cpu",
    "mesh_version": "0.68.0",
    "skippy_abi": "0.1.25",
    "platform": {"os": "linux", "arch": "x86_64"},
    "backend": {"kind": "cpu"},
    "libraries": ["lib/libllama.so"]
  }
}"#,
        )
        .unwrap();

        let error = NativeRuntimeManifest::read_from_dir(temp.path()).unwrap_err();

        assert!(
            error
                .to_string()
                .contains("does not declare file checksums")
        );
    }

    #[test]
    fn rejects_runtime_checksum_path_traversal() {
        let artifact = NativeRuntimeArtifact {
            id: "meshllm-runtime-linux-x86_64-cpu".to_string(),
            mesh_version: Some("0.68.0".to_string()),
            skippy_abi: "0.1.25".to_string(),
            platform: NativeRuntimePlatform {
                os: "linux".to_string(),
                arch: "x86_64".to_string(),
                target: None,
            },
            backend: NativeRuntimeBackend::cpu(),
            rank: 0,
            libraries: vec!["lib/libllama.so".to_string()],
            files: BTreeMap::from([("../outside".to_string(), "0".repeat(64))]),
            tools: Default::default(),
            url: None,
            sha256: None,
            signature: None,
        };

        let error = validate_artifact(&artifact).unwrap_err();

        assert!(error.to_string().contains("safe relative file path"));
    }
}