@@ -3,57 +3,85 @@ Determine whether Fibers can be used and add Boost fiber assembly files support
33if available for the platform. As a Boost fallback alternative ucontext support 
44is checked if it can be used. 
55
6- Control variables :
6+ Configuration options :
77
8- * ZEND_FIBER_ASM - Whether to use Boost fiber assembly files.  
8+ * ZEND_FIBER_ASM 
99
10- Cache  variables:
10+ Result  variables:
1111
12- * ZEND_FIBER_UCONTEXT - Whether <ucontext.h> header file is available and should 
13-   be used. 
14- 
15- Interface library: 
16- 
17- * Zend::Fibers 
18- 
19-   Interface library using Boost fiber assembly files and compile options if 
20-   available. 
12+ * ZEND_FIBER_UCONTEXT 
2113#]=============================================================================] 
2214
2315include (CheckIncludeFiles)
2416include (CheckSourceRuns)
17+ include (CMakeDependentOption)
2518include (CMakePushCheckState)
2619
20+ cmake_dependent_option(
21+   ZEND_FIBER_ASM
22+   "Enable the use of Boost fiber assembly files" 
23+   ON 
24+   [[NOT CMAKE_SYSTEM_NAME STREQUAL "Windows"]]
25+   ON 
26+ )
27+ mark_as_advanced (ZEND_FIBER_ASM)
28+ 
29+ # Create interface library for using Boost fiber assembly files and compile 
30+ # options if available. 
2731add_library (zend_fibers INTERFACE )
2832add_library (Zend::Fibers ALIAS zend_fibers)
33+ target_link_libraries (zend PRIVATE  Zend::Fibers)
34+ 
35+ ################################################################################ 
36+ # Check shadow stack. 
37+ ################################################################################ 
2938
30- if (NOT  DEFINED  SHADOW_STACK_SYSCALL)
31-   message (CHECK_START "Whether syscall to create shadow stack exists" )
32-   cmake_push_check_state(RESET)
33-     set (CMAKE_REQUIRED_QUIET TRUE )
34- 
35-     check_source_runs(C [[
36-       #include <unistd.h>
37-       #include <sys/mman.h>
38-       int main(void)
39-       {
40-         void* base = (void *)syscall(451, 0, 0x20000, 0x1);
41-         if (base != (void*)-1) {
42-           munmap(base, 0x20000);
43-           return 0;
39+ function (_php_zend_fibers_shadow_stack_syscall)
40+   if (CMAKE_SYSTEM_NAME  STREQUAL  "Windows" )
41+     set (PHP_ZEND_SHADOW_STACK_SYSCALL FALSE )
42+   endif ()
43+ 
44+   if (NOT  DEFINED  PHP_ZEND_SHADOW_STACK_SYSCALL)
45+     message (CHECK_START "Whether syscall to create shadow stack exists" )
46+     cmake_push_check_state(RESET)
47+       set (CMAKE_REQUIRED_QUIET TRUE )
48+ 
49+       check_source_runs(C [[
50+         #include <unistd.h>
51+         #include <sys/mman.h>
52+         int main(void)
53+         {
54+           void* base = (void *)syscall(451, 0, 0x20000, 0x1);
55+           if (base != (void*)-1) {
56+             munmap(base, 0x20000);
57+             return 0;
58+           }
59+           return 1;
4460        }
45-         return 1; 
46-       } 
47-     ]] SHADOW_STACK_SYSCALL) 
48-   cmake_pop_check_state( )
49-   if  (SHADOW_STACK_SYSCALL )
50-     message  (CHECK_PASS  "yes" )
51-   else  () 
52-     # If the syscall doesn't exist, we may block the final ELF from 
53-     # __PROPERTY_SHSTK via redefine macro as "-D__CET__=1". 
54-     message  (CHECK_FAIL  "no" )
61+       ]] PHP_ZEND_SHADOW_STACK_SYSCALL) 
62+     cmake_pop_check_state() 
63+ 
64+     if  (PHP_ZEND_SHADOW_STACK_SYSCALL )
65+       message  (CHECK_PASS  "yes" )
66+     else  ( )
67+        # If the syscall doesn't exist, we may block the final ELF from 
68+        # __PROPERTY_SHSTK via redefine macro as "-D__CET__=1". 
69+       message  (CHECK_FAIL  "no" ) 
70+     endif  ( )
5571  endif ()
56- endif ()
72+ 
73+   # Use compile definitions because ASM files can't see macro definitions from 
74+   # the PHP configuration header (php_config.h/config.w32.h). 
75+   target_compile_definitions (
76+     zend_fibers
77+     INTERFACE 
78+       $<IF:$<BOOL :${PHP_ZEND_SHADOW_STACK_SYSCALL} >,SHADOW_STACK_SYSCALL=1,SHADOW_STACK_SYSCALL=0>
79+   )
80+ endfunction ()
81+ 
82+ ################################################################################ 
83+ # Configure fibers. 
84+ ################################################################################ 
5785
5886block()
5987  set (cpu "" )
@@ -162,13 +190,7 @@ block()
162190
163191    target_sources (zend_fibers INTERFACE  ${asmSources} )
164192
165-     # Use compile definitions because ASM files can't see macro definitions from 
166-     # the PHP configuration header (php_config.h/config.w32.h). 
167-     target_compile_definitions (
168-       zend_fibers
169-       INTERFACE 
170-         $<IF:$<BOOL :${SHADOW_STACK_SYSCALL} >,SHADOW_STACK_SYSCALL=1,SHADOW_STACK_SYSCALL=0>
171-     )
193+     _php_zend_fibers_shadow_stack_syscall()
172194  else ()
173195    cmake_push_check_state(RESET)
174196      # To use ucontext.h on macOS, the _XOPEN_SOURCE needs to be defined to any 
@@ -189,16 +211,17 @@ block()
189211        )
190212      endif ()
191213
192-       check_include_files(ucontext.h ZEND_FIBER_UCONTEXT )
214+       check_include_files(ucontext.h PHP_ZEND_FIBER_UCONTEXT )
193215    cmake_pop_check_state()
194216
195-     if (NOT  ZEND_FIBER_UCONTEXT )
217+     if (NOT  PHP_ZEND_FIBER_UCONTEXT )
196218      message (CHECK_FAIL "no" )
197219      message (
198220        FATAL_ERROR
199221        "Fibers are not available on this platform, <ucontext.h> not found." 
200222      )
201223    endif ()
202224    message (CHECK_PASS "yes, ucontext" )
225+     set (ZEND_FIBER_UCONTEXT TRUE  PARENT_SCOPE)
203226  endif ()
204227endblock()
0 commit comments