1
1
% Unsafe
2
2
3
+ <!--
3
4
Rust’s main draw is its powerful static guarantees about behavior. But safety
4
5
checks are conservative by nature: there are some programs that are actually
5
6
safe, but the compiler is not able to verify this is true. To write these kinds
6
7
of programs, we need to tell the compiler to relax its restrictions a bit. For
7
8
this, Rust has a keyword, `unsafe`. Code using `unsafe` has less restrictions
8
9
than normal code does.
9
-
10
+ -->
11
+ Rustの主たる魅力は、プログラムの動作についての強力で静的な保証です。
12
+ しかしながら、安全性検査は本来保守的なものです。
13
+ すなわち、実際には安全なのに、そのことがコンパイラには検証できないプログラムがいくらか存在します。
14
+ その類のプログラムを書くためには、制約を少し緩和するようコンパイラに対して伝えることが要ります。
15
+ そのために、Rustには ` unsafe ` というキーワードがあります。
16
+ ` unsafe ` を使ったコードは、普通のコードよりも制約が少なくなります。
17
+
18
+ <!--
10
19
Let’s go over the syntax, and then we’ll talk semantics. `unsafe` is used in
11
20
four contexts. The first one is to mark a function as unsafe:
21
+ -->
22
+ まずシンタックスをみて、それからセマンティクスについて話しましょう。
23
+ ` unsafe ` は4つの場面で使われます。
24
+ 1つめは、関数がアンセーフであることを印付ける場合です。
12
25
13
26
``` rust
14
27
unsafe fn danger_will_robinson () {
15
- // scary stuff
28
+ # // scary stuff
29
+ // 恐ろしいもの
16
30
}
17
31
```
18
32
33
+ <!--
19
34
All functions called from [FFI][ffi] must be marked as `unsafe`, for example.
20
35
The second use of `unsafe` is an unsafe block:
36
+ -->
37
+ たとえば、[ FFI] [ ffi ] から呼び出されるすべての関数は` unsafe ` で印付けることが必要です。
38
+ ` unsafe ` の2つめの用途は、アンセーフブロックです。
21
39
22
40
[ ffi ] : ffi.html
23
41
24
42
``` rust
25
43
unsafe {
26
- // scary stuff
44
+ # // scary stuff
45
+ // 恐ろしいもの
27
46
}
28
47
```
29
48
30
- The third is for unsafe traits:
49
+ <!-- The third is for unsafe traits:-->
50
+ 3つめは、アンセーフトレイトです。
31
51
32
52
``` rust
33
53
unsafe trait Scary { }
34
54
```
35
55
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つめは、そのアンセーフトレイトを実装する場合です。
37
58
38
59
``` rust
39
60
# unsafe trait Scary { }
40
61
unsafe impl Scary for i32 {}
41
62
```
42
63
64
+ <!--
43
65
It’s important to be able to explicitly delineate code that may have bugs that
44
66
cause big problems. If a Rust program segfaults, you can be sure it’s somewhere
45
67
in the sections marked `unsafe`.
68
+ -->
69
+ 大きな問題を引き起こすバグがあるかもしれないコードを明示できるのは重要なことです。
70
+ もしRustのプログラムがセグメンテーション違反を起こしても、バグは ` unsafe ` で印付けられた区間のどこかにあると確信できます。
46
71
47
- # What does ‘safe’ mean?
72
+ # 「安全」とはどういう意味か?
73
+ <!-- # What does ‘safe’ mean?-->
48
74
75
+ <!--
49
76
Safe, in the context of Rust, means ‘doesn’t do anything unsafe’. It’s also
50
77
important to know that there are certain behaviors that are probably not
51
78
desirable in your code, but are expressly _not_ unsafe:
79
+ -->
80
+ Rustの文脈で、安全とは「どのようなアンセーフなこともしない」ことを意味します。
81
+
82
+ > 訳注:
83
+ 正確には、安全とは「決して未定義動作を起こさない」ということです。
84
+ そして、安全性が保証されていないことを「アンセーフ」と呼びます。
85
+ つまり、未定義動作が起きるおそれがあるなら、それはアンセーフです。
52
86
87
+ 知っておくべき重要なことに、たいていのコードにおいて望ましくないが、アンセーフ _ ではない_ とされている動作がいくらか存在するということがあります。
88
+
89
+ <!--
53
90
* Deadlocks
54
91
* Leaks of memory or other resources
55
92
* Exiting without calling destructors
56
93
* Integer overflow
94
+ -->
95
+ * デッドロック
96
+ * メモリやその他のリソースのリーク
97
+ * デストラクタを呼び出さないプログラム終了
98
+ * 整数オーバーフロー
57
99
100
+ <!--
58
101
Rust cannot prevent all kinds of software problems. Buggy code can and will be
59
102
written in Rust. These things aren’t great, but they don’t qualify as `unsafe`
60
103
specifically.
104
+ -->
105
+ Rustはソフトウェアが抱えるすべての種類の問題を防げるわけではありません。
106
+ Rustでバグのあるコードを書くことはできますし、実際に書かれるでしょう。
107
+ これらの動作は良いことではありませんが、特にアンセーフだとは見なされません。
61
108
109
+ <!--
62
110
In addition, the following are all undefined behaviors in Rust, and must be
63
111
avoided, even when writing `unsafe` code:
112
+ -->
113
+ さらに、Rustにおいては、次のものは未定義動作で、 ` unsafe ` コード中であっても、避ける必要があります。
114
+
115
+ > 訳注:
116
+ 関数に付いている` unsafe ` は「その関数の処理はアンセーフである」ということを表します。
117
+ その一方で、ブロックに付いている` unsafe ` は「ブロック中の個々の操作はアンセーフだが、全体としては安全な処理である」ということを表します。
118
+ 避ける必要があるのは、未定義動作が起こりうる処理をアンセーフブロックの中に書くことです。
119
+ それは、アンセーフブロックの処理が安全であるために、その内部で未定義動作が決して起こらないことが必要だからです。
120
+ アンセーフ関数には安全性の保証が要らないので、未定義動作が起こりうるアンセーフ関数を定義することに問題はありません。
64
121
122
+ <!--
65
123
* Data races
66
124
* Dereferencing a null/dangling raw pointer
67
125
* Reads of [undef][undef] (uninitialized) memory
@@ -84,59 +142,123 @@ avoided, even when writing `unsafe` code:
84
142
* Non-UTF-8 byte sequences in a `str`
85
143
* Unwinding into Rust from foreign code or unwinding from Rust into foreign
86
144
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から他言語への巻き戻し
87
163
88
164
[ noalias ] : http://llvm.org/docs/LangRef.html#noalias
89
165
[ undef ] : http://llvm.org/docs/LangRef.html#undefined-values
90
166
[ aliasing ] : http://llvm.org/docs/LangRef.html#pointer-aliasing-rules
91
167
92
- # Unsafe Superpowers
168
+ # アンセーフの能力
169
+ <!-- # Unsafe Superpowers-->
93
170
171
+ <!--
94
172
In both unsafe functions and unsafe blocks, Rust will let you do three things
95
173
that you normally can not do. Just three. Here they are:
174
+ -->
175
+ アンセーフ関数・アンセーフブロックでは、Rustは普段できない3つのことをさせてくれます。たった3つです。それは、
96
176
177
+ <!--
97
178
1. Access or update a [static mutable variable][static].
98
179
2. Dereference a raw pointer.
99
180
3. Call unsafe functions. This is the most powerful ability.
181
+ -->
182
+ 1 . [ 静的ミュータブル変数] [ static ] のアクセスとアップデート。
183
+ 2 . 生ポインタの参照外し。
184
+ 3 . アンセーフ関数の呼び出し。これが最も強力な能力です。
100
185
186
+ <!--
101
187
That’s it. It’s important that `unsafe` does not, for example, ‘turn off the
102
188
borrow checker’. Adding `unsafe` to some random Rust code doesn’t change its
103
189
semantics, it won’t just start accepting anything. But it will let you write
104
190
things that _do_ break some of the rules.
191
+ -->
192
+ 以上です。
193
+ 重要なのは、 ` unsafe ` が、たとえば「借用チェッカをオフにする」といったことを行わないことです。
194
+ Rustのコードの適当な位置に ` unsafe ` を加えてもセマンティクスは変わらず、何でもただ受理するようになるということにはなりません。
195
+ それでも、` unsafe ` はルールのいくつかを破るコードを書けるようにはするのです。
105
196
197
+ <!--
106
198
You will also encounter the `unsafe` keyword when writing bindings to foreign
107
199
(non-Rust) interfaces. You're encouraged to write a safe, native Rust interface
108
200
around the methods provided by the library.
201
+ -->
202
+ また、` unsafe ` キーワードは、Rust以外の言語とのインターフェースを書くときに遭遇するでしょう。
203
+ ライブラリの提供するメソッドの周りに、安全な、Rustネイティブのインターフェースを書くことが推奨されています。
109
204
205
+ <!--
110
206
Let’s go over the basic three abilities listed, in order.
207
+ -->
208
+ これから、その基本的な3つの能力を順番に見ていきましょう。
111
209
112
- ## Access or update a ` static mut `
210
+ ## ` static mut ` のアクセスとアップデート。
211
+ <!-- ## Access or update a `static mut`-->
113
212
213
+ <!--
114
214
Rust has a feature called ‘`static mut`’ which allows for mutable global state.
115
215
Doing so can cause a data race, and as such is inherently not safe. For more
116
216
details, see the [static][static] section of the book.
217
+ -->
218
+ Rustには「` static mut ` 」という、ミュータブルでグローバルな状態を実現する機能があります。
219
+ これを使うことはデータレースが起こるおそれがあるので、本質的に安全ではありません。
220
+ 詳細は、この本の[ static] [ static ] セクションを参照してください。
117
221
118
222
[ static ] : const-and-static.html#static
119
223
120
- ## Dereference a raw pointer
224
+ ## 生ポインタの参照外し
225
+ <!-- ## Dereference a raw pointer-->
121
226
227
+ <!--
122
228
Raw pointers let you do arbitrary pointer arithmetic, and can cause a number of
123
229
different memory safety and security issues. In some senses, the ability to
124
230
dereference an arbitrary pointer is one of the most dangerous things you can
125
231
do. For more on raw pointers, see [their section of the book][rawpointers].
232
+ -->
233
+ 生ポインタによって任意のポインタ演算が可能になりますが、いくつもの異なるメモリ安全とセキュリティの問題が起こるおそれがあります。
234
+ ある意味で、任意のポインタを参照外しする能力は行いうる操作のうち最も危険なもののひとつです。
235
+ 詳細は、[ この本の生ポインタに関するセクション] [ rawpointers ] を参照してください。
126
236
127
237
[ rawpointers ] : raw-pointers.html
128
238
129
- ## Call unsafe functions
239
+ ## アンセーフ関数の呼び出し
240
+ <!-- ## Call unsafe functions-->
130
241
242
+ <!--
131
243
This last ability works with both aspects of `unsafe`: you can only call
132
244
functions marked `unsafe` from inside an unsafe block.
245
+ -->
246
+ この最後の能力は、` unsafe ` の両面とともに働きます。
247
+ すなわち、` unsafe ` で印付けられた関数は、アンセーフブロックの内部からのみ呼び出すことができます。
133
248
249
+ <!--
134
250
This ability is powerful and varied. Rust exposes some [compiler
135
251
intrinsics][intrinsics] as unsafe functions, and some unsafe functions bypass
136
252
safety checks, trading safety for speed.
253
+ -->
254
+ この能力は強力で多彩です。
255
+ Rustはいくらかの[ compiler intrinsics] [ intrinsics ] をアンセーフ関数として公開しており、また、いくつかのアンセーフ関数は安全性検査を回避することで、安全性とスピードを引き換えています。
137
256
257
+ <!--
138
258
I’ll repeat again: even though you _can_ do arbitrary things in unsafe blocks
139
259
and functions doesn’t mean you should. The compiler will act as though you’re
140
260
upholding its invariants, so be careful!
261
+ -->
262
+ 繰り返しになりますが、アンセーフブロックと関数の内部で任意のことが _ できる_ としても、それをすべきだということを意味しません。コンパイラは、あなたが不変量を守っているかのように動作しますから、注意してください!
141
263
142
264
[ intrinsics ] : intrinsics.html
0 commit comments