Browse Source

把rand_bytes提取为公共的方法

longjin 4 months ago
parent
commit
9c1ca444b6
3 changed files with 43 additions and 22 deletions
  1. 1 2
      kernel/src/arch/x86_64/process/syscall.rs
  2. 0 20
      kernel/src/arch/x86_64/rand.rs
  3. 42 0
      kernel/src/libs/rand.rs

+ 1 - 2
kernel/src/arch/x86_64/process/syscall.rs

@@ -5,10 +5,10 @@ use crate::{
     arch::{
         interrupt::TrapFrame,
         process::table::{USER_CS, USER_DS},
-        rand::rand_bytes,
         CurrentIrqArch,
     },
     exception::InterruptArch,
+    libs::rand::rand_bytes,
     mm::ucontext::AddressSpace,
     process::{
         exec::{load_binary_file, ExecParam, ExecParamFlags},
@@ -74,7 +74,6 @@ impl Syscall {
         param.init_info_mut().envs = envp;
 
         // 生成16字节随机数
-        // TODO 暂时设为0
         param.init_info_mut().rand_num = rand_bytes::<16>();
 
         // 把proc_init_info写到用户栈上

+ 0 - 20
kernel/src/arch/x86_64/rand.rs

@@ -3,23 +3,3 @@ use core::arch::x86_64::_rdtsc;
 pub fn rand() -> usize {
     return unsafe { (_rdtsc() * _rdtsc() + 998244353_u64 * _rdtsc()) as usize };
 }
-
-//TODO move it out from arch module
-pub fn rand_bytes<const N: usize>() -> [u8; N] {
-    let mut bytes = [0u8; N];
-    let mut remaining = N;
-    let mut index = 0;
-
-    while remaining > 0 {
-        let random_num = rand();
-        let random_bytes = random_num.to_le_bytes();
-
-        let to_copy = core::cmp::min(remaining, size_of::<usize>());
-        bytes[index..index + to_copy].copy_from_slice(&random_bytes[..to_copy]);
-
-        index += to_copy;
-        remaining -= to_copy;
-    }
-
-    bytes
-}

+ 42 - 0
kernel/src/libs/rand.rs

@@ -1,3 +1,5 @@
+use crate::arch::rand::rand;
+
 bitflags! {
     pub struct GRandFlags: u8{
         const GRND_NONBLOCK = 0x0001;
@@ -5,3 +7,43 @@ bitflags! {
         const GRND_INSECURE = 0x0004;
     }
 }
+
+/// Generates an array of random bytes of size `N`.
+///
+/// This function fills an array of size `N` with random bytes by repeatedly
+/// generating random numbers and converting them to little-endian byte arrays.
+/// The function ensures that the entire array is filled with random bytes,
+/// even if the size of the array is not a multiple of the size of `usize`.
+///
+/// # Type Parameters
+///
+/// * `N`: The size of the array to be filled with random bytes.
+///
+/// # Returns
+///
+/// An array of size `N` filled with random bytes.
+///
+/// # Example
+///
+/// ```rust
+/// let random_bytes = rand_bytes::<16>();
+/// assert_eq!(random_bytes.len(), 16);
+/// ```
+pub fn rand_bytes<const N: usize>() -> [u8; N] {
+    let mut bytes = [0u8; N];
+    let mut remaining = N;
+    let mut index = 0;
+
+    while remaining > 0 {
+        let random_num = rand();
+        let random_bytes = random_num.to_le_bytes();
+
+        let to_copy = core::cmp::min(remaining, size_of::<usize>());
+        bytes[index..index + to_copy].copy_from_slice(&random_bytes[..to_copy]);
+
+        index += to_copy;
+        remaining -= to_copy;
+    }
+
+    bytes
+}