Skip to content

Commit 09bb3c0

Browse files
committed
auto merge of #10878 : vadimcn/rust/manifest, r=alexcrichton
In order to avoid UAC installer detection silliness... Closes #10512
2 parents 1b12dca + d4d1310 commit 09bb3c0

File tree

4 files changed

+135
-0
lines changed

4 files changed

+135
-0
lines changed

src/librustc/back/link.rs

+7
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
use back::archive::{Archive, METADATA_FILENAME};
1313
use back::rpath;
14+
use back::manifest;
1415
use driver::driver::CrateTranslation;
1516
use driver::session::Session;
1617
use driver::session;
@@ -800,6 +801,12 @@ fn link_binary_output(sess: Session,
800801
}
801802
session::OutputExecutable => {
802803
link_natively(sess, false, obj_filename, &out_filename);
804+
// Windows linker will add an ".exe" extension if there was none
805+
let out_filename = match out_filename.extension() {
806+
Some(_) => out_filename.clone(),
807+
None => out_filename.with_extension(win32::EXE_EXTENSION)
808+
};
809+
manifest::postprocess_executable(sess, &out_filename);
803810
}
804811
session::OutputDylib => {
805812
link_natively(sess, true, obj_filename, &out_filename);

src/librustc/back/manifest.rs

+108
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
12+
// To avoid problems with Windows UAC installer detection heuristics,
13+
// Rust-produced executables need an application manifest.
14+
// For details, see issue #10512.
15+
16+
// No-op on other platforms.
17+
18+
use driver::session::Session;
19+
use std::path::Path;
20+
21+
#[cfg(not(windows))]
22+
pub fn postprocess_executable(_sess: Session, _filename: &Path) {}
23+
24+
#[cfg(windows)]
25+
pub fn postprocess_executable(sess: Session, filename: &Path) {
26+
27+
let default_manifest = concat!(
28+
"<?xml version='1.0' encoding='UTF-8' standalone='yes'?>",
29+
"<assembly xmlns='urn:schemas-microsoft-com:asm.v1' manifestVersion='1.0'>",
30+
" <trustInfo xmlns='urn:schemas-microsoft-com:asm.v3'>",
31+
" <security>",
32+
" <requestedPrivileges>",
33+
" <requestedExecutionLevel level='asInvoker' uiAccess='false' />",
34+
" </requestedPrivileges>",
35+
" </security>",
36+
" </trustInfo>",
37+
"</assembly>");
38+
39+
match windows::embed_manifest(filename, default_manifest) {
40+
Ok(_) => (),
41+
Err(msg) => sess.err(format!("Could not embed application manifest: {}", msg))
42+
}
43+
}
44+
45+
#[cfg(windows)]
46+
mod windows {
47+
use std::libc::types::os::arch::extra::{BOOL,WORD,DWORD,HANDLE,LPCWSTR,LPCVOID};
48+
use std::libc::consts::os::extra::FALSE;
49+
use std::cast::transmute;
50+
use std::os;
51+
52+
// FIXME #9053: should import as_utf16_p from std rather than re-defining here
53+
//use std::os::win32::as_utf16_p;
54+
fn as_utf16_p<T>(s: &str, f: |*u16| -> T) -> T {
55+
let mut t = s.to_utf16();
56+
// Null terminate before passing on.
57+
t.push(0u16);
58+
t.as_imm_buf(|buf, _len| f(buf))
59+
}
60+
61+
#[link_name = "kernel32"]
62+
extern "system" {
63+
pub fn BeginUpdateResourceW(pFileName: LPCWSTR,
64+
bDeleteExistingResources: BOOL) -> HANDLE;
65+
pub fn UpdateResourceW(hUpdate: HANDLE,
66+
lpType: LPCWSTR,
67+
lpName: LPCWSTR,
68+
wLanguage: WORD,
69+
lpData: LPCVOID,
70+
cbData: DWORD) -> BOOL;
71+
pub fn EndUpdateResourceW(hUpdate: HANDLE,
72+
fDiscard: BOOL) -> BOOL;
73+
}
74+
75+
fn MAKEINTRESOURCEW(id: int) -> LPCWSTR {
76+
unsafe{ transmute(id) }
77+
}
78+
79+
pub fn embed_manifest(filename: &Path,
80+
manifest: &str) -> Result<(),~str> {
81+
unsafe {
82+
let hUpdate = as_utf16_p(filename.as_str().unwrap(), |path| {
83+
BeginUpdateResourceW(path, FALSE)
84+
});
85+
if hUpdate.is_null() {
86+
return Err(format!("failure in BeginUpdateResourceW: {}", os::last_os_error()));
87+
}
88+
89+
let ok = manifest.as_imm_buf(|p, len| {
90+
UpdateResourceW(hUpdate,
91+
MAKEINTRESOURCEW(24), // RT_MANIFEST
92+
MAKEINTRESOURCEW(1), // CREATEPROCESS_MANIFEST_RESOURCE_ID
93+
0, // LANG_NEUTRAL, SUBLANG_NEUTRAL
94+
p as LPCVOID,
95+
len as u32)
96+
});
97+
if ok == FALSE {
98+
return Err(format!("failure in UpdateResourceW: {}", os::last_os_error()));
99+
}
100+
101+
let ok = EndUpdateResourceW(hUpdate, FALSE);
102+
if ok == FALSE {
103+
return Err(format!("failure in EndUpdateResourceW: {}", os::last_os_error()));
104+
}
105+
Ok(())
106+
}
107+
}
108+
}

src/librustc/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ pub mod front {
9393
pub mod back {
9494
pub mod archive;
9595
pub mod link;
96+
pub mod manifest;
9697
pub mod abi;
9798
pub mod upcall;
9899
pub mod arm;

src/test/run-pass/setup.rs

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// On Windows this test checks that Rust binaries have an embedded
12+
// application manifest and don't trip UAC installer detection
13+
// heuristics ("setup" is one of the "installer" keywords).
14+
// For details, see issue #10512.
15+
16+
// On other platforms this is a no-op.
17+
18+
pub fn main() {
19+
}

0 commit comments

Comments
 (0)