Skip to content

Commit 280485a

Browse files
committed
Improve FFI test suite for Windows
We add Windows support to four existing test cases, extract some useful utility functions, and use them to simplify further test cases. We also remove the Windows specific code from preload.inc, since preloading isn't supported on Windows anyway.
1 parent 10bf541 commit 280485a

File tree

11 files changed

+64
-52
lines changed

11 files changed

+64
-52
lines changed

ext/ffi/tests/100.phpt

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22
FFI 100: PHP symbols
33
--SKIPIF--
44
<?php require_once('skipif.inc'); ?>
5+
<?php require_once('utils.inc'); ?>
56
<?php
67
try {
7-
FFI::cdef("extern void *zend_printf;");
8+
ffi_cdef("extern void *zend_printf;", ffi_get_php_dll_name());
89
} catch (Throwable $e) {
910
die('skip PHP symbols not available');
1011
}
@@ -13,14 +14,9 @@ try {
1314
ffi.enable=1
1415
--FILE--
1516
<?php
16-
// Check if target supports "fastcall" calling convention
17-
try {
18-
FFI::cdef("extern size_t __attribute__((fastcall)) (*zend_printf)(const char *format);");
19-
$fastcall = "__attribute__((fastcall)) ";
20-
} catch (Throwable $e) {
21-
$fastcall = "";
22-
}
23-
$zend = FFI::cdef("
17+
require_once('utils.inc');
18+
$fastcall = ffi_get_fastcall_specifier();
19+
$zend = ffi_cdef("
2420
const char *get_zend_version(void);
2521
//char *get_zend_version(void);
2622
extern size_t (*zend_printf)(const char *format, ...);
@@ -29,7 +25,7 @@ $zend = FFI::cdef("
2925
3026
void $fastcall zend_str_tolower(char *str, size_t length);
3127
32-
");
28+
", ffi_get_php_dll_name());
3329
var_dump(trim(explode("\n",$zend->get_zend_version())[0]));
3430
//var_dump(trim(FFI::string($zend->get_zend_version())));
3531
var_dump($zend->zend_printf);

ext/ffi/tests/101.phpt

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22
FFI 101: PHP symbols (function address)
33
--SKIPIF--
44
<?php require_once('skipif.inc'); ?>
5+
<?php require_once('utils.inc'); ?>
56
<?php
67
try {
7-
FFI::cdef("extern void *zend_printf;");
8+
ffi_cdef("extern void *zend_printf;", ffi_get_php_dll_name());
89
} catch (Throwable $e) {
910
die('skip PHP symbols not available');
1011
}
@@ -13,14 +14,9 @@ try {
1314
ffi.enable=1
1415
--FILE--
1516
<?php
16-
// Check if target supports "fastcall" calling convention
17-
try {
18-
FFI::cdef("extern size_t __attribute__((fastcall)) (*zend_printf)(const char *format);");
19-
$fastcall = "__attribute__((fastcall)) ";
20-
} catch (Throwable $e) {
21-
$fastcall = "";
22-
}
23-
$zend = FFI::cdef("
17+
require_once('utils.inc');
18+
$fastcall = ffi_get_fastcall_specifier();
19+
$zend = ffi_cdef("
2420
const char *get_zend_version(void);
2521
//char *get_zend_version(void);
2622
extern size_t (*zend_printf)(const char *format, ...);
@@ -29,7 +25,7 @@ $zend = FFI::cdef("
2925
3026
void $fastcall zend_str_tolower(char *str, size_t length);
3127
32-
");
28+
", ffi_get_php_dll_name());
3329
$f = $zend->get_zend_version;
3430
var_dump(trim(explode("\n",$f())[0]));
3531
//var_dump(trim(FFI::string($zend->get_zend_version())));

ext/ffi/tests/200.phpt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22
FFI 200: PHP callbacks
33
--SKIPIF--
44
<?php require_once('skipif.inc'); ?>
5+
<?php require_once('utils.inc'); ?>
56
<?php
67
try {
7-
FFI::cdef("void* zend_write;");
8+
FFI::cdef("void* zend_write;", ffi_get_php_dll_name());
89
} catch (Throwable $e) {
910
die('skip PHP symbols not available');
1011
}
@@ -13,10 +14,11 @@ try {
1314
ffi.enable=1
1415
--FILE--
1516
<?php
17+
require_once('utils.inc');
1618
$zend = FFI::cdef("
1719
typedef int (*zend_write_func_t)(const char *str, size_t str_length);
1820
extern zend_write_func_t zend_write;
19-
");
21+
", ffi_get_php_dll_name());
2022

2123
echo "Hello World!\n";
2224

ext/ffi/tests/301-win32.phpt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@ FFI 301: FFI loading on Windows
77
ffi.enable=1
88
--FILE--
99
<?php
10+
require_once('utils.inc');
1011
$fn = __DIR__ . "/300-win32.h";
1112
$cont = str_replace(
1213
"PHP_DLL_NAME",
13-
"php" . PHP_MAJOR_VERSION . (PHP_ZTS ? "ts" : "") . (PHP_DEBUG ? "_debug" : "") . ".dll",
14+
ffi_get_php_dll_name(),
1415
file_get_contents("$fn.in")
1516
);
1617
file_put_contents($fn, $cont);

ext/ffi/tests/bug77632b.phpt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ Bug #77632 (FFI function pointers with variadics)
33
--SKIPIF--
44
<?php
55
require_once('skipif.inc');
6+
require_once('utils.inc');
67
try {
7-
FFI::cdef("extern void *zend_printf;");
8+
FFI::cdef("extern void *zend_printf;", ffi_get_php_dll_name());
89
} catch (Throwable $_) {
910
die('skip PHP symbols not available');
1011
}
@@ -13,7 +14,8 @@ try {
1314
ffi.enable=1
1415
--FILE--
1516
<?php
16-
$libc = FFI::cdef("extern size_t (*zend_printf)(const char *format, ...);");
17+
require_once('utils.inc');
18+
$libc = FFI::cdef("extern size_t (*zend_printf)(const char *format, ...);", ffi_get_php_dll_name());
1719
$args = ["test from zend_printf\n"];
1820
($libc->zend_printf)(...$args);
1921
$args2 = ["Hello, %s from zend_printf\n", "world"];

ext/ffi/tests/bug78270_1.phpt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,26 @@ FR #78270 (Usage of __vectorcall convention with FFI)
55
require_once('skipif.inc');
66
if (substr(PHP_OS, 0, 3) != 'WIN') die("skip this test is for Windows platforms only");
77

8+
require_once('utils.inc');
89
ob_start();
910
phpinfo(INFO_GENERAL);
1011
$info = ob_get_clean();
1112
if (preg_match('/Compiler => .*clang.*/', $info)) die("skip not for clang");
1213

13-
$dll = 'php7' . (PHP_ZTS ? 'ts' : '') . (PHP_DEBUG ? '_debug' : '') . '.dll';
1414
try {
1515
FFI::cdef(<<<EOC
1616
__vectorcall int zend_atoi(const char *str, size_t str_len);
17-
EOC, $dll);
17+
EOC, ffi_get_php_dll_name());
1818
} catch (FFI\ParserException $ex) {
1919
die('skip __vectorcall not supported');
2020
}
2121
?>
2222
--FILE--
2323
<?php
24-
$dll = 'php7' . (PHP_ZTS ? 'ts' : '') . (PHP_DEBUG ? '_debug' : '') . '.dll';
24+
require_once('utils.inc');
2525
$ffi = FFI::cdef(<<<EOC
2626
__vectorcall int zend_atoi(const char *str, size_t str_len);
27-
EOC, $dll);
27+
EOC, ffi_get_php_dll_name());
2828
var_dump($ffi->zend_atoi("17.4", 4));
2929
?>
3030
--EXPECT--

ext/ffi/tests/bug78270_2.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ FR #78270 (Usage of __vectorcall convention with FFI)
55
require_once('skipif.inc');
66
if (substr(PHP_OS, 0, 3) != 'WIN') die("skip this test is for Windows platforms only");
77

8-
$dll = 'php7' . (PHP_ZTS ? 'ts' : '') . (PHP_DEBUG ? '_debug' : '') . '.dll';
8+
require_once('utils.inc');
99
try {
1010
FFI::cdef(<<<EOC
1111
__vectorcall int zend_atoi(const char *str, size_t str_len);
12-
EOC, $dll);
12+
EOC, ffi_get_php_dll_name());
1313
} catch (FFI\ParserException $ex) {
1414
die('skip __vectorcall not supported');
1515
}

ext/ffi/tests/bug78714.phpt

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,9 @@ Bug #78714 (funcs returning pointer can't use call convention spec)
66
ffi.enable=1
77
--FILE--
88
<?php
9+
require_once('utils.inc');
910
$def = 'char * __cdecl get_zend_version(void);';
10-
if (substr(PHP_OS, 0, 3) != 'WIN') {
11-
$ffi = FFI::cdef($def);
12-
} else {
13-
$dll = 'php7' . (PHP_ZTS ? 'ts' : '') . (PHP_DEBUG ? '_debug' : '') . '.dll';
14-
$ffi = FFI::cdef($def, $dll);
15-
}
11+
$ffi = ffi_cdef($def, ffi_get_php_dll_name());
1612
echo substr(FFI::string($ffi->get_zend_version()), 0, 4) . "\n";
1713
?>
1814
--EXPECT--

ext/ffi/tests/bug79096.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ if (!extension_loaded('zend-test')) die('skip zend-test extension not available'
77
?>
88
--FILE--
99
<?php
10+
require_once('utils.inc');
1011
$header = <<<HEADER
1112
struct bug79096 {
1213
uint64_t a;
@@ -22,8 +23,7 @@ if (PHP_OS_FAMILY !== 'Windows') {
2223
try {
2324
$ffi = FFI::cdef($header, 'php_zend_test.dll');
2425
} catch (FFI\Exception $ex) {
25-
$dll = $dll = 'php7' . (PHP_ZTS ? 'ts' : '') . (PHP_DEBUG ? '_debug' : '') . '.dll';
26-
$ffi = FFI::cdef($header, $dll);
26+
$ffi = FFI::cdef($header, ffi_get_php_dll_name());
2727
}
2828
}
2929

ext/ffi/tests/preload.inc

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,3 @@
11
<?php
2-
if (substr(PHP_OS, 0, 3) == 'WIN') {
3-
$fn = __DIR__ . "/300-win32.h";
4-
$cont = str_replace(
5-
"PHP_DLL_NAME",
6-
"php" . PHP_MAJOR_VERSION . (PHP_ZTS ? "ts" : "") . (PHP_DEBUG ? "_debug" : "") . ".dll",
7-
file_get_contents("$fn.in")
8-
);
9-
file_put_contents($fn, $cont);
10-
$ffi = FFI::load($fn);
11-
/* Test should cleanup this. */
12-
} else {
13-
FFI::load(__DIR__ . "/300.h");
14-
}
2+
3+
FFI::load(__DIR__ . "/300.h");

ext/ffi/tests/utils.inc

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
function ffi_cdef($code, $lib)
4+
{
5+
if (isset($lib)) {
6+
return FFI::cdef($code, $lib);
7+
} else {
8+
return FFI::cdef($code);
9+
}
10+
}
11+
12+
function ffi_get_php_dll_name()
13+
{
14+
if (PHP_OS_FAMILY === 'Windows') {
15+
return "php" . PHP_MAJOR_VERSION . (PHP_ZTS ? "ts" : "") . (PHP_DEBUG ? "_debug" : "") . ".dll";
16+
} else {
17+
return null;
18+
}
19+
}
20+
21+
function ffi_get_fastcall_specifier()
22+
{
23+
foreach (['__attribute__((fastcall))', '__fastcall', '__vectorcall'] as $spec) {
24+
try {
25+
ffi_cdef("extern size_t $spec zend_list_insert(void *ptr, int type);", ffi_get_php_dll_name());
26+
return "$spec ";
27+
} catch (Throwable $e) {}
28+
}
29+
return "";
30+
}

0 commit comments

Comments
 (0)