Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ubsan fixes 3 #3926

Merged
merged 72 commits into from
Dec 27, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
72 commits
Select commit Hold shift + click to select a range
ce0cfbc
Removed optimization that confuses UBSan in gcc [#CLICKHOUSE-2]
alexey-milovidov Dec 25, 2018
aedf858
Merge branch 'update-mariadb' into ubsan-fixes-3
alexey-milovidov Dec 25, 2018
0678311
Fixed UB [#CLICKHOUSE-2]
alexey-milovidov Dec 25, 2018
4cb4312
Fixed UB #3926
alexey-milovidov Dec 25, 2018
4c0b95a
Less dependencies [#CLICKHOUSE-2]
alexey-milovidov Dec 25, 2018
3be5543
Less dependencies [#CLICKHOUSE-2]
alexey-milovidov Dec 25, 2018
436b9b6
Less dependencies [#CLICKHOUSE-2]
alexey-milovidov Dec 25, 2018
a204ed2
Less dependencies [#CLICKHOUSE-2]
alexey-milovidov Dec 25, 2018
bbbefe2
Less dependencies [#CLICKHOUSE-2]
alexey-milovidov Dec 25, 2018
cfc1e20
Fixed UB #3926
alexey-milovidov Dec 25, 2018
07bfa2f
Added instruction for UBSan usage #3926
alexey-milovidov Dec 25, 2018
c858c14
Fixed UB #3926
alexey-milovidov Dec 25, 2018
1191f15
Less dependencies [#CLICKHOUSE-2]
alexey-milovidov Dec 25, 2018
5d1bc85
Less dependencies [#CLICKHOUSE-2]
alexey-milovidov Dec 25, 2018
4104a06
Added a test #3569
alexey-milovidov Dec 26, 2018
e8dc222
Fixed UB #3926
alexey-milovidov Dec 26, 2018
b1b1c67
Fixed error #3926
alexey-milovidov Dec 26, 2018
ee953b4
Fixed UB (requires performance testing) #3569
alexey-milovidov Dec 26, 2018
37bbc0e
Fixed UB #3926
alexey-milovidov Dec 26, 2018
985aa2c
Fixed build #3926
alexey-milovidov Dec 26, 2018
897199c
Fixed UB #3926
alexey-milovidov Dec 26, 2018
f2d789d
Fixed UB #3926
alexey-milovidov Dec 26, 2018
a2fae24
Fixed UB #3926
alexey-milovidov Dec 26, 2018
b7f085c
Fixed UB #3926
alexey-milovidov Dec 26, 2018
56bd082
Fixed UB #3926
alexey-milovidov Dec 26, 2018
b501319
Fixed UB #3569
alexey-milovidov Dec 26, 2018
665fc23
Fixed UB #3569
alexey-milovidov Dec 26, 2018
0ba7c1d
Fixed UB #3926
alexey-milovidov Dec 26, 2018
c86e861
Fixed error #3926
alexey-milovidov Dec 26, 2018
3d33851
Fixed error #3926
alexey-milovidov Dec 26, 2018
fc39ce8
Fixed UB #3926
alexey-milovidov Dec 26, 2018
5ca0148
Fixed UB #3926
alexey-milovidov Dec 26, 2018
4a45db2
Fixed UB #3926
alexey-milovidov Dec 26, 2018
ed50d90
Allowed UB in certain cases #3926
alexey-milovidov Dec 26, 2018
d54d2f7
Allowed UB in certain cases #3926
alexey-milovidov Dec 26, 2018
ee4c7fe
Allowed UB in certain cases #3926
alexey-milovidov Dec 26, 2018
14e1dfa
Fixed UB #3926
alexey-milovidov Dec 26, 2018
5abb2d0
Fixed UB #3926
alexey-milovidov Dec 26, 2018
15a218f
Fixed UB #3926
alexey-milovidov Dec 26, 2018
c88fd60
Merge branch 'master' of github.com:yandex/ClickHouse into ubsan-fixes-3
alexey-milovidov Dec 26, 2018
7509db5
Merge branch 'master' of github.com:yandex/ClickHouse into ubsan-fixes-3
alexey-milovidov Dec 26, 2018
380f27a
Empty commit
alexey-milovidov Dec 26, 2018
0d03fa8
Fixed error #3924
alexey-milovidov Dec 26, 2018
10362ab
Fixed build #3926
alexey-milovidov Dec 26, 2018
37a2d51
Added attribute #3926
alexey-milovidov Dec 26, 2018
2b58438
Added warning suppression for 3rd party library #3926
alexey-milovidov Dec 26, 2018
732f60c
Allowed some UB under UBSan #3926
alexey-milovidov Dec 26, 2018
c6ecb1d
Merge branch 'master' of github.com:yandex/ClickHouse into ubsan-fixes-3
alexey-milovidov Dec 26, 2018
2e32319
Addition to prev. revision #3926
alexey-milovidov Dec 27, 2018
701dfd8
Addition to prev. revision #3926
alexey-milovidov Dec 27, 2018
408f935
Addition to prev. revision #3926
alexey-milovidov Dec 27, 2018
2e20bd4
Addition to prev. revision #3926
alexey-milovidov Dec 27, 2018
e308d2e
Addition to prev. revision #3926
alexey-milovidov Dec 27, 2018
d6c7825
Addition to prev. revision #3926
alexey-milovidov Dec 27, 2018
3949213
Allowed some UB #3926
alexey-milovidov Dec 27, 2018
713a44c
Fixed UB #3926
alexey-milovidov Dec 27, 2018
3c84e47
Fixed UB #3926
alexey-milovidov Dec 27, 2018
c20f058
Moved code; added comment #3926
alexey-milovidov Dec 27, 2018
673fa80
Allowed some UB #3926
alexey-milovidov Dec 27, 2018
fec30ac
Allowed some UB #3926
alexey-milovidov Dec 27, 2018
1c0659a
Fixed test #3926
alexey-milovidov Dec 27, 2018
e8328c5
Fixed UB #3926
alexey-milovidov Dec 27, 2018
4c35a73
Fixed UB #3926
alexey-milovidov Dec 27, 2018
d2b7a84
Allow tests to execute in random order #3926
alexey-milovidov Dec 27, 2018
589d078
Fixed UB #3926
alexey-milovidov Dec 27, 2018
da5b62a
Fixed UB #3926
alexey-milovidov Dec 27, 2018
8b33718
Removed useless flag #3926
alexey-milovidov Dec 27, 2018
882531b
Fixed UB #3926
alexey-milovidov Dec 27, 2018
76dafd4
Fixed UB #3926
alexey-milovidov Dec 27, 2018
353c226
Fixed UB #3926
alexey-milovidov Dec 27, 2018
160ffe8
Fixed UB #3926
alexey-milovidov Dec 27, 2018
b94bfa5
Fixed test #3926
alexey-milovidov Dec 27, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion contrib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-old-style-cast -Wno-unused-function -Wno-unused-variable -Wno-unused-but-set-variable -Wno-unused-result -Wno-deprecated-declarations -Wno-non-virtual-dtor -Wno-maybe-uninitialized -Wno-format -Wno-misleading-indentation -Wno-implicit-fallthrough -Wno-class-memaccess -Wno-sign-compare -std=c++1z")
elseif (CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-unused-function -Wno-unused-variable -Wno-unused-result -Wno-deprecated-declarations -Wno-format -Wno-parentheses-equality")
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-old-style-cast -Wno-unused-function -Wno-unused-variable -Wno-unused-result -Wno-deprecated-declarations -Wno-non-virtual-dtor -Wno-format -std=c++1z")
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-old-style-cast -Wno-unused-function -Wno-unused-variable -Wno-unused-result -Wno-deprecated-declarations -Wno-non-virtual-dtor -Wno-format -Wno-inconsistent-missing-override -std=c++1z")
endif ()

if (USE_INTERNAL_BOOST_LIBRARY)
Expand Down
2 changes: 1 addition & 1 deletion dbms/src/AggregateFunctions/AggregateFunctionAvg.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ struct AggregateFunctionAvgData
UInt64 count = 0;

template <typename ResultT>
ResultT result() const
ResultT NO_SANITIZE_UNDEFINED result() const
{
if constexpr (std::is_floating_point_v<ResultT>)
if constexpr (std::numeric_limits<ResultT>::is_iec559)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ class AggregateFunctionBoundingRatio final : public IAggregateFunctionDataHelper
/** Calculates the slope of a line between leftmost and rightmost data points.
* (y2 - y1) / (x2 - x1)
*/
Float64 getBoundingRatio(const AggregateFunctionBoundingRatioData & data) const
Float64 NO_SANITIZE_UNDEFINED getBoundingRatio(const AggregateFunctionBoundingRatioData & data) const
{
if (data.empty)
return std::numeric_limits<Float64>::quiet_NaN();
Expand Down
13 changes: 9 additions & 4 deletions dbms/src/AggregateFunctions/AggregateFunctionGroupArray.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,14 @@ class GroupArrayNumericImpl final

if (!limit_num_elems)
{
cur_elems.value.insert(rhs_elems.value.begin(), rhs_elems.value.end(), arena);
if (rhs_elems.value.size())
cur_elems.value.insert(rhs_elems.value.begin(), rhs_elems.value.end(), arena);
}
else
{
UInt64 elems_to_insert = std::min(static_cast<size_t>(max_elems) - cur_elems.value.size(), rhs_elems.value.size());
cur_elems.value.insert(rhs_elems.value.begin(), rhs_elems.value.begin() + elems_to_insert, arena);
if (elems_to_insert)
cur_elems.value.insert(rhs_elems.value.begin(), rhs_elems.value.begin() + elems_to_insert, arena);
}
}

Expand Down Expand Up @@ -121,8 +123,11 @@ class GroupArrayNumericImpl final

offsets_to.push_back(offsets_to.back() + size);

typename ColumnVector<T>::Container & data_to = static_cast<ColumnVector<T> &>(arr_to.getData()).getData();
data_to.insert(this->data(place).value.begin(), this->data(place).value.end());
if (size)
{
typename ColumnVector<T>::Container & data_to = static_cast<ColumnVector<T> &>(arr_to.getData()).getData();
data_to.insert(this->data(place).value.begin(), this->data(place).value.end());
}
}

bool allocatesMemoryInArena() const override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ class AggreagteFunctionGroupUniqArrayGeneric
for (auto & rhs_elem : rhs_set)
{
cur_set.emplace(rhs_elem, it, inserted);
if (inserted)
if (inserted && it->size)
it->data = arena->insert(it->data, it->size);
}
}
Expand Down
10 changes: 5 additions & 5 deletions dbms/src/AggregateFunctions/AggregateFunctionStatisticsSimple.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,12 @@ struct VarMoments
readPODBinary(*this, buf);
}

T getPopulation() const
T NO_SANITIZE_UNDEFINED getPopulation() const
{
return (m2 - m1 * m1 / m0) / m0;
}

T getSample() const
T NO_SANITIZE_UNDEFINED getSample() const
{
if (m0 == 0)
return std::numeric_limits<T>::quiet_NaN();
Expand Down Expand Up @@ -177,12 +177,12 @@ struct CovarMoments
readPODBinary(*this, buf);
}

T getPopulation() const
T NO_SANITIZE_UNDEFINED getPopulation() const
{
return (xy - x1 * y1 / m0) / m0;
}

T getSample() const
T NO_SANITIZE_UNDEFINED getSample() const
{
if (m0 == 0)
return std::numeric_limits<T>::quiet_NaN();
Expand Down Expand Up @@ -232,7 +232,7 @@ struct CorrMoments
readPODBinary(*this, buf);
}

T get() const
T NO_SANITIZE_UNDEFINED get() const
{
return (m0 * xy - x1 * y1) / sqrt((m0 * x2 - x1 * x1) * (m0 * y2 - y1 * y1));
}
Expand Down
20 changes: 10 additions & 10 deletions dbms/src/AggregateFunctions/Helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ static IAggregateFunction * createWithNumericType(const IDataType & argument_typ
if (which.idx == TypeIndex::TYPE) return new AggregateFunctionTemplate<TYPE>(std::forward<TArgs>(args)...);
FOR_NUMERIC_TYPES(DISPATCH)
#undef DISPATCH
if (which.idx == TypeIndex::Enum8) return new AggregateFunctionTemplate<UInt8>(std::forward<TArgs>(args)...);
if (which.idx == TypeIndex::Enum16) return new AggregateFunctionTemplate<UInt16>(std::forward<TArgs>(args)...);
if (which.idx == TypeIndex::Enum8) return new AggregateFunctionTemplate<Int8>(std::forward<TArgs>(args)...);
if (which.idx == TypeIndex::Enum16) return new AggregateFunctionTemplate<Int16>(std::forward<TArgs>(args)...);
return nullptr;
}

Expand All @@ -41,8 +41,8 @@ static IAggregateFunction * createWithNumericType(const IDataType & argument_typ
if (which.idx == TypeIndex::TYPE) return new AggregateFunctionTemplate<TYPE, Data>(std::forward<TArgs>(args)...);
FOR_NUMERIC_TYPES(DISPATCH)
#undef DISPATCH
if (which.idx == TypeIndex::Enum8) return new AggregateFunctionTemplate<UInt8, Data>(std::forward<TArgs>(args)...);
if (which.idx == TypeIndex::Enum16) return new AggregateFunctionTemplate<UInt16, Data>(std::forward<TArgs>(args)...);
if (which.idx == TypeIndex::Enum8) return new AggregateFunctionTemplate<Int8, Data>(std::forward<TArgs>(args)...);
if (which.idx == TypeIndex::Enum16) return new AggregateFunctionTemplate<Int16, Data>(std::forward<TArgs>(args)...);
return nullptr;
}

Expand All @@ -54,8 +54,8 @@ static IAggregateFunction * createWithNumericType(const IDataType & argument_typ
if (which.idx == TypeIndex::TYPE) return new AggregateFunctionTemplate<TYPE, Data<TYPE>>(std::forward<TArgs>(args)...);
FOR_NUMERIC_TYPES(DISPATCH)
#undef DISPATCH
if (which.idx == TypeIndex::Enum8) return new AggregateFunctionTemplate<UInt8, Data<UInt8>>(std::forward<TArgs>(args)...);
if (which.idx == TypeIndex::Enum16) return new AggregateFunctionTemplate<UInt16, Data<UInt16>>(std::forward<TArgs>(args)...);
if (which.idx == TypeIndex::Enum8) return new AggregateFunctionTemplate<Int8, Data<Int8>>(std::forward<TArgs>(args)...);
if (which.idx == TypeIndex::Enum16) return new AggregateFunctionTemplate<Int16, Data<Int16>>(std::forward<TArgs>(args)...);
return nullptr;
}

Expand Down Expand Up @@ -106,8 +106,8 @@ static IAggregateFunction * createWithTwoNumericTypesSecond(const IDataType & se
if (which.idx == TypeIndex::TYPE) return new AggregateFunctionTemplate<FirstType, TYPE>(std::forward<TArgs>(args)...);
FOR_NUMERIC_TYPES(DISPATCH)
#undef DISPATCH
if (which.idx == TypeIndex::Enum8) return new AggregateFunctionTemplate<FirstType, UInt8>(std::forward<TArgs>(args)...);
if (which.idx == TypeIndex::Enum16) return new AggregateFunctionTemplate<FirstType, UInt16>(std::forward<TArgs>(args)...);
if (which.idx == TypeIndex::Enum8) return new AggregateFunctionTemplate<FirstType, Int8>(std::forward<TArgs>(args)...);
if (which.idx == TypeIndex::Enum16) return new AggregateFunctionTemplate<FirstType, Int16>(std::forward<TArgs>(args)...);
return nullptr;
}

Expand All @@ -121,9 +121,9 @@ static IAggregateFunction * createWithTwoNumericTypes(const IDataType & first_ty
FOR_NUMERIC_TYPES(DISPATCH)
#undef DISPATCH
if (which.idx == TypeIndex::Enum8)
return createWithTwoNumericTypesSecond<UInt8, AggregateFunctionTemplate>(second_type, std::forward<TArgs>(args)...);
return createWithTwoNumericTypesSecond<Int8, AggregateFunctionTemplate>(second_type, std::forward<TArgs>(args)...);
if (which.idx == TypeIndex::Enum16)
return createWithTwoNumericTypesSecond<UInt16, AggregateFunctionTemplate>(second_type, std::forward<TArgs>(args)...);
return createWithTwoNumericTypesSecond<Int16, AggregateFunctionTemplate>(second_type, std::forward<TArgs>(args)...);
return nullptr;
}

Expand Down
2 changes: 1 addition & 1 deletion dbms/src/Columns/ColumnAggregateFunction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ const char * ColumnAggregateFunction::deserializeAndInsertFromArena(const char *
* as we cannot legally compare pointers after last element + 1 of some valid memory region.
* Probably this will not work under UBSan.
*/
ReadBufferFromMemory read_buffer(src_arena, std::numeric_limits<char *>::max() - src_arena);
ReadBufferFromMemory read_buffer(src_arena, std::numeric_limits<char *>::max() - src_arena - 1);
func->deserialize(data.back(), read_buffer, &dst_arena);

return read_buffer.position();
Expand Down
8 changes: 4 additions & 4 deletions dbms/src/Columns/ColumnArray.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,13 +134,13 @@ StringRef ColumnArray::getDataAt(size_t n) const
* since it contains only the data laid in succession, but not the offsets.
*/

size_t array_size = sizeAt(n);
if (array_size == 0)
return StringRef();

size_t offset_of_first_elem = offsetAt(n);
StringRef first = getData().getDataAtWithTerminatingZero(offset_of_first_elem);

size_t array_size = sizeAt(n);
if (array_size == 0)
return StringRef(first.data, 0);

size_t offset_of_last_elem = getOffsets()[n] - 1;
StringRef last = getData().getDataAtWithTerminatingZero(offset_of_last_elem);

Expand Down
4 changes: 2 additions & 2 deletions dbms/src/Columns/ColumnArray.h
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,8 @@ class ColumnArray final : public COWPtrHelper<IColumn, ColumnArray>
ColumnPtr data;
ColumnPtr offsets;

size_t ALWAYS_INLINE offsetAt(size_t i) const { return getOffsets()[i - 1]; }
size_t ALWAYS_INLINE sizeAt(size_t i) const { return getOffsets()[i] - getOffsets()[i - 1]; }
size_t ALWAYS_INLINE offsetAt(ssize_t i) const { return getOffsets()[i - 1]; }
size_t ALWAYS_INLINE sizeAt(ssize_t i) const { return getOffsets()[i] - getOffsets()[i - 1]; }


/// Multiply values if the nested column is ColumnVector<T>.
Expand Down
5 changes: 3 additions & 2 deletions dbms/src/Columns/ColumnDecimal.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <cmath>

#include <Columns/IColumn.h>
#include <Columns/ColumnVectorHelper.h>


namespace DB
Expand Down Expand Up @@ -53,13 +54,13 @@ class DecimalPaddedPODArray : public PaddedPODArray<T>

/// A ColumnVector for Decimals
template <typename T>
class ColumnDecimal final : public COWPtrHelper<IColumn, ColumnDecimal<T>>
class ColumnDecimal final : public COWPtrHelper<ColumnVectorHelper, ColumnDecimal<T>>
{
static_assert(IsDecimalNumber<T>);

private:
using Self = ColumnDecimal;
friend class COWPtrHelper<IColumn, Self>;
friend class COWPtrHelper<ColumnVectorHelper, Self>;

public:
using Container = DecimalPaddedPODArray<T>;
Expand Down
7 changes: 4 additions & 3 deletions dbms/src/Columns/ColumnFixedString.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
#pragma once

#include <string.h> // memcpy
#include <string.h> // memcmp

#include <Common/PODArray.h>
#include <Columns/IColumn.h>
#include <Columns/ColumnVectorHelper.h>


namespace DB
Expand All @@ -12,10 +13,10 @@ namespace DB
/** A column of values of "fixed-length string" type.
* If you insert a smaller string, it will be padded with zero bytes.
*/
class ColumnFixedString final : public COWPtrHelper<IColumn, ColumnFixedString>
class ColumnFixedString final : public COWPtrHelper<ColumnVectorHelper, ColumnFixedString>
{
public:
friend class COWPtrHelper<IColumn, ColumnFixedString>;
friend class COWPtrHelper<ColumnVectorHelper, ColumnFixedString>;

using Chars = PaddedPODArray<UInt8>;

Expand Down
4 changes: 2 additions & 2 deletions dbms/src/Columns/ColumnString.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ class ColumnString final : public COWPtrHelper<IColumn, ColumnString>
/// For convenience, every string ends with terminating zero byte. Note that strings could contain zero bytes in the middle.
Chars chars;

size_t ALWAYS_INLINE offsetAt(size_t i) const { return offsets[i - 1]; }
size_t ALWAYS_INLINE offsetAt(ssize_t i) const { return offsets[i - 1]; }

/// Size of i-th element, including terminating zero.
size_t ALWAYS_INLINE sizeAt(size_t i) const { return offsets[i] - offsets[i - 1]; }
size_t ALWAYS_INLINE sizeAt(ssize_t i) const { return offsets[i] - offsets[i - 1]; }

template <bool positive>
struct less;
Expand Down
36 changes: 3 additions & 33 deletions dbms/src/Columns/ColumnVector.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <cmath>
#include <Columns/IColumn.h>
#include <Columns/ColumnVectorHelper.h>
#include <common/unaligned.h>


Expand Down Expand Up @@ -86,47 +87,16 @@ template <> struct CompareHelper<Float32> : public FloatCompareHelper<Float32> {
template <> struct CompareHelper<Float64> : public FloatCompareHelper<Float64> {};


/** To implement `get64` function.
*/
template <typename T>
inline UInt64 unionCastToUInt64(T x) { return x; }

template <> inline UInt64 unionCastToUInt64(Float64 x)
{
union
{
Float64 src;
UInt64 res;
};

src = x;
return res;
}

template <> inline UInt64 unionCastToUInt64(Float32 x)
{
union
{
Float32 src;
UInt64 res;
};

res = 0;
src = x;
return res;
}


/** A template for columns that use a simple array to store.
*/
template <typename T>
class ColumnVector final : public COWPtrHelper<IColumn, ColumnVector<T>>
class ColumnVector final : public COWPtrHelper<ColumnVectorHelper, ColumnVector<T>>
{
static_assert(!IsDecimalNumber<T>);

private:
using Self = ColumnVector;
friend class COWPtrHelper<IColumn, Self>;
friend class COWPtrHelper<ColumnVectorHelper, Self>;

struct less;
struct greater;
Expand Down
39 changes: 39 additions & 0 deletions dbms/src/Columns/ColumnVectorHelper.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#pragma once

#include <Columns/IColumn.h>


namespace DB
{

/** Allows to access internal array of ColumnVector or ColumnFixedString without cast to concrete type.
* We will inherit ColumnVector and ColumnFixedString from this class instead of IColumn.
* Assumes data layout of ColumnVector, ColumnFixedString and PODArray.
*
* Why it is needed?
*
* There are some algorithms that specialize on the size of data type but doesn't care about concrete type.
* The same specialization may work for UInt64, Int64, Float64, FixedString(8), if it only does byte moving and hashing.
* To avoid code bloat and compile time increase, we can use single template instantiation for these cases
* and just static_cast pointer to some single column type (e. g. ColumnUInt64) assuming that all types have identical memory layout.
*
* But this static_cast (downcast to unrelated type) is illegal according to the C++ standard and UBSan warns about it.
* To allow functional tests to work under UBSan we have to separate some base class that will present the memory layout in explicit way,
* and we will do static_cast to this class.
*/
class ColumnVectorHelper : public IColumn
{
public:
const char * getRawDataBegin() const
{
return *reinterpret_cast<const char * const *>(reinterpret_cast<const char *>(this) + sizeof(*this));
}

template <size_t ELEMENT_SIZE>
void insertRawData(const char * ptr)
{
return reinterpret_cast<PODArrayBase<ELEMENT_SIZE, 4096, Allocator<false>, 15, 16> *>(reinterpret_cast<char *>(this) + sizeof(*this))->push_back_raw(ptr);
}
};

}
8 changes: 7 additions & 1 deletion dbms/src/Common/Arena.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class Arena : private boost::noncopyable
static constexpr size_t pad_right = 15;

/// Contiguous chunk of memory and pointer to free space inside it. Member of single-linked list.
struct Chunk : private Allocator<false> /// empty base optimization
struct alignas(16) Chunk : private Allocator<false> /// empty base optimization
{
char * begin;
char * pos;
Expand Down Expand Up @@ -149,6 +149,12 @@ class Arena : private boost::noncopyable
} while (true);
}

template <typename T>
T * alloc()
{
return reinterpret_cast<T *>(alignedAlloc(sizeof(T), alignof(T)));
}

/** Rollback just performed allocation.
* Must pass size not more that was just allocated.
*/
Expand Down
Loading