解决因rust字符串不以0结尾导致的内存错误

This commit is contained in:
pointer-to-bios 2024-01-21 16:35:43 +08:00
parent 73267fd2ec
commit ac80977151
3 changed files with 21 additions and 4 deletions

View File

@ -76,6 +76,7 @@ libk:
@echo -e "\e[33m-------------------------\e[0m" @echo -e "\e[33m-------------------------\e[0m"
rust: rust:
@echo -e "\e[1m\e[33mrustc\e[0m \e[34m-->\e[0m \e[1m\e[32m$@.o\e[0m"
@rustc ${RSCFLAGS} lib.rs -o rust.o @rustc ${RSCFLAGS} lib.rs -o rust.o
clear: clear:

View File

@ -1,14 +1,18 @@
use std::panic;
use crate::kernel::tty::tty::{Color, MessageBuilder, Tty}; use crate::kernel::tty::tty::{Color, MessageBuilder, Tty};
#[no_mangle] #[no_mangle]
extern "C" fn kmain_rust() { extern "C" fn kmain_rust() {
let tty = Tty::from_id(0).unwrap();
// let ttyptr = unsafe { tty.get_c_tty_t() };
let hello = MessageBuilder::new() let hello = MessageBuilder::new()
.message("Hello, ".to_string()) .message("Hello, ".to_string())
.message("Metaverse".to_string()) .message("Metaverse".to_string())
.foreground_color(Color(0xa, 0xee, 0xa)) .foreground_color(Color(0xa, 0xee, 0xa))
.message("!\n".to_string()) .message("!\n".to_string())
.build(); .build();
let tty = Tty::from_id(0).unwrap();
tty.print(hello); tty.print(hello);
loop {} loop {}
} }

View File

@ -1,9 +1,12 @@
use std::ptr::null_mut; use std::{
io::{BufWriter, Write},
ptr::null_mut,
};
extern "C" { extern "C" {
fn tty_new(tty_type: u8, mode: u8) -> *mut u8; fn tty_new(tty_type: u8, mode: u8) -> *mut u8;
fn tty_get(id: usize) -> *mut *mut u8; fn tty_get(id: usize) -> *mut *mut u8;
fn tty_text_print(ttyx: *mut u8, string: *mut u8, color: u32, bgcolor: u32); pub fn tty_text_print(ttyx: *mut u8, string: *mut u8, color: u32, bgcolor: u32);
fn tty_get_id(tty: *mut u8) -> usize; fn tty_get_id(tty: *mut u8) -> usize;
} }
@ -39,6 +42,10 @@ impl Tty {
} }
} }
pub unsafe fn get_c_tty_t(&self) -> *mut u8 {
self.tty_pointer
}
pub fn id(&self) -> usize { pub fn id(&self) -> usize {
unsafe { tty_get_id(self.tty_pointer) } unsafe { tty_get_id(self.tty_pointer) }
} }
@ -51,12 +58,17 @@ impl Tty {
} in msg.into_iter() } in msg.into_iter()
{ {
unsafe { unsafe {
let string = msg.as_bytes_mut() as *mut [u8] as *mut u8;
let string = string.offset(msg.len() as isize);
let swp = *string;
*string = 0;
tty_text_print( tty_text_print(
self.tty_pointer, self.tty_pointer,
msg.as_bytes_mut() as *mut [u8] as *mut u8, msg.as_bytes_mut() as *mut [u8] as *mut u8,
u32::from(fgcolor), u32::from(fgcolor),
u32::from(bgcolor), u32::from(bgcolor),
) );
*string = swp;
}; };
} }
} }