gpt4 book ai didi

operating-system - Rust 调用失败::fail_bounds_check 启用无着陆垫标志

转载 作者:行者123 更新时间:2023-11-29 08:19:34 25 4
gpt4 key购买 nike

我一直在尝试用 Rust 编写一个基本内核,但链接脚本失败并出现以下错误:roost.rs:(.text.kmain+0x12a):对“failure::fail_bounds_check::hee3207bbe41f708990v::v0.11.0”的 undefined reference

我使用以下标志编译 rust 源文件:

-O --target i686-unknown-linux-gnu -Z no-landing-pads --crate-type lib --emit=obj

如果我正确理解了 Rust 编译器,-Z no-landing-pads 选项应该会阻止编译器生成故障函数。从测试中我可以看出失败函数仅在 kmain 函数调用我的函数 io::write_char(c: char)

这是 io::write_char(c: char) 的定义

pub fn write_char(c: char) {
unsafe {
vga::place_char_at(c, vga::cursor_x, vga::cursor_y);

vga::cursor_y =
if vga::cursor_x >= vga::VGA_WIDTH {
vga::cursor_y + 1
} else {
vga::cursor_y
};
vga::cursor_x =
if vga::cursor_x >= vga::VGA_WIDTH {
0
} else {
vga::cursor_x + 1
};

vga::set_cursor_location(vga::cursor_x, vga::cursor_y);
}
}

如何阻止 Rust 尝试调用不存在的函数 failure::fail_bounds_check?

编辑:进一步测试表明 vga::place_char_at 函数是原因。这是代码:

pub fn place_char_at(c: char, x: u8, y: u8) {
let tmpx =
if x >= VGA_WIDTH {
VGA_WIDTH - 1
} else {
x
};
let tmpy =
if y >= VGA_HEIGHT {
VGA_HEIGHT - 1
} else {
y
};
unsafe {
(*SCREEN)[(tmpy as uint) * 80 + (tmpx as uint)].char = c as u8;
}
}

据我所知,问题是 Rust 想要绑定(bind)检查我正在做的数组访问,有没有办法让编译器确保检查已经完成或关闭该功能的功能?

Edit2:所以我在一些工作后解决了它。在深入研究文档后,我发现 rust 具有绕过绑定(bind)检查的矢量访问功能。为了使用它,我将 place_char_at 函数更改为:

pub fn place_char_at(c: char, x: u8, y: u8) {
let tmpx =
if x >= VGA_WIDTH {
VGA_WIDTH - 1
} else {
x
};
let tmpy =
if y >= VGA_HEIGHT {
VGA_HEIGHT - 1
} else {
y
};
unsafe {
(*SCREEN).unsafe_mut_ref((tmpy as uint) * 80 + (tmpx as uint)).char = c as u8;
}
}

最佳答案

确保您链接到 libcore。 libcore 也有一个依赖项:失败的定义。确保在异常代码中的某处标记函数 #[lang="begin_unwind"]。要求是 begin_unwind 不返回。 See here以我为例。

is there a way to ... turn off the feature for that function?

没有。用 bstrie 的话来说,if there were a compiler flag to eliminate array bounds checks, then bstrie would fork the language and make the flag delete your hard drive .换句话说,安全是最重要的。

关于operating-system - Rust 调用失败::fail_bounds_check 启用无着陆垫标志,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24988583/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com