-
Notifications
You must be signed in to change notification settings - Fork 12.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a "link-guard" to avoid accidentally linking to a wrong dylib at …
…runtime. We want to prevent compiling something against one version of a dynamic library and then, at runtime accidentally using a different version of the dynamic library. With the old symbol-naming scheme this could not happen because every symbol had the SVH in it and you'd get an error by the dynamic linker when using the wrong version of a dylib. With the new naming scheme this isn't the case any more, so this patch adds the "link-guard" to prevent this error case. This is implemented as follows: - In every crate that we compile, we emit a function called "__rustc_link_guard_<crate-name>_<crate-svh>" - The body of this function contains calls to the "__rustc_link_guard" functions of all dependencies. - An executable contains a call to it's own "__rustc_link_guard" function. As a consequence the "__rustc_link_guard" function call graph mirrors the crate graph and the dynamic linker will fail if a wrong dylib is loaded somewhere because its "__rustc_link_guard" function will contain a different SVH in its name.
- Loading branch information
1 parent
00c206f
commit f6b0f17
Showing
14 changed files
with
241 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
// Copyright 2012-2016 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 <LICENSE-APACHE or | ||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | ||
// option. This file may not be copied, modified, or distributed | ||
// except according to those terms. | ||
|
||
use back::svh::Svh; | ||
use libc::c_uint; | ||
use llvm; | ||
use std::ffi::CString; | ||
use std::ptr; | ||
use trans::attributes; | ||
use trans::builder; | ||
use trans::CrateContext; | ||
use trans::declare; | ||
use trans::type_::Type; | ||
|
||
const GUARD_PREFIX: &'static str = "__rustc_link_guard_"; | ||
|
||
pub fn link_guard_name(crate_name: &str, crate_svh: &Svh) -> String { | ||
|
||
let mut guard_name = String::new(); | ||
|
||
guard_name.push_str(GUARD_PREFIX); | ||
guard_name.push_str(crate_name); | ||
guard_name.push_str("_"); | ||
guard_name.push_str(crate_svh.as_str()); | ||
|
||
guard_name | ||
} | ||
|
||
pub fn get_or_insert_link_guard<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>) | ||
-> llvm::ValueRef { | ||
|
||
let guard_name = link_guard_name(&ccx.tcx().crate_name[..], | ||
&ccx.link_meta().crate_hash); | ||
|
||
let guard_function = unsafe { | ||
let guard_name_c_string = CString::new(&guard_name[..]).unwrap(); | ||
llvm::LLVMGetNamedValue(ccx.llmod(), guard_name_c_string.as_ptr()) | ||
}; | ||
|
||
if guard_function != ptr::null_mut() { | ||
return guard_function; | ||
} | ||
|
||
let llfty = Type::func(&[], &Type::void(ccx)); | ||
let guard_function = declare::define_cfn(ccx, | ||
&guard_name[..], | ||
llfty, | ||
ccx.tcx().mk_nil()).unwrap_or_else(|| { | ||
ccx.sess().bug("Link guard already defined."); | ||
}); | ||
|
||
attributes::emit_uwtable(guard_function, true); | ||
attributes::unwind(guard_function, false); | ||
|
||
let bld = ccx.raw_builder(); | ||
unsafe { | ||
let llbb = llvm::LLVMAppendBasicBlockInContext(ccx.llcx(), | ||
guard_function, | ||
"link_guard_top\0".as_ptr() as *const _); | ||
llvm::LLVMPositionBuilderAtEnd(bld, llbb); | ||
|
||
for crate_num in ccx.sess().cstore.crates() { | ||
if !ccx.sess().cstore.is_explicitly_linked(crate_num) { | ||
continue; | ||
} | ||
|
||
let crate_name = ccx.sess().cstore.original_crate_name(crate_num); | ||
let svh = ccx.sess().cstore.crate_hash(crate_num); | ||
|
||
let dependency_guard_name = link_guard_name(&crate_name[..], &svh); | ||
|
||
let decl = declare::declare_cfn(ccx, | ||
&dependency_guard_name[..], | ||
llfty, | ||
ccx.tcx().mk_nil()); | ||
attributes::unwind(decl, false); | ||
|
||
llvm::LLVMPositionBuilderAtEnd(bld, llbb); | ||
|
||
let args: &[llvm::ValueRef] = &[]; | ||
llvm::LLVMRustBuildCall(bld, | ||
decl, | ||
args.as_ptr(), | ||
args.len() as c_uint, | ||
0 as *mut _, | ||
builder::noname()); | ||
} | ||
|
||
llvm::LLVMBuildRetVoid(bld); | ||
} | ||
|
||
guard_function | ||
} | ||
|
||
pub fn insert_reference_to_link_guard<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, | ||
llbb: llvm::BasicBlockRef) { | ||
let guard_function = get_or_insert_link_guard(ccx); | ||
|
||
unsafe { | ||
llvm::LLVMPositionBuilderAtEnd(ccx.raw_builder(), llbb); | ||
let args: &[llvm::ValueRef] = &[]; | ||
llvm::LLVMRustBuildCall(ccx.raw_builder(), | ||
guard_function, | ||
args.as_ptr(), | ||
args.len() as c_uint, | ||
0 as *mut _, | ||
builder::noname()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
-include ../tools.mk | ||
|
||
all: | ||
-mkdir -p $(TMPDIR)/good | ||
-mkdir -p $(TMPDIR)/bad | ||
$(BARE_RUSTC) ./good/lib.rs -C prefer-dynamic --out-dir="$(TMPDIR)/good" | ||
$(BARE_RUSTC) ./bad/lib.rs -C prefer-dynamic --out-dir="$(TMPDIR)/bad" | ||
$(BARE_RUSTC) -L "$(TMPDIR)/good" -C prefer-dynamic -Crpath ./main.rs --out-dir="$(TMPDIR)" | ||
# This should succeed because the correct library is in LD_LIBRARY_PATH | ||
$(LD_LIB_PATH_ENVVAR)="$(TMPDIR)/good:$($(LD_LIB_PATH_ENVVAR))" $(TMPDIR)/main | ||
# This should fail because the wrong library is in LD_LIBRARY_PATH | ||
OUTPUT=`$(LD_LIB_PATH_ENVVAR)="$(TMPDIR)/bad:$($(LD_LIB_PATH_ENVVAR))" $(TMPDIR)/main || exit 0` | ||
if ["$(OUTPUT)" == "bad"]; then exit 1; fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// Copyright 2016 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 <LICENSE-APACHE or | ||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | ||
// option. This file may not be copied, modified, or distributed | ||
// except according to those terms. | ||
|
||
#![crate_name="thelibrary"] | ||
#![crate_type="dylib"] | ||
|
||
pub fn some_library_function() { | ||
println!("bad"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// Copyright 2016 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 <LICENSE-APACHE or | ||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | ||
// option. This file may not be copied, modified, or distributed | ||
// except according to those terms. | ||
|
||
#![crate_name="thelibrary"] | ||
#![crate_type="dylib"] | ||
|
||
pub fn some_library_function() { | ||
println!("bad"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// Copyright 2016 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 <LICENSE-APACHE or | ||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | ||
// option. This file may not be copied, modified, or distributed | ||
// except according to those terms. | ||
|
||
extern crate thelibrary; | ||
|
||
fn main() { | ||
thelibrary::some_library_function(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters