annotate paper/akasha.tex @ 100:ebe838b83ada

Self review
author atton <atton@cr.ie.u-ryukyu.ac.jp>
date Sun, 12 Feb 2017 11:52:20 +0900
parents 2bc816f4af27
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
76
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1 \chapter{メタ計算ライブラリ akasha における検証}
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
2 \label{chapter:akasha}
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
3 第~\ref{chapter:cbc}章では Continuation based C 言語の概要と、CbC で記述された GearsOS について述べた。
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
4 GearsOS の持つメタ計算として、モデル検査的なアプローチで CodeGear の仕様を検証していく。
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
5
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
6 % {{{ モデル検査
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
7 \section{モデル検査}
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
8 モデル検査とは、ソフトウェアの全ての状態において仕様が満たされるかを確認するものである。
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
9 このモデル検査を行なうソフトウェアをモデル検査器と呼ぶ。
100
ebe838b83ada Self review
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 94
diff changeset
10 モデルは検査器は、仕様の定義とその検証ができる。
76
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
11 加えて、仕様を満たさない場合にはソフトウェアがどのような状態であったか反例を返す。
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
12
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
13 モデル検査器には Spin\cite{spin} やCBMC\cite{cbmc} などが存在する。
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
14
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
15 Spin は Promela と呼ばれる言語でモデルを記述し、その中に論理式として仕様を記述する。
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
16 論理式は assert でモデルの内部に埋め込まれ、並列に実行してもその仕様が満たされるかをチェックする。
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
17 また、Promela で記述されたモデルからC言語を生成することができる。
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
18 しかし、Promela で記述されたモデルは元のC言語とはかなり異なる構文をしており、ユーザが記述する難易度が高い。
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
19
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
20 そこで、モデルを個別に記述せずに実装そのものを検査するアプローチがある。
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
21 例えばモデル検査器 CBMC は C言語を直接検証できる。
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
22 CBMC でも仕様は論理式で記述され、 assert と組み合わせる。
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
23 C 言語の実行は通常の実行とは異なり、記号実行という形で実行される。
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
24 プログラム上の変数は記号として処理され、\verb/a < b/ といった条件式により分岐が行なわれたのなら、その条件を持つ場合の経路、持たない場合の経路、と分岐していくのである。
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
25
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
26 GearsOS におけるモデル検査的なアプローチはCBMC のように実装言語をそのまま検証できるようにしたい。
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
27 そのために、assert を利用した仕様の定義と、その検査、必要なら反例を提出するようなメタ計算を定義する。
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
28 このメタ計算をメタ計算ライブラリ akasha として実装した。
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
29
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
30 この章では、メタ計算ライブラリ akasha を用いて GearsOS のデータ構造を検証していく。
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
31 % }}}
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
32
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
33 % {{{ GearsOS における非破壊赤黒木
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
34 \section{GearsOS における非破壊赤黒木}
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
35 現状の GearsOS に実装されているメタ計算として、非破壊赤黒木が存在する。
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
36 非破壊赤黒木はユーザがデータを保存する際に利用することを想定している。
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
37 メタ計算として定義することで、ノーマルレベルからは木のバランスを考慮せず木への操作が行なえる。
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
38
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
39 なお、赤黒木とは二分探索木の一種であり、木のバランスを取るための情報として各ノードは赤か黒の色を持っている。
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
40
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
41 二分探索木の条件は以下である。
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
42
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
43 \begin{itemize}
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
44 \item 左の子孫の値は親の値より小さい
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
45 \item 右の子孫の値は親の値より大きい
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
46 \end{itemize}
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
47
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
48 加えて、赤黒木が持つ具体的な条件は以下のものである。
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
49
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
50 \begin{itemize}
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
51 \item 各ノードは赤か黒の色を持つ。
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
52 \item ルートノードの色は黒である。
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
53 \item 葉ノードの色は黒である。
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
54 \item 赤ノードは2つの黒ノードを子として持つ(よって赤ノードが続くことは無い)。
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
55 \item ルートから最下位ノードへの経路に含まれる黒ノードの数はどの最下位ノードでも一定である。
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
56 \end{itemize}
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
57
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
58
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
59 数値を要素に持つ赤黒木の例を図\ref{fig:rbtree}に示す。
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
60 条件に示されている通り、ルートノードは黒であり、赤ノードは連続していない。
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
61 加えて各最下位ノードへの経路に含まれる黒ノードの個数は全て2である。
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
62
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
63 \begin{figure}[htbp]
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
64 \begin{center}
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
65 \includegraphics[scale=0.5]{fig/rbtree.pdf}
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
66 \caption{赤黒木の例}
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
67 \label{fig:rbtree}
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
68 \end{center}
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
69 \end{figure}
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
70
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
71 赤黒木の持つ条件を言い変えるのなら、「木をルートから辿った際に最も長い経路は最も短い経路の高々二倍に収まる」とも言える。
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
72 この言い換えは「赤が続くことはない」という条件と「ルートから最下位への経路の黒ノードはどの最下位ノードでも同じ」であることから導ける。
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
73 具体的には、最短経路は「黒のみの経路」であり、最長経路は「黒と赤が交互に続く経路」となる。
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
74 この条件を言い変えた性質を仕様とし、検証していく。
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
75
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
76 GearsOS で実装されている赤黒木は特に非破壊赤黒木であり、一度構築した木構造は破壊される操作ごとに新しい木構造が生成される。
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
77 非破壊の性質を付与した理由として、並列実行時のデータの保存がある。
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
78 同じ赤黒木をロックせずに同時に更新した場合、ノードの値は実行順に依存したり、競合したりする。
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
79 しかし、ロックを行なって更新した場合は同じ木に対する処理に待ち合わせが発生し、全体の並列度が下がる。
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
80 この問題に対し GearsOS では、各スレッドは処理を行なう際には非破壊の木を利用することで並列度は保ち、値の更新が発生する時のみ木をアトミックな操作で置き換えることで競合を回避する。
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
81 具体的には木の操作を行なった後はルートのノードを元に CAS で置き換え、失敗した時は木を読み込み直して処理を再実行する。
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
82 CAS(Check and Set) とは、アトミックに値を置き換える操作であり、使う際は更新前の値と更新後の値を渡す。
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
83 CAS で渡された更新前の値が、保存している値と同じであれば競合していないために値の更新に成功し、異なる場合は他に書き込みがあったことを示すために値の更新が失敗する操作のことである。
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
84
100
ebe838b83ada Self review
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 94
diff changeset
85 非破壊赤黒木の実装の基本的な戦略は、変更したいノードへのルートノードからの経路を全て複製し、変更後に新たなルートノードとすることである。
ebe838b83ada Self review
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 94
diff changeset
86 この際に変更されていない部分は変更前の木と共有する(図\ref{fig:non-destructive-rbtree})。
76
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
87 これは一度構築された木構造は破壊されないという非破壊の性質を用いたメモリ使用量の最適化である。
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
88
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
89 \begin{figure}[htbp]
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
90 \begin{center}
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
91 \includegraphics[scale=0.5]{fig/non-destructive-rbtree}
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
92 \caption{非破壊赤黒木の編集}
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
93 \label{fig:non-destructive-rbtree}
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
94 \end{center}
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
95 \end{figure}
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
96
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
97 CbC を用いて赤黒木を実装する際の問題として、関数の呼び出しスタックが存在しないことがある。
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
98 C における実装では関数の再帰呼び出しによって木が辿るが、それが行なえない。
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
99 経路を辿るためには、ノードに親への参照を持たせるか、挿入や削除の際に辿った経路を記憶する必要がある。
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
100 ノードが親への参照を持つ非破壊木構造は共通部分の共有が行なえないため、経路を記憶する方法を使う。
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
101 経路の記憶にはスタックを用い、スタックは Meta DataSegment に保持する。
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
102
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
103 赤黒木を格納する DataSegment と Meta DataSegment の定義をリスト\ref{src:rbtree-context}に示す。
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
104 経路の記憶に用いるスタックは Meta DataSegment である Context 内部の \verb/node_stack/ である。
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
105 DataSegment は各ノード情報を持つ \verb/Node/構造体と、赤黒木を格納する \verb/Tree/構造体、挿入などで操作中の一時的な木を格納する \verb/Traverse/共用体などがある。
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
106
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
107 \lstinputlisting[label=src:rbtree-context, caption=赤黒木の DataSegment と Meta DataSegment] {src/rbtreeContext.h}
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
108
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
109 Meta DataSegment を初期化する Meta CodeSegment initLLRBContext をリスト\ref{src:init-rbtree-context}に示す。
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
110 この Meta CodeSegment ではメモリ領域の確保、CodeSegment 名と CodeSegment の実体の対応表の作成などを行なう。
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
111 メモリ領域はプログラムの起動時に一定数のメモリを確保し、ヒープとして \verb/heap/ フィールドに保持させる。
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
112 CodeSegment 名と CodeSegment の実体との対応は、enum で定義された CodeSegment 名の添字へと CodeSegment の関数ポインタを代入することにより持つ。
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
113 例えば \verb/Put/ の実体は \verb/put_stub/ である。
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
114 他にも DataSegment の初期化(リスト\ref{src:init-rbtree-context} 34-48)とスタックの初期化(リスト\ref{src:init-rbtree-context} 50-51)を行なう。
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
115
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
116 \lstinputlisting[label=src:init-rbtree-context, caption=赤黒木の Meta DataSegment の初期化を行なう Meta CodeSegment ] {src/initLLRBContext.c}
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
117
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
118 実際の赤黒木の実装に用いられている Meta CodeSegment の一例をリスト\ref{src:rbtree-insert-case-2}に示す。
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
119 Meta CodeSegment \verb/insertCase2/ は要素を挿入した場合に呼ばれる Meta CodeSegment の一つであり、親ノードの色によって処理を変える。
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
120 まず、色を確認するために経路を記憶しているスタックから親の情報を取り出す。
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
121 親の色が黒ならば処理を終了し、次の CodeSegment へと軽量継続する(リスト\ref{src:rbtree-insert-case-2} 5-8)。
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
122 親の色が赤であるならばさらに処理を続行して \verb/InsertCase3/ へと軽量継続する。
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
123 ここで、経路情報を再現するためにスタックへと親を再代入してから軽量継続を行なっている。
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
124 なお、Meta CodeSegment でも Context から DataSegment を展開する処理は stub によって行なわれる(リスト\ref{src:rbtree-insert-case-2} 14-16)。
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
125
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
126 \lstinputlisting[label=src:rbtree-insert-case-2, caption=赤黒木の実装に用いられている Meta CodeSegment例] {src/insertCase2.c}
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
127
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
128 % }}}
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
129
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
130 % {{{ メタ計算ライブラリ akasha を用いた赤黒木の実装の検証
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
131
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
132 \section{メタ計算ライブラリ akasha を用いた赤黒木の実装の検証}
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
133 赤黒木の仕様の定義とその確認を CbC で行なっていく。
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
134 仕様には赤黒木の利用方法などによっていくつかのものが考えられる。
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
135 赤黒木に対する操作の仕様と、その操作によって保証されるべき赤黒木の状態を示すと以下のようになる。
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
136
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
137 \begin{itemize}
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
138 \item 挿入したデータは参照できること
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
139 \item 削除したデータは参照できないこと
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
140 \item 値を更新した後は更新された値が参照されること
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
141 \item 操作を行なった後の木はバランスしていること
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
142 \end{itemize}
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
143
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
144 今回はバランスに関する仕様を確認する。
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
145 操作を挿入に限定し、どのような順番で要素を挿入しても木がバランスすることを検証する。
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
146 検証には当研究室で開発しているメタ計算ライブラリ akasha を用いる。
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
147
100
ebe838b83ada Self review
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 94
diff changeset
148 akasha では仕様は常に成り立つべき CbC の条件式として定義する。
76
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
149 具体的には Meta CodeSegment に定義した assert が仕様に相当する。
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
150 仕様の例として「木をルートから辿った際に最も長い経路は最も短い経路の高々2倍に収まる」という式を定義する(リスト\ref{src:assert})。
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
151
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
152 \lstinputlisting[label=src:assert, caption=木の高さに関する仕様記述] {src/assert.c}
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
153
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
154 リスト\ref{src:assert} で定義した仕様がプログラムの持つ全ての状態に成り立つかを確認する。
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
155 また、成り立たない場合には仕様に反する状態を反例として提出する。
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
156
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
157 まずは最も単純な検証として要素数を有限に固定し、その挿入順番を数え上げる。
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
158 最初に、検証の対象となる赤黒木と、検証に必要な DataSegment を含む Meta DataSegment を定義する(リスト\ref{src:akasha-context})。
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
159 これが akasha のレベルで利用する Meta DataSegment である。
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
160 赤黒木自体はユーザから見るとメタレベル計算であるが、今回はその実装の検証するため、赤黒木がノーマルレベルとなる。
92
c407b7403548 Add figure
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 78
diff changeset
161 よって akasha はメタメタレベルの計算とも考えられる(図~\ref{fig:metameta})。
76
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
162
92
c407b7403548 Add figure
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 78
diff changeset
163 \begin{figure}[htbp]
c407b7403548 Add figure
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 78
diff changeset
164 \begin{center}
c407b7403548 Add figure
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 78
diff changeset
165 \includegraphics[width=250pt]{fig/metameta.pdf}
c407b7403548 Add figure
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 78
diff changeset
166 \caption{akasha とメタの階層構造}
c407b7403548 Add figure
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 78
diff changeset
167 \label{fig:metameta}
c407b7403548 Add figure
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 78
diff changeset
168 \end{center}
c407b7403548 Add figure
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 78
diff changeset
169 \end{figure}
76
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
170
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
171 akasha が使う DataSegment は データの挿入順を数え上げるためには使う環状リスト \verb/Iterator/ とその要素 \verb/IterElem/、検証に使う情報を保持する \verb/AkashaInfo/、木をなぞる際に使う \verb/AkashaNode/ がある。
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
172
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
173 \lstinputlisting[label=src:akasha-context, caption=検証を行なうための Meta DataSegment] {src/akashaContext.h}
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
174
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
175 挿入順番の数え上げには環状リストを用いた深さ優先探索を用いる。
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
176 最初に検証する要素を全て持つ環状リストを作成し、木に挿入した要素を除きながら環状リストを複製していく。
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
177 環状リストが空になった時が組み合わせを一つ列挙し終えた状態となる。
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
178 列挙し終えた後、前の深さの環状リストを再現してリストの先頭を進めることで異なる組み合わせを列挙する。
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
179
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
180 仕様には木の高さが含まれるので、高さを取得する Meta CodeSegment が必要となる。
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
181 リスト\ref{src:get-min-height}に木の最も低い経路の長さを取得する Meta CodeSegment を示す。
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
182
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
183 木を辿るためのスタックに相当する \verb/AkshaNode/を用いて経路を保持しつつ、高さを確認している。
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
184 スタックが空であれば全てのノードを確認したので次の CodeSegment へと軽量継続を行なう。
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
185 空でなければ今辿っているノードが葉であるか確認し、葉ならば高さを更新して次のノードを確認するため自身へと軽量継続する。
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
186 葉でなければ高さを1増やして左右の子をスタックに積み、自身へと軽量継続を行なう。
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
187
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
188 \lstinputlisting[label=src:get-min-height, caption=木の最も短かい経路の長さを確認する Meta CodeSegment] {src/getMinHeight.c}
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
189
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
190 同様に最も高い高さを取得し、仕様であるリスト\ref{src:assert}の assert を挿入の度に実行する。
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
191 assert は CodeSegment の結合を行なうメタ計算である \verb/meta/ を上書きすることにより実現する。
92
c407b7403548 Add figure
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 78
diff changeset
192 イメージとしては、挿入を行なう Meta CodeSegment を利用するプログラム(図~\ref{fig:put}) の途中に検証用のメタ計算を挟むことで実現できる(図~\ref{fig:akashaPut})。
76
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
193
92
c407b7403548 Add figure
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 78
diff changeset
194 \begin{figure}[htbp]
c407b7403548 Add figure
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 78
diff changeset
195 \begin{center}
c407b7403548 Add figure
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 78
diff changeset
196 \includegraphics[width=300pt]{fig/put.pdf}
c407b7403548 Add figure
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 78
diff changeset
197 \caption{put を利用するプログラム}
c407b7403548 Add figure
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 78
diff changeset
198 \label{fig:put}
c407b7403548 Add figure
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 78
diff changeset
199 \end{center}
c407b7403548 Add figure
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 78
diff changeset
200 \end{figure}
c407b7403548 Add figure
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 78
diff changeset
201
c407b7403548 Add figure
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 78
diff changeset
202 \begin{figure}[htbp]
c407b7403548 Add figure
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 78
diff changeset
203 \begin{center}
c407b7403548 Add figure
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 78
diff changeset
204 \includegraphics[width=300pt]{fig/akashaPut.pdf}
c407b7403548 Add figure
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 78
diff changeset
205 \caption{put を利用するプログラムのメタを上書きする}
94
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 92
diff changeset
206 \label{fig:akashaPut}
92
c407b7403548 Add figure
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 78
diff changeset
207 \end{center}
c407b7403548 Add figure
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 78
diff changeset
208 \end{figure}
76
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
209
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
210 \verb/meta/ はリスト\ref{src:rbtree-insert-case-2}の \verb/insertCase2/ のように軽量継続を行なう際に CodeSegment 名と DataSegment を指定するものである。
94
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 92
diff changeset
211 検証を行なわない通常の \verb/meta/ の実装は CodeSegment 名から対応する実体への軽量継続であった(リスト\ref{src:default-meta})。
76
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
212
94
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 92
diff changeset
213 \lstinputlisting[label=src:default-meta, caption=通常の CodeSegment の軽量継続] {src/meta.c}
76
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
214
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
215 これを、検証を行なうように変更することで \verb/insertCase2/ といった赤黒木の実装のコードを修正することなく検証を行なうことができる。
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
216 検証を行ないながら軽量継続する \verb/meta/ はリスト\ref{src:akasha-meta}のように定義される。
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
217 実際の検証部分は \verb/PutAndGoToNextDepth/ の後に行なわれるため、直接は記述されていない。
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
218 この \verb/meta/ が行なうのは検証用にメモリの管理である。
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
219 状態の数え上げを行なう際に状態を保存したり、元の状態に戻す処理が行なわれる。
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
220 このメタ計算を用いた検証では、要素数13個までの任意の順で挿入の際に仕様が満たされることを確認できた。
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
221 また、赤黒木の処理内部に恣意的なバグを追加した際には反例を返した。
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
222
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
223 \lstinputlisting[label=src:akasha-meta, caption=検証を行なう CodeSegment の軽量継続] {src/akashaMeta.c}
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
224
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
225 % }}}
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
226
77
50c2d2f1a186 Update chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 76
diff changeset
227 % {{{ モデル検査器 CBMC との比較
50c2d2f1a186 Update chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 76
diff changeset
228
76
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
229 \section{モデル検査器 CBMC との比較}
94
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 92
diff changeset
230 akasha の比較対象として、 C言語の有限モデルチェッカ CBMC\cite{cbmc} を用いて赤黒木を検証した。
77
50c2d2f1a186 Update chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 76
diff changeset
231 CBMC は ANSI-C を記号実行し、仕様の否定となるような実行パターンが無いかを検証するツールである。
50c2d2f1a186 Update chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 76
diff changeset
232
50c2d2f1a186 Update chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 76
diff changeset
233 比較のために全く同じ赤黒木のソースコードを用いたいが、CbCの構文は厳密にはCとは異なるために変換が必要である。
50c2d2f1a186 Update chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 76
diff changeset
234 具体的には、\verb/__code/ を \verb/void/ に、 \verb/goto/ を \verb/return/ に置換することで機械的にC言語に変換できる。
50c2d2f1a186 Update chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 76
diff changeset
235
50c2d2f1a186 Update chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 76
diff changeset
236 CBMC における仕様は bool を返す式として記述するため、akashaと同様の仕様定義が利用できる(リスト\ref{src:cbmc-assert}。
50c2d2f1a186 Update chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 76
diff changeset
237 assert が true になるような実行パターンを CBMC が見付けると、その実行パターンが反例として出力される。
50c2d2f1a186 Update chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 76
diff changeset
238
50c2d2f1a186 Update chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 76
diff changeset
239 \lstinputlisting[label=src:cbmc-assert, caption=CBMC における仕様記述] {src/cbmc-assert.c}
50c2d2f1a186 Update chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 76
diff changeset
240
50c2d2f1a186 Update chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 76
diff changeset
241 挿入順の数え上げにはCBMCの機能に存在する非決定的な値 \verb/nondet_int()/ を用いた(リスト\ref{src:enumerate-inputs})。
50c2d2f1a186 Update chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 76
diff changeset
242 この \verb/nondet_int()/ 関数は int の持ちうる値の内から非決定的に値を取得する関数である。
50c2d2f1a186 Update chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 76
diff changeset
243 akasha では有限の要素個分の組み合わせを用いて挿入順の数え上げとしたが、CBMC では要素数回分だけランダムな値を入力させることで数え上げとする。
50c2d2f1a186 Update chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 76
diff changeset
244
50c2d2f1a186 Update chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 76
diff changeset
245 \lstinputlisting[label=src:enumerate-inputs, caption= CBMC における挿入順の数え上げ] {src/enumerate-inputs.c}
50c2d2f1a186 Update chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 76
diff changeset
246
50c2d2f1a186 Update chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 76
diff changeset
247 CBMCでは有限のステップ数だけC言語を記号実行し、その範囲内で仕様が満たされるかを確認する。
50c2d2f1a186 Update chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 76
diff changeset
248 条件分岐や繰り返しなどは展開されて実行される。
50c2d2f1a186 Update chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 76
diff changeset
249 基本的にはメモリの許す限り展開を行なうことができるが、今回の赤黒木の検証では411回まで展開することができた。
50c2d2f1a186 Update chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 76
diff changeset
250 この411回のうちの実行パスでは赤黒木の仕様は常に満たされる。
50c2d2f1a186 Update chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 76
diff changeset
251 しかし、この展開された回数は挿入された回数とは無関係であり、実際どの程度検証することができたか確認できない。
50c2d2f1a186 Update chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 76
diff changeset
252 実際、赤黒木に恣意的なバグを追加した際にも仕様の反例は得られず、CBMC で扱える範囲内では赤黒木の性質は検証できなかった。
50c2d2f1a186 Update chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 76
diff changeset
253
50c2d2f1a186 Update chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 76
diff changeset
254 よって、CBMCでは検証できない範囲の検証を akasha で行なえることが確認できた。
50c2d2f1a186 Update chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 76
diff changeset
255
50c2d2f1a186 Update chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 76
diff changeset
256 % }}}