11% Unsafe
22
3+ <!--
34Rust’s main draw is its powerful static guarantees about behavior. But safety
45checks are conservative by nature: there are some programs that are actually
56safe, but the compiler is not able to verify this is true. To write these kinds
67of programs, we need to tell the compiler to relax its restrictions a bit. For
78this, Rust has a keyword, `unsafe`. Code using `unsafe` has less restrictions
89than normal code does.
9-
10+ -->
11+ Rustの主たる魅力は、プログラムの動作についての強力で静的な保証です。
12+ しかしながら、安全性検査は本来保守的なものです。
13+ すなわち、実際には安全なのに、そのことがコンパイラには検証できないプログラムがいくらか存在します。
14+ その類のプログラムを書くためには、制約を少し緩和するようコンパイラに対して伝えることが要ります。
15+ そのために、Rustには ` unsafe ` というキーワードがあります。
16+ ` unsafe ` を使ったコードは、普通のコードよりも制約が少なくなります。
17+
18+ <!--
1019Let’s go over the syntax, and then we’ll talk semantics. `unsafe` is used in
1120four contexts. The first one is to mark a function as unsafe:
21+ -->
22+ まずシンタックスをみて、それからセマンティクスについて話しましょう。
23+ ` unsafe ` は4つの場面で使われます。
24+ 1つめは、関数がアンセーフであることを印付ける場合です。
1225
1326``` rust
1427unsafe fn danger_will_robinson () {
15- // scary stuff
28+ # // scary stuff
29+ // 恐ろしいもの
1630}
1731```
1832
33+ <!--
1934All functions called from [FFI][ffi] must be marked as `unsafe`, for example.
2035The second use of `unsafe` is an unsafe block:
36+ -->
37+ たとえば、[ FFI] [ ffi ] から呼び出されるすべての関数は` unsafe ` で印付けることが必要です。
38+ ` unsafe ` の2つめの用途は、アンセーフブロックです。
2139
2240[ ffi ] : ffi.html
2341
2442``` rust
2543unsafe {
26- // scary stuff
44+ # // scary stuff
45+ // 恐ろしいもの
2746}
2847```
2948
30- The third is for unsafe traits:
49+ <!-- The third is for unsafe traits:-->
50+ 3つめは、アンセーフトレイトです。
3151
3252``` rust
3353unsafe trait Scary { }
3454```
3555
36- And the fourth is for ` impl ` ementing one of those traits:
56+ <!-- And the fourth is for `impl`ementing one of those traits:-->
57+ そして、4つめは、そのアンセーフトレイトを実装する場合です。
3758
3859``` rust
3960# unsafe trait Scary { }
4061unsafe impl Scary for i32 {}
4162```
4263
64+ <!--
4365It’s important to be able to explicitly delineate code that may have bugs that
4466cause big problems. If a Rust program segfaults, you can be sure it’s somewhere
4567in the sections marked `unsafe`.
68+ -->
69+ 大きな問題を引き起こすバグがあるかもしれないコードを明示できるのは重要なことです。
70+ もしRustのプログラムがセグメンテーション違反を起こしても、バグは ` unsafe ` で印付けられた区間のどこかにあると確信できます。
4671
47- # What does ‘safe’ mean?
72+ # 「安全」とはどういう意味か?
73+ <!-- # What does ‘safe’ mean?-->
4874
75+ <!--
4976Safe, in the context of Rust, means ‘doesn’t do anything unsafe’. It’s also
5077important to know that there are certain behaviors that are probably not
5178desirable in your code, but are expressly _not_ unsafe:
79+ -->
80+ Rustの文脈で、安全とは「どのようなアンセーフなこともしない」ことを意味します。
81+
82+ > 訳注:
83+ 正確には、安全とは「決して未定義動作を起こさない」ということです。
84+ そして、安全性が保証されていないことを「アンセーフ」と呼びます。
85+ つまり、未定義動作が起きるおそれがあるなら、それはアンセーフです。
5286
87+ 知っておくべき重要なことに、たいていのコードにおいて望ましくないが、アンセーフ _ ではない_ とされている動作がいくらか存在するということがあります。
88+
89+ <!--
5390* Deadlocks
5491* Leaks of memory or other resources
5592* Exiting without calling destructors
5693* Integer overflow
94+ -->
95+ * デッドロック
96+ * メモリやその他のリソースのリーク
97+ * デストラクタを呼び出さないプログラム終了
98+ * 整数オーバーフロー
5799
100+ <!--
58101Rust cannot prevent all kinds of software problems. Buggy code can and will be
59102written in Rust. These things aren’t great, but they don’t qualify as `unsafe`
60103specifically.
104+ -->
105+ Rustはソフトウェアが抱えるすべての種類の問題を防げるわけではありません。
106+ Rustでバグのあるコードを書くことはできますし、実際に書かれるでしょう。
107+ これらの動作は良いことではありませんが、特にアンセーフだとは見なされません。
61108
109+ <!--
62110In addition, the following are all undefined behaviors in Rust, and must be
63111avoided, even when writing `unsafe` code:
112+ -->
113+ さらに、Rustにおいては、次のものは未定義動作で、 ` unsafe ` コード中であっても、避ける必要があります。
114+
115+ > 訳注:
116+ 関数に付いている` unsafe ` は「その関数の処理はアンセーフである」ということを表します。
117+ その一方で、ブロックに付いている` unsafe ` は「ブロック中の個々の操作はアンセーフだが、全体としては安全な処理である」ということを表します。
118+ 避ける必要があるのは、未定義動作が起こりうる処理をアンセーフブロックの中に書くことです。
119+ それは、アンセーフブロックの処理が安全であるために、その内部で未定義動作が決して起こらないことが必要だからです。
120+ アンセーフ関数には安全性の保証が要らないので、未定義動作が起こりうるアンセーフ関数を定義することに問題はありません。
64121
122+ <!--
65123* Data races
66124* Dereferencing a null/dangling raw pointer
67125* Reads of [undef][undef] (uninitialized) memory
@@ -84,59 +142,123 @@ avoided, even when writing `unsafe` code:
84142 * Non-UTF-8 byte sequences in a `str`
85143* Unwinding into Rust from foreign code or unwinding from Rust into foreign
86144 code.
145+ -->
146+ * データ競合
147+ * ヌル・ダングリング生ポインタの参照外し
148+ * [ undef] [ undef ] (未初期化)メモリの読み出し
149+ * 生ポインタによる [ pointer aliasing rules] [ aliasing ] の違反
150+ * ` &mut T ` と ` &T ` は、 ` UnsafeCell<U> ` を含む ` &T ` を除き、LLVMのスコープ化された [ noalias] [ noalias ] モデルに従っています。アンセーフコードは、それら参照のエイリアシング保証を破ってはいけません。
151+ * ` UnsafeCell<U> ` を持たないイミュータブルな値・参照の変更
152+ * コンパイラIntrinsic経由の未定義挙動の呼び出し
153+ * ` std::ptr::offset ` (` offset ` intrinsic) を使って、オブジェクトの範囲外を指すこと。ただし、オブジェクトの最後より1バイト後を指すことは許されている。
154+ * 範囲の重なったバッファに対して ` std::ptr::copy_nonoverlapping_memory ` (` memcpy32 ` /` memcpy64 `
155+ intrinsics) を使う
156+ * プリミティブ型の不正な値(プライベートなフィールドやローカル変数を含む)
157+ * ヌルかダングリングである参照やボックス
158+ * ` bool ` における、 ` false ` (0) か ` true ` (1) でない値
159+ * ` enum ` の定義に含まれていない判別子
160+ * ` char ` における、サロゲートか ` char::MAX ` を超えた値
161+ * ` str ` における、UTF-8でないバイト列
162+ * 他言語からRustへの巻き戻しや、Rustから他言語への巻き戻し
87163
88164[ noalias ] : http://llvm.org/docs/LangRef.html#noalias
89165[ undef ] : http://llvm.org/docs/LangRef.html#undefined-values
90166[ aliasing ] : http://llvm.org/docs/LangRef.html#pointer-aliasing-rules
91167
92- # Unsafe Superpowers
168+ # アンセーフの能力
169+ <!-- # Unsafe Superpowers-->
93170
171+ <!--
94172In both unsafe functions and unsafe blocks, Rust will let you do three things
95173that you normally can not do. Just three. Here they are:
174+ -->
175+ アンセーフ関数・アンセーフブロックでは、Rustは普段できない3つのことをさせてくれます。たった3つです。それは、
96176
177+ <!--
971781. Access or update a [static mutable variable][static].
981792. Dereference a raw pointer.
991803. Call unsafe functions. This is the most powerful ability.
181+ -->
182+ 1 . [ 静的ミュータブル変数] [ static ] のアクセスとアップデート。
183+ 2 . 生ポインタの参照外し。
184+ 3 . アンセーフ関数の呼び出し。これが最も強力な能力です。
100185
186+ <!--
101187That’s it. It’s important that `unsafe` does not, for example, ‘turn off the
102188borrow checker’. Adding `unsafe` to some random Rust code doesn’t change its
103189semantics, it won’t just start accepting anything. But it will let you write
104190things that _do_ break some of the rules.
191+ -->
192+ 以上です。
193+ 重要なのは、 ` unsafe ` が、たとえば「借用チェッカをオフにする」といったことを行わないことです。
194+ Rustのコードの適当な位置に ` unsafe ` を加えてもセマンティクスは変わらず、何でもただ受理するようになるということにはなりません。
195+ それでも、` unsafe ` はルールのいくつかを破るコードを書けるようにはするのです。
105196
197+ <!--
106198You will also encounter the `unsafe` keyword when writing bindings to foreign
107199(non-Rust) interfaces. You're encouraged to write a safe, native Rust interface
108200around the methods provided by the library.
201+ -->
202+ また、` unsafe ` キーワードは、Rust以外の言語とのインターフェースを書くときに遭遇するでしょう。
203+ ライブラリの提供するメソッドの周りに、安全な、Rustネイティブのインターフェースを書くことが推奨されています。
109204
205+ <!--
110206Let’s go over the basic three abilities listed, in order.
207+ -->
208+ これから、その基本的な3つの能力を順番に見ていきましょう。
111209
112- ## Access or update a ` static mut `
210+ ## ` static mut ` のアクセスとアップデート。
211+ <!-- ## Access or update a `static mut`-->
113212
213+ <!--
114214Rust has a feature called ‘`static mut`’ which allows for mutable global state.
115215Doing so can cause a data race, and as such is inherently not safe. For more
116216details, see the [static][static] section of the book.
217+ -->
218+ Rustには「` static mut ` 」という、ミュータブルでグローバルな状態を実現する機能があります。
219+ これを使うことはデータレースが起こるおそれがあるので、本質的に安全ではありません。
220+ 詳細は、この本の[ static] [ static ] セクションを参照してください。
117221
118222[ static ] : const-and-static.html#static
119223
120- ## Dereference a raw pointer
224+ ## 生ポインタの参照外し
225+ <!-- ## Dereference a raw pointer-->
121226
227+ <!--
122228Raw pointers let you do arbitrary pointer arithmetic, and can cause a number of
123229different memory safety and security issues. In some senses, the ability to
124230dereference an arbitrary pointer is one of the most dangerous things you can
125231do. For more on raw pointers, see [their section of the book][rawpointers].
232+ -->
233+ 生ポインタによって任意のポインタ演算が可能になりますが、いくつもの異なるメモリ安全とセキュリティの問題が起こるおそれがあります。
234+ ある意味で、任意のポインタを参照外しする能力は行いうる操作のうち最も危険なもののひとつです。
235+ 詳細は、[ この本の生ポインタに関するセクション] [ rawpointers ] を参照してください。
126236
127237[ rawpointers ] : raw-pointers.html
128238
129- ## Call unsafe functions
239+ ## アンセーフ関数の呼び出し
240+ <!-- ## Call unsafe functions-->
130241
242+ <!--
131243This last ability works with both aspects of `unsafe`: you can only call
132244functions marked `unsafe` from inside an unsafe block.
245+ -->
246+ この最後の能力は、` unsafe ` の両面とともに働きます。
247+ すなわち、` unsafe ` で印付けられた関数は、アンセーフブロックの内部からのみ呼び出すことができます。
133248
249+ <!--
134250This ability is powerful and varied. Rust exposes some [compiler
135251intrinsics][intrinsics] as unsafe functions, and some unsafe functions bypass
136252safety checks, trading safety for speed.
253+ -->
254+ この能力は強力で多彩です。
255+ Rustはいくらかの[ compiler intrinsics] [ intrinsics ] をアンセーフ関数として公開しており、また、いくつかのアンセーフ関数は安全性検査を回避することで、安全性とスピードを引き換えています。
137256
257+ <!--
138258I’ll repeat again: even though you _can_ do arbitrary things in unsafe blocks
139259and functions doesn’t mean you should. The compiler will act as though you’re
140260upholding its invariants, so be careful!
261+ -->
262+ 繰り返しになりますが、アンセーフブロックと関数の内部で任意のことが _ できる_ としても、それをすべきだということを意味しません。コンパイラは、あなたが不変量を守っているかのように動作しますから、注意してください!
141263
142264[ intrinsics ] : intrinsics.html
0 commit comments