@@ -96,7 +96,7 @@ pub trait Kernel: MappedKernel {
96
96
fn get < O : Offset > ( self , off : O ) -> O :: Ops {
97
97
let addr = unsafe { self . addr ( ) . add ( off. get ( ) ) } ;
98
98
99
- <O :: Ops as OffsetOps >:: new ( addr)
99
+ unsafe { <O :: Ops as OffsetOps >:: new ( addr) }
100
100
}
101
101
102
102
/// # Safety
@@ -295,7 +295,9 @@ pub trait Offset: Copy {
295
295
296
296
/// Contains possible operations on an item at the [`Offset`].
297
297
pub trait OffsetOps : Copy {
298
- fn new ( addr : * const u8 ) -> Self ;
298
+ /// # Safety
299
+ /// `addr` must be valid for this [`OffsetOps`] to operate on.
300
+ unsafe fn new ( addr : * const u8 ) -> Self ;
299
301
}
300
302
301
303
/// Offset of an immutable static value in the kernel.
@@ -338,7 +340,7 @@ impl<T> Offset for Static<T> {
338
340
pub struct ImmutableOps < T > ( * const T ) ;
339
341
340
342
impl < T > OffsetOps for ImmutableOps < T > {
341
- fn new ( addr : * const u8 ) -> Self {
343
+ unsafe fn new ( addr : * const u8 ) -> Self {
342
344
Self ( addr. cast ( ) )
343
345
}
344
346
}
@@ -429,7 +431,7 @@ impl<T> Clone for MutableOps<T> {
429
431
impl < T > Copy for MutableOps < T > { }
430
432
431
433
impl < T > OffsetOps for MutableOps < T > {
432
- fn new ( addr : * const u8 ) -> Self {
434
+ unsafe fn new ( addr : * const u8 ) -> Self {
433
435
Self ( addr. cast_mut ( ) . cast ( ) )
434
436
}
435
437
}
@@ -475,7 +477,8 @@ pub struct FunctionOps<T> {
475
477
476
478
impl < T : KernelFn > FunctionOps < T > {
477
479
pub fn as_ptr ( self ) -> T {
478
- T :: from_addr ( self . addr )
480
+ // SAFETY: self.addr is guarantee to be valid by Function::new.
481
+ unsafe { T :: from_addr ( self . addr ) }
479
482
}
480
483
}
481
484
@@ -488,7 +491,7 @@ impl<T> Clone for FunctionOps<T> {
488
491
impl < T > Copy for FunctionOps < T > { }
489
492
490
493
impl < T > OffsetOps for FunctionOps < T > {
491
- fn new ( addr : * const u8 ) -> Self {
494
+ unsafe fn new ( addr : * const u8 ) -> Self {
492
495
Self {
493
496
addr,
494
497
phantom : PhantomData ,
@@ -498,11 +501,13 @@ impl<T> OffsetOps for FunctionOps<T> {
498
501
499
502
/// Provides method to cast kernel address into a function pointer.
500
503
pub trait KernelFn : Copy {
501
- fn from_addr ( addr : * const u8 ) -> Self ;
504
+ /// # Safety
505
+ /// `addr` must be the first instruction of this function.
506
+ unsafe fn from_addr ( addr : * const u8 ) -> Self ;
502
507
}
503
508
504
509
impl < R , A1 > KernelFn for extern "C" fn ( A1 , ...) -> R {
505
- fn from_addr ( addr : * const u8 ) -> Self {
510
+ unsafe fn from_addr ( addr : * const u8 ) -> Self {
506
511
unsafe { transmute ( addr) }
507
512
}
508
513
}
0 commit comments