|
|
|
|
<!DOCTYPE HTML>
|
|
|
|
|
<html lang="zh-CN" class="sidebar-visible no-js light">
|
|
|
|
|
<head>
|
|
|
|
|
<!-- Book generated using mdBook -->
|
|
|
|
|
<meta charset="UTF-8">
|
|
|
|
|
<title>Miri - Rust语言圣经(Rust Course)</title>
|
|
|
|
|
<!-- Custom HTML head -->
|
|
|
|
|
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
|
|
|
|
|
<meta name="description" content="">
|
|
|
|
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
|
|
|
<meta name="theme-color" content="#ffffff" />
|
|
|
|
|
|
|
|
|
|
<link rel="icon" href="../../favicon.svg">
|
|
|
|
|
<link rel="shortcut icon" href="../../favicon.png">
|
|
|
|
|
<link rel="stylesheet" href="../../css/variables.css">
|
|
|
|
|
<link rel="stylesheet" href="../../css/general.css">
|
|
|
|
|
<link rel="stylesheet" href="../../css/chrome.css">
|
|
|
|
|
<link rel="stylesheet" href="../../css/print.css" media="print">
|
|
|
|
|
<!-- Fonts -->
|
|
|
|
|
<link rel="stylesheet" href="../../FontAwesome/css/font-awesome.css">
|
|
|
|
|
<link rel="stylesheet" href="../../fonts/fonts.css">
|
|
|
|
|
<!-- Highlight.js Stylesheets -->
|
|
|
|
|
<link rel="stylesheet" href="../../highlight.css">
|
|
|
|
|
<link rel="stylesheet" href="../../tomorrow-night.css">
|
|
|
|
|
<link rel="stylesheet" href="../../ayu-highlight.css">
|
|
|
|
|
|
|
|
|
|
<!-- Custom theme stylesheets -->
|
|
|
|
|
<link rel="stylesheet" href="../../theme/style.css">
|
|
|
|
|
</head>
|
|
|
|
|
<body>
|
|
|
|
|
<!-- Provide site root to javascript -->
|
|
|
|
|
<script type="text/javascript">
|
|
|
|
|
var path_to_root = "../../";
|
|
|
|
|
var default_theme = window.matchMedia("(prefers-color-scheme: dark)").matches ? "navy" : "light";
|
|
|
|
|
</script>
|
|
|
|
|
|
|
|
|
|
<!-- Work around some values being stored in localStorage wrapped in quotes -->
|
|
|
|
|
<script type="text/javascript">
|
|
|
|
|
try {
|
|
|
|
|
var theme = localStorage.getItem('mdbook-theme');
|
|
|
|
|
var sidebar = localStorage.getItem('mdbook-sidebar');
|
|
|
|
|
if (theme.startsWith('"') && theme.endsWith('"')) {
|
|
|
|
|
localStorage.setItem('mdbook-theme', theme.slice(1, theme.length - 1));
|
|
|
|
|
}
|
|
|
|
|
if (sidebar.startsWith('"') && sidebar.endsWith('"')) {
|
|
|
|
|
localStorage.setItem('mdbook-sidebar', sidebar.slice(1, sidebar.length - 1));
|
|
|
|
|
}
|
|
|
|
|
} catch (e) { }
|
|
|
|
|
</script>
|
|
|
|
|
|
|
|
|
|
<!-- Set the theme before any content is loaded, prevents flash -->
|
|
|
|
|
<script type="text/javascript">
|
|
|
|
|
var theme;
|
|
|
|
|
try { theme = localStorage.getItem('mdbook-theme'); } catch(e) { }
|
|
|
|
|
if (theme === null || theme === undefined) { theme = default_theme; }
|
|
|
|
|
var html = document.querySelector('html');
|
|
|
|
|
html.classList.remove('no-js')
|
|
|
|
|
html.classList.remove('light')
|
|
|
|
|
html.classList.add(theme);
|
|
|
|
|
html.classList.add('js');
|
|
|
|
|
</script>
|
|
|
|
|
|
|
|
|
|
<!-- Hide / unhide sidebar before it is displayed -->
|
|
|
|
|
<script type="text/javascript">
|
|
|
|
|
var html = document.querySelector('html');
|
|
|
|
|
var sidebar = 'hidden';
|
|
|
|
|
if (document.body.clientWidth >= 1080) {
|
|
|
|
|
try { sidebar = localStorage.getItem('mdbook-sidebar'); } catch(e) { }
|
|
|
|
|
sidebar = sidebar || 'visible';
|
|
|
|
|
}
|
|
|
|
|
html.classList.remove('sidebar-visible');
|
|
|
|
|
html.classList.add("sidebar-" + sidebar);
|
|
|
|
|
</script>
|
|
|
|
|
|
|
|
|
|
<nav id="sidebar" class="sidebar" aria-label="Table of contents">
|
|
|
|
|
<div class="sidebar-scrollbox">
|
|
|
|
|
<ol class="chapter"><li class="chapter-item affix "><a href="../../about-book.html">关于本书</a></li><li class="chapter-item affix "><a href="../../into-rust.html">进入 Rust 编程世界</a></li><li class="chapter-item affix "><a href="../../first-try/sth-you-should-not-do.html">避免从入门到放弃</a></li><li class="chapter-item affix "><a href="../../community.html">社区和锈书</a></li><li class="chapter-item affix "><li class="part-title">Rust 语言基础学习</li><li class="spacer"></li><li class="chapter-item "><a href="../../first-try/intro.html"><strong aria-hidden="true">1.</strong> 寻找牛刀,以便小试</a><a class="toggle"><div>❱</div></a></li><li><ol class="section"><li class="chapter-item "><a href="../../first-try/installation.html"><strong aria-hidden="true">1.1.</strong> 安装 Rust 环境</a></li><li class="chapter-item "><a href="../../first-try/editor.html"><strong aria-hidden="true">1.2.</strong> 墙推 VSCode!</a></li><li class="chapter-item "><a href="../../first-try/cargo.html"><strong aria-hidden="true">1.3.</strong> 认识 Cargo</a></li><li class="chapter-item "><a href="../../first-try/hello-world.html"><strong aria-hidden="true">1.4.</strong> 不仅仅是 Hello world</a></li><li class="chapter-item "><a href="../../first-try/slowly-downloading.html"><strong aria-hidden="true">1.5.</strong> 下载依赖太慢了?</a></li></ol></li><li class="chapter-item "><a href="../../basic/intro.html"><strong aria-hidden="true">2.</strong> Rust 基础入门</a><a class="toggle"><div>❱</div></a></li><li><ol class="section"><li class="chapter-item "><a href="../../basic/variable.html"><strong aria-hidden="true">2.1.</strong> 变量绑定与解构</a></li><li class="chapter-item "><a href="../../basic/base-type/index.html"><strong aria-hidden="true">2.2.</strong> 基本类型</a><a class="toggle"><div>❱</div></a></li><li><ol class="section"><li class="chapter-item "><a href="../../basic/base-type/numbers.html"><strong aria-hidden="true">2.2.1.</strong> 数值类型</a></li><li class="chapter-item "><a href="../../basic/base-type/char-bool.html"><strong aria-hidden="true">2.2.2.</strong> 字符、布尔、单元类型</a></li><li class="chapter-item "><a href="../../basic/base-type/statement-expression.html"><strong aria-hidden="true">2.2.3.</strong> 语句与表达式</a></li><li class="chapter-item "><a href="../../basic/base-type/function.html"><strong aria-hidden="true">2.2.4.</strong> 函数</a></li></ol></li><li class="chapter-item "><a href="../../basic/ownership/index.html"><strong aria-hidden="true">2.3.</strong> 所有权和借用</a><a class="toggle"><div>❱</div></a></li><li><ol class="section"><li class="chapter-item "><a href="../../basic/ownership/ownership.html"><strong aria-hidden="true">2.3.1.</strong> 所有权</a></li><li class="chapter-item "><a href="../../basic/ownership/borrowing.html"><strong aria-hidden="true">2.3.2.</strong> 引用与借用</a></li></ol></li><li class="chapter-item "><a href="../../basic/compound-type/intro.html"><strong aria-hidden="true">2.4.</strong> 复合类型</a><a class="toggle"><div>❱</div></a></li><li><ol class="section"><li class="chapter-item "><a href="../../basic/compound-type/string-slice.html"><strong aria-hidden="true">2.4.1.</strong> 字符串与切片</a></li><li class="chapter-item "><a href="../../basic/compound-type/tuple.html"><strong aria-hidden="true">2.4.2.</strong> 元组</a></li><li class="chapter-item "><a href="../../basic/compound-type/struct.html"><strong aria-hidden="true">2.4.3.</strong> 结构体</a></li><li class="chapter-item "><a href="../../basic/compound-type/enum.html"><strong aria-hidden="true">2.4.4.</strong> 枚举</a></li><li class="chapter-item "><a href="../../basic/compound-type/array.html"><strong aria-hidden="true">2.4.5.</strong> 数组</a></li></ol></li><li class="chapter-item "><a href="../../basic/flow-control.html"><strong aria-hidden="true">2.5.</strong> 流程控制</a></li><li class="chapter-item "><a href="../../basic/match-pattern/intro.html"><strong aria-hidde
|
|
|
|
|
</div>
|
|
|
|
|
<div id="sidebar-resize-handle" class="sidebar-resize-handle"></div>
|
|
|
|
|
</nav>
|
|
|
|
|
|
|
|
|
|
<div id="page-wrapper" class="page-wrapper">
|
|
|
|
|
|
|
|
|
|
<div class="page">
|
|
|
|
|
<div id="menu-bar-hover-placeholder"></div>
|
|
|
|
|
<div id="menu-bar" class="menu-bar sticky bordered">
|
|
|
|
|
<div class="left-buttons">
|
|
|
|
|
<button id="sidebar-toggle" class="icon-button" type="button" title="Toggle Table of Contents" aria-label="Toggle Table of Contents" aria-controls="sidebar">
|
|
|
|
|
<i class="fa fa-bars"></i>
|
|
|
|
|
</button>
|
|
|
|
|
<button id="theme-toggle" class="icon-button" type="button" title="Change theme" aria-label="Change theme" aria-haspopup="true" aria-expanded="false" aria-controls="theme-list">
|
|
|
|
|
<i class="fa fa-paint-brush"></i>
|
|
|
|
|
</button>
|
|
|
|
|
<ul id="theme-list" class="theme-popup" aria-label="Themes" role="menu">
|
|
|
|
|
<li role="none"><button role="menuitem" class="theme" id="light">Light (default)</button></li>
|
|
|
|
|
<li role="none"><button role="menuitem" class="theme" id="rust">Rust</button></li>
|
|
|
|
|
<li role="none"><button role="menuitem" class="theme" id="coal">Coal</button></li>
|
|
|
|
|
<li role="none"><button role="menuitem" class="theme" id="navy">Navy</button></li>
|
|
|
|
|
<li role="none"><button role="menuitem" class="theme" id="ayu">Ayu</button></li>
|
|
|
|
|
</ul>
|
|
|
|
|
<button id="search-toggle" class="icon-button" type="button" title="Search. (Shortkey: s)" aria-label="Toggle Searchbar" aria-expanded="false" aria-keyshortcuts="S" aria-controls="searchbar">
|
|
|
|
|
<i class="fa fa-search"></i>
|
|
|
|
|
</button>
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<h1 class="menu-title">Rust语言圣经(Rust Course)</h1>
|
|
|
|
|
|
|
|
|
|
<div class="right-buttons">
|
|
|
|
|
<a href="../../print.html" title="Print this book" aria-label="Print this book">
|
|
|
|
|
<i id="print-button" class="fa fa-print"></i>
|
|
|
|
|
</a>
|
|
|
|
|
<a href="https://github.com/sunface/rust-course" title="Git repository" aria-label="Git repository">
|
|
|
|
|
<i id="git-repository-button" class="fa fa-github"></i>
|
|
|
|
|
</a>
|
|
|
|
|
<a href="https://github.com/sunface/rust-course/edit/main/src/too-many-lists/unsafe-queue/miri.md" title="Suggest an edit" aria-label="Suggest an edit">
|
|
|
|
|
<i id="git-edit-button" class="fa fa-edit"></i>
|
|
|
|
|
</a>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<div id="search-wrapper" class="hidden">
|
|
|
|
|
<form id="searchbar-outer" class="searchbar-outer">
|
|
|
|
|
<input type="search" id="searchbar" name="searchbar" placeholder="Search this book ..." aria-controls="searchresults-outer" aria-describedby="searchresults-header">
|
|
|
|
|
</form>
|
|
|
|
|
<div id="searchresults-outer" class="searchresults-outer hidden">
|
|
|
|
|
<div id="searchresults-header" class="searchresults-header"></div>
|
|
|
|
|
<ul id="searchresults">
|
|
|
|
|
</ul>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
<!-- Apply ARIA attributes after the sidebar and the sidebar toggle button are added to the DOM -->
|
|
|
|
|
<script type="text/javascript">
|
|
|
|
|
document.getElementById('sidebar-toggle').setAttribute('aria-expanded', sidebar === 'visible');
|
|
|
|
|
document.getElementById('sidebar').setAttribute('aria-hidden', sidebar !== 'visible');
|
|
|
|
|
|
|
|
|
|
// Get viewed page store
|
|
|
|
|
var viewed_key = 'mdbook-viewed';
|
|
|
|
|
var viewed_map = {};
|
|
|
|
|
try {
|
|
|
|
|
var viewed_storage = localStorage.getItem(viewed_key);
|
|
|
|
|
if (viewed_storage) {
|
|
|
|
|
viewed_map = JSON.parse(viewed_storage)
|
|
|
|
|
}
|
|
|
|
|
} catch (e) { }
|
|
|
|
|
|
|
|
|
|
Array.from(document.querySelectorAll('#sidebar a')).forEach(function(link) {
|
|
|
|
|
link.setAttribute('tabIndex', sidebar === 'visible' ? 0 : -1);
|
|
|
|
|
|
|
|
|
|
// Apply viewed style
|
|
|
|
|
if (viewed_map[link.pathname]) {
|
|
|
|
|
link.classList.add('md-viewed')
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Mark viewed after 30s
|
|
|
|
|
setTimeout(function() {
|
|
|
|
|
viewed_map[location.pathname] = 1;
|
|
|
|
|
localStorage.setItem(viewed_key, JSON.stringify(viewed_map));
|
|
|
|
|
}, 30000)
|
|
|
|
|
</script>
|
|
|
|
|
|
|
|
|
|
<div id="content" class="content">
|
|
|
|
|
<!-- Page table of contents -->
|
|
|
|
|
<div class="sidetoc"><nav class="pagetoc"></nav></div>
|
|
|
|
|
<main>
|
|
|
|
|
<h1 id="miri"><a class="header" href="#miri">Miri</a></h1>
|
|
|
|
|
<p>看到这里,大家是不是暗中松了口气?unsafe 不过如此嘛,不知道为何其它人都谈之色变。</p>
|
|
|
|
|
<p>怎么说呢?你以为的编译器已经不是以前的编译器了,它不报错不代表没有错误。包括测试用例也是,正常地运行不能意味着代码没有任何错误。</p>
|
|
|
|
|
<p>在周星驰电影功夫中,还有一个奇怪大叔 10 元一本主动上门卖如来神掌,那么有没有 10 元一本的 Rust 秘笈呢?( 喂,Rust语言圣经都免费让你读了,有了摩托车,还要什么拖拉机... 哈哈,开个玩笑 )</p>
|
|
|
|
|
<p>有的,奇怪大叔正在赶来,他告诉我们先来安装一个命令:</p>
|
|
|
|
|
<pre><code class="language-shell">rustup +nightly-2022-01-21 component add miri
|
|
|
|
|
info: syncing channel updates for 'nightly-2022-01-21-x86_64-pc-windows-msvc'
|
|
|
|
|
info: latest update on 2022-01-21, rust version 1.60.0-nightly (777bb86bc 2022-01-20)
|
|
|
|
|
info: downloading component 'cargo'
|
|
|
|
|
info: downloading component 'clippy'
|
|
|
|
|
info: downloading component 'rust-docs'
|
|
|
|
|
info: downloading component 'rust-std'
|
|
|
|
|
info: downloading component 'rustc'
|
|
|
|
|
info: downloading component 'rustfmt'
|
|
|
|
|
info: installing component 'cargo'
|
|
|
|
|
info: installing component 'clippy'
|
|
|
|
|
info: installing component 'rust-docs'
|
|
|
|
|
info: installing component 'rust-std'
|
|
|
|
|
info: installing component 'rustc'
|
|
|
|
|
info: installing component 'rustfmt'
|
|
|
|
|
info: downloading component 'miri'
|
|
|
|
|
info: installing component 'miri'
|
|
|
|
|
</code></pre>
|
|
|
|
|
<p>等等,你在我电脑上装了什么奇怪的东西?! "好东西"</p>
|
|
|
|
|
<blockquote>
|
|
|
|
|
<p>miri 目前只能在 nightly Rust 上安装,<code>+nightly-2022-01-21</code> 告诉 <code>rustup</code> 我们想要安装的 <code>nightly</code> 版本,事实上,你可以直接通过 <code>rustup +nightly component add miri</code> 安装,这里指定版本主要因为 <code>miri</code> 有时候会因为某些版本而出错。</p>
|
|
|
|
|
<p>2022-01-21 是我所知的 miri 可以成功运行的版本,你可以检查<a href="https://rust-lang.github.io/rustup-components-history/">这个网址</a>获取更多信息</p>
|
|
|
|
|
<ul>
|
|
|
|
|
<li>是一种临时性的规则运用,如果你不想每次都使用 <code>+nightly-2022-01-21</code>,可以使用 <a href="https://course.rs/appendix/rust-version.html#rustup-%E5%92%8C-rust-nightly-%E7%9A%84%E8%81%8C%E8%B4%A3"><code>rustup override set</code></a> 命令对当前项目的 Rust 版本进行覆盖</li>
|
|
|
|
|
</ul>
|
|
|
|
|
</blockquote>
|
|
|
|
|
<pre><code class="language-shell">$ cargo +nightly-2022-01-21 miri test
|
|
|
|
|
|
|
|
|
|
I will run `"cargo.exe" "install" "xargo"` to install
|
|
|
|
|
a recent enough xargo. Proceed? [Y/n]
|
|
|
|
|
</code></pre>
|
|
|
|
|
<p>额,<code>xargo</code> 是什么东东?"不要担心,选择 y 就行,我像是会坑你的人吗?"</p>
|
|
|
|
|
<pre><code class="language-shell">> y
|
|
|
|
|
|
|
|
|
|
Updating crates.io index
|
|
|
|
|
Installing xargo v0.3.24
|
|
|
|
|
...
|
|
|
|
|
Finished release [optimized] target(s) in 10.65s
|
|
|
|
|
Installing C:\Users\ninte\.cargo\bin\xargo-check.exe
|
|
|
|
|
Installing C:\Users\ninte\.cargo\bin\xargo.exe
|
|
|
|
|
Installed package `xargo v0.3.24` (executables `xargo-check.exe`, `xargo.exe`)
|
|
|
|
|
|
|
|
|
|
I will run `"rustup" "component" "add" "rust-src"` to install
|
|
|
|
|
the `rust-src` component for the selected toolchain. Proceed? [Y/n]
|
|
|
|
|
</code></pre>
|
|
|
|
|
<p>额? "不要怕,多给你一份 Rust 源代码,不开心嘛?"</p>
|
|
|
|
|
<pre><code class="language-shell">> y
|
|
|
|
|
|
|
|
|
|
info: downloading component 'rust-src'
|
|
|
|
|
info: installing component 'rust-src'
|
|
|
|
|
</code></pre>
|
|
|
|
|
<p>"看吧,我就说我不会骗你的,不相信我,等着错误砸脸吧!" 真是一个奇怪的大叔...</p>
|
|
|
|
|
<pre><code class="language-shell"> Compiling lists v0.1.0 (C:\Users\ninte\dev\tmp\lists)
|
|
|
|
|
Finished test [unoptimized + debuginfo] target(s) in 0.25s
|
|
|
|
|
Running unittests (lists-5cc11d9ee5c3e924.exe)
|
|
|
|
|
|
|
|
|
|
error: Undefined Behavior: trying to reborrow for Unique at alloc84055,
|
|
|
|
|
but parent tag <209678> does not have an appropriate item in
|
|
|
|
|
the borrow stack
|
|
|
|
|
|
|
|
|
|
--> \lib\rustlib\src\rust\library\core\src\option.rs:846:18
|
|
|
|
|
|
|
|
|
|
|
846 | Some(x) => Some(f(x)),
|
|
|
|
|
| ^ trying to reborrow for Unique at alloc84055,
|
|
|
|
|
| but parent tag <209678> does not have an
|
|
|
|
|
| appropriate item in the borrow stack
|
|
|
|
|
|
|
|
|
|
|
= help: this indicates a potential bug in the program:
|
|
|
|
|
it performed an invalid operation, but the rules it
|
|
|
|
|
violated are still experimental
|
|
|
|
|
= help: see https://github.com/rust-lang/unsafe-code-guidelines/blob/master/wip/stacked-borrows.md
|
|
|
|
|
for further information
|
|
|
|
|
|
|
|
|
|
= note: inside `std::option::Option::<std::boxed::Box<fifth::Node<i32>>>::map::<i32, [closure@src\fifth.rs:31:30: 40:10]>` at \lib\rustlib\src\rust\library\core\src\option.rs:846:18
|
|
|
|
|
|
|
|
|
|
note: inside `fifth::List::<i32>::pop` at src\fifth.rs:31:9
|
|
|
|
|
--> src\fifth.rs:31:9
|
|
|
|
|
|
|
|
|
|
|
31 | / self.head.take().map(|head| {
|
|
|
|
|
32 | | let head = *head;
|
|
|
|
|
33 | | self.head = head.next;
|
|
|
|
|
34 | |
|
|
|
|
|
... |
|
|
|
|
|
39 | | head.elem
|
|
|
|
|
40 | | })
|
|
|
|
|
| |__________^
|
|
|
|
|
note: inside `fifth::test::basics` at src\fifth.rs:74:20
|
|
|
|
|
--> src\fifth.rs:74:20
|
|
|
|
|
|
|
|
|
|
|
74 | assert_eq!(list.pop(), Some(1));
|
|
|
|
|
| ^^^^^^^^^^
|
|
|
|
|
note: inside closure at src\fifth.rs:62:5
|
|
|
|
|
--> src\fifth.rs:62:5
|
|
|
|
|
|
|
|
|
|
|
61 | #[test]
|
|
|
|
|
| ------- in this procedural macro expansion
|
|
|
|
|
62 | / fn basics() {
|
|
|
|
|
63 | | let mut list = List::new();
|
|
|
|
|
64 | |
|
|
|
|
|
65 | | // Check empty list behaves right
|
|
|
|
|
... |
|
|
|
|
|
96 | | assert_eq!(list.pop(), None);
|
|
|
|
|
97 | | }
|
|
|
|
|
| |_____^
|
|
|
|
|
...
|
|
|
|
|
error: aborting due to previous error
|
|
|
|
|
</code></pre>
|
|
|
|
|
<p>咦还真有错误,大叔,这是什么错误?大叔?...奇怪的大叔默默离开了,留下我在风中凌乱。</p>
|
|
|
|
|
<p>果然不靠谱...还是得靠自己,首先得了解下何为 <code>miri</code>。</p>
|
|
|
|
|
<p><a href="https://github.com/rust-lang/miri"><code>miri</code></a> 可以生成 Rust 的中间层表示 MIR,对于编译器来说,我们的 Rust 代码首先会被编译为 MIR ,然后再提交给 LLVM 进行处理。</p>
|
|
|
|
|
<p>可以通过 <code>rustup component add miri</code> 来安装它,并通过 <code>cargo miri</code> 来使用,同时还可以使用 <code>cargo miri test</code> 来运行测试代码。</p>
|
|
|
|
|
<p><code>miri</code> 可以帮助我们检查常见的未定义行为(UB = Undefined Behavior),以下列出了一部分:</p>
|
|
|
|
|
<ul>
|
|
|
|
|
<li>内存越界检查和内存释放后再使用(use-after-free)</li>
|
|
|
|
|
<li>使用未初始化的数据</li>
|
|
|
|
|
<li>数据竞争</li>
|
|
|
|
|
<li>内存对齐问题</li>
|
|
|
|
|
</ul>
|
|
|
|
|
<p>UB 检测是必须的,因为它发生在运行时,因此很难发现,如果 <code>miri</code> 能在编译期检测出来,那自然是最好不过的。</p>
|
|
|
|
|
<p>总之,<code>miri</code> 的使用很简单:</p>
|
|
|
|
|
<pre><code class="language-shell">$ cargo +nightly-2022-01-21 miri test
|
|
|
|
|
</code></pre>
|
|
|
|
|
<p>下面来看看具体的错误:</p>
|
|
|
|
|
<pre><code class="language-shell">error: Undefined Behavior: trying to reborrow for Unique at alloc84055, but parent tag <209678> does not have an appropriate item in the borrow stack
|
|
|
|
|
|
|
|
|
|
--> \lib\rustlib\src\rust\library\core\src\option.rs:846:18
|
|
|
|
|
|
|
|
|
|
|
846 | Some(x) => Some(f(x)),
|
|
|
|
|
| ^ trying to reborrow for Unique at alloc84055,
|
|
|
|
|
| but parent tag <209678> does not have an
|
|
|
|
|
| appropriate item in the borrow stack
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
= help: this indicates a potential bug in the program: it
|
|
|
|
|
performed an invalid operation, but the rules it
|
|
|
|
|
violated are still experimental
|
|
|
|
|
|
|
|
|
|
= help: see
|
|
|
|
|
https://github.com/rust-lang/unsafe-code-guidelines/blob/master/wip/stacked-borrows.md
|
|
|
|
|
for further information
|
|
|
|
|
</code></pre>
|
|
|
|
|
<p>嗯,只能看出是一个错误,其它完全看不懂了,例如什么是 <code>borrow stack</code>?</p>
|
|
|
|
|
|
|
|
|
|
<div id="giscus-container"></div>
|
|
|
|
|
</main>
|
|
|
|
|
|
|
|
|
|
<nav class="nav-wrapper" aria-label="Page navigation">
|
|
|
|
|
<!-- Mobile navigation buttons -->
|
|
|
|
|
<a rel="prev" href="../../too-many-lists/unsafe-queue/basics.html" class="mobile-nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
|
|
|
|
|
<i class="fa fa-angle-left"></i>
|
|
|
|
|
</a>
|
|
|
|
|
<a rel="next" href="../../too-many-lists/unsafe-queue/stacked-borrow.html" class="mobile-nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
|
|
|
|
|
<i class="fa fa-angle-right"></i>
|
|
|
|
|
</a>
|
|
|
|
|
<div style="clear: both"></div>
|
|
|
|
|
</nav>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<nav class="nav-wide-wrapper" aria-label="Page navigation">
|
|
|
|
|
<a rel="prev" href="../../too-many-lists/unsafe-queue/basics.html" class="nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
|
|
|
|
|
<i class="fa fa-angle-left"></i>
|
|
|
|
|
</a>
|
|
|
|
|
<a rel="next" href="../../too-many-lists/unsafe-queue/stacked-borrow.html" class="nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
|
|
|
|
|
<i class="fa fa-angle-right"></i>
|
|
|
|
|
</a>
|
|
|
|
|
</nav>
|
|
|
|
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<script type="text/javascript">
|
|
|
|
|
window.playground_copyable = true;
|
|
|
|
|
</script>
|
|
|
|
|
<script src="../../ace.js" type="text/javascript" charset="utf-8"></script>
|
|
|
|
|
<script src="../../editor.js" type="text/javascript" charset="utf-8"></script>
|
|
|
|
|
<script src="../../mode-rust.js" type="text/javascript" charset="utf-8"></script>
|
|
|
|
|
<script src="../../theme-dawn.js" type="text/javascript" charset="utf-8"></script>
|
|
|
|
|
<script src="../../theme-tomorrow_night.js" type="text/javascript" charset="utf-8"></script>
|
|
|
|
|
<script src="../../elasticlunr.min.js" type="text/javascript" charset="utf-8"></script>
|
|
|
|
|
<script src="../../mark.min.js" type="text/javascript" charset="utf-8"></script>
|
|
|
|
|
<script src="../../searcher.js" type="text/javascript" charset="utf-8"></script>
|
|
|
|
|
<script src="../../clipboard.min.js" type="text/javascript" charset="utf-8"></script>
|
|
|
|
|
<script src="../../highlight.js" type="text/javascript" charset="utf-8"></script>
|
|
|
|
|
<script src="../../book.js" type="text/javascript" charset="utf-8"></script>
|
|
|
|
|
<script type="text/javascript" charset="utf-8">
|
|
|
|
|
var pagePath = "too-many-lists/unsafe-queue/miri.md"
|
|
|
|
|
</script>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<!-- Custom JS scripts -->
|
|
|
|
|
<script type="text/javascript" src="../../assets/custom.js"></script>
|
|
|
|
|
<script type="text/javascript" src="../../assets/bigPicture.js"></script>
|
|
|
|
|
</body>
|
|
|
|
|
</html>
|