Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion decompiler/crates/kuna-cli/src/decompile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1174,7 +1174,7 @@ fn usage() {
(a `0x`-prefixed target implies it); an address inside another function is\n\
decompiled as asked, with a note naming that function. --json emits the\n\
decompile-all record for that one function\n\
({{binary,count,functions:[{{name,address,code,variables,..}}]}});\n\
({{binary,count,functions:[{{name,address,code,error,warnings,variables,..}}]}});\n\
without it, the C text alone. With --json, --omit KEY[,KEY].. leaves those\n\
record collections out (aliases, line_mappings, variables -- with\n\
stack_objects -- and types) and lists them in a top-level `omitted` array;\n\
Expand Down
48 changes: 45 additions & 3 deletions decompiler/crates/kuna-cli/src/decompile_all.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2450,6 +2450,7 @@ fn function_json(
"unstructured_gotos".into(),
Json::Number(f.unstructured_gotos().to_string()),
),
("warnings".into(), Json::Array(f.warnings.iter().map(warning_json).collect())),
]);
if !omit.line_mappings {
record.push((
Expand Down Expand Up @@ -2501,6 +2502,15 @@ fn assertions_json(outcomes: &[kuna_console::assertions::Outcome]) -> Json {
)
}

fn warning_json(warning: &kuna_decomp::kuna_functionwarnings::Warning) -> Json {
Json::Object(vec![
("kind".into(), Json::Str(warning.kind())),
("message".into(), Json::Str(warning.message())),
("line".into(), Json::Number(warning.line.to_string())),
("address".into(), warning.address.map_or(Json::Null, |a| Json::Number(a.to_string()))),
])
}

fn line_mapping_json(mapping: &LineMapping) -> Json {
Json::Object(vec![
("line_number".into(), Json::Number(mapping.line_number.to_string())),
Expand Down Expand Up @@ -2640,6 +2650,7 @@ mod provenance_json_tests {
callee_hints: Vec::new(),
synth: None,
pointerargs: None,
warnings: Vec::new(),
detail: None,
}
}
Expand Down Expand Up @@ -2689,7 +2700,7 @@ mod provenance_json_tests {
let record = doc["functions"][0].as_object().unwrap();
assert!(!record.contains_key("line_mappings") && !record.contains_key("types"));
assert_eq!(record["variables"][0]["line_numbers"], serde_json::json!([3]));
for key in ["name", "address", "code", "error", "unstructured_gotos"] {
for key in ["name", "address", "code", "error", "unstructured_gotos", "warnings"] {
assert!(record.contains_key(key), "{key} must survive --omit");
}
assert_eq!(doc["language"], "c-language");
Expand All @@ -2703,6 +2714,33 @@ mod provenance_json_tests {
assert!(!doc["functions"][0].as_object().unwrap().contains_key("variables"));
}

#[test]
fn a_record_lists_its_warnings_and_a_failed_one_none() {
use kuna_decomp::kuna_functionwarnings::Warning;
let warned = FuncResult {
warnings: vec![
Warning { address: Some(0x401000), text: "WARNING: Subroutine does not return".into(), line: 1 },
Warning { address: Some(0x401010), text: "WARNING: Function flows out of bounds".into(), line: 4 },
Warning { address: None, text: "WARNING: structured blocks unavailable".into(), line: 5 },
],
..function()
};
let failed = FuncResult { code: None, error: Some("boom".into()), ..function() };
let text = dumps_indent2(&result_json(
"fixture", &[warned, failed], "c-language", None, None, &[], &Default::default(), Omit::default(),
));
let doc: serde_json::Value = serde_json::from_str(&text).unwrap();
assert_eq!(
doc["functions"][0]["warnings"],
serde_json::json!([
{"kind": "no-return", "message": "Subroutine does not return", "line": 1, "address": 0x401000},
{"kind": "out-of-bounds", "message": "Function flows out of bounds", "line": 4, "address": 0x401010},
{"kind": "structuring-declined", "message": "structured blocks unavailable", "line": 5, "address": null},
])
);
assert_eq!(doc["functions"][1]["warnings"], serde_json::json!([]));
}

#[test]
fn omitted_aliases_leave_the_record_and_keep_its_identity() {
let aliased = FuncResult { aliases: vec!["sub_401000".into()], ..function() };
Expand Down Expand Up @@ -3223,8 +3261,12 @@ fn usage_decompile_all() {
\x20 [--raw-image --target T --base VMA (--entry|--addr VMA)..]\n\
\n\
Decompile every CODE-backed function in one in-process load (load-once,\n\
decompile-many). --json emits {{binary,count,functions:[{{name,address,code,variables,..}}]}};\n\
without it, concatenated C with `// Function:` headers.\n\
decompile-many). --json emits {{binary,count,functions:[{{name,address,code,error,warnings,variables,..}}]}};\n\
without it, concatenated C with `// Function:` headers. A record's\n\
`warnings` lists the engine warnings its code carries as comments, as\n\
{{kind,message,line,address}}, each at the instruction it was recorded\n\
at (a header warning at the entry); `error` is set only for a failed\n\
function.\n\
--omit KEY[,KEY].. (with --json) leaves those record collections out of\n\
every record -- aliases, line_mappings, variables (with stack_objects),\n\
types -- and lists them in a top-level `omitted` array, so an omitted\n\
Expand Down
7 changes: 6 additions & 1 deletion decompiler/crates/kuna-cli/src/decompile_all/result_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,12 +153,16 @@ mod tests {
callee_hints: Vec::new(),
synth: None,
pointerargs: None,
warnings: Vec::new(),
detail: None,
}
}

fn check(mut store: ResultStore) {
store.put(2, result("c", Some("int c(void)\n{\n}")));
let warning = kuna_decomp::kuna_functionwarnings::Warning {
address: Some(0x1004), text: "WARNING: Bad instruction".into(), line: 2,
};
store.put(2, FuncResult { warnings: vec![warning.clone()], ..result("c", Some("int c(void)\n{\n}")) });
store.put(0, result("a", None));
store.put(0, result("a2", Some("void a(void)")));
let mut tokens = result("d", Some("void d(void)"));
Expand All @@ -171,6 +175,7 @@ mod tests {
assert_eq!(all.iter().map(|r| r.name.as_str()).collect::<Vec<_>>(), ["a2", "c", "d"]);
assert_eq!(all[1].code.as_deref(), Some("int c(void)\n{\n}"));
assert_eq!(all[1].aliases, ["alias"]);
assert_eq!(all[1].warnings, [warning]);
assert!(all[2].detail.is_some());
}

Expand Down
93 changes: 91 additions & 2 deletions decompiler/crates/kuna-cli/src/jobs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1646,6 +1646,7 @@ fn lost_result(t: &TargetSpec, reason: &str) -> FuncResult {
callee_hints: Vec::new(),
synth: None,
pointerargs: None,
warnings: Vec::new(),
detail: None,
}
}
Expand Down Expand Up @@ -2162,6 +2163,7 @@ mod tests {
callee_hints: vec![0x401200, 0x401340, 0xffff_ffff_ffff_fff0],
synth: None,
pointerargs: None,
warnings: Vec::new(),
detail: None,
}
}
Expand All @@ -2182,6 +2184,7 @@ mod tests {
&& a.callee_hints == b.callee_hints
&& a.field_calls == b.field_calls
&& a.pointerargs == b.pointerargs
&& a.warnings == b.warnings
&& a.synth == b.synth
&& a.line_mappings == b.line_mappings
&& a.globals == b.globals
Expand Down Expand Up @@ -2240,6 +2243,14 @@ mod tests {
expression: "&v1".into(),
}],
});
let warning = |address, text: &str, line| {
kuna_decomp::kuna_functionwarnings::Warning { address, text: text.into(), line }
};
a.warnings = vec![
warning(Some(0x401000), "WARNING: Subroutine does not return", 2),
warning(Some(0x401234), "WARNING: Bad instruction", 7),
warning(None, "WARNING: structured blocks unavailable", 5),
];
let b = FuncResult {
name: "sub_1234".into(),
address: 0x1234,
Expand All @@ -2262,6 +2273,7 @@ mod tests {
callee_hints: Vec::new(),
synth: None,
pointerargs: None,
warnings: Vec::new(),
detail: None,
};
{
Expand Down Expand Up @@ -2292,7 +2304,7 @@ mod tests {
}

#[test]
fn result_encoding_matches_the_version_eleven_wire_layout() {
fn result_encoding_matches_the_version_twelve_wire_layout() {
let dir = ScratchDir::create().unwrap();
let path = dir.path().join("wire.bin").to_string_lossy().into_owned();
let result = FuncResult {
Expand All @@ -2317,10 +2329,11 @@ mod tests {
callee_hints: Vec::new(),
synth: None,
pointerargs: None,
warnings: Vec::new(),
detail: None,
};
let expected = [
b"KUNAJOBRES11\x01\x51\0\0\0".as_slice(),
b"KUNAJOBRES12\x01\x55\0\0\0".as_slice(),
b"\x10\0\0\0\0\0\0\0\x20\0\0\0\0\0\0\0",
b"\xff\xff\xff\xff\xff\xff\xff\xff",
b"\x01\0\0\0f\0\x01\x01\0\0\0e\0\0",
Expand All @@ -2334,6 +2347,7 @@ mod tests {
b"\0\0\0\0", // field calls
b"\0\0\0\0", // callee hints
b"\0\0", // synth and pointer arguments
b"\0\0\0\0", // warnings
b"\0\0\0\0", // call declarations
b"\0\0\0\0", // callwide prototypes
]
Expand Down Expand Up @@ -2365,6 +2379,80 @@ mod tests {
assert!(decode_results(&std::fs::read(&path).unwrap()).unwrap().results.is_empty());
}

#[test]
fn a_malformed_warnings_block_does_not_cross_the_worker_boundary() {
use kuna_decomp::kuna_functionwarnings::Warning;
let at = Warning {
address: Some(0x8877_6655_4433_2211),
text: "WARNING: Bad instruction".into(),
line: 0x0a09_0807_0605_0403,
};
let shell = Warning {
address: None,
text: "WARNING: blocks \u{e9}".into(),
line: 0x1c1b_1a19_1817_1615,
};
let at_bytes = [
b"\x01\x11\x22\x33\x44\x55\x66\x77\x88".as_slice(),
b"\x18\0\0\0WARNING: Bad instruction",
b"\x03\x04\x05\x06\x07\x08\x09\x0a",
]
.concat();
let shell_bytes =
[b"\0\x12\0\0\0WARNING: blocks \xc3\xa9".as_slice(), b"\x15\x16\x17\x18\x19\x1a\x1b\x1c"].concat();
let pointerargs = kuna_decomp::kuna_pointerargs::Record {
entry: ("ram".into(), 0x401000),
parameters: vec![Some(kuna_decomp::kuna_pointerargs::Parameter {
storage: (("register".into(), 0x38), 8),
spelling: "unsigned char *".into(),
})],
calls: Vec::new(),
};
for (before, warnings, records) in [
(None, vec![at.clone(), shell.clone()], [at_bytes.as_slice(), &shell_bytes]),
(Some(pointerargs), vec![shell.clone(), at.clone()], [shell_bytes.as_slice(), &at_bytes]),
] {
let mut record = sample_result();
record.pointerargs = before;
record.warnings = warnings;
let body = encode_result(&record);
let after = record.call_declarations[0].as_bytes();
let end = body.windows(after.len()).position(|w| w == after).unwrap() - 8;
let block = [b"\x02\0\0\0".as_slice(), records[0], records[1]].concat();
let start = end - block.len();
assert_eq!(body[start..end], block[..]);
assert_eq!(body[end..end + 8], *b"\x01\0\0\0\x1e\0\0\0");
let decoded = decode_result(&body).unwrap();
assert!(same(&decoded, &record));
assert_eq!(decoded.pointerargs, record.pointerargs);
assert_eq!(decoded.call_declarations, record.call_declarations);

let patched = |offset: usize, bytes: &[u8]| {
let mut bad = body.clone();
bad[offset..offset + bytes.len()].copy_from_slice(bytes);
decode_result(&bad)
};
for count in [0u32, 1, 3, u32::MAX] {
assert!(patched(start, &count.to_le_bytes()).is_none(), "count {count}");
}
let mut tag = start + 4;
for bytes in records {
for invalid in [2u8, 0xff] {
assert!(patched(tag, &[invalid]).is_none(), "address tag {invalid}");
}
let length = tag + if bytes[0] == 1 { 9 } else { 1 };
for oversized in [(body.len() - length - 3) as u32, u32::MAX] {
assert!(patched(length, &oversized.to_le_bytes()).is_none(), "text length {oversized}");
}
assert!(patched(bytes.len() + tag - 9, &[0xff]).is_none(), "text that is not UTF-8");
tag += bytes.len();
}
for cut in start..end {
assert!(decode_result(&body[..cut]).is_none(), "cut at {cut}");
}
}
}

/// The dynamic source is the only thing `--stream` changes about the pool:
/// every chunk it hands out is served once, and a drained source ends the
/// thread's loop.
Expand Down Expand Up @@ -2793,6 +2881,7 @@ mod tests {
callee_hints: Vec::new(),
synth: None,
pointerargs: None,
warnings: Vec::new(),
detail: None,
};
let results = vec![
Expand Down
32 changes: 31 additions & 1 deletion decompiler/crates/kuna-cli/src/jobs/wire.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use kuna_decomp::kuna_structsynth::shard::FunctionRecord;
use super::TargetSpec;

const SPEC_MAGIC: &[u8; 12] = b"KUNAJOBSPEC3";
pub(super) const RESULT_MAGIC: &[u8; 12] = b"KUNAJOBRES11";
pub(super) const RESULT_MAGIC: &[u8; 12] = b"KUNAJOBRES12";

/// Result-stream frame kinds. The envelope is what lets a truncated tail be
/// dropped rather than guessed.
Expand Down Expand Up @@ -389,6 +389,7 @@ pub(crate) fn encode_result(r: &FuncResult) -> Vec<u8> {
None => body.push(0),
}
put_pointerargs(&mut body, r.pointerargs.as_ref());
put_warnings(&mut body, &r.warnings);
put_u32(&mut body, r.call_declarations.len() as u32);
for declaration in &r.call_declarations { put_str(&mut body, declaration); }
put_u32(&mut body, r.callwide_prototypes.len() as u32);
Expand Down Expand Up @@ -574,6 +575,7 @@ pub(crate) fn decode_result(body: &[u8]) -> Option<FuncResult> {
_ => return None,
};
let pointerargs = read_pointerargs(&mut r)?;
let warnings = read_warnings(&mut r)?;
let count = r.u32()? as usize;
let mut call_declarations = r.sized(count);
for _ in 0..count { call_declarations.push(r.string()?); }
Expand Down Expand Up @@ -618,6 +620,7 @@ pub(crate) fn decode_result(body: &[u8]) -> Option<FuncResult> {
callee_hints,
synth,
pointerargs,
warnings,
detail: None,
})
}
Expand Down Expand Up @@ -659,6 +662,33 @@ fn put_pointerargs(out: &mut Vec<u8>, record: Option<&kuna_decomp::kuna_pointera
}
}

fn put_warnings(out: &mut Vec<u8>, warnings: &[kuna_decomp::kuna_functionwarnings::Warning]) {
put_u32(out, warnings.len() as u32);
for warning in warnings {
match warning.address {
Some(address) => {
out.push(1);
put_u64(out, address);
}
None => out.push(0),
}
put_str(out, &warning.text);
put_u64(out, warning.line as u64);
}
}

fn read_warnings(r: &mut Reader<'_>) -> Option<Vec<kuna_decomp::kuna_functionwarnings::Warning>> {
let n = r.u32()? as usize;
let mut warnings = r.sized(n);
for _ in 0..n {
let address = match r.u8()? { 0 => None, 1 => Some(r.u64()?), _ => return None };
let text = r.string()?;
let line = r.u64()?.try_into().ok()?;
warnings.push(kuna_decomp::kuna_functionwarnings::Warning { address, text, line });
}
Some(warnings)
}

fn read_entry(r: &mut Reader<'_>) -> Option<kuna_decomp::kuna_pointerargs::Entry> {
Some((r.string()?, r.u64()?))
}
Expand Down
1 change: 1 addition & 0 deletions decompiler/crates/kuna-cli/src/project_stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1358,6 +1358,7 @@ mod tests {
callee_hints: hints,
synth: None,
pointerargs: None,
warnings: Vec::new(),
detail: None,
}
}
Expand Down
Loading
Loading