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_shm.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
23
24
#if defined(__FreeBSD__)
25
# include <machine/param.h>
26
#endif
27
#include <sys/types.h>
28
#include <sys/shm.h>
29
#include <sys/ipc.h>
30
#include <signal.h>
31
#include <stdio.h>
32
#include <stdlib.h>
33
#include <unistd.h>
34
#include <errno.h>
35
36
#include <sys/stat.h>
37
#include <fcntl.h>
38
39
#ifndef MIN
40
# define MIN(x, y) ((x) > (y)? (y) : (x))
41
#endif
42
43
0
#define SEG_ALLOC_SIZE_MIN 2*1024*1024
44
45
typedef struct  {
46
    zend_shared_segment common;
47
    int shm_id;
48
} zend_shared_segment_shm;
49
50
static int create_segments(size_t requested_size, zend_shared_segment_shm ***shared_segments_p, int *shared_segments_count, const char **error_in)
51
0
{
52
0
  int i;
53
0
  size_t allocate_size = 0, remaining_bytes, seg_allocate_size;
54
0
  int first_segment_id = -1;
55
0
  key_t first_segment_key = -1;
56
0
  struct shmid_ds sds;
57
0
  int shmget_flags;
58
0
  zend_shared_segment_shm *shared_segments;
59
60
0
  shmget_flags = IPC_CREAT|SHM_R|SHM_W|IPC_EXCL;
61
62
  /* Try contiguous allocation first. */
63
0
  seg_allocate_size = requested_size;
64
0
  first_segment_id = shmget(first_segment_key, seg_allocate_size, shmget_flags);
65
0
  if (UNEXPECTED(first_segment_id == -1)) {
66
    /* Search for biggest n^2 < requested_size. */
67
0
    seg_allocate_size = SEG_ALLOC_SIZE_MIN;
68
0
    while (seg_allocate_size < requested_size / 2) {
69
0
      seg_allocate_size *= 2;
70
0
    }
71
72
    /* try allocating this much, if not - try shrinking */
73
0
    while (seg_allocate_size >= SEG_ALLOC_SIZE_MIN) {
74
0
      first_segment_id = shmget(first_segment_key, seg_allocate_size, shmget_flags);
75
0
      if (first_segment_id != -1) {
76
0
        break;
77
0
      }
78
0
      seg_allocate_size >>= 1; /* shrink the allocated block */
79
0
    }
80
81
0
    if (first_segment_id == -1) {
82
0
      *error_in = "shmget";
83
0
      return ALLOC_FAILURE;
84
0
    }
85
0
  }
86
87
0
  *shared_segments_count = ((requested_size - 1) / seg_allocate_size) + 1;
88
0
  *shared_segments_p = (zend_shared_segment_shm **) calloc(1, (*shared_segments_count) * sizeof(zend_shared_segment_shm) + sizeof(void *) * (*shared_segments_count));
89
0
  if (!*shared_segments_p) {
90
0
    *error_in = "calloc";
91
0
    return ALLOC_FAILURE;
92
0
  }
93
0
  shared_segments = (zend_shared_segment_shm *)((char *)(*shared_segments_p) + sizeof(void *) * (*shared_segments_count));
94
0
  for (i = 0; i < *shared_segments_count; i++) {
95
0
    (*shared_segments_p)[i] = shared_segments + i;
96
0
  }
97
98
0
  remaining_bytes = requested_size;
99
0
  for (i = 0; i < *shared_segments_count; i++) {
100
0
    allocate_size = MIN(remaining_bytes, seg_allocate_size);
101
0
    if (i != 0) {
102
0
      shared_segments[i].shm_id = shmget(IPC_PRIVATE, allocate_size, shmget_flags);
103
0
    } else {
104
0
      shared_segments[i].shm_id = first_segment_id;
105
0
    }
106
107
0
    if (shared_segments[i].shm_id == -1) {
108
0
      return ALLOC_FAILURE;
109
0
    }
110
111
0
    shared_segments[i].common.p = shmat(shared_segments[i].shm_id, NULL, 0);
112
0
    if (shared_segments[i].common.p == (void *)-1) {
113
0
      *error_in = "shmat";
114
0
      shmctl(shared_segments[i].shm_id, IPC_RMID, &sds);
115
0
      return ALLOC_FAILURE;
116
0
    }
117
0
    shmctl(shared_segments[i].shm_id, IPC_RMID, &sds);
118
119
0
    shared_segments[i].common.pos = 0;
120
0
    shared_segments[i].common.size = allocate_size;
121
0
    remaining_bytes -= allocate_size;
122
0
  }
123
0
  return ALLOC_SUCCESS;
124
0
}
125
126
static int detach_segment(zend_shared_segment_shm *shared_segment)
127
0
{
128
0
  shmdt(shared_segment->common.p);
129
0
  return 0;
130
0
}
131
132
static size_t segment_type_size(void)
133
0
{
134
0
  return sizeof(zend_shared_segment_shm);
135
0
}
136
137
const zend_shared_memory_handlers zend_alloc_shm_handlers = {
138
  (create_segments_t)create_segments,
139
  (detach_segment_t)detach_segment,
140
  segment_type_size
141
};
142
143
#endif /* USE_SHM */