解决内存allocate函数有可能访问未知内存的问题
This commit is contained in:
parent
e99d51e81f
commit
0542277e31
|
@ -6,14 +6,5 @@ edition = "2021"
|
||||||
# 此Cargo.toml仅用于rust-analyzer识别rust部分的代码
|
# 此Cargo.toml仅用于rust-analyzer识别rust部分的代码
|
||||||
# 不应使用cargo编译
|
# 不应使用cargo编译
|
||||||
|
|
||||||
[build]
|
|
||||||
type = "staticlib"
|
|
||||||
rustflags = [
|
|
||||||
"-C", "embed-bitcode=no",
|
|
||||||
"-C", "code-model=large",
|
|
||||||
"-C", "relocation-model=static",
|
|
||||||
"-L", "crate=${PWD}/../rustlib/${ARCH}/src/"
|
|
||||||
]
|
|
||||||
|
|
||||||
[target.'cfg(target_arch = "x86_64")']
|
[target.'cfg(target_arch = "x86_64")']
|
||||||
target = "x86_64-unknown-none"
|
target = "x86_64-unknown-none"
|
||||||
|
|
|
@ -139,9 +139,9 @@ allocator对象在进程与内核之间传递时一律使用内核空间的映
|
||||||
*/
|
*/
|
||||||
void *memm_allocate(usize size, usize pid);
|
void *memm_allocate(usize size, usize pid);
|
||||||
#define memm_addr_set_allocator(mem, allocator) \
|
#define memm_addr_set_allocator(mem, allocator) \
|
||||||
*(allocator_t **)(mem - 16) = allocator;
|
*(allocator_t **)((void *)(mem) - 16) = allocator;
|
||||||
#define memm_addr_get_allocator(mem) \
|
#define memm_addr_get_allocator(mem) \
|
||||||
((*(allocator_t **)(mem - 16)))
|
((*(allocator_t **)((void *)(mem) - 16)))
|
||||||
|
|
||||||
void *memm_kernel_allocate(usize size);
|
void *memm_kernel_allocate(usize size);
|
||||||
|
|
||||||
|
|
|
@ -200,6 +200,7 @@ void *memm_allocate(usize size, usize pid)
|
||||||
ptr = new_allocator->allocate(&new_allocator->allocator_instance, orgsize, 0);
|
ptr = new_allocator->allocate(&new_allocator->allocator_instance, orgsize, 0);
|
||||||
|
|
||||||
after_allocation:
|
after_allocation:
|
||||||
|
if (ptr != nullptr)
|
||||||
memm_addr_set_allocator(ptr, allocator);
|
memm_addr_set_allocator(ptr, allocator);
|
||||||
return ptr;
|
return ptr;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,9 @@
|
||||||
extern crate core;
|
extern crate core;
|
||||||
|
|
||||||
use core::alloc::{GlobalAlloc, Layout};
|
use core::{
|
||||||
|
alloc::{GlobalAlloc, Layout},
|
||||||
|
ptr::null_mut,
|
||||||
|
};
|
||||||
|
|
||||||
extern "C" {
|
extern "C" {
|
||||||
pub fn memm_kernel_allocate(size: usize) -> *mut u8;
|
pub fn memm_kernel_allocate(size: usize) -> *mut u8;
|
||||||
|
@ -12,7 +15,14 @@ pub struct KernelAllocator {}
|
||||||
|
|
||||||
unsafe impl GlobalAlloc for KernelAllocator {
|
unsafe impl GlobalAlloc for KernelAllocator {
|
||||||
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
|
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
|
||||||
memm_kernel_allocate(layout.size())
|
let res = memm_kernel_allocate(layout.size());
|
||||||
|
if res == null_mut() {
|
||||||
|
panic!(
|
||||||
|
"Kernel allocator failed to allocate {} byte(s) memory.",
|
||||||
|
layout.size()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
res
|
||||||
}
|
}
|
||||||
|
|
||||||
unsafe fn dealloc(&self, ptr: *mut u8, _layout: Layout) {
|
unsafe fn dealloc(&self, ptr: *mut u8, _layout: Layout) {
|
||||||
|
|
|
@ -96,6 +96,10 @@ SECTIONS {
|
||||||
{
|
{
|
||||||
*(.debug_pubtypes)
|
*(.debug_pubtypes)
|
||||||
}
|
}
|
||||||
|
.debug_frame :
|
||||||
|
{
|
||||||
|
*(.debug_frame)
|
||||||
|
}
|
||||||
.kend :
|
.kend :
|
||||||
{
|
{
|
||||||
*(.kend)
|
*(.kend)
|
||||||
|
|
Reference in New Issue