Skip to content

Commit 5d79350

Browse files
committed
Merge pull request #87 from mandel59/unsafe
4.36 Unsafe
2 parents 1d4223b + 4a77252 commit 5d79350

File tree

2 files changed

+146
-10
lines changed

2 files changed

+146
-10
lines changed

1.6/ja/book/unsafe.md

Lines changed: 132 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,125 @@
11
% Unsafe
22

3+
<!--
34
Rust’s main draw is its powerful static guarantees about behavior. But safety
45
checks are conservative by nature: there are some programs that are actually
56
safe, but the compiler is not able to verify this is true. To write these kinds
67
of programs, we need to tell the compiler to relax its restrictions a bit. For
78
this, Rust has a keyword, `unsafe`. Code using `unsafe` has less restrictions
89
than normal code does.
9-
10+
-->
11+
Rustの主たる魅力は、プログラムの動作についての強力で静的な保証です。
12+
しかしながら、安全性検査は本来保守的なものです。
13+
すなわち、実際には安全なのに、そのことがコンパイラには検証できないプログラムがいくらか存在します。
14+
その類のプログラムを書くためには、制約を少し緩和するようコンパイラに対して伝えることが要ります。
15+
そのために、Rustには `unsafe` というキーワードがあります。
16+
`unsafe` を使ったコードは、普通のコードよりも制約が少なくなります。
17+
18+
<!--
1019
Let’s go over the syntax, and then we’ll talk semantics. `unsafe` is used in
1120
four contexts. The first one is to mark a function as unsafe:
21+
-->
22+
まずシンタックスをみて、それからセマンティクスについて話しましょう。
23+
`unsafe` は4つの場面で使われます。
24+
1つめは、関数がアンセーフであることを印付ける場合です。
1225

1326
```rust
1427
unsafe fn danger_will_robinson() {
15-
// scary stuff
28+
# // scary stuff
29+
// 恐ろしいもの
1630
}
1731
```
1832

33+
<!--
1934
All functions called from [FFI][ffi] must be marked as `unsafe`, for example.
2035
The second use of `unsafe` is an unsafe block:
36+
-->
37+
たとえば、[FFI][ffi]から呼び出されるすべての関数は`unsafe`で印付けることが必要です。
38+
`unsafe`の2つめの用途は、アンセーフブロックです。
2139

2240
[ffi]: ffi.html
2341

2442
```rust
2543
unsafe {
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
3353
unsafe 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 { }
4061
unsafe impl Scary for i32 {}
4162
```
4263

64+
<!--
4365
It’s important to be able to explicitly delineate code that may have bugs that
4466
cause big problems. If a Rust program segfaults, you can be sure it’s somewhere
4567
in the sections marked `unsafe`.
68+
-->
69+
大きな問題を引き起こすバグがあるかもしれないコードを明示できるのは重要なことです。
70+
もしRustのプログラムがセグメンテーション違反を起こしても、バグは `unsafe` で印付けられた区間のどこかにあると確信できます。
4671

47-
# What does ‘safe’ mean?
72+
# 「安全」とはどういう意味か?
73+
<!--# What does ‘safe’ mean?-->
4874

75+
<!--
4976
Safe, in the context of Rust, means ‘doesn’t do anything unsafe’. It’s also
5077
important to know that there are certain behaviors that are probably not
5178
desirable 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+
<!--
58101
Rust cannot prevent all kinds of software problems. Buggy code can and will be
59102
written in Rust. These things aren’t great, but they don’t qualify as `unsafe`
60103
specifically.
104+
-->
105+
Rustはソフトウェアが抱えるすべての種類の問題を防げるわけではありません。
106+
Rustでバグのあるコードを書くことはできますし、実際に書かれるでしょう。
107+
これらの動作は良いことではありませんが、特にアンセーフだとは見なされません。
61108

109+
<!--
62110
In addition, the following are all undefined behaviors in Rust, and must be
63111
avoided, 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+
<!--
94172
In both unsafe functions and unsafe blocks, Rust will let you do three things
95173
that you normally can not do. Just three. Here they are:
174+
-->
175+
アンセーフ関数・アンセーフブロックでは、Rustは普段できない3つのことをさせてくれます。たった3つです。それは、
96176

177+
<!--
97178
1. Access or update a [static mutable variable][static].
98179
2. Dereference a raw pointer.
99180
3. Call unsafe functions. This is the most powerful ability.
181+
-->
182+
1. [静的ミュータブル変数][static]のアクセスとアップデート。
183+
2. 生ポインタの参照外し。
184+
3. アンセーフ関数の呼び出し。これが最も強力な能力です。
100185

186+
<!--
101187
That’s it. It’s important that `unsafe` does not, for example, ‘turn off the
102188
borrow checker’. Adding `unsafe` to some random Rust code doesn’t change its
103189
semantics, it won’t just start accepting anything. But it will let you write
104190
things that _do_ break some of the rules.
191+
-->
192+
以上です。
193+
重要なのは、 `unsafe` が、たとえば「借用チェッカをオフにする」といったことを行わないことです。
194+
Rustのコードの適当な位置に `unsafe` を加えてもセマンティクスは変わらず、何でもただ受理するようになるということにはなりません。
195+
それでも、`unsafe` はルールのいくつかを破るコードを書けるようにはするのです。
105196

197+
<!--
106198
You 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
108200
around the methods provided by the library.
201+
-->
202+
また、`unsafe` キーワードは、Rust以外の言語とのインターフェースを書くときに遭遇するでしょう。
203+
ライブラリの提供するメソッドの周りに、安全な、Rustネイティブのインターフェースを書くことが推奨されています。
109204

205+
<!--
110206
Let’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+
<!--
114214
Rust has a feature called ‘`static mut`’ which allows for mutable global state.
115215
Doing so can cause a data race, and as such is inherently not safe. For more
116216
details, 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+
<!--
122228
Raw pointers let you do arbitrary pointer arithmetic, and can cause a number of
123229
different memory safety and security issues. In some senses, the ability to
124230
dereference an arbitrary pointer is one of the most dangerous things you can
125231
do. 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+
<!--
131243
This last ability works with both aspects of `unsafe`: you can only call
132244
functions marked `unsafe` from inside an unsafe block.
245+
-->
246+
この最後の能力は、`unsafe`の両面とともに働きます。
247+
すなわち、`unsafe`で印付けられた関数は、アンセーフブロックの内部からのみ呼び出すことができます。
133248

249+
<!--
134250
This ability is powerful and varied. Rust exposes some [compiler
135251
intrinsics][intrinsics] as unsafe functions, and some unsafe functions bypass
136252
safety checks, trading safety for speed.
253+
-->
254+
この能力は強力で多彩です。
255+
Rustはいくらかの[compiler intrinsics][intrinsics]をアンセーフ関数として公開しており、また、いくつかのアンセーフ関数は安全性検査を回避することで、安全性とスピードを引き換えています。
137256

257+
<!--
138258
I’ll repeat again: even though you _can_ do arbitrary things in unsafe blocks
139259
and functions doesn’t mean you should. The compiler will act as though you’re
140260
upholding its invariants, so be careful!
261+
-->
262+
繰り返しになりますが、アンセーフブロックと関数の内部で任意のことが _できる_ としても、それをすべきだということを意味しません。コンパイラは、あなたが不変量を守っているかのように動作しますから、注意してください!
141263

142264
[intrinsics]: intrinsics.html

TranslationTable.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
| block | ブロック
2525
| borrowing | 借用
2626
| bounds | 境界
27+
| bug | バグ
2728
| channel | チャネル
2829
| capture | キャプチャ
2930
| closure | クロージャ
@@ -36,11 +37,15 @@
3637
| continuous integration | 継続的インテグレーション
3738
| crate | クレート
3839
| dangling | ダングリング
40+
| data race | データ競合
41+
| deadlock | デッドロック
3942
| declaration statement | 宣言文
4043
| dereferencing | 参照外し
44+
| destructor | デストラクタ
4145
| destructuring | 分配
4246
| directory | ディレクトリ
4347
| directive | ディレクティブ
48+
| discriminant | 判別子
4449
| distribution | 配布物
4550
| diverge | ダイバージ
4651
| diverging | ダイバージング
@@ -59,6 +64,7 @@
5964
| hash | ハッシュ
6065
| identifier | 識別子
6166
| immutable | イミュータブル
67+
| implement | 実装する
6268
| initialize | 初期化する
6369
| input lifetime | 入力ライフタイム
6470
| install | インストール
@@ -68,6 +74,7 @@
6874
| keyword | キーワード
6975
| Intrinsics | Intrinsic
7076
| Lang Items | Lang Item
77+
| leak | リーク
7178
| lending | 貸付け
7279
| library | ライブラリ
7380
| lifetime | ライフタイム
@@ -83,11 +90,13 @@
8390
| mutable | ミュータブル
8491
| mutability | ミュータビリティ
8592
| mutable binding | ミュータブルな束縛
93+
| null | ヌル
8694
| offline | オフライン
8795
| opaque | オペーク
8896
| open source | オープンソース
8997
| option | オプション
9098
| output lifetime | 出力ライフタイム
99+
| overflow | オーバーフロー
91100
| owner | 所有者
92101
| ownership | 所有権
93102
| panic | パニック
@@ -98,12 +107,16 @@
98107
| platform | プラットフォーム
99108
| pointer | ポインタ
100109
| process | プロセス
110+
| raw pointer | 生ポインタ
101111
| re-assignment | 再代入
102112
| rebind | 再束縛
103113
| regression | リグレッション
104114
| release | リリース
105115
| return | 返す
116+
| safe | 安全
117+
| safety check | 安全性検査
106118
| scope | スコープ
119+
| scoped | スコープ化された
107120
| script | スクリプト
108121
| shadow | 覆い隠す
109122
| shadowing | シャドーイング
@@ -129,6 +142,7 @@
129142
| unsafe | アンセーフ
130143
| unsigned | 符号無し
131144
| unsized type | サイズ不定型
145+
| unwinding | 巻き戻し
132146
| variable | 変数
133147
| variable binding | 変数束縛
134148
| variant | ヴァリアント

0 commit comments

Comments
 (0)