From 544ed0328c717cbfb7702e9cdaf587803f2a29f3 Mon Sep 17 00:00:00 2001 From: Vadim Chugunov Date: Sun, 8 Dec 2013 23:56:47 -0800 Subject: [PATCH 1/2] Embed Windows application manifest. --- src/librustc/back/link.rs | 7 +++ src/librustc/back/manifest.rs | 108 ++++++++++++++++++++++++++++++++++ src/librustc/lib.rs | 1 + src/test/run-pass/setup.rs | 19 ++++++ 4 files changed, 135 insertions(+) create mode 100644 src/librustc/back/manifest.rs create mode 100644 src/test/run-pass/setup.rs diff --git a/src/librustc/back/link.rs b/src/librustc/back/link.rs index 786b463c37e..b7e16cf2f38 100644 --- a/src/librustc/back/link.rs +++ b/src/librustc/back/link.rs @@ -11,6 +11,7 @@ use back::archive::{Archive, METADATA_FILENAME}; use back::rpath; +use back::manifest; use driver::driver::CrateTranslation; use driver::session::Session; use driver::session; @@ -800,6 +801,12 @@ fn link_binary_output(sess: Session, } session::OutputExecutable => { link_natively(sess, false, obj_filename, &out_filename); + // Windows linker will add an ".exe" extension if there was none + let out_filename = match out_filename.extension() { + Some(_) => out_filename.clone(), + None => out_filename.with_extension(win32::EXE_EXTENSION) + }; + manifest::postprocess_executable(sess, &out_filename); } session::OutputDylib => { link_natively(sess, true, obj_filename, &out_filename); diff --git a/src/librustc/back/manifest.rs b/src/librustc/back/manifest.rs new file mode 100644 index 00000000000..8f370ab5057 --- /dev/null +++ b/src/librustc/back/manifest.rs @@ -0,0 +1,108 @@ +// Copyright 2013 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + + +// To avoid problems with Windows UAC installer detection heuristics, +// Rust-produced executables need an application manifest. +// For details, see issue #10512. + +// No-op on other platforms. + +use driver::session::Session; +use std::path::Path; + +#[cfg(not(windows))] +pub fn postprocess_executable(sess: Session, filename: &Path) {} + +#[cfg(windows)] +pub fn postprocess_executable(sess: Session, filename: &Path) { + + let default_manifest = concat!( + "", + "", + " ", + " ", + " ", + " ", + " ", + " ", + " ", + ""); + + match windows::embed_manifest(filename, default_manifest) { + Ok(_) => (), + Err(msg) => sess.err(format!("Could not embed application manifest: {}", msg)) + } +} + +#[cfg(windows)] +mod windows { + use std::libc::types::os::arch::extra::{BOOL,WORD,DWORD,HANDLE,LPCWSTR,LPCVOID}; + use std::libc::consts::os::extra::FALSE; + use std::cast::transmute; + use std::os; + + // FIXME #9053: should import as_utf16_p from std rather than re-defining here + //use std::os::win32::as_utf16_p; + fn as_utf16_p(s: &str, f: |*u16| -> T) -> T { + let mut t = s.to_utf16(); + // Null terminate before passing on. + t.push(0u16); + t.as_imm_buf(|buf, _len| f(buf)) + } + + #[link_name = "kernel32"] + extern "system" { + pub fn BeginUpdateResourceW(pFileName: LPCWSTR, + bDeleteExistingResources: BOOL) -> HANDLE; + pub fn UpdateResourceW(hUpdate: HANDLE, + lpType: LPCWSTR, + lpName: LPCWSTR, + wLanguage: WORD, + lpData: LPCVOID, + cbData: DWORD) -> BOOL; + pub fn EndUpdateResourceW(hUpdate: HANDLE, + fDiscard: BOOL) -> BOOL; + } + + fn MAKEINTRESOURCEW(id: int) -> LPCWSTR { + unsafe{ transmute(id) } + } + + pub fn embed_manifest(filename: &Path, + manifest: &str) -> Result<(),~str> { + unsafe { + let hUpdate = as_utf16_p(filename.as_str().unwrap(), |path| { + BeginUpdateResourceW(path, FALSE) + }); + if hUpdate.is_null() { + return Err(format!("failure in BeginUpdateResourceW: {}", os::last_os_error())); + } + + let ok = manifest.as_imm_buf(|p, len| { + UpdateResourceW(hUpdate, + MAKEINTRESOURCEW(24), // RT_MANIFEST + MAKEINTRESOURCEW(1), // CREATEPROCESS_MANIFEST_RESOURCE_ID + 0, // LANG_NEUTRAL, SUBLANG_NEUTRAL + p as LPCVOID, + len as u32) + }); + if ok == FALSE { + return Err(format!("failure in UpdateResourceW: {}", os::last_os_error())); + } + + let ok = EndUpdateResourceW(hUpdate, FALSE); + if ok == FALSE { + return Err(format!("failure in EndUpdateResourceW: {}", os::last_os_error())); + } + Ok(()) + } + } +} diff --git a/src/librustc/lib.rs b/src/librustc/lib.rs index 090329bc4a0..7adb014de67 100644 --- a/src/librustc/lib.rs +++ b/src/librustc/lib.rs @@ -93,6 +93,7 @@ pub mod front { pub mod back { pub mod archive; pub mod link; + pub mod manifest; pub mod abi; pub mod upcall; pub mod arm; diff --git a/src/test/run-pass/setup.rs b/src/test/run-pass/setup.rs new file mode 100644 index 00000000000..12231907940 --- /dev/null +++ b/src/test/run-pass/setup.rs @@ -0,0 +1,19 @@ +// Copyright 2013 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +// On Windows this test checks that Rust binaries have an embedded +// application manifest and don't trip UAC installer detection +// heuristics ("setup" is one of the "installer" keywords). +// For details, see issue #10512. + +// On other platforms this is a no-op. + +pub fn main() { +} From d4d1310c5c5562592069d93683d558d6b8f87236 Mon Sep 17 00:00:00 2001 From: Vadim Chugunov Date: Wed, 11 Dec 2013 18:05:02 -0800 Subject: [PATCH 2/2] Fixed "unused variable" errors and trailing whitespace. --- src/librustc/back/manifest.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/librustc/back/manifest.rs b/src/librustc/back/manifest.rs index 8f370ab5057..1b39935800b 100644 --- a/src/librustc/back/manifest.rs +++ b/src/librustc/back/manifest.rs @@ -19,7 +19,7 @@ use driver::session::Session; use std::path::Path; #[cfg(not(windows))] -pub fn postprocess_executable(sess: Session, filename: &Path) {} +pub fn postprocess_executable(_sess: Session, _filename: &Path) {} #[cfg(windows)] pub fn postprocess_executable(sess: Session, filename: &Path) { @@ -76,7 +76,7 @@ mod windows { unsafe{ transmute(id) } } - pub fn embed_manifest(filename: &Path, + pub fn embed_manifest(filename: &Path, manifest: &str) -> Result<(),~str> { unsafe { let hUpdate = as_utf16_p(filename.as_str().unwrap(), |path| {