File: FFICasterTest.php

package info (click to toggle)
symfony 7.4.0~rc2%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 133,256 kB
  • sloc: php: 1,543,830; xml: 7,134; javascript: 979; sh: 584; makefile: 250; pascal: 70
file content (534 lines) | stat: -rw-r--r-- 15,924 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
<?php

/*
 * This file is part of the Symfony package.
 *
 * (c) Fabien Potencier <fabien@symfony.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Symfony\Component\VarDumper\Tests\Caster;

use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\RequiresPhpExtension;
use PHPUnit\Framework\TestCase;
use Symfony\Component\VarDumper\Caster\FFICaster;
use Symfony\Component\VarDumper\Test\VarDumperTestTrait;

/**
 * @author Kirill Nesmeyanov <nesk@xakep.ru>
 */
#[RequiresPhpExtension('ffi')]
class FFICasterTest extends TestCase
{
    use VarDumperTestTrait;

    /**
     * @see FFICaster::MAX_STRING_LENGTH
     */
    private const MAX_STRING_LENGTH = 255;

    protected function setUp(): void
    {
        if (\in_array(\PHP_SAPI, ['cli', 'phpdbg'], true) && 'preload' === \ini_get('ffi.enable')) {
            return;
        }
        if (!filter_var(\ini_get('ffi.enable'), \FILTER_VALIDATE_BOOL)) {
            $this->markTestSkipped('FFI not enabled for CLI SAPI');
        }
    }

    public function testCastAnonymousStruct()
    {
        $this->assertDumpEquals(<<<'PHP'
            FFI\CData<struct <anonymous>> size 4 align 4 {
              uint32_t x: 0
            }
            PHP,
            \FFI::cdef()->new('struct { uint32_t x; }')
        );
    }

    public function testCastNamedStruct()
    {
        $this->assertDumpEquals(<<<'PHP'
            FFI\CData<struct Example> size 4 align 4 {
              uint32_t x: 0
            }
            PHP,
            \FFI::cdef()->new('struct Example { uint32_t x; }')
        );
    }

    public function testCastAnonymousUnion()
    {
        $this->assertDumpEquals(<<<'PHP'
            FFI\CData<union <anonymous>> size 4 align 4 {
              uint32_t x: 0
              uint32_t y: 0
            }
            PHP,
            \FFI::cdef()->new('union { uint32_t x; uint32_t y; }')
        );
    }

    public function testCastNamedUnion()
    {
        $this->assertDumpEquals(<<<'PHP'
            FFI\CData<union Example> size 4 align 4 {
              uint32_t x: 0
              uint32_t y: 0
            }
            PHP,
            \FFI::cdef()->new('union Example { uint32_t x; uint32_t y; }')
        );
    }

    public function testCastAnonymousEnum()
    {
        $this->assertDumpEquals(<<<'PHP'
            FFI\CData<enum <anonymous>> size 4 align 4 {
              cdata: 0
            }
            PHP,
            \FFI::cdef()->new('enum { a, b }')
        );
    }

    public function testCastNamedEnum()
    {
        $this->assertDumpEquals(<<<'PHP'
            FFI\CData<enum Example> size 4 align 4 {
              cdata: 0
            }
            PHP,
            \FFI::cdef()->new('enum Example { a, b }')
        );
    }

    public static function scalarsDataProvider(): array
    {
        return [
            'int8_t' => ['int8_t', '0', 1, 1],
            'uint8_t' => ['uint8_t', '0', 1, 1],
            'int16_t' => ['int16_t', '0', 2, 2],
            'uint16_t' => ['uint16_t', '0', 2, 2],
            'int32_t' => ['int32_t', '0', 4, 4],
            'uint32_t' => ['uint32_t', '0', 4, 4],
            // 'int64_t' => ['int64_t', '0', 8, 8],
            // 'uint64_t' => ['uint64_t', '0', 8, 8],

            'bool' => ['bool', 'false', 1, 1],
            'char' => ['char', '"\x00"', 1, 1],
            'float' => ['float', '0.0', 4, 4],
            // 'double' => ['double', '0.0', 8, 8],
        ];
    }

    #[DataProvider('scalarsDataProvider')]
    public function testCastScalar(string $type, string $value, int $size, int $align)
    {
        $this->assertDumpEquals(<<<PHP
            FFI\CData<$type> size $size align $align {
              cdata: $value
            }
            PHP,
            \FFI::cdef()->new($type)
        );
    }

    public function testCastVoidFunction()
    {
        self::markTestSkipped('Failing test (different output) on various architectures');

        $abi = \PHP_OS_FAMILY === 'Windows' ? '[cdecl]' : '[fastcall]';

        $this->assertDumpEquals(<<<PHP
            $abi callable(): void {
              returnType: FFI\CType<void> size 1 align 1 {}
            }
            PHP,
            \FFI::cdef()->new('void (*)(void)')
        );
    }

    public function testCastIntFunction()
    {
        self::markTestSkipped('Failing test (different output) on various architectures');

        $abi = \PHP_OS_FAMILY === 'Windows' ? '[cdecl]' : '[fastcall]';

        $this->assertDumpEquals(<<<PHP
            $abi callable(): uint64_t {
              returnType: FFI\CType<uint64_t> size 8 align 8 {}
            }
            PHP,
            \FFI::cdef()->new('unsigned long long (*)(void)')
        );
    }

    public function testCastFunctionWithArguments()
    {
        self::markTestSkipped('Failing test (different output) on various architectures');

        $abi = \PHP_OS_FAMILY === 'Windows' ? '[cdecl]' : '[fastcall]';

        $this->assertDumpEquals(<<<PHP
            $abi callable(int32_t, char*): void {
              returnType: FFI\CType<void> size 1 align 1 {}
            }
            PHP,
            \FFI::cdef()->new('void (*)(int a, const char* b)')
        );
    }

    public function testCastNonCuttedPointerToChar()
    {
        $actualMessage = "Hello World!\0";

        $string = \FFI::cdef()->new('char[100]');
        $pointer = \FFI::addr($string[0]);
        \FFI::memcpy($pointer, $actualMessage, \strlen($actualMessage));
        $size = \PHP_INT_SIZE;

        $this->assertDumpMatchesFormat(<<<PHP
            FFI\CData<char*> size $size align $size {
              cdata: "Hello World!%A"
            }
            PHP,
            $pointer
        );
    }

    public function testCastCuttedPointerToChar()
    {
        $actualMessage = str_repeat('Hello World!', 30)."\0";
        $actualLength = \strlen($actualMessage);
        $expectedMessage = substr($actualMessage, 0, self::MAX_STRING_LENGTH);

        $string = \FFI::cdef()->new('char['.$actualLength.']');
        $pointer = \FFI::addr($string[0]);
        \FFI::memcpy($pointer, $actualMessage, $actualLength);

        // the max length is platform-dependent and can be less than 255,
        // so we need to cut the expected message to the maximum length
        // allowed by pages size of the current system
        $ffi = \FFI::cdef(<<<C
                size_t zend_get_page_size(void);
            C
        );

        $pageSize = $ffi->zend_get_page_size();
        $start = $ffi->cast('uintptr_t', $ffi->cast('char*', $pointer))->cdata;
        $max = min(self::MAX_STRING_LENGTH, ($start | ($pageSize - 1)) - $start);
        $expectedMessage = substr($expectedMessage, 0, $max);
        $size = \PHP_INT_SIZE;

        $this->assertDumpEquals(<<<PHP
            FFI\CData<char*> size $size align $size {
              cdata: "$expectedMessage"…
            }
            PHP,
            $pointer
        );
    }

    public function testCastNonTrailingCharPointer()
    {
        $actualMessage = 'Hello World!';
        $actualLength = \strlen($actualMessage);

        $string = \FFI::cdef()->new('char['.($actualLength + 1).']');
        $pointer = \FFI::addr($string[0]);
        \FFI::memcpy($pointer, $actualMessage, $actualLength);

        $pointer = \FFI::cdef()->cast('char*', \FFI::cdef()->cast('void*', $pointer));
        $pointer[$actualLength] = "\x01";
        $size = \PHP_INT_SIZE;

        $this->assertDumpMatchesFormat(<<<PHP
            FFI\CData<char*> size $size align $size {
              cdata: %A"$actualMessage%s"
            }
            PHP,
            $pointer
        );
    }

    public function testCastUnionWithDirectReferencedFields()
    {
        $ffi = \FFI::cdef(<<<'CPP'
            typedef union Event {
                int32_t x;
                float y;
            } Event;
            CPP
        );

        $this->assertDumpEquals(<<<'OUTPUT'
            FFI\CData<union Event> size 4 align 4 {
              int32_t x: 0
              float y: 0.0
            }
            OUTPUT,
            $ffi->new('Event')
        );
    }

    public function testCastUnionWithPointerReferencedFields()
    {
        $ffi = \FFI::cdef(<<<'CPP'
            typedef union Event {
                void* something;
                char* string;
            } Event;
            CPP
        );
        $size = \PHP_INT_SIZE;

        $this->assertDumpEquals(<<<OUTPUT
            FFI\CData<union Event> size $size align $size {
              something?: FFI\CType<void*> size $size align $size {
                0: FFI\CType<void> size 1 align 1 {}
              }
              string?: FFI\CType<char*> size $size align $size {
                0: FFI\CType<char> size 1 align 1 {}
              }
            }
            OUTPUT,
            $ffi->new('Event')
        );
    }

    public function testCastUnionWithMixedFields()
    {
        $ffi = \FFI::cdef(<<<'CPP'
            typedef union Event {
                void* a;
                int32_t b;
                char* c;
                ptrdiff_t d;
            } Event;
            CPP
        );
        $size = \PHP_INT_SIZE;
        $intt = \PHP_INT_SIZE === 4 ? 'int32_t' : 'int64_t';

        $this->assertDumpEquals(<<<OUTPUT
            FFI\CData<union Event> size $size align $size {
              a?: FFI\CType<void*> size $size align $size {
                0: FFI\CType<void> size 1 align 1 {}
              }
              int32_t b: 0
              c?: FFI\CType<char*> size $size align $size {
                0: FFI\CType<char> size 1 align 1 {}
              }
              $intt d: 0
            }
            OUTPUT,
            $ffi->new('Event')
        );
    }

    public function testCastPointerToEmptyScalars()
    {
        $ffi = \FFI::cdef(<<<'CPP'
            typedef struct {
                int8_t *a;
                uint8_t *b;
                int64_t *c;
                uint64_t *d;
                float *e;
                double *f;
                bool *g;
            } Example;
            CPP
        );
        $size = \PHP_INT_SIZE === 4 ? 28 : 56;
        $isize = \PHP_INT_SIZE;

        $this->assertDumpEquals(<<<OUTPUT
            FFI\CData<struct <anonymous>> size $size align $isize {
              int8_t* a: null
              uint8_t* b: null
              int64_t* c: null
              uint64_t* d: null
              float* e: null
              double* f: null
              bool* g: null
            }
            OUTPUT,
            $ffi->new('Example')
        );
    }

    public function testCastPointerToNonEmptyScalars()
    {
        $ffi = \FFI::cdef(<<<'CPP'
            typedef struct {
                int8_t *a;
                uint8_t *b;
                int64_t *c;
                uint64_t *d;
                float *e;
                double *f;
                bool *g;
            } Example;
            CPP
        );

        // Create values
        $int = \FFI::cdef()->new('int64_t');
        $int->cdata = 42;
        $float = \FFI::cdef()->new('float');
        $float->cdata = 42.0;
        $double = \FFI::cdef()->new('double');
        $double->cdata = 42.2;
        $bool = \FFI::cdef()->new('bool');
        $bool->cdata = true;

        // Fill struct
        $struct = $ffi->new('Example');
        $struct->a = \FFI::addr(\FFI::cdef()->cast('int8_t', $int));
        $struct->b = \FFI::addr(\FFI::cdef()->cast('uint8_t', $int));
        $struct->c = \FFI::addr(\FFI::cdef()->cast('int64_t', $int));
        $struct->d = \FFI::addr(\FFI::cdef()->cast('uint64_t', $int));
        $struct->e = \FFI::addr(\FFI::cdef()->cast('float', $float));
        $struct->f = \FFI::addr(\FFI::cdef()->cast('double', $double));
        $struct->g = \FFI::addr(\FFI::cdef()->cast('bool', $bool));
        $size = \PHP_INT_SIZE === 4 ? 28 : 56;
        $isize = \PHP_INT_SIZE;

        $this->assertDumpEquals(<<<OUTPUT
            FFI\CData<struct <anonymous>> size $size align $isize {
              a: FFI\CData<int8_t*> size $isize align $isize {
                cdata: 42
              }
              b: FFI\CData<uint8_t*> size $isize align $isize {
                cdata: 42
              }
              c: FFI\CData<int64_t*> size $isize align $isize {
                cdata: 42
              }
              d: FFI\CData<uint64_t*> size $isize align $isize {
                cdata: 42
              }
              e: FFI\CData<float*> size $isize align $isize {
                cdata: 42.0
              }
              f: FFI\CData<double*> size $isize align $isize {
                cdata: 42.2
              }
              g: FFI\CData<bool*> size $isize align $isize {
                cdata: true
              }
            }
            OUTPUT,
            $struct
        );
    }

    public function testCastPointerToStruct()
    {
        $ffi = \FFI::cdef(<<<'CPP'
            typedef struct {
                int8_t a;
            } Example;
            CPP
        );

        $struct = $ffi->new('Example', false);
        $size = \PHP_INT_SIZE;

        $this->assertDumpEquals(<<<OUTPUT
            FFI\CData<struct <anonymous>*> size $size align $size {
              cdata: FFI\CData<struct <anonymous>> size 1 align 1 {
                int8_t a: 0
              }
            }
            OUTPUT,
            \FFI::addr($struct)
        );

        // Save the pointer as variable so that
        // it is not cleaned up by the GC
        $pointer = \FFI::addr($struct);

        $this->assertDumpEquals(<<<OUTPUT
            FFI\CData<struct <anonymous>**> size $size align $size {
              cdata: FFI\CData<struct <anonymous>*> size $size align $size {
                cdata: FFI\CData<struct <anonymous>> size 1 align 1 {
                  int8_t a: 0
                }
              }
            }
            OUTPUT,
            \FFI::addr($pointer)
        );

        \FFI::free($struct);
    }

    public function testCastComplexType()
    {
        self::markTestSkipped('Failing test (different output) on various architectures');

        $ffi = \FFI::cdef(<<<'CPP'
            typedef struct {
                int x;
                int y;
            } Point;
            typedef struct Example {
                uint8_t a[32];
                long b;
                __extension__ union {
                    __extension__ struct {
                        short c;
                        long d;
                    };
                    struct {
                        Point point;
                        float e;
                    };
                };
                short f;
                bool g;
                int (*func)(
                    struct __sub *h
                );
            } Example;
            CPP
        );

        $var = $ffi->new('Example');
        $var->func = (static fn (object $p) => 42);

        $abi = \PHP_OS_FAMILY === 'Windows' ? '[cdecl]' : '[fastcall]';
        $longSize = \FFI::cdef()->type('long')->getSize();
        $longType = 8 === $longSize ? 'int64_t' : 'int32_t';
        $structSize = 56 + $longSize * 2;

        $this->assertDumpEquals(<<<OUTPUT
            FFI\CData<struct Example> size $structSize align 8 {
              a: FFI\CData<uint8_t[32]> size 32 align 1 {}
              $longType b: 0
              int16_t c: 0
              $longType d: 0
              point: FFI\CData<struct <anonymous>> size 8 align 4 {
                int32_t x: 0
                int32_t y: 0
              }
              float e: 0.0
              int16_t f: 0
              bool g: false
              func: $abi callable(struct __sub*): int32_t {
                returnType: FFI\CType<int32_t> size 4 align 4 {}
              }
            }
            OUTPUT,
            $var
        );
    }
}