Skip to content
Merged
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
16 changes: 15 additions & 1 deletion crates/xcl/src/loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,8 @@ unsafe fn run_tls_callbacks(m: &MappedModule, reason: u32) {
pub fn init_dll(m: &Arc<MappedModule>) -> Result<(), String> {
unsafe {
run_tls_callbacks(m, 1);
if m.entry_rva != 0 {
// an executable loaded as a library: Windows' loader does not call its entry point
if m.is_dll && m.entry_rva != 0 {
let f: DllMain = std::mem::transmute(m.base + m.entry_rva as usize);
crate::xlog_info!("DllMain({}, PROCESS_ATTACH)", m.name);
if f(m.base as *mut c_void, 1, std::ptr::null_mut()) == 0 {
Expand Down Expand Up @@ -503,6 +504,19 @@ mod tests {
assert_eq!(load_game_dll(&p).unwrap().base, m.base);
}

/// An executable loaded as a library is mapped, its entry point is not called as a DllMain
/// (before: its mainCRTStartup ran with DllMain's arguments; here it returns 0, the load failed)
#[test]
fn an_exe_loaded_as_a_library_does_not_run_its_entry_point() {
// xor eax, eax; ret
let mut img = tiny_dll(0x7E54_0000_0000, &[0x31, 0xC0, 0xC3], None);
img[0x56..0x58].copy_from_slice(&0x0022u16.to_le_bytes()); // executable | large address aware, not a DLL
let p = write("xcl_exe_as_library.exe", &img);
let m = load_game_dll(&p).expect("an EXE loads as a library");
assert!(!m.is_dll);
assert_eq!(load_game_dll(&p).unwrap().base, m.base);
}

/// An import table that does not parse: the load fails and the module – whose memory is
/// freed – is not found afterwards (before: still registered over freed memory, and a second
/// load returned it).
Expand Down
Loading