|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Advisory locks must exclude same-process owners even when flock() grants |
| 4 | + * every handle, as php-wasm (WordPress Playground) does within one process. |
| 5 | + */ |
| 6 | + |
| 7 | +declare( strict_types=1 ); |
| 8 | + |
| 9 | +define( 'ABSPATH', __DIR__ . '/' ); |
| 10 | +require_once __DIR__ . '/../inc/native/class-wp-markdown-native-advisory-locks.php'; |
| 11 | + |
| 12 | +/** Passthrough file wrapper whose stream_lock() always succeeds, like php-wasm. */ |
| 13 | +final class MDI_Permissive_Flock_Stream { |
| 14 | + /** @var resource|null */ |
| 15 | + public $context; |
| 16 | + /** @var resource */ |
| 17 | + private $handle; |
| 18 | + |
| 19 | + private static function real( string $path ): string { |
| 20 | + return substr( $path, strlen( 'mdi-permissive://' ) ); |
| 21 | + } |
| 22 | + public function stream_open( string $path, string $mode ): bool { |
| 23 | + $handle = fopen( self::real( $path ), $mode ); |
| 24 | + if ( false === $handle ) { |
| 25 | + return false; |
| 26 | + } |
| 27 | + $this->handle = $handle; |
| 28 | + return true; |
| 29 | + } |
| 30 | + public function stream_lock( int $operation ): bool { |
| 31 | + unset( $operation ); |
| 32 | + return true; |
| 33 | + } |
| 34 | + public function stream_close(): void { fclose( $this->handle ); } |
| 35 | + public function stream_read( int $count ): string|false { return fread( $this->handle, $count ); } |
| 36 | + public function stream_write( string $data ): int|false { return fwrite( $this->handle, $data ); } |
| 37 | + public function stream_eof(): bool { return feof( $this->handle ); } |
| 38 | + public function stream_stat(): array|false { return fstat( $this->handle ); } |
| 39 | + public function url_stat( string $path, int $flags ): array|false { |
| 40 | + $real = self::real( $path ); |
| 41 | + return file_exists( $real ) ? stat( $real ) : false; |
| 42 | + } |
| 43 | + public function mkdir( string $path, int $mode, int $options ): bool { |
| 44 | + return mkdir( self::real( $path ), $mode, (bool) ( $options & STREAM_MKDIR_RECURSIVE ) ); |
| 45 | + } |
| 46 | +} |
| 47 | +stream_wrapper_register( 'mdi-permissive', MDI_Permissive_Flock_Stream::class ); |
| 48 | + |
| 49 | +$root = 'mdi-permissive://' . sys_get_temp_dir() . '/mdi-permissive-flock-' . bin2hex( random_bytes( 6 ) ); |
| 50 | +$first = new WP_Markdown_Native_Advisory_Locks( $root ); |
| 51 | +$second = new WP_Markdown_Native_Advisory_Locks( $root ); |
| 52 | + |
| 53 | +$assertions = array( |
| 54 | + 'the first owner acquires' => true === $first->acquire( 'venue-lock', 0 ), |
| 55 | + 'a same-process second owner is excluded' => false === $second->acquire( 'venue-lock', 0 ), |
| 56 | + 'a same-process non-owner release reports 0' => 0 === $second->release( 'venue-lock' ), |
| 57 | + 'the owner remains reentrant' => true === $first->acquire( 'venue-lock', 0 ) && 1 === $first->release( 'venue-lock' ), |
| 58 | + 'still held after one reentrant release' => false === $second->acquire( 'venue-lock', 0 ), |
| 59 | + 'final release transfers ownership' => 1 === $first->release( 'venue-lock' ) && true === $second->acquire( 'venue-lock', 0 ), |
| 60 | + 'close releases in-process ownership' => ( static function () use ( $first, $second ): bool { $second->close(); return true === $first->acquire( 'venue-lock', 0 ); } )(), |
| 61 | + 'unrelated names do not contend' => true === $second->acquire( 'other-lock', 0 ), |
| 62 | +); |
| 63 | +$passed = ! in_array( false, $assertions, true ); |
| 64 | +fwrite( $passed ? STDOUT : STDERR, json_encode( array( 'assertions' => $assertions, 'passed' => $passed ), JSON_PRETTY_PRINT | JSON_THROW_ON_ERROR ) . "\n" ); |
| 65 | +exit( $passed ? 0 : 1 ); |
0 commit comments