Coverage Report

Created: 2026-07-25 06:39

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 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
  int i, shared_segment_sizes;
56
57
  shared_segment_sizes = getpagesizes(shared_segment_sindexes, entries);
58
59
  if (shared_segment_sizes > 0) {
60
    for (i = shared_segment_sizes - 1; i >= 0; i --) {
61
      if (shared_segment_sindexes[i] != 0 &&
62
          !(requested_size % shared_segment_sindexes[i])) {
63
        shared_segment_lg_index = i;
64
        break;
65
      }
66
    }
67
  }
68
#endif
69
70
0
  *shared_segments_count = 1;
71
0
  *shared_segments_p = (zend_shared_segment_posix **) calloc(1, sizeof(zend_shared_segment_posix) + sizeof(void *));
72
0
  if (!*shared_segments_p) {
73
0
    *error_in = "calloc";
74
0
    return ALLOC_FAILURE;
75
0
  }
76
0
  shared_segment = (zend_shared_segment_posix *)((char *)(*shared_segments_p) + sizeof(void *));
77
0
  (*shared_segments_p)[0] = shared_segment;
78
79
0
  snprintf(shared_segment_name, sizeof(shared_segment_name), "/ZendAccelerator.%d", getpid());
80
#if defined(HAVE_SHM_CREATE_LARGEPAGE)
81
  if (shared_segment_lg_index > 0) {
82
    shared_segment->shm_fd =  shm_create_largepage(shared_segment_name, shared_segment_flags, shared_segment_lg_index, SHM_LARGEPAGE_ALLOC_DEFAULT, shared_segment_mode);
83
    if (shared_segment->shm_fd != -1) {
84
      goto truncate_segment;
85
    }
86
  }
87
#endif
88
89
0
  shared_segment->shm_fd = shm_open(shared_segment_name, shared_segment_flags, shared_segment_mode);
90
0
  if (shared_segment->shm_fd == -1) {
91
0
    *error_in = "shm_open";
92
0
    return ALLOC_FAILURE;
93
0
  }
94
95
#if defined(HAVE_SHM_CREATE_LARGEPAGE)
96
truncate_segment:
97
#endif
98
0
  if (ftruncate(shared_segment->shm_fd, requested_size) != 0) {
99
0
    *error_in = "ftruncate";
100
0
    shm_unlink(shared_segment_name);
101
0
    return ALLOC_FAILURE;
102
0
  }
103
104
0
  shared_segment->common.p = mmap(0, requested_size, PROT_READ | PROT_WRITE, MAP_SHARED, shared_segment->shm_fd, 0);
105
0
  if (shared_segment->common.p == MAP_FAILED) {
106
0
    *error_in = "mmap";
107
0
    shm_unlink(shared_segment_name);
108
0
    return ALLOC_FAILURE;
109
0
  }
110
0
  shm_unlink(shared_segment_name);
111
112
0
  shared_segment->common.pos = 0;
113
0
  shared_segment->common.size = requested_size;
114
115
0
  return ALLOC_SUCCESS;
116
0
}
117
118
static int detach_segment(zend_shared_segment_posix *shared_segment)
119
0
{
120
0
  munmap(shared_segment->common.p, shared_segment->common.size);
121
0
  close(shared_segment->shm_fd);
122
0
  return 0;
123
0
}
124
125
static size_t segment_type_size(void)
126
0
{
127
0
  return sizeof(zend_shared_segment_posix);
128
0
}
129
130
const zend_shared_memory_handlers zend_alloc_posix_handlers = {
131
  (create_segments_t)create_segments,
132
  (detach_segment_t)detach_segment,
133
  segment_type_size
134
};
135
136
#endif /* USE_SHM_OPEN */