Coverage Report

Created: 2026-02-14 06:52

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/php-src/ext/random/php_random.h
Line
Count
Source
1
/*
2
   +----------------------------------------------------------------------+
3
   | Copyright (c) The PHP Group                                          |
4
   +----------------------------------------------------------------------+
5
   | This source file is subject to version 3.01 of the PHP license,      |
6
   | that is bundled with this package in the file LICENSE, and is        |
7
   | available through the world-wide-web at the following url:           |
8
   | https://www.php.net/license/3_01.txt                                 |
9
   | If you did not receive a copy of the PHP license and are unable to   |
10
   | obtain it through the world-wide-web, please send a note to          |
11
   | license@php.net so we can mail you a copy immediately.               |
12
   +----------------------------------------------------------------------+
13
   | Authors: Rasmus Lerdorf <rasmus@php.net>                             |
14
   |          Zeev Suraski <zeev@php.net>                                 |
15
   |          Sascha Schumann <sascha@schumann.cx>                        |
16
   |          Pedro Melo <melo@ip.pt>                                     |
17
   |          Sterling Hughes <sterling@php.net>                          |
18
   |          Sammy Kaye Powers <me@sammyk.me>                            |
19
   |          Go Kudo <zeriyoshi@php.net>                                 |
20
   |                                                                      |
21
   | Based on code from: Richard J. Wagner <rjwagner@writeme.com>         |
22
   |                     Makoto Matsumoto <matumoto@math.keio.ac.jp>      |
23
   |                     Takuji Nishimura                                 |
24
   |                     Shawn Cokus <Cokus@math.washington.edu>          |
25
   |                     David Blackman                                   |
26
   |                     Sebastiano Vigna <vigna@acm.org>                 |
27
   |                     Melissa O'Neill <oneill@pcg-random.org>          |
28
   +----------------------------------------------------------------------+
29
*/
30
31
#ifndef PHP_RANDOM_H
32
# define PHP_RANDOM_H
33
34
# include "php.h"
35
# include "php_random_csprng.h"
36
# include "php_random_uint128.h"
37
# include "random_decl.h"
38
39
PHPAPI double php_combined_lcg(void);
40
41
typedef struct _php_random_fallback_seed_state php_random_fallback_seed_state;
42
43
PHPAPI uint64_t php_random_generate_fallback_seed(void);
44
PHPAPI uint64_t php_random_generate_fallback_seed_ex(php_random_fallback_seed_state *state);
45
46
static inline zend_long GENERATE_SEED(void)
47
0
{
48
0
  return (zend_long)php_random_generate_fallback_seed();
49
0
}
Unexecuted instantiation: exif.c:GENERATE_SEED
Unexecuted instantiation: csprng.c:GENERATE_SEED
Unexecuted instantiation: engine_mt19937.c:GENERATE_SEED
Unexecuted instantiation: engine_pcgoneseq128xslrr64.c:GENERATE_SEED
Unexecuted instantiation: engine_secure.c:GENERATE_SEED
Unexecuted instantiation: engine_user.c:GENERATE_SEED
Unexecuted instantiation: engine_xoshiro256starstar.c:GENERATE_SEED
Unexecuted instantiation: gammasection.c:GENERATE_SEED
Unexecuted instantiation: random.c:GENERATE_SEED
Unexecuted instantiation: randomizer.c:GENERATE_SEED
Unexecuted instantiation: zend_utils.c:GENERATE_SEED
Unexecuted instantiation: spl_directory.c:GENERATE_SEED
Unexecuted instantiation: spl_observer.c:GENERATE_SEED
Unexecuted instantiation: array.c:GENERATE_SEED
Unexecuted instantiation: basic_functions.c:GENERATE_SEED
Unexecuted instantiation: exec.c:GENERATE_SEED
Unexecuted instantiation: file.c:GENERATE_SEED
Unexecuted instantiation: filters.c:GENERATE_SEED
Unexecuted instantiation: ftp_fopen_wrapper.c:GENERATE_SEED
Unexecuted instantiation: head.c:GENERATE_SEED
Unexecuted instantiation: html.c:GENERATE_SEED
Unexecuted instantiation: http_fopen_wrapper.c:GENERATE_SEED
Unexecuted instantiation: link.c:GENERATE_SEED
Unexecuted instantiation: mail.c:GENERATE_SEED
Unexecuted instantiation: php_fopen_wrapper.c:GENERATE_SEED
Unexecuted instantiation: streamsfuncs.c:GENERATE_SEED
Unexecuted instantiation: string.c:GENERATE_SEED
Unexecuted instantiation: strnatcmp.c:GENERATE_SEED
Unexecuted instantiation: uniqid.c:GENERATE_SEED
Unexecuted instantiation: url_scanner_ex.c:GENERATE_SEED
Unexecuted instantiation: var.c:GENERATE_SEED
Unexecuted instantiation: fopen_wrappers.c:GENERATE_SEED
Unexecuted instantiation: main.c:GENERATE_SEED
Unexecuted instantiation: php_open_temporary_file.c:GENERATE_SEED
Unexecuted instantiation: php_variables.c:GENERATE_SEED
Unexecuted instantiation: streams.c:GENERATE_SEED
Unexecuted instantiation: internal_functions_cli.c:GENERATE_SEED
50
51
0
# define PHP_MT_RAND_MAX ((zend_long) (0x7FFFFFFF)) /* (1<<31) - 1 */
52
53
enum php_random_mt19937_mode {
54
  MT_RAND_MT19937 = 0,
55
  MT_RAND_PHP = 1,
56
};
57
58
0
#define PHP_RANDOM_RANGE_ATTEMPTS (50)
59
60
PHPAPI void php_mt_srand(uint32_t seed);
61
PHPAPI uint32_t php_mt_rand(void);
62
PHPAPI zend_long php_mt_rand_range(zend_long min, zend_long max);
63
PHPAPI zend_long php_mt_rand_common(zend_long min, zend_long max);
64
65
typedef struct _php_random_status_state_mt19937 {
66
  uint32_t count;
67
  enum php_random_mt19937_mode mode;
68
  uint32_t state[624];
69
} php_random_status_state_mt19937;
70
71
typedef struct _php_random_status_state_pcgoneseq128xslrr64 {
72
  php_random_uint128_t state;
73
} php_random_status_state_pcgoneseq128xslrr64;
74
75
typedef struct _php_random_status_state_xoshiro256starstar {
76
  uint64_t state[4];
77
} php_random_status_state_xoshiro256starstar;
78
79
typedef struct _php_random_status_state_user {
80
  zend_object *object;
81
  zend_function *generate_method;
82
} php_random_status_state_user;
83
84
typedef struct _php_random_result {
85
  uint64_t result;
86
  size_t size;
87
} php_random_result;
88
89
typedef struct _php_random_algo {
90
  const size_t state_size;
91
  php_random_result (*generate)(void *state);
92
  zend_long (*range)(void *state, zend_long min, zend_long max);
93
  bool (*serialize)(void *state, HashTable *data);
94
  bool (*unserialize)(void *state, HashTable *data);
95
} php_random_algo;
96
97
typedef struct _php_random_algo_with_state {
98
  const php_random_algo *algo;
99
  void *state;
100
} php_random_algo_with_state;
101
102
typedef struct _php_random_fallback_seed_state {
103
  bool initialized;
104
  unsigned char seed[20];
105
} php_random_fallback_seed_state;
106
107
extern PHPAPI const php_random_algo php_random_algo_mt19937;
108
extern PHPAPI const php_random_algo php_random_algo_pcgoneseq128xslrr64;
109
extern PHPAPI const php_random_algo php_random_algo_xoshiro256starstar;
110
extern PHPAPI const php_random_algo php_random_algo_secure;
111
extern PHPAPI const php_random_algo php_random_algo_user;
112
113
typedef struct _php_random_engine {
114
  php_random_algo_with_state engine;
115
  zend_object std;
116
} php_random_engine;
117
118
typedef struct _php_random_randomizer {
119
  php_random_algo_with_state engine;
120
  bool is_userland_algo;
121
  zend_object std;
122
} php_random_randomizer;
123
124
extern PHPAPI zend_class_entry *random_ce_Random_Engine;
125
extern PHPAPI zend_class_entry *random_ce_Random_CryptoSafeEngine;
126
127
extern PHPAPI zend_class_entry *random_ce_Random_RandomError;
128
extern PHPAPI zend_class_entry *random_ce_Random_BrokenRandomEngineError;
129
extern PHPAPI zend_class_entry *random_ce_Random_RandomException;
130
131
extern PHPAPI zend_class_entry *random_ce_Random_Engine_PcgOneseq128XslRr64;
132
extern PHPAPI zend_class_entry *random_ce_Random_Engine_Mt19937;
133
extern PHPAPI zend_class_entry *random_ce_Random_Engine_Xoshiro256StarStar;
134
extern PHPAPI zend_class_entry *random_ce_Random_Engine_Secure;
135
136
extern PHPAPI zend_class_entry *random_ce_Random_Randomizer;
137
138
extern PHPAPI zend_class_entry *random_ce_Random_IntervalBoundary;
139
140
97
static inline php_random_engine *php_random_engine_from_obj(zend_object *object) {
141
97
  return (php_random_engine *)((char *)(object) - XtOffsetOf(php_random_engine, std));
142
97
}
Unexecuted instantiation: exif.c:php_random_engine_from_obj
Unexecuted instantiation: csprng.c:php_random_engine_from_obj
engine_mt19937.c:php_random_engine_from_obj
Line
Count
Source
140
11
static inline php_random_engine *php_random_engine_from_obj(zend_object *object) {
141
  return (php_random_engine *)((char *)(object) - XtOffsetOf(php_random_engine, std));
142
11
}
engine_pcgoneseq128xslrr64.c:php_random_engine_from_obj
Line
Count
Source
140
4
static inline php_random_engine *php_random_engine_from_obj(zend_object *object) {
141
  return (php_random_engine *)((char *)(object) - XtOffsetOf(php_random_engine, std));
142
4
}
Unexecuted instantiation: engine_secure.c:php_random_engine_from_obj
Unexecuted instantiation: engine_user.c:php_random_engine_from_obj
engine_xoshiro256starstar.c:php_random_engine_from_obj
Line
Count
Source
140
9
static inline php_random_engine *php_random_engine_from_obj(zend_object *object) {
141
  return (php_random_engine *)((char *)(object) - XtOffsetOf(php_random_engine, std));
142
9
}
Unexecuted instantiation: gammasection.c:php_random_engine_from_obj
random.c:php_random_engine_from_obj
Line
Count
Source
140
69
static inline php_random_engine *php_random_engine_from_obj(zend_object *object) {
141
  return (php_random_engine *)((char *)(object) - XtOffsetOf(php_random_engine, std));
142
69
}
randomizer.c:php_random_engine_from_obj
Line
Count
Source
140
4
static inline php_random_engine *php_random_engine_from_obj(zend_object *object) {
141
  return (php_random_engine *)((char *)(object) - XtOffsetOf(php_random_engine, std));
142
4
}
Unexecuted instantiation: zend_utils.c:php_random_engine_from_obj
Unexecuted instantiation: spl_directory.c:php_random_engine_from_obj
Unexecuted instantiation: spl_observer.c:php_random_engine_from_obj
Unexecuted instantiation: array.c:php_random_engine_from_obj
Unexecuted instantiation: basic_functions.c:php_random_engine_from_obj
Unexecuted instantiation: exec.c:php_random_engine_from_obj
Unexecuted instantiation: file.c:php_random_engine_from_obj
Unexecuted instantiation: filters.c:php_random_engine_from_obj
Unexecuted instantiation: ftp_fopen_wrapper.c:php_random_engine_from_obj
Unexecuted instantiation: head.c:php_random_engine_from_obj
Unexecuted instantiation: html.c:php_random_engine_from_obj
Unexecuted instantiation: http_fopen_wrapper.c:php_random_engine_from_obj
Unexecuted instantiation: link.c:php_random_engine_from_obj
Unexecuted instantiation: mail.c:php_random_engine_from_obj
Unexecuted instantiation: php_fopen_wrapper.c:php_random_engine_from_obj
Unexecuted instantiation: streamsfuncs.c:php_random_engine_from_obj
Unexecuted instantiation: string.c:php_random_engine_from_obj
Unexecuted instantiation: strnatcmp.c:php_random_engine_from_obj
Unexecuted instantiation: uniqid.c:php_random_engine_from_obj
Unexecuted instantiation: url_scanner_ex.c:php_random_engine_from_obj
Unexecuted instantiation: var.c:php_random_engine_from_obj
Unexecuted instantiation: fopen_wrappers.c:php_random_engine_from_obj
Unexecuted instantiation: main.c:php_random_engine_from_obj
Unexecuted instantiation: php_open_temporary_file.c:php_random_engine_from_obj
Unexecuted instantiation: php_variables.c:php_random_engine_from_obj
Unexecuted instantiation: streams.c:php_random_engine_from_obj
Unexecuted instantiation: internal_functions_cli.c:php_random_engine_from_obj
143
144
12
static inline php_random_randomizer *php_random_randomizer_from_obj(zend_object *object) {
145
12
  return (php_random_randomizer *)((char *)(object) - XtOffsetOf(php_random_randomizer, std));
146
12
}
Unexecuted instantiation: exif.c:php_random_randomizer_from_obj
Unexecuted instantiation: csprng.c:php_random_randomizer_from_obj
Unexecuted instantiation: engine_mt19937.c:php_random_randomizer_from_obj
Unexecuted instantiation: engine_pcgoneseq128xslrr64.c:php_random_randomizer_from_obj
Unexecuted instantiation: engine_secure.c:php_random_randomizer_from_obj
Unexecuted instantiation: engine_user.c:php_random_randomizer_from_obj
Unexecuted instantiation: engine_xoshiro256starstar.c:php_random_randomizer_from_obj
Unexecuted instantiation: gammasection.c:php_random_randomizer_from_obj
random.c:php_random_randomizer_from_obj
Line
Count
Source
144
6
static inline php_random_randomizer *php_random_randomizer_from_obj(zend_object *object) {
145
  return (php_random_randomizer *)((char *)(object) - XtOffsetOf(php_random_randomizer, std));
146
6
}
randomizer.c:php_random_randomizer_from_obj
Line
Count
Source
144
6
static inline php_random_randomizer *php_random_randomizer_from_obj(zend_object *object) {
145
  return (php_random_randomizer *)((char *)(object) - XtOffsetOf(php_random_randomizer, std));
146
6
}
Unexecuted instantiation: zend_utils.c:php_random_randomizer_from_obj
Unexecuted instantiation: spl_directory.c:php_random_randomizer_from_obj
Unexecuted instantiation: spl_observer.c:php_random_randomizer_from_obj
Unexecuted instantiation: array.c:php_random_randomizer_from_obj
Unexecuted instantiation: basic_functions.c:php_random_randomizer_from_obj
Unexecuted instantiation: exec.c:php_random_randomizer_from_obj
Unexecuted instantiation: file.c:php_random_randomizer_from_obj
Unexecuted instantiation: filters.c:php_random_randomizer_from_obj
Unexecuted instantiation: ftp_fopen_wrapper.c:php_random_randomizer_from_obj
Unexecuted instantiation: head.c:php_random_randomizer_from_obj
Unexecuted instantiation: html.c:php_random_randomizer_from_obj
Unexecuted instantiation: http_fopen_wrapper.c:php_random_randomizer_from_obj
Unexecuted instantiation: link.c:php_random_randomizer_from_obj
Unexecuted instantiation: mail.c:php_random_randomizer_from_obj
Unexecuted instantiation: php_fopen_wrapper.c:php_random_randomizer_from_obj
Unexecuted instantiation: streamsfuncs.c:php_random_randomizer_from_obj
Unexecuted instantiation: string.c:php_random_randomizer_from_obj
Unexecuted instantiation: strnatcmp.c:php_random_randomizer_from_obj
Unexecuted instantiation: uniqid.c:php_random_randomizer_from_obj
Unexecuted instantiation: url_scanner_ex.c:php_random_randomizer_from_obj
Unexecuted instantiation: var.c:php_random_randomizer_from_obj
Unexecuted instantiation: fopen_wrappers.c:php_random_randomizer_from_obj
Unexecuted instantiation: main.c:php_random_randomizer_from_obj
Unexecuted instantiation: php_open_temporary_file.c:php_random_randomizer_from_obj
Unexecuted instantiation: php_variables.c:php_random_randomizer_from_obj
Unexecuted instantiation: streams.c:php_random_randomizer_from_obj
Unexecuted instantiation: internal_functions_cli.c:php_random_randomizer_from_obj
147
148
24
# define Z_RANDOM_ENGINE_P(zval) php_random_engine_from_obj(Z_OBJ_P(zval))
149
150
6
# define Z_RANDOM_RANDOMIZER_P(zval) php_random_randomizer_from_obj(Z_OBJ_P(zval));
151
152
PHPAPI void *php_random_status_alloc(const php_random_algo *algo, const bool persistent);
153
PHPAPI void *php_random_status_copy(const php_random_algo *algo, void *old_status, void *new_status);
154
PHPAPI void php_random_status_free(void *status, const bool persistent);
155
PHPAPI php_random_engine *php_random_engine_common_init(zend_class_entry *ce, const php_random_algo *algo);
156
PHPAPI void php_random_engine_common_free_object(zend_object *object);
157
PHPAPI zend_object *php_random_engine_common_clone_object(zend_object *object);
158
PHPAPI uint32_t php_random_range32(php_random_algo_with_state engine, uint32_t umax);
159
PHPAPI uint64_t php_random_range64(php_random_algo_with_state engine, uint64_t umax);
160
PHPAPI zend_long php_random_range(php_random_algo_with_state engine, zend_long min, zend_long max);
161
PHPAPI const php_random_algo *php_random_default_algo(void);
162
PHPAPI void *php_random_default_status(void);
163
164
static inline php_random_algo_with_state php_random_default_engine(void)
165
15
{
166
15
  php_random_algo_with_state raws;
167
15
  raws.algo = php_random_default_algo();
168
15
  raws.state = php_random_default_status();
169
15
  return raws;
170
15
}
Unexecuted instantiation: exif.c:php_random_default_engine
Unexecuted instantiation: csprng.c:php_random_default_engine
Unexecuted instantiation: engine_mt19937.c:php_random_default_engine
Unexecuted instantiation: engine_pcgoneseq128xslrr64.c:php_random_default_engine
Unexecuted instantiation: engine_secure.c:php_random_default_engine
Unexecuted instantiation: engine_user.c:php_random_default_engine
Unexecuted instantiation: engine_xoshiro256starstar.c:php_random_default_engine
Unexecuted instantiation: gammasection.c:php_random_default_engine
Unexecuted instantiation: random.c:php_random_default_engine
Unexecuted instantiation: randomizer.c:php_random_default_engine
Unexecuted instantiation: zend_utils.c:php_random_default_engine
Unexecuted instantiation: spl_directory.c:php_random_default_engine
Unexecuted instantiation: spl_observer.c:php_random_default_engine
Unexecuted instantiation: array.c:php_random_default_engine
Unexecuted instantiation: basic_functions.c:php_random_default_engine
Unexecuted instantiation: exec.c:php_random_default_engine
Unexecuted instantiation: file.c:php_random_default_engine
Unexecuted instantiation: filters.c:php_random_default_engine
Unexecuted instantiation: ftp_fopen_wrapper.c:php_random_default_engine
Unexecuted instantiation: head.c:php_random_default_engine
Unexecuted instantiation: html.c:php_random_default_engine
Unexecuted instantiation: http_fopen_wrapper.c:php_random_default_engine
Unexecuted instantiation: link.c:php_random_default_engine
Unexecuted instantiation: mail.c:php_random_default_engine
Unexecuted instantiation: php_fopen_wrapper.c:php_random_default_engine
Unexecuted instantiation: streamsfuncs.c:php_random_default_engine
string.c:php_random_default_engine
Line
Count
Source
165
15
{
166
15
  php_random_algo_with_state raws;
167
15
  raws.algo = php_random_default_algo();
168
15
  raws.state = php_random_default_status();
169
15
  return raws;
170
15
}
Unexecuted instantiation: strnatcmp.c:php_random_default_engine
Unexecuted instantiation: uniqid.c:php_random_default_engine
Unexecuted instantiation: url_scanner_ex.c:php_random_default_engine
Unexecuted instantiation: var.c:php_random_default_engine
Unexecuted instantiation: fopen_wrappers.c:php_random_default_engine
Unexecuted instantiation: main.c:php_random_default_engine
Unexecuted instantiation: php_open_temporary_file.c:php_random_default_engine
Unexecuted instantiation: php_variables.c:php_random_default_engine
Unexecuted instantiation: streams.c:php_random_default_engine
Unexecuted instantiation: internal_functions_cli.c:php_random_default_engine
171
172
PHPAPI zend_string *php_random_bin2hex_le(const void *ptr, const size_t len);
173
PHPAPI bool php_random_hex2bin_le(zend_string *hexstr, void *dest);
174
175
PHPAPI void php_random_mt19937_seed32(php_random_status_state_mt19937 *state, uint32_t seed);
176
PHPAPI void php_random_mt19937_seed_default(php_random_status_state_mt19937 *state);
177
178
PHPAPI void php_random_pcgoneseq128xslrr64_seed128(php_random_status_state_pcgoneseq128xslrr64 *s, php_random_uint128_t seed);
179
PHPAPI void php_random_pcgoneseq128xslrr64_advance(php_random_status_state_pcgoneseq128xslrr64 *state, uint64_t advance);
180
181
PHPAPI void php_random_xoshiro256starstar_seed64(php_random_status_state_xoshiro256starstar *state, uint64_t seed);
182
PHPAPI void php_random_xoshiro256starstar_seed256(php_random_status_state_xoshiro256starstar *state, uint64_t s0, uint64_t s1, uint64_t s2, uint64_t s3);
183
PHPAPI void php_random_xoshiro256starstar_jump(php_random_status_state_xoshiro256starstar *state);
184
PHPAPI void php_random_xoshiro256starstar_jump_long(php_random_status_state_xoshiro256starstar *state);
185
186
PHPAPI double php_random_gammasection_closed_open(php_random_algo_with_state engine, double min, double max);
187
PHPAPI double php_random_gammasection_closed_closed(php_random_algo_with_state engine, double min, double max);
188
PHPAPI double php_random_gammasection_open_closed(php_random_algo_with_state engine, double min, double max);
189
PHPAPI double php_random_gammasection_open_open(php_random_algo_with_state engine, double min, double max);
190
191
extern zend_module_entry random_module_entry;
192
# define phpext_random_ptr &random_module_entry
193
194
PHP_MINIT_FUNCTION(random);
195
PHP_MSHUTDOWN_FUNCTION(random);
196
PHP_RINIT_FUNCTION(random);
197
198
ZEND_BEGIN_MODULE_GLOBALS(random)
199
  bool combined_lcg_seeded;
200
  bool mt19937_seeded;
201
  php_random_fallback_seed_state fallback_seed_state;
202
  int32_t combined_lcg[2];
203
  php_random_status_state_mt19937 mt19937;
204
ZEND_END_MODULE_GLOBALS(random)
205
206
PHPAPI ZEND_EXTERN_MODULE_GLOBALS(random)
207
208
383k
# define RANDOM_G(v)  ZEND_MODULE_GLOBALS_ACCESSOR(random, v)
209
210
#endif  /* PHP_RANDOM_H */