From b00321aa1e1b0fd2cd86b5ebe90d64fc3541f225 Mon Sep 17 00:00:00 2001 From: Adeeb Rahman Date: Tue, 24 Nov 2026 15:35:46 -0500 Subject: [PATCH] Implement caller-owned UCRT wide tokenization Signed-off-by: Adeeb Rahman --- crates/import-gen/src/classify.rs | 4 +- crates/import-gen/src/metadata.rs | 45 +++++++++------------ crates/loader/src/services/crt.rs | 53 ++++++++++++++++--------- crates/loader/tests/runtime_services.rs | 28 +++++++++++++ 4 files changed, 83 insertions(+), 47 deletions(-) diff --git a/crates/import-gen/src/classify.rs b/crates/import-gen/src/classify.rs index 45268a3..989f19b 100644 --- a/crates/import-gen/src/classify.rs +++ b/crates/import-gen/src/classify.rs @@ -217,8 +217,8 @@ impl SymbolDb { "strlen", "strnlen", "strcmp", "strncmp", "wcslen", "wcsnlen", "wcscmp", "wcsncmp", "memset", "memcpy", "memmove", "memcmp", "memchr", "strcpy", "strncpy", "strcat", "strncat", "strstr", "wcsstr", "strspn", "strcspn", "strtok", "strtok_s", - "strpbrk", "wcsspn", "wcscspn", "wcspbrk", "wcstok", "wcscpy", "wcsncpy", "wcscat", - "wcschr", "wcsrchr", "isalnum", "isalpha", "isdigit", "islower", "isspace", + "strpbrk", "wcsspn", "wcscspn", "wcspbrk", "wcstok", "wcstok_s", "wcscpy", "wcsncpy", + "wcscat", "wcschr", "wcsrchr", "isalnum", "isalpha", "isdigit", "islower", "isspace", "isxdigit", "isupper", "isprint", "iscntrl", "ispunct", "tolower", "toupper", ] { self.ins(module, symbol, Classification::StandardService); diff --git a/crates/import-gen/src/metadata.rs b/crates/import-gen/src/metadata.rs index 3531601..547b6f0 100644 --- a/crates/import-gen/src/metadata.rs +++ b/crates/import-gen/src/metadata.rs @@ -257,33 +257,6 @@ impl MetadataCatalog { "", "Compares up to count 16-bit Windows code units of two strings", ); - self.add( - "ucrtbase.dll", - "strtok_s", - AbiType::Pointer { - is_const: false, - pointee: "char".into(), - }, - vec![ - ( - "str", - AbiType::Pointer { - is_const: false, - pointee: "char".into(), - }, - ), - ("delimiters", AbiType::StringAnsi), - ( - "context", - AbiType::Pointer { - is_const: false, - pointee: "char *".into(), - }, - ), - ], - "", - "Splits writable guest text using a caller-owned continuation pointer", - ); for (character, string, header, tokenize, search, find_any, span, complement) in [ ( "char", @@ -328,6 +301,24 @@ impl MetadataCatalog { header, "Splits writable guest text into tokens", ); + self.add( + "ucrtbase.dll", + &format!("{tokenize}_s"), + pointer.clone(), + vec![ + ("str", pointer.clone()), + ("delimiters", string.clone()), + ( + "context", + AbiType::Pointer { + is_const: false, + pointee: format!("{character} *"), + }, + ), + ], + header, + "Splits writable guest text using a caller-owned continuation pointer", + ); for (symbol, argument, description) in [ (search, "needle", "Finds the first matching substring"), ( diff --git a/crates/loader/src/services/crt.rs b/crates/loader/src/services/crt.rs index f8c98f1..4074498 100644 --- a/crates/loader/src/services/crt.rs +++ b/crates/loader/src/services/crt.rs @@ -281,9 +281,11 @@ pub(super) fn register(registry: &mut ImportRegistry) { registry.register(UCRT, "strtok", |args, cpu, memory| { tokenize(memory, cpu.gs_base, args.rcx, args.rdx) }); - registry.register(UCRT, "strtok_s", |args, cpu, memory| { - tokenize_s(memory, cpu.gs_base, args.rcx, args.rdx, args.r8) - }); + for (symbol, unit) in [("strtok_s", StringUnit::Byte), ("wcstok_s", StringUnit::Wide)] { + registry.register(UCRT, symbol, move |args, cpu, memory| { + tokenize_s(memory, cpu.gs_base, args.rcx, args.rdx, args.r8, unit) + }); + } registry.register(UCRT, "wcstok", |args, _, memory| { tokenize_wide(memory, args.rcx, args.rdx, args.r8) }); @@ -305,7 +307,7 @@ pub(super) fn register(registry: &mut ImportRegistry) { for symbol in [ "memchr", "strcpy", "strncpy", "strcat", "strncat", "strstr", "wcsstr", "strspn", "strcspn", "wcscpy", "wcsncpy", "wcscat", "wcschr", "wcsrchr", "strtok", "strpbrk", - "wcsspn", "wcscspn", "wcspbrk", "wcstok", "strtok_s", + "wcsspn", "wcscspn", "wcspbrk", "wcstok", "strtok_s", "wcstok_s", ] { registry.alias(CRT_STRING_API, UCRT, symbol); registry.set_status(UCRT, symbol, ServiceStatus::Partial); @@ -591,6 +593,7 @@ fn tokenize_s( input: u64, delimiters: u64, context: u64, + unit: StringUnit, ) -> Result { if delimiters == 0 || context == 0 { set_errno(memory, teb, EINVAL)?; @@ -605,19 +608,30 @@ fn tokenize_s( set_errno(memory, teb, EINVAL)?; return Ok(0); } - let present = byte_set(memory, delimiters)?; - let (token, position, split) = - locate_token(memory, cursor, StringUnit::Byte, |byte| present[byte])?; + let (token, position, split) = match unit { + StringUnit::Byte => { + let present = byte_set(memory, delimiters)?; + locate_token(memory, cursor, unit, |byte| present[byte])? + } + StringUnit::Wide => { + let present = wide_set(memory, delimiters)?; + locate_token(memory, cursor, unit, |value| { + present[value / 64] & (1 << (value % 64)) != 0 + })? + } + }; memory .check_write(context, 8) .map_err(ExitReason::MemoryFault)?; let next = if split { let next = position - .checked_add(1) + .checked_add(unit.width()) .ok_or(ExitReason::MemoryFault(MemoryError::Overflow))?; - memory - .write_u8(position, 0) - .map_err(ExitReason::MemoryFault)?; + match unit { + StringUnit::Byte => memory.write_u8(position, 0), + StringUnit::Wide => memory.write_u16(position, 0), + } + .map_err(ExitReason::MemoryFault)?; next } else { position @@ -1260,30 +1274,33 @@ fn strtok_s_keeps_context_and_errno_separate() { memory.write_bytes(0x1000, b",a,b;c\0").unwrap(); memory.write_bytes(0x1010, b"x,y\0").unwrap(); memory.write_bytes(0x1020, b",\0;\0").unwrap(); + let call = |memory: &mut GuestMemory, input, delimiters, context| { + tokenize_s(memory, 0, input, delimiters, context, StringUnit::Byte).unwrap() + }; let errno = memory.crt_errno_address(0).unwrap(); - assert_eq!(tokenize_s(&mut memory, 0, 0, 0x1020, 0x1040).unwrap(), 0); + assert_eq!(call(&mut memory, 0, 0x1020, 0x1040), 0); assert_eq!(memory.read_u32(errno).unwrap(), EINVAL); memory.write_u32(errno, 0).unwrap(); assert_eq!( - tokenize_s(&mut memory, 0, 0x1000, 0x1020, 0x1040).unwrap(), + call(&mut memory, 0x1000, 0x1020, 0x1040), 0x1001 ); assert_eq!( - tokenize_s(&mut memory, 0, 0x1010, 0x1020, 0x1048).unwrap(), + call(&mut memory, 0x1010, 0x1020, 0x1048), 0x1010 ); assert_eq!( - tokenize_s(&mut memory, 0, 0, 0x1022, 0x1040).unwrap(), + call(&mut memory, 0, 0x1022, 0x1040), 0x1003 ); assert_eq!( - tokenize_s(&mut memory, 0, 0, 0x1022, 0x1040).unwrap(), + call(&mut memory, 0, 0x1022, 0x1040), 0x1005 ); assert_eq!(memory.read_u64(0x1040).unwrap(), 0x1006); - assert_eq!(tokenize_s(&mut memory, 0, 0, 0x1022, 0x1040).unwrap(), 0); + assert_eq!(call(&mut memory, 0, 0x1022, 0x1040), 0); assert_eq!( - tokenize_s(&mut memory, 0, 0, 0x1020, 0x1048).unwrap(), + call(&mut memory, 0, 0x1020, 0x1048), 0x1012 ); assert_eq!(memory.read_u32(errno).unwrap(), 0); diff --git a/crates/loader/tests/runtime_services.rs b/crates/loader/tests/runtime_services.rs index da74a42..2f23497 100644 --- a/crates/loader/tests/runtime_services.rs +++ b/crates/loader/tests/runtime_services.rs @@ -376,6 +376,34 @@ fn call_tokenization_import( cpu.get(Register::Rax) } +#[test] +fn guest_wcstok_s_resumes_in_wide_units_and_sets_errno() { + let (mut engine, mut memory) = tokenization_guest("wcstok_s"); + let input = SCRATCH; + let delimiters = SCRATCH + 32; + let context = SCRATCH + 64; + for (index, value) in [0x2603, 0x03bb, 0x2603, 0x03bc, 0].into_iter().enumerate() { + memory.write_u16(input + 2 * index as u64, value).unwrap(); + } + memory.write_u16(delimiters, 0x2603).unwrap(); + + let mut call = |memory: &mut GuestMemory, slot, args| { + call_tokenization_import(&mut engine, memory, slot, args) + }; + let errno = call(&mut memory, 2, [0; 3]); + assert_eq!(call(&mut memory, 1, [0, delimiters, context]), 0); + assert_eq!(memory.read_u32(errno).unwrap(), 22); + memory.write_u32(errno, 0).unwrap(); + + assert_eq!(call(&mut memory, 0, [input, delimiters, context]), input + 2); + assert_eq!(memory.read_u16(input + 4).unwrap(), 0); + assert_eq!(memory.read_u64(context).unwrap(), input + 6); + assert_eq!(call(&mut memory, 1, [0, delimiters, context]), input + 6); + assert_eq!(call(&mut memory, 1, [0, delimiters, context]), 0); + assert_eq!(memory.read_u64(context).unwrap(), input + 8); + assert_eq!(memory.read_u32(errno).unwrap(), 0); +} + #[test] fn guest_wcstok_resumes_from_its_win64_context_argument() { let (mut engine, mut memory) = tokenization_guest("wcstok");