Coverage Report

Created: 2026-06-02 06:36

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/php-src/ext/opcache/shared_alloc_posix.c
Line
Count
Source
1
/*
2
   +----------------------------------------------------------------------+
3
   | Zend OPcache                                                         |
4
   +----------------------------------------------------------------------+
5
   | Copyright © The PHP Group and Contributors.                          |
6
   +----------------------------------------------------------------------+
7
   | This source file is subject to the Modified BSD License that is      |
8
   | bundled with this package in the file LICENSE, and is available      |
9
   | through the World Wide Web at <https://www.php.net/license/>.        |
10
   |                                                                      |
11
   | SPDX-License-Identifier: BSD-3-Clause                                |
12
   +----------------------------------------------------------------------+
13
   | Authors: Andi Gutmans <andi@php.net>                                 |
14
   |          Zeev Suraski <zeev@php.net>                                 |
15
   |          Stanislav Malyshev <stas@zend.com>                          |
16
   |          Dmitry Stogov <dmitry@php.net>                              |
17
   +----------------------------------------------------------------------+
18
*/
19
20
#include "zend_shared_alloc.h"
21
22
#ifdef USE_SHM_OPEN
23
24
#include <sys/types.h>
25
#include <sys/stat.h>
26
#include <stdio.h>
27
#include <fcntl.h>
28
#include <sys/mman.h>
29
#include <unistd.h>
30
#include <stdlib.h>
31
32
typedef struct  {
33
    zend_shared_segment common;
34
    int shm_fd;
35
} zend_shared_segment_posix;
36
37
static int create_segments(size_t requested_size, zend_shared_segment_posix ***shared_segments_p, int *shared_segments_count, const char **error_in)
38
0
{
39
0
  zend_shared_segment_posix *shared_segment;
40
0
  char shared_segment_name[sizeof("/ZendAccelerator.") + 20];
41
0
  int shared_segment_flags = O_RDWR|O_CREAT|O_TRUNC;
42
0
  mode_t shared_segment_mode = 0600;
43
44
#if defined(HAVE_SHM_CREATE_LARGEPAGE)
45
  /**
46
   * architectures have 3 entries max and we are interested
47
   * from the second offset minimum to be worthy creating
48
   * a special shared segment tagged as 'large'.
49
   * only then amd64/i386/arm64 and perharps risc64*
50
   * archs are on interest here.
51
   */
52
  size_t i, shared_segment_sizes = 0, shared_segment_lg_index = 0;
53
  size_t shared_segment_sindexes[3] = {0};
54
  const size_t entries = sizeof(shared_segment_sindexes) / sizeof(shared_segment_sindexes[0]);
55
56
  shared_segment_sizes = getpagesizes(shared_segment_sindexes, entries);
57
58
  if (shared_segment_sizes > 0) {
59
    for (i = shared_segment_sizes - 1; i >= 0; i --) {
60
      if (shared_segment_sindexes[i] != 0 &&
61
          !(requested_size % shared_segment_sindexes[i])) {
62
        shared_segment_lg_index = i;
63
        break;
64
      }
65
    }
66
  }
67
#endif
68
69
0
  *shared_segments_count = 1;
70
0
  *shared_segments_p = (zend_shared_segment_posix **) calloc(1, sizeof(zend_shared_segment_posix) + sizeof(void *));
71
0
  if (!*shared_segments_p) {
72
0
    *error_in = "calloc";
73
0
    return ALLOC_FAILURE;
74
0
  }
75
0
  shared_segment = (zend_shared_segment_posix *)((char *)(*shared_segments_p) + sizeof(void *));
76
0
  (*shared_segments_p)[0] = shared_segment;
77
78
0
  snprintf(shared_segment_name, sizeof(shared_segment_name), "/ZendAccelerator.%d", getpid());
79
#if defined(HAVE_SHM_CREATE_LARGEPAGE)
80
  if (shared_segment_lg_index > 0) {
81
    shared_segment->shm_fd =  shm_create_largepage(shared_segment_name, shared_segment_flags, shared_segment_lg_index, SHM_LARGEPAGE_ALLOC_DEFAULT, shared_segment_mode);
82
    if (shared_segment->shm_fd != -1) {
83
      goto truncate_segment;
84
    }
85
  }
86
#endif
87
88
0
  shared_segment->shm_fd = shm_open(shared_segment_name, shared_segment_flags, shared_segment_mode);
89
0
  if (shared_segment->shm_fd == -1) {
90
0
    *error_in = "shm_open";
91
0
    return ALLOC_FAILURE;
92
0
  }
93
94
#if defined(HAVE_SHM_CREATE_LARGEPAGE)
95
truncate_segment:
96
#endif
97
0
  if (ftruncate(shared_segment->shm_fd, requested_size) != 0) {
98
0
    *error_in = "ftruncate";
99
0
    shm_unlink(shared_segment_name);
100
0
    return ALLOC_FAILURE;
101
0
  }
102
103
0
  shared_segment->common.p = mmap(0, requested_size, PROT_READ | PROT_WRITE, MAP_SHARED, shared_segment->shm_fd, 0);
104
0
  if (shared_segment->common.p == MAP_FAILED) {
105
0
    *error_in = "mmap";
106
0
    shm_unlink(shared_segment_name);
107
0
    return ALLOC_FAILURE;
108
0
  }
109
0
  shm_unlink(shared_segment_name);
110
111
0
  shared_segment->common.pos = 0;
112
0
  shared_segment->common.size = requested_size;
113
114
0
  return ALLOC_SUCCESS;
115
0
}
116
117
static int detach_segment(zend_shared_segment_posix *shared_segment)
118
0
{
119
0
  munmap(shared_segment->common.p, shared_segment->common.size);
120
0
  close(shared_segment->shm_fd);
121
0
  return 0;
122
0
}
123
124
static size_t segment_type_size(void)
125
0
{
126
0
  return sizeof(zend_shared_segment_posix);
127
0
}
128
129
const zend_shared_memory_handlers zend_alloc_posix_handlers = {
130
  (create_segments_t)create_segments,
131
  (detach_segment_t)detach_segment,
132
  segment_type_size
133
};
134
135
#endif /* USE_SHM_OPEN */