BUG: DataStore constructors accept a tuple shape whose product overflows

オープン
#1,883 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 6 日以内に返信

まだ誰も着手していません。

評価

難易度
4/5
見積もり時間
3〜5日
初心者へのやさしさ
54/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
cpp
領域
backend

調査の方向性

Read src/simplnx/DataStructure/DataStore.hpp, focusing on both constructors and resizeTuples(), and review EmptyDataStore::CheckedPlaceholderProduct in src/simplnx/DataStructure/EmptyDataStore.hpp for the zero-dimension rule. Start by checking the existing DataStore tests in test/DataArrayTest.cpp; add coverage for overflowing tuple, component, element, and byte counts in both constructors. Done when those cases reject overflow while shapes containing a zero dimension remain valid.

索引モデルが issue の本文から書いたものです。

説明

bug

Problem

The DataStore constructors do not check the tuple-shape product or the component-shape product for overflow. When a product does not fit in usize, the count wraps around. The store then allocates a buffer for the wrapped count, but it keeps the original shape.

For example, DataStore<int32>({2^63, 2}, {1}, 7) succeeds on a 64-bit platform with 0 tuples and 0 values, and getTupleShape() returns {9223372036854775808, 2}. The expected behavior is that the constructor rejects the shape. Every later operation that uses the shape and the count together gets inconsistent values.

Occurs at develop commit 547f28a75e6325358fe7823e6dcb00defda81cd1.

Steps to Reproduce

  1. Build simplnx_test from develop (Release, 64-bit).
  2. Add the test in Verification Unit Test to test/DataArrayTest.cpp.
  3. Construct DataStore<int32> with the tuple shape {usize{1} << 63, 2}, the component shape {1} and the initial value 7.
  4. The constructor returns normally. No exception is thrown.

Cause

The (tupleShape, componentShape, initValue) constructor computes both counts with std::accumulate and no overflow check, then allocates getSize() values:

https://github.com/BlueQuartzSoftware/simplnx/blob/547f28a75e6325358fe7823e6dcb00defda81cd1/src/simplnx/DataStructure/DataStore.hpp#L89-L106

, m_NumComponents(std::accumulate(m_ComponentShape.cbegin(), m_ComponentShape.cend(), static_cast<usize>(1), std::multiplies<>()))
, m_NumTuples(std::accumulate(m_TupleShape.cbegin(), m_TupleShape.cend(), static_cast<usize>(1), std::multiplies<>()))
...
m_Data = std::unique_ptr<value_type[]>(new value_type[this->getSize()]);

2^63 * 2 is 2^64, which wraps to 0. getSize() is then 0, so new value_type[0] succeeds.

The constructor that takes ownership of a buffer computes the counts in the same way:

https://github.com/BlueQuartzSoftware/simplnx/blob/547f28a75e6325358fe7823e6dcb00defda81cd1/src/simplnx/DataStructure/DataStore.hpp#L117-L127

getNumberOfComponents() * getNumberOfTuples() (the element count) and the byte count of the allocation are not checked either.

Verification Unit Test

File: test/DataArrayTest.cpp

TEST_CASE("DataStore constructor rejects an overflowing tuple shape", "[simplnx][DataStore]")
{
  // The tuple-shape product 2^63 * 2 = 2^64 does not fit a 64-bit usize.
  const ShapeType overflowingTupleShape = {usize{1} << 63, 2};

  REQUIRE_THROWS(DataStore<int32>(overflowingTupleShape, ShapeType{1}, 7));
}

Command (from the build directory):

ctest -R "^DataStore constructor rejects an overflowing tuple shape$" --output-on-failure

Failure on develop (macOS arm64, AppleClang, Release):

DataArrayTest.cpp:860: FAILED:
  REQUIRE_THROWS( DataStore<int32>(overflowingTupleShape, ShapeType{1}, 7) )
because no exception was thrown where one was expected:
test cases: 1 | 1 failed
assertions: 1 | 1 failed

Proposed Fix

  • In both constructors, compute the tuple count, the component count, the element count and the byte count with overflow checks before the allocation, and throw (for example std::overflow_error) when a count does not fit in usize. A constructor cannot return a Result<>, so an exception is the only way to reject the shape.
  • A shape that contains a zero dimension has a count of zero. Check for a zero dimension before the multiplication, so that a large partial product of the other dimensions is not reported as an overflow. EmptyDataStore::CheckedPlaceholderProduct (src/simplnx/DataStructure/EmptyDataStore.hpp) already uses this approach.
  • Put the checked product in one helper that the constructors and resizeTuples() share.

Notes

  • PR #1834 adds the same tuple, element and byte overflow checks to DataStore::resizeTuples(), with the zero-dimension rule above. The constructors are not part of that PR.
  • StringStore and ListStore have the same unchecked product in resizeTuples() (#1884). An allocation above the AddressSanitizer limit is reported in #1885.
主要言語
C++
スター
18
フォーク
15
平均マージ
4日 23時間
マージ済み PR(30日)
42

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

BlueQuartzSoftware/simplnx のほかの issue

BlueQuartzSoftware/simplnx の issue をすべて見る

似ている issue

C++ の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。