Skip to content

Commit 9f07e18

Browse files
committed
Harden Turbo traversal, arena, and iterable parity
1 parent df059f7 commit 9f07e18

15 files changed

Lines changed: 236 additions & 179 deletions

‎src/Internal/CombinationsHelper.php‎

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,34 +3,18 @@
33
namespace PHPStan\Internal;
44

55
use PHPStan\Turbo\ShadowedByTurboExtension;
6-
use Traversable;
7-
use function array_pop;
86

97
#[ShadowedByTurboExtension(implementation: __DIR__ . '/../../turbo-ext/src/CombinationsHelper.cpp')]
108
final class CombinationsHelper
119
{
1210

1311
/**
1412
* @param array<iterable<mixed>> $arrays
15-
* @return Traversable<list<mixed>>
13+
* @return iterable<list<mixed>>
1614
*/
1715
public static function combinations(array $arrays): iterable
1816
{
19-
// from https://stackoverflow.com/a/70800936/565782 by Arnaud Le Blanc
20-
if ($arrays === []) {
21-
yield [];
22-
return;
23-
}
24-
25-
$last = array_pop($arrays);
26-
27-
foreach (self::combinations($arrays) as $combination) {
28-
foreach ($last as $elem) {
29-
$comb = $combination;
30-
$comb[] = $elem;
31-
yield $comb;
32-
}
33-
}
17+
return IterableHelper::combinations($arrays);
3418
}
3519

3620
}

‎src/Internal/IterableHelper.php‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Internal;
4+
5+
use PHPStan\Turbo\ReferencedByTurboExtension;
6+
use function array_pop;
7+
8+
/** @internal */
9+
#[ReferencedByTurboExtension(key: 'iterableHelper')]
10+
final class IterableHelper
11+
{
12+
13+
/**
14+
* @param array<iterable<mixed>> $iterables
15+
* @return iterable<list<mixed>>
16+
*/
17+
public static function combinations(array $iterables): iterable
18+
{
19+
if ($iterables === []) {
20+
yield [];
21+
return;
22+
}
23+
24+
$last = array_pop($iterables);
25+
26+
foreach (self::combinations($iterables) as $combination) {
27+
foreach ($last as $value) {
28+
$next = $combination;
29+
$next[] = $value;
30+
yield $next;
31+
}
32+
}
33+
}
34+
35+
/**
36+
* @template T
37+
* @param list<T> $values
38+
* @return iterable<T>
39+
*/
40+
public static function yieldValues(array $values): iterable
41+
{
42+
yield from $values;
43+
}
44+
45+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Internal;
4+
5+
use Generator;
6+
use PHPUnit\Framework\TestCase;
7+
use function iterator_to_array;
8+
9+
class IterableHelperTest extends TestCase
10+
{
11+
12+
public function testYieldValuesReturnsGenerator(): void
13+
{
14+
$values = [['fact'], ['another fact']];
15+
$iterator = IterableHelper::yieldValues($values);
16+
17+
$this->assertInstanceOf(Generator::class, $iterator);
18+
$this->assertSame($values, iterator_to_array($iterator));
19+
}
20+
21+
}

‎turbo-ext/src/ArenaCache.cpp‎

Lines changed: 37 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
*
2020
* Concurrency is lock-free and write-once:
2121
*
22-
* - allocation is a fetch-add bump cursor in the arena header;
22+
* - allocation is a compare-and-swap bump cursor in the arena header;
2323
* - publication is a compare-and-swap of an index slot from 0 to the record
2424
* offset, performed only after the record bytes are fully written (release
2525
* ordering; readers load slots with acquire ordering). A torn record is
@@ -173,22 +173,27 @@ static zend_always_inline bool atomicCasRelease(uint64_t *cell, uint64_t expecte
173173
#endif
174174
}
175175

176-
static zend_always_inline uint64_t atomicFetchAdd(uint64_t *cell, uint64_t add)
177-
{
178-
#ifdef _MSC_VER
179-
return (uint64_t) InterlockedExchangeAdd64((volatile LONG64 *) cell, (LONG64) add);
180-
#else
181-
return __atomic_fetch_add(cell, add, __ATOMIC_ACQ_REL);
182-
#endif
183-
}
184-
185176
/* }}} */
186177

187178
static zend_always_inline uint64_t alignUp8(uint64_t value)
188179
{
189180
return (value + 7) & ~(uint64_t) 7;
190181
}
191182

183+
static zend_always_inline bool checkedAdd(uint64_t left, uint64_t right, uint64_t *result)
184+
{
185+
if (right > UINT64_MAX - left) return false;
186+
*result = left + right;
187+
return true;
188+
}
189+
190+
static zend_always_inline bool checkedAlignUp8(uint64_t value, uint64_t *result)
191+
{
192+
if (value > UINT64_MAX - 7) return false;
193+
*result = alignUp8(value);
194+
return true;
195+
}
196+
192197
static uint64_t fnv1a64(const void *data, size_t len)
193198
{
194199
uint64_t hash = 0xcbf29ce484222325ULL;
@@ -639,9 +644,12 @@ struct RecordView
639644

640645
static bool recordAt(uint64_t offset, RecordView *view)
641646
{
642-
if (offset < arenaDataStart() || offset + sizeof(RecordHeader) > pt_arena_total) return false;
647+
if (offset < arenaDataStart() || offset > pt_arena_total || sizeof(RecordHeader) > pt_arena_total - offset) return false;
643648
const RecordHeader *header = (const RecordHeader *) ((char *) pt_arena_base + offset);
644-
uint64_t payloadStart = alignUp8(offset + sizeof(RecordHeader) + header->keyLen);
649+
uint64_t keyStart = offset + sizeof(RecordHeader);
650+
if (header->keyLen > pt_arena_total - keyStart) return false;
651+
uint64_t payloadStart;
652+
if (!checkedAlignUp8(keyStart + header->keyLen, &payloadStart)) return false;
645653
if (payloadStart > pt_arena_total || header->payloadLen > pt_arena_total - payloadStart) return false;
646654
view->header = header;
647655
view->key = (const char *) (header + 1);
@@ -671,9 +679,21 @@ static void publishRecord(const char *key, size_t keyLen, uint32_t kind, const W
671679
{
672680
if (pt_arena_base == NULL || keyLen > UINT32_MAX) return;
673681

674-
uint64_t recordSize = alignUp8(sizeof(RecordHeader) + keyLen) + payload.bytes.size();
675-
uint64_t offset = atomicFetchAdd(&arenaHeader()->allocCursor, alignUp8(recordSize));
676-
if (offset > pt_arena_total || recordSize > pt_arena_total - offset) return; /* arena full: analysis continues, just unshared */
682+
uint64_t payloadOffset;
683+
uint64_t recordSize;
684+
uint64_t allocationSize;
685+
if (!checkedAlignUp8(sizeof(RecordHeader) + (uint64_t) keyLen, &payloadOffset)
686+
|| !checkedAdd(payloadOffset, (uint64_t) payload.bytes.size(), &recordSize)
687+
|| !checkedAlignUp8(recordSize, &allocationSize)) {
688+
return;
689+
}
690+
691+
uint64_t offset;
692+
for (;;) {
693+
offset = atomicLoadAcquire(&arenaHeader()->allocCursor);
694+
if (offset > pt_arena_total || allocationSize > pt_arena_total - offset) return; /* arena full: analysis continues, just unshared */
695+
if (atomicCasRelease(&arenaHeader()->allocCursor, offset, offset + allocationSize)) break;
696+
}
677697

678698
/* The callers checked the index before building the payload; check it once
679699
* more before writing it. Building a record takes long enough for another
@@ -690,7 +710,7 @@ static void publishRecord(const char *key, size_t keyLen, uint32_t kind, const W
690710
header.payloadLen = payload.bytes.size();
691711
memcpy(record, &header, sizeof(header));
692712
memcpy(record + sizeof(header), key, keyLen);
693-
memcpy(record + alignUp8(sizeof(header) + keyLen), payload.bytes.data(), payload.bytes.size());
713+
memcpy(record + payloadOffset, payload.bytes.data(), payload.bytes.size());
694714

695715
uint64_t hash = fnv1a64(key, keyLen);
696716
uint64_t mask = INDEX_SLOT_COUNT - 1;
@@ -844,7 +864,7 @@ static bool hashRecordFind(const RecordView &view, const char *entryKey, size_t
844864
uint64_t entryOffset;
845865
memcpy(&entryOffset, slots + ((hash + probe) & mask) * sizeof(uint64_t), sizeof(entryOffset));
846866
if (entryOffset == 0) return false;
847-
if (entryOffset < entriesStart || entryOffset + sizeof(uint32_t) > payloadLen) return false;
867+
if (entryOffset < entriesStart || entryOffset > payloadLen || sizeof(uint32_t) > payloadLen - entryOffset) return false;
848868
uint32_t keyLen;
849869
memcpy(&keyLen, payload + entryOffset, sizeof(keyLen));
850870
if (keyLen > payloadLen - entryOffset - sizeof(uint32_t)) return false;

‎turbo-ext/src/CombinationsHelper.cpp‎

Lines changed: 38 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -6,33 +6,30 @@
66
#include "support.h"
77
#include "generated/CombinationsHelper.h"
88
#include "zv.h"
9+
#include "TypeTraits.h"
910

1011
static zend_class_entry *pt_ce_combinations = nullptr;
1112

1213
namespace phpstanturbo {
1314

14-
/* Mirrors PHPStan\Internal\CombinationsHelper. The PHP twin recurses through
15-
* a generator; the native implementation materializes the full product
16-
* iteratively, walking an odometer over the input arrays. */
15+
/* Mirrors PHPStan\Internal\CombinationsHelper. PHP-visible calls use the
16+
* twin's lazy generator helper; native consumers walk the same product one
17+
* combination at a time without materializing it. */
1718
class CombinationsHelper
1819
{
1920
public:
20-
/* caps the result array's pre-allocation hint, not the product itself */
21-
static constexpr zend_ulong SIZE_HINT_LIMIT = 1048576;
22-
23-
/* combinations() refuses products beyond this instead of letting the
24-
* multiply overflow and silently truncate the result */
25-
static constexpr zend_ulong PRODUCT_LIMIT = 1ULL << 32;
21+
static zv::Val combinations(zval *arrays)
22+
{
23+
return pt_type_call_static(PT_CLASS_ITERABLE_HELPER, PT_LC("combinations"), 1, arrays);
24+
}
2625

27-
/* UNDEF = pending exception */
28-
static zv::Val combinations(zv::ArrRef arrays)
26+
/* false = the consumer stopped or a pending exception */
27+
static bool forEach(zv::ArrRef arrays, pt_combination_consumer consumer, void *context)
2928
{
3029
uint32_t n = arrays.size();
3130
if (n == 0) {
32-
/* combinations([]) yields a single empty combination */
33-
zv::Arr result = zv::Arr::create(0);
34-
result.push(zv::Arr::create(0));
35-
return zv::Val(std::move(result));
31+
zv::Arr combination = zv::Arr::create(0);
32+
return consumer(combination.raw(), context);
3633
}
3734

3835
/* borrow the inner array of each element (the input owns them) */
@@ -43,15 +40,14 @@ class CombinationsHelper
4340
if (UNEXPECTED(!element.isArray())) {
4441
efree(inner);
4542
zend_type_error("PHPStanTurbo\\CombinationsHelper::combinations() expects an array of arrays");
46-
return zv::Val();
43+
return false;
4744
}
4845
inner[i++] = element.raw();
4946
}
5047

5148
/* flatten each inner array into a raw element-slot vector */
5249
uint32_t *sizes = (uint32_t *) emalloc(n * sizeof(uint32_t));
5350
zval ***vecs = (zval ***) emalloc(n * sizeof(zval **));
54-
zend_ulong total = 1;
5551
bool hasEmptyInner = false;
5652

5753
for (i = 0; i < n; i++) {
@@ -63,23 +59,6 @@ class CombinationsHelper
6359
vecs[i] = NULL;
6460
continue;
6561
}
66-
if (UNEXPECTED(total > PRODUCT_LIMIT / sizes[i])) {
67-
/* The PHP twin is a lazy generator and never materializes the
68-
* product, but every consumer iterates it fully, so a product
69-
* this size is unreachable in practice. Failing loudly beats
70-
* the silent truncation an overflowed multiply would cause. */
71-
for (uint32_t k = 0; k < i; k++) {
72-
if (vecs[k] != NULL) {
73-
efree(vecs[k]);
74-
}
75-
}
76-
efree(vecs);
77-
efree(sizes);
78-
efree(inner);
79-
pt_throw_should_not_happen();
80-
return zv::Val();
81-
}
82-
total *= sizes[i];
8362
vecs[i] = (zval **) emalloc(sizes[i] * sizeof(zval *));
8463
uint32_t j = 0;
8564
for (auto entry : innerArr) {
@@ -89,11 +68,27 @@ class CombinationsHelper
8968
}
9069
}
9170

92-
zv::Val result;
93-
if (hasEmptyInner) {
94-
result = zv::Arr::create(0);
95-
} else {
96-
result = product(vecs, sizes, n, total);
71+
bool completed = true;
72+
if (!hasEmptyInner) {
73+
uint32_t *indices = (uint32_t *) ecalloc(n, sizeof(uint32_t));
74+
for (;;) {
75+
zv::Arr combination = zv::Arr::create(n);
76+
for (i = 0; i < n; i++) {
77+
combination.push(zv::Ref(vecs[i][indices[i]]));
78+
}
79+
if (!consumer(combination.raw(), context)) {
80+
completed = false;
81+
break;
82+
}
83+
84+
int64_t j = (int64_t) n - 1;
85+
for (; j >= 0; j--) {
86+
if (++indices[j] < sizes[j]) break;
87+
indices[j] = 0;
88+
}
89+
if (j < 0) break;
90+
}
91+
efree(indices);
9792
}
9893

9994
for (i = 0; i < n; i++) {
@@ -104,31 +99,7 @@ class CombinationsHelper
10499
efree(vecs);
105100
efree(sizes);
106101
efree(inner);
107-
return result;
108-
}
109-
110-
private:
111-
static zv::Val product(zval *const *const *vecs, const uint32_t *sizes, uint32_t n, zend_ulong total)
112-
{
113-
uint32_t *indices = (uint32_t *) ecalloc(n, sizeof(uint32_t));
114-
zv::Arr result = zv::Arr::create((uint32_t) (total > SIZE_HINT_LIMIT ? SIZE_HINT_LIMIT : total));
115-
116-
for (zend_ulong c = 0; c < total; c++) {
117-
zv::Arr comb = zv::Arr::create(n);
118-
for (uint32_t i = 0; i < n; i++) {
119-
comb.push(zv::Ref(vecs[i][indices[i]]));
120-
}
121-
result.push(std::move(comb));
122-
123-
/* odometer: advance the rightmost index, carrying leftwards */
124-
for (int64_t j = (int64_t) n - 1; j >= 0; j--) {
125-
if (++indices[j] < sizes[j]) break;
126-
indices[j] = 0;
127-
}
128-
}
129-
130-
efree(indices);
131-
return zv::Val(std::move(result));
102+
return completed;
132103
}
133104
};
134105

@@ -151,7 +122,7 @@ void pt_register_combinations_helper()
151122
if (!zp::parse<zp::Ht>(execute_data, arrays)) RETURN_THROWS();
152123
zval arraysZv;
153124
ZVAL_ARR(&arraysZv, arrays);
154-
zv::Val result = CombinationsHelper::combinations(zv::ArrRef(&arraysZv));
125+
zv::Val result = CombinationsHelper::combinations(&arraysZv);
155126
if (UNEXPECTED(result.isUndef())) RETURN_THROWS();
156127
result.intoReturnValue(return_value);
157128
});
@@ -163,9 +134,9 @@ void pt_register_combinations_helper()
163134

164135
/* {{{ shared with the compound family (TypeTraits.h) */
165136

166-
zv::Val pt_combinations_helper_combinations(zval *arrays)
137+
bool pt_combinations_helper_for_each(zval *arrays, pt_combination_consumer consumer, void *context)
167138
{
168-
return CombinationsHelper::combinations(zv::ArrRef(arrays));
139+
return CombinationsHelper::forEach(zv::ArrRef(arrays), consumer, context);
169140
}
170141

171142
/* }}} */

0 commit comments

Comments
 (0)