Skip to content

Commit

Permalink
Get rid of zero length array extension (#1076)
Browse files Browse the repository at this point in the history
  • Loading branch information
mkornaukhov03 authored Sep 3, 2024
1 parent 3cb2120 commit e511730
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 86 deletions.
16 changes: 5 additions & 11 deletions runtime-core/core-types/decl/array_decl.inl
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,6 @@ struct array_size {
inline array_size &min(const array_size &other) noexcept;
};

#if defined(__clang__) || (defined(__GNUC__) && __GNUC__ < 10)
// clang complains about 'flexible array member x of type T[] with non-trivial destruction'
#define KPHP_ARRAY_TAIL_SIZE 0
#else
// gcc10 complains about out-of-bounds access to an array of zero size
#define KPHP_ARRAY_TAIL_SIZE
#endif

namespace dl {
template<class T, class TT, class T1>
Expand Down Expand Up @@ -95,23 +88,24 @@ private:
uint32_t string_size{0};
};

struct array_inner : array_inner_control {
struct alignas(array_bucket) array_inner : array_inner_control {
static constexpr uint32_t MAX_HASHTABLE_SIZE = (1 << 26);
//empty hash_entry identified by (next == EMPTY_POINTER)
static constexpr entry_pointer_type EMPTY_POINTER = 0;

array_bucket entries[KPHP_ARRAY_TAIL_SIZE];
static constexpr size_t ENTRIES_OFFSET = sizeof(array_inner);

inline bool is_vector() const noexcept __attribute__ ((always_inline));

inline array_bucket* entries() noexcept __attribute__ ((always_inline));
inline const array_bucket* entries() const noexcept __attribute__ ((always_inline));

inline list_hash_entry *get_entry(entry_pointer_type pointer) const __attribute__ ((always_inline));
inline entry_pointer_type get_pointer(list_hash_entry *entry) const __attribute__ ((always_inline));

inline const array_bucket *begin() const __attribute__ ((always_inline)) ubsan_supp("alignment");
inline const array_bucket *next(const array_bucket *ptr) const __attribute__ ((always_inline)) ubsan_supp("alignment");
inline const array_bucket *prev(const array_bucket *ptr) const __attribute__ ((always_inline)) ubsan_supp("alignment");
inline const array_bucket *end() const __attribute__ ((always_inline)) ubsan_supp("alignment");

inline array_bucket *begin() __attribute__ ((always_inline)) ubsan_supp("alignment");
inline array_bucket *next(array_bucket *ptr) __attribute__ ((always_inline)) ubsan_supp("alignment");
inline array_bucket *prev(array_bucket *ptr) __attribute__ ((always_inline)) ubsan_supp("alignment");
Expand Down
10 changes: 5 additions & 5 deletions runtime-core/core-types/decl/array_iterator.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class array_iterator {

inline key_type get_key() const noexcept __attribute__ ((always_inline)) {
if (self_->is_vector()) {
return key_type{static_cast<int64_t>(reinterpret_cast<value_type *>(entry_) - reinterpret_cast<value_type *>(self_->entries))};
return key_type{static_cast<int64_t>(reinterpret_cast<value_type *>(entry_) - reinterpret_cast<value_type *>(self_->entries()))};
}

if (is_string_key()) {
Expand Down Expand Up @@ -112,15 +112,15 @@ class array_iterator {
static inline array_iterator make_begin(std::add_const_t<array_type> &arr) noexcept __attribute__ ((always_inline)) {
static_assert(std::is_const<T>{}, "expected to be const");
return arr.is_vector()
? array_iterator{arr.p, arr.p->entries}
? array_iterator{arr.p, arr.p->entries()}
: array_iterator{arr.p, arr.p->begin()};
}

static inline array_iterator make_begin(std::remove_const_t<array_type> &arr) noexcept __attribute__ ((always_inline)) {
static_assert(!std::is_const<T>{}, "expected to be mutable");
if (arr.is_vector()) {
arr.mutate_if_vector_shared();
return array_iterator{arr.p, arr.p->entries};
return array_iterator{arr.p, arr.p->entries()};
}

arr.mutate_if_map_shared();
Expand All @@ -129,7 +129,7 @@ class array_iterator {

static inline array_iterator make_end(array_type &arr) noexcept __attribute__ ((always_inline)) {
return arr.is_vector()
? array_iterator{arr.p, reinterpret_cast<list_hash_type *>(reinterpret_cast<value_type *>(arr.p->entries) + arr.p->size)}
? array_iterator{arr.p, reinterpret_cast<list_hash_type *>(reinterpret_cast<value_type *>(arr.p->entries()) + arr.p->size)}
: array_iterator{arr.p, arr.p->end()};
}

Expand All @@ -147,7 +147,7 @@ class array_iterator {
return make_end(arr);
}

return array_iterator{arr.p, reinterpret_cast<list_hash_type *>(reinterpret_cast<value_type *>(arr.p->entries) + n)};
return array_iterator{arr.p, reinterpret_cast<list_hash_type *>(reinterpret_cast<value_type *>(arr.p->entries()) + n)};
}

if (n < -l / 2) {
Expand Down
Loading

0 comments on commit e511730

Please sign in to comment.