Coverage Report

Created: 2025-09-27 06:26

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/php-src/ext/opcache/zend_shared_alloc.h
Line
Count
Source
1
/*
2
   +----------------------------------------------------------------------+
3
   | Zend OPcache                                                         |
4
   +----------------------------------------------------------------------+
5
   | Copyright (c) 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
   | https://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@php.net>                                 |
16
   |          Zeev Suraski <zeev@php.net>                                 |
17
   |          Stanislav Malyshev <stas@zend.com>                          |
18
   |          Dmitry Stogov <dmitry@php.net>                              |
19
   +----------------------------------------------------------------------+
20
*/
21
22
#ifndef ZEND_SHARED_ALLOC_H
23
#define ZEND_SHARED_ALLOC_H
24
25
#include "zend.h"
26
#include "ZendAccelerator.h"
27
28
#if defined(__APPLE__) && defined(__MACH__) /* darwin */
29
# ifdef HAVE_SHM_MMAP_POSIX
30
#  define USE_SHM_OPEN  1
31
# endif
32
# ifdef HAVE_SHM_MMAP_ANON
33
#  define USE_MMAP      1
34
# endif
35
#elif defined(__linux__) || defined(_AIX)
36
# ifdef HAVE_SHM_MMAP_POSIX
37
#  define USE_SHM_OPEN  1
38
# endif
39
# ifdef HAVE_SHM_IPC
40
#  define USE_SHM       1
41
# endif
42
# ifdef HAVE_SHM_MMAP_ANON
43
#  define USE_MMAP      1
44
# endif
45
#elif defined(__sparc) || defined(__sun)
46
# ifdef HAVE_SHM_MMAP_POSIX
47
#  define USE_SHM_OPEN  1
48
# endif
49
# ifdef HAVE_SHM_IPC
50
#  define USE_SHM       1
51
# endif
52
# if defined(__i386)
53
#  ifdef HAVE_SHM_MMAP_ANON
54
#   define USE_MMAP     1
55
#  endif
56
# endif
57
#else
58
# ifdef HAVE_SHM_MMAP_POSIX
59
#  define USE_SHM_OPEN  1
60
# endif
61
# ifdef HAVE_SHM_MMAP_ANON
62
#  define USE_MMAP      1
63
# endif
64
# ifdef HAVE_SHM_IPC
65
#  define USE_SHM       1
66
# endif
67
#endif
68
69
16
#define ALLOC_FAILURE           0
70
32
#define ALLOC_SUCCESS           1
71
16
#define FAILED_REATTACHED       2
72
16
#define SUCCESSFULLY_REATTACHED 4
73
#define ALLOC_FAIL_MAPPING      8
74
#define ALLOC_FALLBACK          9
75
0
#define NO_SHM_BACKEND          10
76
77
typedef struct _zend_shared_segment {
78
    size_t  size;
79
    size_t  end;
80
    size_t  pos;  /* position for simple stack allocator */
81
    void   *p;
82
} zend_shared_segment;
83
84
typedef int (*create_segments_t)(size_t requested_size, zend_shared_segment ***shared_segments, int *shared_segment_count, const char **error_in);
85
typedef int (*detach_segment_t)(zend_shared_segment *shared_segment);
86
87
typedef struct {
88
  create_segments_t create_segments;
89
  detach_segment_t detach_segment;
90
  size_t (*segment_type_size)(void);
91
} zend_shared_memory_handlers;
92
93
typedef struct _handler_entry {
94
  const char                  *name;
95
  const zend_shared_memory_handlers *handler;
96
} zend_shared_memory_handler_entry;
97
98
typedef struct _zend_shared_memory_state {
99
  size_t *positions;  /* current positions for each segment */
100
  size_t shared_free; /* amount of free shared memory */
101
} zend_shared_memory_state;
102
103
typedef struct _zend_smm_shared_globals {
104
    /* Shared Memory Manager */
105
    zend_shared_segment      **shared_segments;
106
    /* Number of allocated shared segments */
107
    int                        shared_segments_count;
108
    /* Amount of free shared memory */
109
    size_t                     shared_free;
110
    /* Amount of shared memory allocated by garbage */
111
    size_t                     wasted_shared_memory;
112
    /* No more shared memory flag */
113
    bool                  memory_exhausted;
114
    /* Saved Shared Allocator State */
115
    zend_shared_memory_state   shared_memory_state;
116
  /* Pointer to the application's shared data structures */
117
  void                      *app_shared_globals;
118
  /* Reserved shared memory */
119
  void                      *reserved;
120
  size_t                     reserved_size;
121
} zend_smm_shared_globals;
122
123
ZEND_EXT_API extern zend_smm_shared_globals *smm_shared_globals;
124
125
4.25M
#define ZSMMG(element)    (smm_shared_globals->element)
126
127
#define SHARED_ALLOC_REATTACHED   (SUCCESS+1)
128
129
BEGIN_EXTERN_C()
130
131
int zend_shared_alloc_startup(size_t requested_size, size_t reserved_size);
132
void zend_shared_alloc_shutdown(void);
133
134
/* allocate shared memory block */
135
void *zend_shared_alloc(size_t size);
136
137
/**
138
 * Wrapper for zend_shared_alloc() which aligns at 64-byte boundary if
139
 * AVX or SSE2 are used.
140
 */
141
60.4k
static inline void *zend_shared_alloc_aligned(size_t size) {
142
60.4k
#if defined(__AVX__) || defined(__SSE2__)
143
  /* Align to 64-byte boundary */
144
60.4k
  void *p = zend_shared_alloc(size + 64);
145
60.4k
  return (void *)(((uintptr_t)p + 63L) & ~63L);
146
#else
147
  return zend_shared_alloc(size);
148
#endif
149
60.4k
}
Unexecuted instantiation: shared_alloc_mmap.c:zend_shared_alloc_aligned
Unexecuted instantiation: shared_alloc_posix.c:zend_shared_alloc_aligned
Unexecuted instantiation: shared_alloc_shm.c:zend_shared_alloc_aligned
Unexecuted instantiation: zend_accelerator_hash.c:zend_shared_alloc_aligned
Unexecuted instantiation: zend_accelerator_module.c:zend_shared_alloc_aligned
Unexecuted instantiation: zend_accelerator_util_funcs.c:zend_shared_alloc_aligned
Unexecuted instantiation: zend_file_cache.c:zend_shared_alloc_aligned
Unexecuted instantiation: zend_persist_calc.c:zend_shared_alloc_aligned
Unexecuted instantiation: zend_persist.c:zend_shared_alloc_aligned
Unexecuted instantiation: zend_shared_alloc.c:zend_shared_alloc_aligned
ZendAccelerator.c:zend_shared_alloc_aligned
Line
Count
Source
141
60.4k
static inline void *zend_shared_alloc_aligned(size_t size) {
142
60.4k
#if defined(__AVX__) || defined(__SSE2__)
143
  /* Align to 64-byte boundary */
144
60.4k
  void *p = zend_shared_alloc(size + 64);
145
60.4k
  return (void *)(((uintptr_t)p + 63L) & ~63L);
146
#else
147
  return zend_shared_alloc(size);
148
#endif
149
60.4k
}
Unexecuted instantiation: zend_jit.c:zend_shared_alloc_aligned
150
151
/* copy into shared memory */
152
void *zend_shared_memdup_get_put_free(void *source, size_t size);
153
void *zend_shared_memdup_put_free(void *source, size_t size);
154
void *zend_shared_memdup_free(void *source, size_t size);
155
void *zend_shared_memdup_get_put(void *source, size_t size);
156
void *zend_shared_memdup_put(void *source, size_t size);
157
void *zend_shared_memdup(void *source, size_t size);
158
159
int  zend_shared_memdup_size(void *p, size_t size);
160
161
bool zend_accel_in_shm(void *ptr);
162
163
typedef union _align_test {
164
  void   *ptr;
165
  double  dbl;
166
  zend_long  lng;
167
} align_test;
168
169
#if ZEND_GCC_VERSION >= 2000
170
# define PLATFORM_ALIGNMENT (__alignof__(align_test) < 8 ? 8 : __alignof__(align_test))
171
#else
172
# define PLATFORM_ALIGNMENT (sizeof(align_test))
173
#endif
174
175
#define ZEND_ALIGNED_SIZE(size) \
176
2.14M
  ZEND_MM_ALIGNED_SIZE_EX(size, PLATFORM_ALIGNMENT)
177
178
/* exclusive locking */
179
void zend_shared_alloc_lock(void);
180
void zend_shared_alloc_unlock(void); /* returns the allocated size during lock..unlock */
181
void zend_shared_alloc_safe_unlock(void);
182
183
/* old/new mapping functions */
184
void zend_shared_alloc_init_xlat_table(void);
185
void zend_shared_alloc_destroy_xlat_table(void);
186
void zend_shared_alloc_clear_xlat_table(void);
187
uint32_t zend_shared_alloc_checkpoint_xlat_table(void);
188
void zend_shared_alloc_restore_xlat_table(uint32_t checkpoint);
189
void zend_shared_alloc_register_xlat_entry(const void *key, const void *value);
190
void *zend_shared_alloc_get_xlat_entry(const void *key);
191
192
size_t zend_shared_alloc_get_free_memory(void);
193
void zend_shared_alloc_save_state(void);
194
void zend_shared_alloc_restore_state(void);
195
const char *zend_accel_get_shared_model(void);
196
197
/**
198
 * Memory write protection
199
 *
200
 * @param protected true to protect shared memory (read-only), false
201
 * to unprotect shared memory (writable)
202
 */
203
void zend_accel_shared_protect(bool protected);
204
205
#ifdef USE_MMAP
206
extern const zend_shared_memory_handlers zend_alloc_mmap_handlers;
207
#endif
208
209
#ifdef USE_SHM
210
extern const zend_shared_memory_handlers zend_alloc_shm_handlers;
211
#endif
212
213
#ifdef USE_SHM_OPEN
214
extern const zend_shared_memory_handlers zend_alloc_posix_handlers;
215
#endif
216
217
#ifdef ZEND_WIN32
218
extern const zend_shared_memory_handlers zend_alloc_win32_handlers;
219
void zend_shared_alloc_create_lock(void);
220
void zend_shared_alloc_lock_win32(void);
221
void zend_shared_alloc_unlock_win32(void);
222
#endif
223
224
END_EXTERN_C()
225
226
#endif /* ZEND_SHARED_ALLOC_H */