ODR違反の修正: ccbench_common を universal definitions 付きでビルドする#118
Merged
Conversation
ccbench_common (common/result.cc) defines and resize()s the global CCBenchResults (std::vector<Result>), but was compiled WITHOUT the universal CCBENCH_* definitions that protocol targets get via ccbench_add_protocol(). Result's layout depends on `#if ADD_ANALYSIS`, so sizeof(Result) differed between ccbench_common (ADD_ANALYSIS=0) and a protocol built with ADD_ANALYSIS=1. leaderBackoffWork()'s range-for over CCBenchResults then walked the buffer with the larger stride and read past its end -> heap-buffer-overflow, seen as a segfault for any BACK_OFF=1 + ADD_ANALYSIS=1 build (confirmed with AddressSanitizer: crash in leaderBackoffWork (backoff.hh), buffer allocated by initResult (result.cc) resize()). Apply ccbench_universal_definitions() to ccbench_common so it sees the same ADD_ANALYSIS (and other universal flags) as the protocols linking it. This compiles result.cc's ADD_ANALYSIS-only display code for the first time, exposing a dead variable (displayForwardingCount's num_txns, whose only uses are commented out) under -Werror=unused-variable; mark it [[maybe_unused]]. Verified with gcc-13: BACK_OFF=1 + ADD_ANALYSIS=1 now runs clean under ASan and emits backoff_latency_rate; default (ADD_ANALYSIS=0) and Release -Werror builds are unaffected. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
問題
CCBENCH_BACK_OFF=1とCCBENCH_ADD_ANALYSIS=1を同時指定したビルドが、起動直後のTxExecutor::leaderWork()で必ず segfault します(スレッド数非依存)。BACK_OFF=1単体・
ADD_ANALYSIS=1単体ではどちらも正常です。根本原因(ODR 違反)
Resultのレイアウトは#if ADD_ANALYSISでフィールドが増えsizeof(Result)が変わります。グローバルの
CCBenchResults(std::vector<Result>)を定義・resize()するのはcommon/result.ccで、これはccbench_common静的ライブラリにコンパイルされます。ところが
ccbench_commonは universal なCCBENCH_*定義を一切受けずにビルドされていました。プロトコル各ターゲットは
ccbench_add_protocol()経由でtarget_compile_definitions(... PRIVATE ...)として受け取りますが、ccbench_commonはset_compile_options()(警告/標準のみ)しか受けません。その結果
ADD_ANALYSIS=1のとき:ccbench_commonはResultをADD_ANALYSIS=0(小レイアウト)でコンパイルし、小さいstride で
resize()する。ADD_ANALYSIS=1(大レイアウト)でコンパイルし、leaderBackoffWork()の range-for が大きい stride でCCBenchResultsを走査する。end()は小 stride で設定されているため、大 stride のループが要素1つ分 buffer 末尾を追い越して読み、heap-buffer-overflow になります。
BACK_OFF=1のときだけ顕在化するのは、プロトコル側が
CCBenchResultsを走査する唯一の箇所leaderBackoffWork()が#if BACK_OFF内にあるためです。
AddressSanitizer:
heap-buffer-overflow ... READ of size 8
#0 loadAcquire<uint64_t> include/atomic_wrapper.hh:34
#1 leaderBackoffWork(...) include/backoff.hh:124
#2 TxExecutor::leaderWork() cc/silo/transaction.cc:597
... buffer allocated by:
#7 initResult(...) common/result.cc:26 (CCBenchResults.resize)
修正
CMakeLists.txt:ccbench_commonにccbench_universal_definitions()を適用し、リンクするプロトコルと同じ
ADD_ANALYSIS(および他の universal フラグ)を見せる。ccbench_common定義時にヘルパが必要なのでinclude(Options)を早めに追加。common/result.cc: 上記で#if ADD_ANALYSIS専用のdisplayForwardingCount()が初めてコンパイルされ、dead 変数
num_txns(使用箇所が全てコメントアウト)が-Werror=unused-variableに引っかかるので[[maybe_unused]]を付与。検証(gcc-13)
BACK_OFF=1 + ADD_ANALYSIS=1が ASan clean で動作しbackoff_latency_rateを出力。ADD_ANALYSIS=0)および Release-Werrorビルドは無影響(silo は 48 スレッドで従来どおり完走)。
再現(修正前)
cmake -S . -B build-aa -DCMAKE_BUILD_TYPE=Debug -DENABLE_SANITIZER=ON
-DCCBENCH_BACK_OFF=1 -DCCBENCH_ADD_ANALYSIS=1
cmake --build build-aa --target ycsb_silo.exe
build-aa/cc/silo/ycsb_silo.exe -thread_num=1 -ycsb_tuple_num=10000 -extime=1