-
Notifications
You must be signed in to change notification settings - Fork 12.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Move OOM handling to liballoc and remove the oom
lang item
#51607
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,8 +14,9 @@ use std::ptr; | |
use attributes; | ||
use libc::c_uint; | ||
use rustc::middle::allocator::AllocatorKind; | ||
use rustc::session::InjectedDefaultOomHook; | ||
use rustc::ty::TyCtxt; | ||
use rustc_allocator::{ALLOCATOR_METHODS, AllocatorTy}; | ||
use rustc_allocator::{ALLOCATOR_METHODS, OOM_HANDLING_METHODS, AllocatorTy}; | ||
|
||
use ModuleLlvm; | ||
use llvm::{self, False, True}; | ||
|
@@ -33,9 +34,10 @@ pub(crate) unsafe fn codegen(tcx: TyCtxt, mods: &ModuleLlvm, kind: AllocatorKind | |
let i8p = llvm::LLVMPointerType(i8, 0); | ||
let void = llvm::LLVMVoidTypeInContext(llcx); | ||
|
||
for method in ALLOCATOR_METHODS { | ||
let build = |name: String, inputs: &[AllocatorTy], output: &AllocatorTy, | ||
callee: Option<String>| { | ||
let mut args = Vec::new(); | ||
for ty in method.inputs.iter() { | ||
for ty in inputs.iter() { | ||
match *ty { | ||
AllocatorTy::Layout => { | ||
args.push(usize); // size | ||
|
@@ -48,7 +50,7 @@ pub(crate) unsafe fn codegen(tcx: TyCtxt, mods: &ModuleLlvm, kind: AllocatorKind | |
AllocatorTy::Unit => panic!("invalid allocator arg"), | ||
} | ||
} | ||
let output = match method.output { | ||
let output = match *output { | ||
AllocatorTy::ResultPtr => Some(i8p), | ||
AllocatorTy::Unit => None, | ||
|
||
|
@@ -60,29 +62,40 @@ pub(crate) unsafe fn codegen(tcx: TyCtxt, mods: &ModuleLlvm, kind: AllocatorKind | |
args.as_ptr(), | ||
args.len() as c_uint, | ||
False); | ||
let name = CString::new(format!("__rust_{}", method.name)).unwrap(); | ||
let name = CString::new(name).unwrap(); | ||
let llfn = llvm::LLVMRustGetOrInsertFunction(llmod, | ||
name.as_ptr(), | ||
ty); | ||
|
||
if tcx.sess.target.target.options.default_hidden_visibility { | ||
llvm::LLVMRustSetVisibility(llfn, llvm::Visibility::Hidden); | ||
} | ||
if tcx.sess.target.target.options.requires_uwtable { | ||
attributes::emit_uwtable(llfn, true); | ||
} | ||
|
||
let callee = CString::new(kind.fn_name(method.name)).unwrap(); | ||
let callee = llvm::LLVMRustGetOrInsertFunction(llmod, | ||
callee.as_ptr(), | ||
ty); | ||
if tcx.sess.target.target.options.requires_uwtable { | ||
attributes::emit_uwtable(llfn, true); | ||
} | ||
|
||
let llbb = llvm::LLVMAppendBasicBlockInContext(llcx, | ||
llfn, | ||
"entry\0".as_ptr() as *const _); | ||
|
||
let llbuilder = llvm::LLVMCreateBuilderInContext(llcx); | ||
llvm::LLVMPositionBuilderAtEnd(llbuilder, llbb); | ||
|
||
let callee = if let Some(callee) = callee { | ||
callee | ||
} else { | ||
// Generate a no-op function | ||
llvm::LLVMBuildRetVoid(llbuilder); | ||
llvm::LLVMDisposeBuilder(llbuilder); | ||
return | ||
}; | ||
|
||
// Forward the call to another function | ||
let callee = CString::new(callee).unwrap(); | ||
let callee = llvm::LLVMRustGetOrInsertFunction(llmod, | ||
callee.as_ptr(), | ||
ty); | ||
|
||
let args = args.iter().enumerate().map(|(i, _)| { | ||
llvm::LLVMGetParam(llfn, i as c_uint) | ||
}).collect::<Vec<_>>(); | ||
|
@@ -98,6 +111,28 @@ pub(crate) unsafe fn codegen(tcx: TyCtxt, mods: &ModuleLlvm, kind: AllocatorKind | |
} else { | ||
llvm::LLVMBuildRetVoid(llbuilder); | ||
} | ||
|
||
llvm::LLVMDisposeBuilder(llbuilder); | ||
}; | ||
|
||
for method in ALLOCATOR_METHODS { | ||
let name = format!("__rust_{}", method.name); | ||
build(name, method.inputs, &method.output, Some(kind.fn_name(method.name))) | ||
} | ||
|
||
let has_plaftom_functions = match tcx.sess.injected_default_alloc_error_hook.get() { | ||
InjectedDefaultOomHook::None => return, | ||
InjectedDefaultOomHook::Noop => false, | ||
InjectedDefaultOomHook::Platform => true, | ||
}; | ||
|
||
for method in OOM_HANDLING_METHODS { | ||
let callee = if has_plaftom_functions { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "has_platform_functions" |
||
Some(format!("__rust_{}", method.name)) | ||
} else { | ||
None | ||
}; | ||
let name = format!("__rust_maybe_{}", method.name); | ||
build(name, method.inputs, &method.output, callee) | ||
} | ||
} |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Initially reading this I was a little confused by this about why both the hook and the abort mechanism were configurable. I think, though, it's to do
sys::abort_internal
, right? (as opposed tointrinsics::abort
)If that's the case, mind adding a small comment here alluding to that?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, exactly