PHP  
 PHP: Test and Code Coverage Analysis
downloads | QA | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | my php.net 
 

LCOV - code coverage report
Current view: top level - ext/opcache - zend_shared_alloc.c (source / functions) Hit Total Coverage
Test: PHP Code Coverage Lines: 131 193 67.9 %
Date: 2015-04-30 Functions: 17 21 81.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :    +----------------------------------------------------------------------+
       3             :    | Zend OPcache                                                         |
       4             :    +----------------------------------------------------------------------+
       5             :    | Copyright (c) 1998-2015 The PHP Group                                |
       6             :    +----------------------------------------------------------------------+
       7             :    | This source file is subject to version 3.01 of the PHP license,      |
       8             :    | that is bundled with this package in the file LICENSE, and is        |
       9             :    | available through the world-wide-web at the following url:           |
      10             :    | http://www.php.net/license/3_01.txt                                  |
      11             :    | If you did not receive a copy of the PHP license and are unable to   |
      12             :    | obtain it through the world-wide-web, please send a note to          |
      13             :    | license@php.net so we can mail you a copy immediately.               |
      14             :    +----------------------------------------------------------------------+
      15             :    | Authors: Andi Gutmans <andi@zend.com>                                |
      16             :    |          Zeev Suraski <zeev@zend.com>                                |
      17             :    |          Stanislav Malyshev <stas@zend.com>                          |
      18             :    |          Dmitry Stogov <dmitry@zend.com>                             |
      19             :    +----------------------------------------------------------------------+
      20             : */
      21             : 
      22             : #include <errno.h>
      23             : #include "ZendAccelerator.h"
      24             : #include "zend_shared_alloc.h"
      25             : #ifdef HAVE_UNISTD_H
      26             : # include <unistd.h>
      27             : #endif
      28             : #include <fcntl.h>
      29             : #ifndef ZEND_WIN32
      30             : # include <sys/types.h>
      31             : # include <dirent.h>
      32             : # include <signal.h>
      33             : # include <sys/stat.h>
      34             : # include <stdio.h>
      35             : #endif
      36             : 
      37             : #ifdef HAVE_MPROTECT
      38             : # include "sys/mman.h"
      39             : #endif
      40             : 
      41             : #define TMP_DIR "/tmp"
      42             : #define SEM_FILENAME_PREFIX ".ZendSem."
      43             : #define S_H(s) g_shared_alloc_handler->s
      44             : 
      45             : /* True globals */
      46             : /* old/new mapping. We can use true global even for ZTS because its usage
      47             :    is wrapped with exclusive lock anyway */
      48             : static HashTable xlat_table;
      49             : static const zend_shared_memory_handlers *g_shared_alloc_handler = NULL;
      50             : static const char *g_shared_model;
      51             : /* pointer to globals allocated in SHM and shared across processes */
      52             : zend_smm_shared_globals *smm_shared_globals;
      53             : 
      54             : #ifndef ZEND_WIN32
      55             : #ifdef ZTS
      56             : static MUTEX_T zts_lock;
      57             : #endif
      58             : int lock_file;
      59             : static char lockfile_name[sizeof(TMP_DIR) + sizeof(SEM_FILENAME_PREFIX) + 8];
      60             : #endif
      61             : 
      62             : static const zend_shared_memory_handler_entry handler_table[] = {
      63             : #ifdef USE_MMAP
      64             :         { "mmap", &zend_alloc_mmap_handlers },
      65             : #endif
      66             : #ifdef USE_SHM
      67             :         { "shm", &zend_alloc_shm_handlers },
      68             : #endif
      69             : #ifdef USE_SHM_OPEN
      70             :         { "posix", &zend_alloc_posix_handlers },
      71             : #endif
      72             : #ifdef ZEND_WIN32
      73             :         { "win32", &zend_alloc_win32_handlers },
      74             : #endif
      75             :         { NULL, NULL}
      76             : };
      77             : 
      78             : #ifndef ZEND_WIN32
      79         372 : void zend_shared_alloc_create_lock(void)
      80             : {
      81             :         int val;
      82             : 
      83             : #ifdef ZTS
      84             :     zts_lock = tsrm_mutex_alloc();
      85             : #endif
      86             : 
      87         372 :         sprintf(lockfile_name, "%s/%sXXXXXX", TMP_DIR, SEM_FILENAME_PREFIX);
      88         372 :         lock_file = mkstemp(lockfile_name);
      89         372 :         fchmod(lock_file, 0666);
      90             : 
      91         372 :         if (lock_file == -1) {
      92           0 :                 zend_accel_error(ACCEL_LOG_FATAL, "Unable to create lock file: %s (%d)", strerror(errno), errno);
      93             :         }
      94         372 :         val = fcntl(lock_file, F_GETFD, 0);
      95         372 :         val |= FD_CLOEXEC;
      96         372 :         fcntl(lock_file, F_SETFD, val);
      97             : 
      98         372 :         unlink(lockfile_name);
      99         372 : }
     100             : #endif
     101             : 
     102           0 : static void no_memory_bailout(size_t allocate_size, char *error)
     103             : {
     104           0 :         zend_accel_error(ACCEL_LOG_FATAL, "Unable to allocate shared memory segment of %ld bytes: %s: %s (%d)", allocate_size, error?error:"unknown", strerror(errno), errno );
     105           0 : }
     106             : 
     107         744 : static void copy_shared_segments(void *to, void *from, int count, int size)
     108             : {
     109         744 :         zend_shared_segment **shared_segments_v = (zend_shared_segment **)to;
     110         744 :         void *shared_segments_to_p = ((char *)to + count*(sizeof(void *)));
     111         744 :         void *shared_segments_from_p = from;
     112             :         int i;
     113             : 
     114        1488 :         for (i = 0; i < count; i++) {
     115         744 :                 shared_segments_v[i] =  shared_segments_to_p;
     116         744 :                 memcpy(shared_segments_to_p, shared_segments_from_p, size);
     117         744 :                 shared_segments_to_p = ((char *)shared_segments_to_p + size);
     118         744 :                 shared_segments_from_p = ((char *)shared_segments_from_p + size);
     119             :         }
     120         744 : }
     121             : 
     122         372 : static int zend_shared_alloc_try(const zend_shared_memory_handler_entry *he, size_t requested_size, zend_shared_segment ***shared_segments_p, int *shared_segments_count, char **error_in)
     123             : {
     124             :         int res;
     125         372 :         g_shared_alloc_handler = he->handler;
     126         372 :         g_shared_model = he->name;
     127         372 :         ZSMMG(shared_segments) = NULL;
     128         372 :         ZSMMG(shared_segments_count) = 0;
     129             : 
     130         372 :         res = S_H(create_segments)(requested_size, shared_segments_p, shared_segments_count, error_in);
     131             : 
     132         372 :         if (res) {
     133             :                 /* this model works! */
     134         372 :                 return res;
     135             :         }
     136           0 :         if (*shared_segments_p) {
     137             :                 int i;
     138             :                 /* cleanup */
     139           0 :                 for (i = 0; i < *shared_segments_count; i++) {
     140           0 :                         if ((*shared_segments_p)[i]->p && (*shared_segments_p)[i]->p != (void *)-1) {
     141           0 :                                 S_H(detach_segment)((*shared_segments_p)[i]);
     142             :                         }
     143             :                 }
     144           0 :                 free(*shared_segments_p);
     145           0 :                 *shared_segments_p = NULL;
     146             :         }
     147           0 :         g_shared_alloc_handler = NULL;
     148           0 :         return ALLOC_FAILURE;
     149             : }
     150             : 
     151         372 : int zend_shared_alloc_startup(size_t requested_size)
     152             : {
     153             :         zend_shared_segment **tmp_shared_segments;
     154             :         size_t shared_segments_array_size;
     155             :         zend_smm_shared_globals tmp_shared_globals, *p_tmp_shared_globals;
     156         372 :         char *error_in = NULL;
     157             :         const zend_shared_memory_handler_entry *he;
     158         372 :         int res = ALLOC_FAILURE;
     159             : 
     160             : 
     161             :         /* shared_free must be valid before we call zend_shared_alloc()
     162             :          * - make it temporarily point to a local variable
     163             :          */
     164         372 :         smm_shared_globals = &tmp_shared_globals;
     165         372 :         ZSMMG(shared_free) = requested_size; /* goes to tmp_shared_globals.shared_free */
     166             : 
     167         372 :         zend_shared_alloc_create_lock();
     168             : 
     169         372 :         if (ZCG(accel_directives).memory_model && ZCG(accel_directives).memory_model[0]) {
     170           0 :                 char *model = ZCG(accel_directives).memory_model;
     171             :                 /* "cgi" is really "shm"... */
     172           0 :                 if (strncmp(ZCG(accel_directives).memory_model, "cgi", sizeof("cgi")) == 0) {
     173           0 :                         model = "shm";
     174             :                 }
     175             : 
     176           0 :                 for (he = handler_table; he->name; he++) {
     177           0 :                         if (strcmp(model, he->name) == 0) {
     178           0 :                                 res = zend_shared_alloc_try(he, requested_size, &ZSMMG(shared_segments), &ZSMMG(shared_segments_count), &error_in);
     179             :                                 if (res) {
     180             :                                         /* this model works! */
     181             :                                 }
     182           0 :                                 break;
     183             :                         }
     184             :                 }
     185             :         }
     186             : 
     187         372 :         if (res == FAILED_REATTACHED) {
     188           0 :                 smm_shared_globals = NULL;
     189           0 :                 return res;
     190             :         }
     191             : 
     192         372 :         if (!g_shared_alloc_handler) {
     193             :                 /* try memory handlers in order */
     194         372 :                 for (he = handler_table; he->name; he++) {
     195         372 :                         res = zend_shared_alloc_try(he, requested_size, &ZSMMG(shared_segments), &ZSMMG(shared_segments_count), &error_in);
     196         372 :                         if (res) {
     197             :                                 /* this model works! */
     198         372 :                                 break;
     199             :                         }
     200             :                 }
     201             :         }
     202             : 
     203         372 :         if (!g_shared_alloc_handler) {
     204           0 :                 no_memory_bailout(requested_size, error_in);
     205           0 :                 return ALLOC_FAILURE;
     206             :         }
     207             : 
     208         372 :         if (res == SUCCESSFULLY_REATTACHED) {
     209           0 :                 return res;
     210             :         }
     211             : 
     212         372 :         shared_segments_array_size = ZSMMG(shared_segments_count) * S_H(segment_type_size)();
     213             : 
     214             :         /* move shared_segments and shared_free to shared memory */
     215         372 :         ZCG(locked) = 1; /* no need to perform a real lock at this point */
     216         372 :         p_tmp_shared_globals = (zend_smm_shared_globals *) zend_shared_alloc(sizeof(zend_smm_shared_globals));
     217         372 :         if (!p_tmp_shared_globals) {
     218           0 :                 zend_accel_error(ACCEL_LOG_FATAL, "Insufficient shared memory!");
     219           0 :                 return ALLOC_FAILURE;;
     220             :         }
     221             : 
     222         372 :         tmp_shared_segments = zend_shared_alloc(shared_segments_array_size + ZSMMG(shared_segments_count) * sizeof(void *));
     223         372 :         if (!tmp_shared_segments) {
     224           0 :                 zend_accel_error(ACCEL_LOG_FATAL, "Insufficient shared memory!");
     225           0 :                 return ALLOC_FAILURE;;
     226             :         }
     227             : 
     228         372 :         copy_shared_segments(tmp_shared_segments, ZSMMG(shared_segments)[0], ZSMMG(shared_segments_count), S_H(segment_type_size)());
     229             : 
     230         372 :         *p_tmp_shared_globals = tmp_shared_globals;
     231         372 :         smm_shared_globals = p_tmp_shared_globals;
     232             : 
     233         372 :         free(ZSMMG(shared_segments));
     234         372 :         ZSMMG(shared_segments) = tmp_shared_segments;
     235             : 
     236         372 :         ZSMMG(shared_memory_state).positions = (int *)zend_shared_alloc(sizeof(int) * ZSMMG(shared_segments_count));
     237         372 :         if (!ZSMMG(shared_memory_state).positions) {
     238           0 :                 zend_accel_error(ACCEL_LOG_FATAL, "Insufficient shared memory!");
     239           0 :                 return ALLOC_FAILURE;;
     240             :         }
     241             : 
     242         372 :         ZCG(locked) = 0;
     243             : 
     244         372 :         return res;
     245             : }
     246             : 
     247         372 : void zend_shared_alloc_shutdown(void)
     248             : {
     249             :         zend_shared_segment **tmp_shared_segments;
     250             :         size_t shared_segments_array_size;
     251             :         zend_smm_shared_globals tmp_shared_globals;
     252             :         int i;
     253             : 
     254         372 :         tmp_shared_globals = *smm_shared_globals;
     255         372 :         smm_shared_globals = &tmp_shared_globals;
     256         372 :         shared_segments_array_size = ZSMMG(shared_segments_count) * (S_H(segment_type_size)() + sizeof(void *));
     257         372 :         tmp_shared_segments = emalloc(shared_segments_array_size);
     258         372 :         copy_shared_segments(tmp_shared_segments, ZSMMG(shared_segments)[0], ZSMMG(shared_segments_count), S_H(segment_type_size)());
     259         372 :         ZSMMG(shared_segments) = tmp_shared_segments;
     260             : 
     261         744 :         for (i = 0; i < ZSMMG(shared_segments_count); i++) {
     262         372 :                 S_H(detach_segment)(ZSMMG(shared_segments)[i]);
     263             :         }
     264         372 :         efree(ZSMMG(shared_segments));
     265         372 :         ZSMMG(shared_segments) = NULL;
     266         372 :         g_shared_alloc_handler = NULL;
     267             : #ifndef ZEND_WIN32
     268         372 :         close(lock_file);
     269             : #endif
     270         372 : }
     271             : 
     272           0 : static size_t zend_shared_alloc_get_largest_free_block(void)
     273             : {
     274             :         int i;
     275           0 :         size_t largest_block_size = 0;
     276             : 
     277           0 :         for (i = 0; i < ZSMMG(shared_segments_count); i++) {
     278           0 :                 size_t block_size = ZSMMG(shared_segments)[i]->size - ZSMMG(shared_segments)[i]->pos;
     279             : 
     280           0 :                 if (block_size>largest_block_size) {
     281           0 :                         largest_block_size = block_size;
     282             :                 }
     283             :         }
     284           0 :         return largest_block_size;
     285             : }
     286             : 
     287             : #define MIN_FREE_MEMORY 64*1024
     288             : 
     289             : #define SHARED_ALLOC_FAILED() do {              \
     290             :                 zend_accel_error(ACCEL_LOG_WARNING, "Not enough free shared space to allocate %pd bytes (%pd bytes free)", (zend_long)size, (zend_long)ZSMMG(shared_free)); \
     291             :                 if (zend_shared_alloc_get_largest_free_block() < MIN_FREE_MEMORY) { \
     292             :                         ZSMMG(memory_exhausted) = 1; \
     293             :                 } \
     294             :         } while (0)
     295             : 
     296        4892 : void *zend_shared_alloc(size_t size)
     297             : {
     298             :         int i;
     299        4892 :         unsigned int block_size = ZEND_ALIGNED_SIZE(size);
     300             : 
     301             : #if 1
     302        4892 :         if (!ZCG(locked)) {
     303           0 :                 zend_accel_error(ACCEL_LOG_ERROR, "Shared memory lock not obtained");
     304             :         }
     305             : #endif
     306        4892 :         if (block_size > ZSMMG(shared_free)) { /* No hope to find a big-enough block */
     307           0 :                 SHARED_ALLOC_FAILED();
     308           0 :                 return NULL;
     309             :         }
     310        4892 :         for (i = 0; i < ZSMMG(shared_segments_count); i++) {
     311        4892 :                 if (ZSMMG(shared_segments)[i]->size - ZSMMG(shared_segments)[i]->pos >= block_size) { /* found a valid block */
     312        4892 :                         void *retval = (void *) (((char *) ZSMMG(shared_segments)[i]->p) + ZSMMG(shared_segments)[i]->pos);
     313             : 
     314        4892 :                         ZSMMG(shared_segments)[i]->pos += block_size;
     315        4892 :                         ZSMMG(shared_free) -= block_size;
     316        4892 :                         memset(retval, 0, block_size);
     317        4892 :                         return retval;
     318             :                 }
     319             :         }
     320           0 :         SHARED_ALLOC_FAILED();
     321           0 :         return NULL;
     322             : }
     323             : 
     324        2897 : int zend_shared_memdup_size(void *source, size_t size)
     325             : {
     326             :         void *old_p;
     327             : 
     328        5794 :         if ((old_p = zend_hash_index_find_ptr(&xlat_table, (zend_ulong)source)) != NULL) {
     329             :                 /* we already duplicated this pointer */
     330          65 :                 return 0;
     331             :         }
     332        2832 :         zend_shared_alloc_register_xlat_entry(source, source);
     333        2832 :         return ZEND_ALIGNED_SIZE(size);
     334             : }
     335             : 
     336        2881 : void *_zend_shared_memdup(void *source, size_t size, zend_bool free_source)
     337             : {
     338             :         void *old_p, *retval;
     339             : 
     340        5762 :         if ((old_p = zend_hash_index_find_ptr(&xlat_table, (zend_ulong)source)) != NULL) {
     341             :                 /* we already duplicated this pointer */
     342          40 :                 return old_p;
     343             :         }
     344        2841 :         retval = ZCG(mem);
     345        2841 :         ZCG(mem) = (void*)(((char*)ZCG(mem)) + ZEND_ALIGNED_SIZE(size));
     346        2841 :         memcpy(retval, source, size);
     347        2841 :         zend_shared_alloc_register_xlat_entry(source, retval);
     348        2841 :         if (free_source) {
     349         650 :                 efree(source);
     350             :         }
     351        2841 :         return retval;
     352             : }
     353             : 
     354         372 : void zend_shared_alloc_safe_unlock(void)
     355             : {
     356         372 :         if (ZCG(locked)) {
     357           0 :                 zend_shared_alloc_unlock();
     358             :         }
     359         372 : }
     360             : 
     361             : #ifndef ZEND_WIN32
     362             : /* name l_type l_whence l_start l_len */
     363             : static FLOCK_STRUCTURE(mem_write_lock, F_WRLCK, SEEK_SET, 0, 1);
     364             : static FLOCK_STRUCTURE(mem_write_unlock, F_UNLCK, SEEK_SET, 0, 1);
     365             : #endif
     366             : 
     367        1544 : void zend_shared_alloc_lock(void)
     368             : {
     369             : #ifndef ZEND_WIN32
     370             : 
     371             : #ifdef ZTS
     372             :         tsrm_mutex_lock(zts_lock);
     373             : #endif
     374             : 
     375             : #if 0
     376             :         /* this will happen once per process, and will un-globalize mem_write_lock */
     377             :         if (mem_write_lock.l_pid == -1) {
     378             :                 mem_write_lock.l_pid = getpid();
     379             :         }
     380             : #endif
     381             : 
     382             :         while (1) {
     383        1544 :                 if (fcntl(lock_file, F_SETLKW, &mem_write_lock) == -1) {
     384           0 :                         if (errno == EINTR) {
     385           0 :                                 continue;
     386             :                         }
     387           0 :                         zend_accel_error(ACCEL_LOG_ERROR, "Cannot create lock - %s (%d)", strerror(errno), errno);
     388             :                 }
     389        1544 :                 break;
     390           0 :         }
     391             : #else
     392             :         zend_shared_alloc_lock_win32();
     393             : #endif
     394             : 
     395        1544 :         ZCG(locked) = 1;
     396             : 
     397             :         /* Prepare translation table
     398             :          *
     399             :          * Make it persistent so that it uses malloc() and allocated blocks
     400             :          * won't be taken from space which is freed by efree in memdup.
     401             :          * Otherwise it leads to false matches in memdup check.
     402             :          */
     403        1544 :         zend_hash_init(&xlat_table, 128, NULL, NULL, 1);
     404        1544 : }
     405             : 
     406        1544 : void zend_shared_alloc_unlock(void)
     407             : {
     408             :         /* Destroy translation table */
     409        1544 :         zend_hash_destroy(&xlat_table);
     410             : 
     411        1544 :         ZCG(locked) = 0;
     412             : 
     413             : #ifndef ZEND_WIN32
     414        1544 :         if (fcntl(lock_file, F_SETLK, &mem_write_unlock) == -1) {
     415           0 :                 zend_accel_error(ACCEL_LOG_ERROR, "Cannot remove lock - %s (%d)", strerror(errno), errno);
     416             :         }
     417             : #ifdef ZTS
     418             :         tsrm_mutex_unlock(zts_lock);
     419             : #endif
     420             : #else
     421             :         zend_shared_alloc_unlock_win32();
     422             : #endif
     423        1544 : }
     424             : 
     425         425 : void zend_shared_alloc_clear_xlat_table(void)
     426             : {
     427         425 :         zend_hash_clean(&xlat_table);
     428         425 : }
     429             : 
     430        5748 : void zend_shared_alloc_register_xlat_entry(const void *old, const void *new)
     431             : {
     432        5748 :         zend_hash_index_update_ptr(&xlat_table, (zend_ulong)old, (void*)new);
     433        5748 : }
     434             : 
     435        1464 : void *zend_shared_alloc_get_xlat_entry(const void *old)
     436             : {
     437             :         void *retval;
     438             : 
     439        2928 :         if ((retval = zend_hash_index_find_ptr(&xlat_table, (zend_ulong)old)) == NULL) {
     440        1448 :                 return NULL;
     441             :         }
     442          16 :         return retval;
     443             : }
     444             : 
     445           2 : size_t zend_shared_alloc_get_free_memory(void)
     446             : {
     447           2 :         return ZSMMG(shared_free);
     448             : }
     449             : 
     450         372 : void zend_shared_alloc_save_state(void)
     451             : {
     452             :         int i;
     453             : 
     454         744 :         for (i = 0; i < ZSMMG(shared_segments_count); i++) {
     455         372 :                 ZSMMG(shared_memory_state).positions[i] = ZSMMG(shared_segments)[i]->pos;
     456             :         }
     457         372 :         ZSMMG(shared_memory_state).shared_free = ZSMMG(shared_free);
     458         372 : }
     459             : 
     460           0 : void zend_shared_alloc_restore_state(void)
     461             : {
     462             :         int i;
     463             : 
     464           0 :         for (i = 0; i < ZSMMG(shared_segments_count); i++) {
     465           0 :                 ZSMMG(shared_segments)[i]->pos = ZSMMG(shared_memory_state).positions[i];
     466             :         }
     467           0 :         ZSMMG(shared_free) = ZSMMG(shared_memory_state).shared_free;
     468           0 :         ZSMMG(memory_exhausted) = 0;
     469           0 :         ZSMMG(wasted_shared_memory) = 0;
     470           0 : }
     471             : 
     472           1 : const char *zend_accel_get_shared_model(void)
     473             : {
     474           1 :         return g_shared_model;
     475             : }
     476             : 
     477           0 : void zend_accel_shared_protect(int mode)
     478             : {
     479             : #ifdef HAVE_MPROTECT
     480             :         int i;
     481             : 
     482           0 :         if (mode) {
     483           0 :                 mode = PROT_READ;
     484             :         } else {
     485           0 :                 mode = PROT_READ|PROT_WRITE;
     486             :         }
     487             : 
     488           0 :         for (i = 0; i < ZSMMG(shared_segments_count); i++) {
     489           0 :                 mprotect(ZSMMG(shared_segments)[i]->p, ZSMMG(shared_segments)[i]->size, mode);
     490             :         }
     491             : #endif
     492           0 : }

Generated by: LCOV version 1.10

Generated at Thu, 30 Apr 2015 23:10:07 +0000 (3 days ago)

Copyright © 2005-2015 The PHP Group
All rights reserved.