Coverage Report

Created: 2026-06-02 06:39

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/php-src/Zend/zend_system_id.c
Line
Count
Source
1
/*
2
   +----------------------------------------------------------------------+
3
   | Copyright © The PHP Group and Contributors.                          |
4
   +----------------------------------------------------------------------+
5
   | This source file is subject to the Modified BSD License that is      |
6
   | bundled with this package in the file LICENSE, and is available      |
7
   | through the World Wide Web at <https://www.php.net/license/>.        |
8
   |                                                                      |
9
   | SPDX-License-Identifier: BSD-3-Clause                                |
10
   +----------------------------------------------------------------------+
11
   | Authors: Sammy Kaye Powers <sammyk@php.net>                          |
12
   |          Dmitry Stogov <dmitry@php.net>                              |
13
   +----------------------------------------------------------------------+
14
 */
15
16
#include "php.h"
17
#include "zend_system_id.h"
18
#include "zend_extensions.h"
19
#include "ext/standard/md5.h"
20
#include "ext/hash/php_hash.h"
21
22
ZEND_API char zend_system_id[32];
23
24
static PHP_MD5_CTX context;
25
static int finalized = 0;
26
27
ZEND_API zend_result zend_add_system_entropy(const char *module_name, const char *hook_name, const void *data, size_t size)
28
2
{
29
2
  if (finalized == 0) {
30
2
    PHP_MD5Update(&context, module_name, strlen(module_name));
31
2
    PHP_MD5Update(&context, hook_name, strlen(hook_name));
32
2
    if (size) {
33
2
      PHP_MD5Update(&context, data, size);
34
2
    }
35
2
    return SUCCESS;
36
2
  }
37
0
  return FAILURE;
38
2
}
39
40
4
#define ZEND_BIN_ID "BIN_" ZEND_TOSTR(SIZEOF_INT) ZEND_TOSTR(SIZEOF_LONG) ZEND_TOSTR(SIZEOF_SIZE_T) ZEND_TOSTR(SIZEOF_ZEND_LONG) ZEND_TOSTR(ZEND_MM_ALIGNMENT)
41
42
void zend_startup_system_id(void)
43
2
{
44
2
  PHP_MD5Init(&context);
45
2
  PHP_MD5Update(&context, PHP_VERSION, sizeof(PHP_VERSION)-1);
46
2
  PHP_MD5Update(&context, ZEND_EXTENSION_BUILD_ID, sizeof(ZEND_EXTENSION_BUILD_ID)-1);
47
2
  PHP_MD5Update(&context, ZEND_BIN_ID, sizeof(ZEND_BIN_ID)-1);
48
2
  if (strstr(PHP_VERSION, "-dev") != 0) {
49
    /* Development versions may be changed from build to build */
50
2
    PHP_MD5Update(&context, __DATE__, sizeof(__DATE__)-1);
51
2
    PHP_MD5Update(&context, __TIME__, sizeof(__TIME__)-1);
52
2
  }
53
2
  zend_system_id[0] = '\0';
54
2
}
55
56
0
#define ZEND_HOOK_AST_PROCESS        (1 << 0)
57
0
#define ZEND_HOOK_COMPILE_FILE       (1 << 1)
58
0
#define ZEND_HOOK_EXECUTE_EX         (1 << 2)
59
0
#define ZEND_HOOK_EXECUTE_INTERNAL   (1 << 3)
60
0
#define ZEND_HOOK_INTERRUPT_FUNCTION (1 << 4)
61
62
void zend_finalize_system_id(void)
63
2
{
64
2
  unsigned char digest[16];
65
2
  uint8_t hooks = 0;
66
67
2
  if (zend_ast_process) {
68
0
    hooks |= ZEND_HOOK_AST_PROCESS;
69
0
  }
70
2
  if (zend_compile_file != compile_file) {
71
0
    hooks |= ZEND_HOOK_COMPILE_FILE;
72
0
  }
73
2
  if (zend_execute_ex != execute_ex) {
74
0
    hooks |= ZEND_HOOK_EXECUTE_EX;
75
0
  }
76
2
  if (zend_execute_internal) {
77
0
    hooks |= ZEND_HOOK_EXECUTE_INTERNAL;
78
0
  }
79
2
  if (zend_interrupt_function) {
80
0
    hooks |= ZEND_HOOK_INTERRUPT_FUNCTION;
81
0
  }
82
2
  PHP_MD5Update(&context, &hooks, sizeof hooks);
83
84
514
  for (int16_t i = 0; i < 256; i++) {
85
512
    if (zend_get_user_opcode_handler((uint8_t) i) != NULL) {
86
0
      PHP_MD5Update(&context, &i, sizeof i);
87
0
    }
88
512
  }
89
90
2
  PHP_MD5Final(digest, &context);
91
2
  php_hash_bin2hex(zend_system_id, digest, sizeof digest);
92
2
  finalized = 1;
93
2
}