mirror of
https://github.com/MacosPorters/betterxen.git
synced 2026-11-26 03:39:52 +00:00
Run CRT byte memory operations through checked guest spans
Signed-off-by: Adeeb Rahman <therealabeeb@icloud.com>
This commit is contained in:
1 parent
e4b1898b00
commit
7134523a08
7 files changed
+109
-44
No files matched your search
@@ -11,6 +11,8 @@ remains unsupported.
|
||||
|
||||
### Added
|
||||
|
||||
- Added bounded guest CRT byte fill, copy, move, and comparison services.
|
||||
|
||||
- Added guest CRT string length services for UCRT and its string API set.
|
||||
|
||||
- Added unnamed guest events with manual and automatic reset, signaling, immediate waits, and same-process handle duplication.
|
||||
|
||||
@@ -205,7 +205,7 @@ impl SymbolDb {
|
||||
fn populate_crt_string(&mut self) {
|
||||
let m = "api-ms-win-crt-string-l1-1-0.dll";
|
||||
for module in [m, "ucrtbase.dll"] {
|
||||
for symbol in ["strlen", "strnlen"] {
|
||||
for symbol in ["strlen", "strnlen", "memset", "memcpy", "memmove", "memcmp"] {
|
||||
self.ins(module, symbol, Classification::StandardService);
|
||||
}
|
||||
}
|
||||
@@ -223,10 +223,6 @@ impl SymbolDb {
|
||||
("tolower", "tolower"),
|
||||
("toupper", "toupper"),
|
||||
("memchr", "memchr"),
|
||||
("memcmp", "memcmp"),
|
||||
("memcpy", "memcpy"),
|
||||
("memmove", "memmove"),
|
||||
("memset", "memset"),
|
||||
("strcat", "strcat"),
|
||||
("strcmp", "strcmp"),
|
||||
("strcpy", "strcpy"),
|
||||
|
||||
@@ -347,41 +347,6 @@ fn symbol_to_ident_lower(sym: &str) -> String {
|
||||
/// Returns a dispatch body for CRT calls with a compatible guest ABI adapter.
|
||||
fn crt_dispatch_expr(symbol: &str) -> Option<String> {
|
||||
Some(match symbol {
|
||||
// memset(dst, val, count) -> dst
|
||||
"memset" => r#"let dst = args.rcx;
|
||||
let val = (args.rdx & 0xFF) as u8;
|
||||
let count = args.r8 as usize;
|
||||
for i in 0..count as u64 {
|
||||
mem.write_u8(dst + i, val).map_err(ExitReason::MemoryFault)?;
|
||||
}
|
||||
Ok(dst)"#
|
||||
.to_string(),
|
||||
// memcpy(dst, src, n) -> dst
|
||||
"memcpy" | "memmove" => r#"let dst = args.rcx;
|
||||
let src = args.rdx;
|
||||
let n = args.r8 as usize;
|
||||
let mut buf = vec![0u8; n];
|
||||
for (i, slot) in buf.iter_mut().enumerate() {
|
||||
*slot = mem.read_u8(src + i as u64).map_err(ExitReason::MemoryFault)?;
|
||||
}
|
||||
for (i, &b) in buf.iter().enumerate() {
|
||||
mem.write_u8(dst + i as u64, b).map_err(ExitReason::MemoryFault)?;
|
||||
}
|
||||
Ok(dst)"#
|
||||
.to_string(),
|
||||
// memcmp(a, b, n) -> int
|
||||
"memcmp" => r#"let a = args.rcx;
|
||||
let b = args.rdx;
|
||||
let n = args.r8 as usize;
|
||||
for i in 0..n as u64 {
|
||||
let ba = mem.read_u8(a + i).map_err(ExitReason::MemoryFault)?;
|
||||
let bb = mem.read_u8(b + i).map_err(ExitReason::MemoryFault)?;
|
||||
if ba != bb {
|
||||
return Ok((ba as i64 - bb as i64) as u64);
|
||||
}
|
||||
}
|
||||
Ok(0)"#
|
||||
.to_string(),
|
||||
// strcmp(a, b) -> int
|
||||
"strcmp" => r#"let a = args.rcx;
|
||||
let b = args.rdx;
|
||||
|
||||
@@ -37,7 +37,7 @@ fn test_builtin_db_case_insensitive() {
|
||||
fn test_manifest_and_codegen_pipeline() {
|
||||
let csv_content = r#""module","symbol_or_ordinal","iat_rva"
|
||||
"api-ms-win-crt-math-l1-1-0.dll","acos",0x1000
|
||||
"api-ms-win-crt-string-l1-1-0.dll","memset",0x1008
|
||||
"api-ms-win-crt-string-l1-1-0.dll","strcmp",0x1008
|
||||
"api-ms-win-core-processthreads-l1-1-0.dll","CreateThread",0x1010
|
||||
"custom.dll","UnknownCustomFunc",0x1018
|
||||
"kernel32.dll","QueryPerformanceCounter",0x1020
|
||||
@@ -68,7 +68,7 @@ fn test_manifest_and_codegen_pipeline() {
|
||||
|
||||
let files = generate(&manifest, &db);
|
||||
assert!(files.crt_rs.contains("register_generated_crt_services"));
|
||||
assert!(files.crt_rs.contains("memset"));
|
||||
assert!(files.crt_rs.contains("strcmp"));
|
||||
let acos = files.crt_rs.split_once("\"acos\"").unwrap().1;
|
||||
let (acos, _) = acos.split_once("registry.register_with_status").unwrap();
|
||||
assert!(acos.contains("ServiceStatus::Stub"));
|
||||
@@ -79,7 +79,7 @@ fn test_manifest_and_codegen_pipeline() {
|
||||
assert!(!files.stubs_rs.contains("GetThreadId"));
|
||||
assert!(!files.stubs_rs.contains("DuplicateHandle"));
|
||||
assert!(!files.stubs_rs.contains("GetTickCount64"));
|
||||
assert!(files.tests_rs.contains("generated_test_memset"));
|
||||
assert!(files.tests_rs.contains("generated_test_strcmp"));
|
||||
|
||||
let out_dir = tmp_dir.join("out");
|
||||
files.write_all(out_dir.to_str().unwrap()).unwrap();
|
||||
|
||||
@@ -28,6 +28,19 @@ fn string_length(memory: &GuestMemory, address: u64, limit: u64) -> Result<u64,
|
||||
Err(ExitReason::IllegalInstruction(0xFE))
|
||||
}
|
||||
|
||||
fn transfer_buffer(count: u64, fill: u8) -> Result<Vec<u8>, ExitReason> {
|
||||
if count > SCAN_BUDGET {
|
||||
return Err(ExitReason::IllegalInstruction(0xFE));
|
||||
}
|
||||
let count = count as usize;
|
||||
let mut buffer = Vec::new();
|
||||
buffer
|
||||
.try_reserve_exact(count)
|
||||
.map_err(|_| ExitReason::MemoryFault(MemoryError::AllocationFailed))?;
|
||||
buffer.resize(count, fill);
|
||||
Ok(buffer)
|
||||
}
|
||||
|
||||
pub(super) fn register(registry: &mut ImportRegistry) {
|
||||
registry.register(UCRT, "strlen", |args, _, memory| {
|
||||
string_length(memory, args.rcx, u64::MAX)
|
||||
@@ -36,7 +49,52 @@ pub(super) fn register(registry: &mut ImportRegistry) {
|
||||
string_length(memory, args.rcx, args.rdx)
|
||||
});
|
||||
|
||||
for symbol in ["strlen", "strnlen"] {
|
||||
registry.register(UCRT, "memset", |args, _, memory| {
|
||||
if args.r8 != 0 {
|
||||
let buffer = transfer_buffer(args.r8, args.rdx as u8)?;
|
||||
memory
|
||||
.write_bytes(args.rcx, &buffer)
|
||||
.map_err(ExitReason::MemoryFault)?;
|
||||
}
|
||||
Ok(args.rcx)
|
||||
});
|
||||
|
||||
for symbol in ["memcpy", "memmove"] {
|
||||
registry.register(UCRT, symbol, |args, _, memory| {
|
||||
if args.r8 != 0 {
|
||||
let mut buffer = transfer_buffer(args.r8, 0)?;
|
||||
memory
|
||||
.read_bytes(args.rdx, &mut buffer)
|
||||
.map_err(ExitReason::MemoryFault)?;
|
||||
memory
|
||||
.write_bytes(args.rcx, &buffer)
|
||||
.map_err(ExitReason::MemoryFault)?;
|
||||
}
|
||||
Ok(args.rcx)
|
||||
});
|
||||
}
|
||||
|
||||
registry.register(UCRT, "memcmp", |args, _, memory| {
|
||||
if args.r8 == 0 {
|
||||
return Ok(0);
|
||||
}
|
||||
let mut left = transfer_buffer(args.r8, 0)?;
|
||||
let mut right = transfer_buffer(args.r8, 0)?;
|
||||
memory
|
||||
.read_bytes(args.rcx, &mut left)
|
||||
.map_err(ExitReason::MemoryFault)?;
|
||||
memory
|
||||
.read_bytes(args.rdx, &mut right)
|
||||
.map_err(ExitReason::MemoryFault)?;
|
||||
let result = left
|
||||
.iter()
|
||||
.zip(right)
|
||||
.find_map(|(&a, b)| (a != b).then_some(i32::from(a) - i32::from(b)))
|
||||
.unwrap_or(0);
|
||||
Ok(u64::from(result as u32))
|
||||
});
|
||||
|
||||
for symbol in ["strlen", "strnlen", "memset", "memcpy", "memmove", "memcmp"] {
|
||||
registry.alias(CRT_STRING_API, UCRT, symbol);
|
||||
registry.set_status(UCRT, symbol, ServiceStatus::Partial);
|
||||
}
|
||||
|
||||
@@ -1022,6 +1022,45 @@ fn test_guest_duplicates_thread_handle_through_iat() {
|
||||
assert_eq!(call("GetThreadId", copy), 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_guest_crt_memory_through_iat() {
|
||||
let state = Arc::new(ServicesState::new(Arc::new(MockClock::new(0))));
|
||||
let slot = PROCESS_FIXTURE_OUTPUT;
|
||||
let call = |symbol, args: &[u64]| {
|
||||
call_service_fixture(&state, "api-ms-win-crt-string-l1-1-0.dll", symbol, args)
|
||||
};
|
||||
let fill = call("memset", &[slot + 2, 0x141, 3]);
|
||||
assert_eq!(fill.execution.final_state.get(Register::Rax), slot + 2);
|
||||
assert_eq!(
|
||||
fill.image.memory.read_u64(slot).unwrap().to_le_bytes(),
|
||||
[255, 255, 65, 65, 65, 255, 255, 255]
|
||||
);
|
||||
|
||||
let copy = call("memcpy", &[slot + 8, slot, 8]);
|
||||
assert_eq!(copy.execution.final_state.get(Register::Rax), slot + 8);
|
||||
assert_eq!(copy.image.memory.read_u64(slot + 8).unwrap(), u64::MAX);
|
||||
|
||||
let shifted = call("memmove", &[slot + 1, slot, 8]);
|
||||
assert_eq!(shifted.image.memory.read_u64(slot + 1).unwrap(), u64::MAX);
|
||||
let shifted = call("memmove", &[slot, slot + 1, 8]);
|
||||
assert_eq!(
|
||||
shifted.image.memory.read_u64(slot).unwrap().to_le_bytes(),
|
||||
[255, 255, 255, 255, 255, 255, 255, 0]
|
||||
);
|
||||
|
||||
for (a, b, expected) in [
|
||||
(slot, slot + 8, 255),
|
||||
(slot + 8, slot, -255),
|
||||
(slot, slot, 0),
|
||||
] {
|
||||
let result = call("memcmp", &[a, b, 8]);
|
||||
assert_eq!(
|
||||
result.execution.final_state.get(Register::Rax) as i32,
|
||||
expected
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_guest_crt_string_length_through_iat() {
|
||||
let state = Arc::new(ServicesState::new(Arc::new(MockClock::new(0))));
|
||||
|
||||
@@ -148,6 +148,11 @@ a null byte or the requested bound for `strnlen`. A scan that would continue
|
||||
past 1 MiB reports an unsupported-service fault. See the
|
||||
[Microsoft CRT string-length contract](https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/strnlen-strnlen-s).
|
||||
|
||||
The same modules expose `memset`, `memcpy`, `memmove`, and `memcmp` for spans
|
||||
up to 1 MiB. Each nonempty span must fit in one mapped guest region with the
|
||||
required permissions. Writes validate the full destination before changing
|
||||
bytes, and moves preserve the original source bytes when ranges overlap.
|
||||
|
||||
### Event signaling
|
||||
|
||||
The synthetic runtime supports unnamed manual-reset and auto-reset events
|
||||
|
||||
Reference in new issue
Block a user