phpdbg_sigsafe.c 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #include "phpdbg_sigsafe.h"
  2. #include "phpdbg.h"
  3. ZEND_EXTERN_MODULE_GLOBALS(phpdbg)
  4. #define STR(x) #x
  5. #define EXP_STR(x) STR(x)
  6. static void* zend_mm_mem_alloc(zend_mm_storage *storage, size_t size, size_t alignment) {
  7. if (EXPECTED(size <= PHPDBG_SIGSAFE_MEM_SIZE && !PHPDBG_G(sigsafe_mem).allocated)) {
  8. PHPDBG_G(sigsafe_mem).allocated = 1;
  9. return (void *) (((size_t) PHPDBG_G(sigsafe_mem).mem & ~(alignment - 1)) + alignment);
  10. }
  11. zend_quiet_write(PHPDBG_G(io)[PHPDBG_STDERR].fd, ZEND_STRL("Tried to allocate more than " EXP_STR(PHPDBG_SIGSAFE_MEM_SIZE) " bytes from stack memory in signal handler ... bailing out of signal handler\n"));
  12. if (*EG(bailout)) {
  13. LONGJMP(*EG(bailout), FAILURE);
  14. }
  15. zend_quiet_write(PHPDBG_G(io)[PHPDBG_STDERR].fd, ZEND_STRL("Bailed out without a bailout address in signal handler!\n"));
  16. return NULL;
  17. }
  18. static void zend_mm_mem_free(zend_mm_storage *storage, void *ptr, size_t size) {
  19. }
  20. void phpdbg_set_sigsafe_mem(char *buffer) {
  21. phpdbg_signal_safe_mem *mem = &PHPDBG_G(sigsafe_mem);
  22. const zend_mm_handlers phpdbg_handlers = {
  23. zend_mm_mem_alloc,
  24. zend_mm_mem_free,
  25. NULL,
  26. NULL,
  27. };
  28. mem->mem = buffer;
  29. mem->allocated = 0;
  30. mem->heap = zend_mm_startup_ex(&phpdbg_handlers, NULL, 0);
  31. mem->old_heap = zend_mm_set_heap(mem->heap);
  32. }
  33. zend_mm_heap *phpdbg_original_heap_sigsafe_mem(void) {
  34. return PHPDBG_G(sigsafe_mem).old_heap;
  35. }
  36. void phpdbg_clear_sigsafe_mem(void) {
  37. zend_mm_set_heap(phpdbg_original_heap_sigsafe_mem());
  38. PHPDBG_G(sigsafe_mem).mem = NULL;
  39. }
  40. bool phpdbg_active_sigsafe_mem(void) {
  41. return !!PHPDBG_G(sigsafe_mem).mem;
  42. }