Skip to content

armgit / kong

Public Imported from GitHub
0

The world's first agentic reverse engineer.

update integration tests

Amruth64aafa0673f5
diff --git a/Cargo.lock b/Cargo.lock
index a452ab4..608945b 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -53,6 +53,12 @@ version = "2.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "843867be96c8daad0d758b57df9392b6d8d271134fce549de6ce169ff98a92af"
+[[package]]
+name = "block"
+version = "0.1.6"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "0d8c1fef690941d3e7788d328517591fecc684c084084702d6ff1641e993699a"
+
[[package]]
name = "bytemuck"
version = "1.25.0"
@@ -86,9 +92,11 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "06ccf5ee3532e66868516d9b315f73aec9f34ea1a37ae98514534d458915dbf1"
dependencies = [
"byteorder",
+ "candle-metal-kernels",
"gemm 0.17.1",
"half",
"memmap2",
+ "metal 0.27.0",
"num-traits",
"num_cpus",
"rand",
@@ -97,10 +105,23 @@ dependencies = [
"safetensors",
"thiserror",
"ug",
+ "ug-metal",
"yoke",
"zip",
]
+[[package]]
+name = "candle-metal-kernels"
+version = "0.8.4"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "52c85c21827c28db94e7112e364abe7e0cf8d2b022c014edf08642be6b94f21e"
+dependencies = [
+ "metal 0.27.0",
+ "once_cell",
+ "thiserror",
+ "tracing",
+]
+
[[package]]
name = "candle-nn"
version = "0.8.4"
@@ -108,7 +129,9 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "be1160c3b63f47d40d91110a3e1e1e566ae38edddbbf492a60b40ffc3bc1ff38"
dependencies = [
"candle-core",
+ "candle-metal-kernels",
"half",
+ "metal 0.27.0",
"num-traits",
"rayon",
"safetensors",
@@ -135,12 +158,49 @@ dependencies = [
"tracing",
]
+[[package]]
+name = "cc"
+version = "1.2.41"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "ac9fe6cdbb24b6ade63616c0a0688e45bb56732262c158df3c0c4bea4ca47cb7"
+dependencies = [
+ "find-msvc-tools",
+ "shlex",
+]
+
[[package]]
name = "cfg-if"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9330f8b2ff13f34540b44e946ef35111825727b38d33286ef986142615121801"
+[[package]]
+name = "core-foundation"
+version = "0.9.4"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "91e195e091a93c46f7102ec7818a2aa394e1e1771c3ab4825963fa03e45afb8f"
+dependencies = [
+ "core-foundation-sys",
+ "libc",
+]
+
+[[package]]
+name = "core-foundation-sys"
+version = "0.8.7"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "773648b94d0e5d620f64f280777445740e61fe701025087ec8b57f45c791888b"
+
+[[package]]
+name = "core-graphics-types"
+version = "0.1.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "45390e6114f68f718cc7a830514a96f903cccd70d02a8f6d9f643ac4ba45afaf"
+dependencies = [
+ "bitflags 1.3.2",
+ "core-foundation",
+ "libc",
+]
+
[[package]]
name = "crc32fast"
version = "1.5.0"
@@ -285,6 +345,39 @@ dependencies = [
"regex-syntax",
]
+[[package]]
+name = "find-msvc-tools"
+version = "0.1.4"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "52051878f80a721bb68ebfbc930e07b65ba72f2da88968ea5c06fd6ca3d3a127"
+
+[[package]]
+name = "foreign-types"
+version = "0.5.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "d737d9aa519fb7b749cbc3b962edcf310a8dd1f4b67c91c4f83975dbdd17d965"
+dependencies = [
+ "foreign-types-macros",
+ "foreign-types-shared",
+]
+
+[[package]]
+name = "foreign-types-macros"
+version = "0.2.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "1a5c6c585bc94aaf2c7b51dd4c2ba22680844aba4c687be581871a6f518c5742"
+dependencies = [
+ "proc-macro2",
+ "quote",
+ "syn 2.0.117",
+]
+
+[[package]]
+name = "foreign-types-shared"
+version = "0.3.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "aa9a19cbb55df58761df49b23516a86d432839add4af60fc256da840f66ed35b"
+
[[package]]
name = "gemm"
version = "0.17.1"
@@ -654,6 +747,21 @@ dependencies = [
"libc",
]
+[[package]]
+name = "log"
+version = "0.4.28"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "34080505efa8e45a4b816c349525ebe327ceaa8559756f0356cba97ef3bf7432"
+
+[[package]]
+name = "malloc_buf"
+version = "0.0.6"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "62bb907fe88d54d8d9ce32a3cceab4218ed2f6b7d35617cafe9adf84e43919cb"
+dependencies = [
+ "libc",
+]
+
[[package]]
name = "memchr"
version = "2.8.0"
@@ -670,6 +778,36 @@ dependencies = [
"stable_deref_trait",
]
+[[package]]
+name = "metal"
+version = "0.27.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "c43f73953f8cbe511f021b58f18c3ce1c3d1ae13fe953293e13345bf83217f25"
+dependencies = [
+ "bitflags 2.11.0",
+ "block",
+ "core-graphics-types",
+ "foreign-types",
+ "log",
+ "objc",
+ "paste",
+]
+
+[[package]]
+name = "metal"
+version = "0.29.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "7ecfd3296f8c56b7c1f6fbac3c71cefa9d78ce009850c45000015f206dc7fa21"
+dependencies = [
+ "bitflags 2.11.0",
+ "block",
+ "core-graphics-types",
+ "foreign-types",
+ "log",
+ "objc",
+ "paste",
+]
+
[[package]]
name = "num"
version = "0.4.3"
@@ -777,6 +915,25 @@ dependencies = [
"syn 2.0.117",
]
+[[package]]
+name = "objc"
+version = "0.2.7"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "915b1b472bc21c53464d6c8461c9d3af805ba1ef837e1cac254428f4a77177b1"
+dependencies = [
+ "malloc_buf",
+ "objc_exception",
+]
+
+[[package]]
+name = "objc_exception"
+version = "0.1.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "ad970fb455818ad6cba4c122ad012fae53ae8b4795f86378bce65e4f6bab2ca4"
+dependencies = [
+ "cc",
+]
+
[[package]]
name = "once_cell"
version = "1.21.4"
@@ -1063,6 +1220,12 @@ dependencies = [
"serde",
]
+[[package]]
+name = "shlex"
+version = "1.3.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64"
+
[[package]]
name = "stable_deref_trait"
version = "1.2.1"
@@ -1245,6 +1408,20 @@ dependencies = [
"yoke",
]
+[[package]]
+name = "ug-metal"
+version = "0.1.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "a02ddc17bf32f7dcaaf016b6735f7198082b82f122df7b3ca15d8ead5911ccef"
+dependencies = [
+ "half",
+ "metal 0.29.0",
+ "objc",
+ "serde",
+ "thiserror",
+ "ug",
+]
+
[[package]]
name = "unicode-ident"
version = "1.0.24"
diff --git a/crates/kong-ml/Cargo.toml b/crates/kong-ml/Cargo.toml
index 602155b..5b9b601 100644
--- a/crates/kong-ml/Cargo.toml
+++ b/crates/kong-ml/Cargo.toml
@@ -3,6 +3,10 @@ name = "kong-ml"
version.workspace = true
edition.workspace = true
+[features]
+default = []
+metal = ["candle-core/metal", "candle-nn/metal"]
+
[dependencies]
kong-types = { path = "../kong-types" }
candle-core = "0.8"
diff --git a/crates/kong-ml/src/detector.rs b/crates/kong-ml/src/detector.rs
index cdd217d..73d200a 100644
--- a/crates/kong-ml/src/detector.rs
+++ b/crates/kong-ml/src/detector.rs
@@ -1,3 +1,5 @@
+use std::time::{Duration, Instant};
+
use candle_core::{Device, IndexOp, Result, Tensor};
use kong_types::function::{AmbiguousRegion, DetectionSource, FunctionBoundary};
@@ -37,8 +39,12 @@ impl XdaDetector {
}
}
- /// Predict function boundaries in ambiguous regions.
pub fn predict(&self, regions: &[AmbiguousRegion]) -> Result<Vec<FunctionBoundary>> {
+ self.predict_timed(regions).map(|(boundaries, _)| boundaries)
+ }
+
+ pub fn predict_timed(&self, regions: &[AmbiguousRegion]) -> Result<(Vec<FunctionBoundary>, Duration)> {
+ let start = Instant::now();
let mut boundaries = Vec::new();
for region in regions {
@@ -46,7 +52,7 @@ impl XdaDetector {
boundaries.extend(region_predictions);
}
- Ok(boundaries)
+ Ok((boundaries, start.elapsed()))
}
fn predict_region(&self, region: &AmbiguousRegion) -> Result<Vec<FunctionBoundary>> {
diff --git a/crates/kong-ml/src/lib.rs b/crates/kong-ml/src/lib.rs
index da1bfaf..f22a515 100644
--- a/crates/kong-ml/src/lib.rs
+++ b/crates/kong-ml/src/lib.rs
@@ -2,4 +2,32 @@ pub mod tokenizer;
pub mod config;
pub mod model;
pub mod detector;
-pub mod registry;
\ No newline at end of file
+pub mod registry;
+
+use candle_core::Device;
+
+pub fn preferred_device() -> Device {
+ let device = try_metal_device().unwrap_or(Device::Cpu);
+
+ if std::env::var_os("KONG_ML_LOG_DEVICE").is_some() {
+ let backend = match device.location() {
+ candle_core::DeviceLocation::Metal { gpu_id } => {
+ format!("Metal (gpu_id={gpu_id})")
+ }
+ other => format!("{other:?}")
+ };
+ eprintln!("kong-ml: using {backend} device");
+ }
+
+ device
+}
+
+#[cfg(all(target_os = "macos", feature = "metal"))]
+fn try_metal_device() -> Option<Device> {
+ Device::new_metal(0).ok()
+}
+
+#[cfg(not(all(target_os = "macos", feature = "metal")))]
+fn try_metal_device() -> Option<Device> {
+ None
+}
\ No newline at end of file
diff --git a/crates/kong-ml/tests/xda_integration.rs b/crates/kong-ml/tests/xda_integration.rs
new file mode 100644
index 0000000..27affdd
--- /dev/null
+++ b/crates/kong-ml/tests/xda_integration.rs
@@ -0,0 +1,96 @@
+#[test]
+fn test_full_pipeline() {
+ let model_dir = match std::env::var("XDA_MODEL_DIR") {
+ Ok(dir) => std::path::PathBuf::from(dir),
+ Err(_) => {
+ eprintln!("Skipping integration test: XDA_MODEL_DIR not set");
+ return;
+ }
+ };
+
+ let device = kong_ml::preferred_device();
+ let registry = kong_ml::registry::ModelRegistry::with_cache_dir(model_dir, 0.85);
+
+ let arch = kong_types::binary::Arch::X86_64;
+ assert!(registry.has_model(arch), "x86_64 model not found in model dir");
+
+ let detector = registry
+ .load_detector(arch, &device)
+ .expect("Failed to load model")
+ .expect("Model should exist");
+
+ // 512 bytes from a real x86_64 Ubuntu ELF .text section (libm).
+ // Contains 3 functions with starts at byte positions 64, 192, 352.
+ #[rustfmt::skip]
+ let text_bytes: Vec<u8> = vec![
+ 0xfe, 0xff, 0x03, 0x00, 0x00, 0x41, 0x0f, 0x93, 0xc0, 0x31, 0xc0, 0x44, 0x84, 0xc7, 0x74, 0x2b,
+ 0x81, 0xc6, 0xe1, 0xfb, 0xff, 0xff, 0xb8, 0xff, 0xff, 0xff, 0xff, 0x83, 0xfe, 0xe0, 0x72, 0x1b,
+ 0x48, 0xb8, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x0f, 0x00, 0x48, 0x21, 0xc1, 0x48, 0x01, 0xc8,
+ 0x48, 0x83, 0xc0, 0x01, 0xb1, 0x33, 0x28, 0xd1, 0x48, 0xd3, 0xe8, 0x5d, 0xc3, 0xcc, 0xcc, 0xcc,
+ 0x55, 0x48, 0x89, 0xe5, 0x66, 0x48, 0x0f, 0x7e, 0xc2, 0x48, 0x85, 0xd2, 0x0f, 0x99, 0xc0, 0x48,
+ 0x89, 0xd1, 0x48, 0xc1, 0xe9, 0x34, 0x89, 0xce, 0x81, 0xe6, 0xff, 0x07, 0x00, 0x00, 0x81, 0xfe,
+ 0xff, 0x03, 0x00, 0x00, 0x40, 0x0f, 0x93, 0xc7, 0x40, 0x84, 0xf8, 0x75, 0x04, 0x31, 0xc0, 0x5d,
+ 0xc3, 0x8d, 0xbe, 0xc1, 0xfb, 0xff, 0xff, 0x48, 0xc7, 0xc0, 0xff, 0xff, 0xff, 0xff, 0x83, 0xff,
+ 0xc0, 0x73, 0x02, 0x5d, 0xc3, 0x48, 0xb8, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x0f, 0x00, 0x48,
+ 0x21, 0xc2, 0x48, 0x01, 0xd0, 0x48, 0x83, 0xc0, 0x01, 0x81, 0xfe, 0x32, 0x04, 0x00, 0x00, 0x77,
+ 0x0b, 0xb2, 0x33, 0x28, 0xca, 0x89, 0xd1, 0x48, 0xd3, 0xe8, 0x5d, 0xc3, 0x80, 0xc1, 0x0d, 0x48,
+ 0xd3, 0xe0, 0x5d, 0xc3, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc,
+ 0x55, 0x48, 0x89, 0xe5, 0x66, 0x48, 0x0f, 0x7e, 0xc1, 0x48, 0x85, 0xc9, 0x41, 0x0f, 0x99, 0xc0,
+ 0x48, 0x89, 0xce, 0x48, 0xc1, 0xee, 0x34, 0x89, 0xf7, 0x81, 0xe7, 0xff, 0x07, 0x00, 0x00, 0x81,
+ 0xff, 0xff, 0x03, 0x00, 0x00, 0x41, 0x0f, 0x93, 0xc1, 0x31, 0xc0, 0xba, 0x00, 0x00, 0x00, 0x00,
+ 0x45, 0x84, 0xc8, 0x74, 0x5c, 0x44, 0x8d, 0x87, 0x81, 0xfb, 0xff, 0xff, 0x48, 0xc7, 0xc0, 0xff,
+ 0xff, 0xff, 0xff, 0x48, 0xc7, 0xc2, 0xff, 0xff, 0xff, 0xff, 0x41, 0x83, 0xf8, 0x80, 0x72, 0x41,
+ 0x48, 0xb8, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x0f, 0x00, 0x48, 0x21, 0xc1, 0x48, 0x01, 0xc8,
+ 0x48, 0x83, 0xc0, 0x01, 0x81, 0xff, 0x32, 0x04, 0x00, 0x00, 0x77, 0x0c, 0xb1, 0x33, 0x40, 0x28,
+ 0xf1, 0x48, 0xd3, 0xe8, 0x31, 0xd2, 0x5d, 0xc3, 0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28, 0x45, 0xe0, 0xe8,
+ 0xcc, 0xfe, 0xff, 0xff, 0x48, 0x89, 0x03, 0x89, 0x53, 0x08, 0x48, 0x83, 0xc4, 0x18, 0x5b, 0x5d,
+ ];
+
+ let region = kong_types::function::AmbiguousRegion {
+ start: 0x401000,
+ bytes: text_bytes,
+ reason: kong_types::function::AmbiguityReason::GapRegion,
+ };
+
+ let (boundaries, elapsed) = detector.predict_timed(&[region]).expect("Inference failed");
+
+ println!("Inference completed in {elapsed:.2?}");
+ println!("Found {} function boundaries:", boundaries.len());
+ for b in &boundaries {
+ println!(
+ " 0x{:x} - 0x{:x} (confidence: {:.2}, source: {:?})",
+ b.start, b.end, b.confidence, b.source
+ );
+ }
+
+ assert!(
+ !boundaries.is_empty(),
+ "Model should detect at least one function boundary in real x86_64 code"
+ );
+}
+
+#[test]
+fn test_no_model_graceful() {
+ let tmp = std::env::temp_dir().join("kong_test_empty_models");
+ let _ = std::fs::create_dir_all(&tmp);
+
+ let registry = kong_ml::registry::ModelRegistry::with_cache_dir(tmp, 0.85);
+ let device = kong_ml::preferred_device();
+
+ let detector = registry
+ .load_detector(kong_types::binary::Arch::X86_64, &device)
+ .expect("Should not error");
+
+ assert!(detector.is_none(), "Should return None when no model exists");
+}