[C++][Compute] year_month_day writes past its child buffers for inputs longer than 32 rows (wrong results, heap corruption)
メンテナーはふだん 1 日以内に返信
まだ誰も着手していません。
評価
調査の方向性
YearMonthDay::Exec(cpp/src/arrow/compute/kernels/scalar_temporal_unary.cc 内)を読み、子である3つの Int64Builder に対する Reserve(1) の呼び出しから始めてください。次に同じファイル内の ISOCalendar::Exec と比較します。こちらは子ごとに in.length を予約しています。予約がそろうように変更し、visitor の UnsafeAppend パスが境界内に収まることを確認してください。32行を超えるテスト(既存のフィクスチャはより短い)が date32、date64、timestamp の入力に対して正しい値を返し、fuzzing/valgrind で検出された範囲外書き込みがなくなれば完了です。
索引モデルが issue の本文から書いたものです。
説明
Describe the bug, including details regarding any error messages, version, and platform.
year_month_day returns wrong values for any input with more than 32 rows, and the kernel writes past the end of its output buffers, corrupting the heap.
import pyarrow as pa
import pyarrow.compute as pc
dates = pa.array(range(40), type=pa.int32()).view(pa.date32())
ymd = pc.year_month_day(dates)
for d, r in list(zip(dates.to_pylist(), ymd.to_pylist()))[30:36]:
print(d, r)
Output with pyarrow 26.0.1 on Linux x86_64:
1970-01-31 {'year': 1970, 'month': 1, 'day': 31}
1970-02-01 {'year': 1970, 'month': 2, 'day': 1}
1970-02-02 {'year': 0, 'month': 0, 'day': 0}
1970-02-03 {'year': 0, 'month': 0, 'day': 0}
1970-02-04 {'year': 0, 'month': 0, 'day': 0}
1970-02-05 {'year': 0, 'month': 0, 'day': 0}
Every row from index 32 on is wrong. The first rows of the month and day children are overwritten too: rows 0-7 come back as (1970, 1970, 2). On 1,000,000 dates, row 0 comes back as {'year': 1970, 'month': 1970, 'day': 1970}. While fuzzing compute kernels, I also saw later, unrelated calls in the same process segfault.
All accepted input types are affected: date32, date64, and timestamp with or without a time zone. I reproduced this in pyarrow 12.0.1, 15.0.2, 18.1.0, 21.0.0, 24.0.0 and 26.0.1. year, month, day and iso_calendar return correct results for the same inputs.
Cause. In YearMonthDay::Exec, each of the three child Int64Builders reserves only one element (scalar_temporal_unary.cc#L384):
RETURN_NOT_OK(field_builders[i]->Reserve(1));
The visitor then appends every value with UnsafeAppend, which does not check capacity. The builder rounds the reservation up to its 32-element minimum, so rows after the 32nd are written past the allocation. Null rows go through StructBuilder::AppendNull, which grows the children, so inputs with nulls show fewer wrong rows but are still affected. ISOCalendar::Exec builds the same kind of struct and reserves in.length for each child (#L1547).
Valgrind, run with ARROW_DEFAULT_MEMORY_POOL=system, reports the out-of-bounds write:
Invalid write of size 8
at ...YearMonthDayVisitValueFunction<..., arrow::Date32Type, ...>::Get(...)::{lambda(int)#1}
by ...YearMonthDay<std::chrono::duration<int, std::ratio<86400l, 1l> >, arrow::Date32Type>::Exec(...)
Address 0x1054a580 is 0 bytes after a block of size 256 alloc'd
256 bytes is 32 int64 values. The existing year_month_day tests use fixtures shorter than 32 rows, so they don't exercise this.
Component(s)
C++
- 主要言語
- C++
- スター
- 17.3k
- フォーク
- 4.5k
- 平均マージ
- 3日 14時間
- マージ済み PR(30日)
- 90
環境構築
- Dockerfile または Docker Compose ファイルあり
- プルリクエストのテンプレートあり
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
apache/arrow のほかの issue
-
[R] abandon_ship() looks up `call` with an inheriting get(), so its "no call" guard never fires対応中かも このイシューにリンクされたプルリクエストがオープン中、またはマージ済みです。 オープンComponent: R
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
メンテナーはふだん 1 日以内に返信
-
[C++][Python][Docs] Some compute functions are missing from the docs対応中かも @rajbhatt2406-collab が 1 日前に担当しました。 オープンComponent: C++ Component: Documentation Component: Python
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
apache/arrow#55762 · コメント 2 件 ·
メンテナーはふだん 1 日以内に返信
-
Component: C++ Type: enhancement
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
メンテナーはふだん 1 日以内に返信
-
Component: Continuous Integration Component: Developer Tools Type: enhancement
難易度 2/5 1〜3時間 初心者へのやさしさ 74/100
apache/arrow#55672 · コメント 4 件 ·
メンテナーはふだん 1 日以内に返信
-
Component: Python Type: enhancement
難易度 2/5 1〜3時間 初心者へのやさしさ 67/100
apache/arrow#55659 · コメント 1 件 · リアクション 1 件 ·
メンテナーはふだん 1 日以内に返信
似ている issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 69/100
メンテナーはふだん 2 日以内に返信
-
Missing repro Platform: Android
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
software-mansion/react-native-reanimated#11554 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 85/100
Niko1221/Strata#7211 · コメント 3 件 ·
メンテナーはふだん 5 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 64/100
-
bug
難易度 2/5 1〜3時間 初心者へのやさしさ 82/100
sleepyviper1213/redis#52 ·