ar.falsy.cat/content/note/info-tech/c-cpp-sanitizer.md
2023-06-21 07:36:18 +09:00

42 lines
1.8 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

---
title: C/C++のsanitizerの使い方
tags: [note, info-tech, development, programming-language, howto]
---
## 概要
- バグを検知するコードを埋め込むためのsanitizerと呼ばれる機能がある
- このページではsanitizerの使い方を説明する
## 使い方
### コンパイル時にsanitizerを埋め込む
- 種類ごとにオプションが異なる
- XXXにはSanitizer名をカンマ区切りで羅列する
- 代表的なSanitizer名については後述
```bash
g++ -fsanitizer=XXX a.cc
gcc -fsanitizer=XXX a.c
```
#### 代表的なSanitizer
- [ここ](https://gcc.gnu.org/onlinedocs/gcc/Instrumentation-Options.html)で`-fsanitizer=`を検索すると一覧が見れる
- `thread`は`address`や`undefined`と同居できない
|名前|説明|
|:--|:--|
|`address`|[Address Sanitizer](https://github.com/google/sanitizers/wiki/AddressSanitizer)|
|`undefined`|[Undefined Behavior Sanitizer](https://clang.llvm.org/docs/UndefinedBehaviorSanitizer.html)|
|`thread`|[Thread Sanitizer](https://github.com/google/sanitizers/wiki#threadsanitizer)|
### 実行時にsanitizerの挙動を指定する
- sanitizerが埋め込まれたバイナリに対して実行時にsanitizerの挙動を環境変数で指定することができる
- 詳細は各sanitizerの説明ページを参照
## 所感
- `thread`が強すぎる
- `thread`は実際に[Nf7](https://git.falsy.cat/nf7/nf7)のデバッグで使ってる
- sanitizerがなければ気づけなかったcondition raceがかなりあった
- 格好つけてロックフリーで書いたせい
- `thread`が`address`や`undefined`と一緒に使えないのが痛い
- どちらともを使うには,`thread`でビルド,テストした後に,オプションを変えてビルドし直し,同じテストを実行しなければならない