|
| 1 | +--TEST-- |
| 2 | +pcntl_setcpuaffinity(): the upper bound the error advertises is itself a valid cpu id |
| 3 | +--EXTENSIONS-- |
| 4 | +pcntl |
| 5 | +--SKIPIF-- |
| 6 | +<?php |
| 7 | +if (PHP_OS_FAMILY === 'Solaris') { |
| 8 | + die("skip broken pset_create()"); |
| 9 | +} |
| 10 | +if (!function_exists("pcntl_setcpuaffinity")) die("skip pcntl_setcpuaffinity is not available"); |
| 11 | +?> |
| 12 | +--FILE-- |
| 13 | +<?php |
| 14 | +$pid = getmypid(); |
| 15 | +$prefix = 'pcntl_setcpuaffinity(): Argument #2 ($cpu_ids) cpu id must be between 0 and '; |
| 16 | + |
| 17 | +/* read the advertised bound out of the message itself */ |
| 18 | +try { |
| 19 | + pcntl_setcpuaffinity($pid, [PHP_INT_MAX]); |
| 20 | + exit("PHP_INT_MAX was accepted as a cpu id" . PHP_EOL); |
| 21 | +} catch (Throwable $e) { |
| 22 | + echo $e::class, "\n"; |
| 23 | + if (!preg_match('/must be between 0 and (\d+) \(/', $e->getMessage(), $m)) { |
| 24 | + exit("unexpected message: " . $e->getMessage() . PHP_EOL); |
| 25 | + } |
| 26 | +} |
| 27 | +$bound = (int) $m[1]; |
| 28 | + |
| 29 | +/* Every id is range checked before any syscall runs, so pairing the advertised |
| 30 | + bound with an out of range id shows which of the two the check rejects, |
| 31 | + without ever changing the process affinity. */ |
| 32 | +try { |
| 33 | + pcntl_setcpuaffinity($pid, [$bound, PHP_INT_MAX]); |
| 34 | +} catch (Throwable $e) { |
| 35 | + echo $e::class, "\n"; |
| 36 | + var_dump($e->getMessage() === $prefix . $bound . ' (' . PHP_INT_MAX . ')'); |
| 37 | +} |
| 38 | + |
| 39 | +/* and the first id past the bound is rejected, naming itself */ |
| 40 | +try { |
| 41 | + pcntl_setcpuaffinity($pid, [$bound + 1]); |
| 42 | +} catch (Throwable $e) { |
| 43 | + echo $e::class, "\n"; |
| 44 | + var_dump($e->getMessage() === $prefix . $bound . ' (' . ($bound + 1) . ')'); |
| 45 | +} |
| 46 | +?> |
| 47 | +--EXPECT-- |
| 48 | +ValueError |
| 49 | +ValueError |
| 50 | +bool(true) |
| 51 | +ValueError |
| 52 | +bool(true) |
0 commit comments