|
7 | 7 | #include "support.h" |
8 | 8 | #include "reg.h" |
9 | 9 |
|
| 10 | +#include "Engine.h" |
10 | 11 | #include "zend_closures.h" |
| 12 | +#include "zend_fibers.h" |
11 | 13 | #include "zend_weakrefs.h" |
12 | 14 |
|
13 | 15 |
|
@@ -237,6 +239,91 @@ static ssize_t abi_stream_read(php_stream *stream, char *buf, size_t count) |
237 | 239 | return php_stream_read(stream, buf, count); |
238 | 240 | } |
239 | 241 |
|
| 242 | +/* {{{ continuing a call on a fresh C stack (pt_engine_with_stack()) */ |
| 243 | + |
| 244 | +namespace { |
| 245 | + |
| 246 | +/* one call continued on a fresh stack: the body, and the VM state it left |
| 247 | + * behind — zend_fiber_switch_context() restores the caller's state from |
| 248 | + * before the switch, which the body may legitimately have changed */ |
| 249 | +struct FreshStackCall |
| 250 | +{ |
| 251 | + void (*body)(void *); |
| 252 | + void *data; |
| 253 | + bool bailout; |
| 254 | + zend_vm_stack vmStack; |
| 255 | + zval *vmStackTop; |
| 256 | + zval *vmStackEnd; |
| 257 | + size_t vmStackPageSize; |
| 258 | + int errorReporting; |
| 259 | +}; |
| 260 | + |
| 261 | +/* the call the coroutine starting next runs (read once, at its start) */ |
| 262 | +FreshStackCall *pt_fresh_stack_call = nullptr; |
| 263 | + |
| 264 | +/* identifies the contexts in fiber observers */ |
| 265 | +char pt_fresh_stack_kind; |
| 266 | + |
| 267 | +ZEND_STACK_ALIGNED void freshStackCoroutine(zend_fiber_transfer *transfer) |
| 268 | +{ |
| 269 | + (void) transfer; /* already addresses the caller, which is where it returns */ |
| 270 | + FreshStackCall *call = pt_fresh_stack_call; |
| 271 | + zend_fiber_context *context = EG(current_fiber_context); |
| 272 | +#ifdef ZEND_CHECK_STACK_LIMIT |
| 273 | + EG(stack_base) = zend_fiber_stack_base(context->stack); |
| 274 | + EG(stack_limit) = zend_fiber_stack_limit(context->stack); |
| 275 | +#endif |
| 276 | + zend_try { |
| 277 | + call->body(call->data); |
| 278 | + } zend_catch { |
| 279 | + call->bailout = true; |
| 280 | + } zend_end_try(); |
| 281 | + call->vmStack = EG(vm_stack); |
| 282 | + call->vmStackTop = EG(vm_stack_top); |
| 283 | + call->vmStackEnd = EG(vm_stack_end); |
| 284 | + call->vmStackPageSize = EG(vm_stack_page_size); |
| 285 | + call->errorReporting = EG(error_reporting); |
| 286 | +} |
| 287 | + |
| 288 | +} // namespace |
| 289 | + |
| 290 | +static void abi_run_on_fresh_stack(void (*body)(void *), void *data) |
| 291 | +{ |
| 292 | + zend_fiber_context context; |
| 293 | + memset(&context, 0, sizeof(context)); |
| 294 | + if (UNEXPECTED(zend_fiber_init_context(&context, &pt_fresh_stack_kind, freshStackCoroutine, PT_ENGINE_FRESH_STACK_SIZE_LIMIT) == FAILURE)) { |
| 295 | + /* no stack to be had: continue here, where the engine's own stack |
| 296 | + * limit decides */ |
| 297 | + if (EG(exception) != NULL) { |
| 298 | + zend_clear_exception(); |
| 299 | + } |
| 300 | + body(data); |
| 301 | + return; |
| 302 | + } |
| 303 | + |
| 304 | + FreshStackCall call = { body, data, false, NULL, NULL, NULL, 0, 0 }; |
| 305 | + FreshStackCall *previous = pt_fresh_stack_call; |
| 306 | + pt_fresh_stack_call = &call; |
| 307 | + zend_fiber_transfer transfer; |
| 308 | + transfer.context = &context; |
| 309 | + transfer.flags = 0; |
| 310 | + ZVAL_NULL(&transfer.value); |
| 311 | + /* returns once the body finished; the dead context is destroyed then */ |
| 312 | + zend_fiber_switch_context(&transfer); |
| 313 | + pt_fresh_stack_call = previous; |
| 314 | + |
| 315 | + EG(vm_stack) = call.vmStack; |
| 316 | + EG(vm_stack_top) = call.vmStackTop; |
| 317 | + EG(vm_stack_end) = call.vmStackEnd; |
| 318 | + EG(vm_stack_page_size) = call.vmStackPageSize; |
| 319 | + EG(error_reporting) = call.errorReporting; |
| 320 | + if (UNEXPECTED(call.bailout)) { |
| 321 | + zend_bailout(); |
| 322 | + } |
| 323 | +} |
| 324 | + |
| 325 | +/* }}} */ |
| 326 | + |
240 | 327 | void pt_abi_init() |
241 | 328 | { |
242 | 329 | #define PT_ABI_EG_FIELD(field) pt_abi.eg_##field = &EG(field); |
@@ -284,4 +371,5 @@ void pt_abi_init() |
284 | 371 | pt_abi.call_known_function = abi_call_known_function; |
285 | 372 | pt_abi.stream_free = abi_stream_free; |
286 | 373 | pt_abi.stream_read = abi_stream_read; |
| 374 | + pt_abi.run_on_fresh_stack = abi_run_on_fresh_stack; |
287 | 375 | } |
0 commit comments