BUG: DataStore constructors accept a tuple shape whose product overflows

Aperta
#1,883 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 6 giorni

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
54/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
cpp
Ambito
backend

Direzione di ricerca

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.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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.
Lingua principale
C++
Stelle
18
Fork
15
Merge medio
4g 23h
PR unite (30g)
42

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di BlueQuartzSoftware/simplnx

Tutte le issue di BlueQuartzSoftware/simplnx

Issue simili

Altre issue su C++

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.