Coverage Report

Created: 2026-07-25 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
21
ZEND_API char zend_system_id[32];
22
23
static PHP_MD5_CTX context;
24
static int finalized = 0;
25
26
ZEND_API zend_result zend_add_system_entropy(const char *module_name, const char *hook_name, const void *data, size_t size)
27
16
{
28
16
  if (finalized == 0) {
29
16
    PHP_MD5Update(&context, module_name, strlen(module_name));
30
16
    PHP_MD5Update(&context, hook_name, strlen(hook_name));
31
16
    if (size) {
32
16
      PHP_MD5Update(&context, data, size);
33
16
    }
34
16
    return SUCCESS;
35
16
  }
36
0
  return FAILURE;
37
16
}
38
39
32
#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)
40
41
void zend_startup_system_id(void)
42
16
{
43
16
  PHP_MD5Init(&context);
44
16
  PHP_MD5Update(&context, PHP_VERSION, sizeof(PHP_VERSION)-1);
45
16
  PHP_MD5Update(&context, ZEND_EXTENSION_BUILD_ID, sizeof(ZEND_EXTENSION_BUILD_ID)-1);
46
16
  PHP_MD5Update(&context, ZEND_BIN_ID, sizeof(ZEND_BIN_ID)-1);
47
16
  if (strstr(PHP_VERSION, "-dev") != 0) {
48
    /* Development versions may be changed from build to build */
49
16
    PHP_MD5Update(&context, __DATE__, sizeof(__DATE__)-1);
50
16
    PHP_MD5Update(&context, __TIME__, sizeof(__TIME__)-1);
51
16
  }
52
16
  zend_system_id[0] = '\0';
53
16
}
54
55
0
#define ZEND_HOOK_AST_PROCESS        (1 << 0)
56
0
#define ZEND_HOOK_COMPILE_FILE       (1 << 1)
57
0
#define ZEND_HOOK_EXECUTE_EX         (1 << 2)
58
0
#define ZEND_HOOK_EXECUTE_INTERNAL   (1 << 3)
59
0
#define ZEND_HOOK_INTERRUPT_FUNCTION (1 << 4)
60
61
void zend_finalize_system_id(void)
62
16
{
63
16
  unsigned char digest[16];
64
16
  uint8_t hooks = 0;
65
66
16
  if (zend_ast_process) {
67
0
    hooks |= ZEND_HOOK_AST_PROCESS;
68
0
  }
69
16
  if (zend_compile_file != compile_file) {
70
0
    hooks |= ZEND_HOOK_COMPILE_FILE;
71
0
  }
72
16
  if (zend_execute_ex != execute_ex) {
73
0
    hooks |= ZEND_HOOK_EXECUTE_EX;
74
0
  }
75
16
  if (zend_execute_internal) {
76
0
    hooks |= ZEND_HOOK_EXECUTE_INTERNAL;
77
0
  }
78
16
  if (zend_interrupt_function) {
79
0
    hooks |= ZEND_HOOK_INTERRUPT_FUNCTION;
80
0
  }
81
16
  PHP_MD5Update(&context, &hooks, sizeof hooks);
82
83
4.11k
  for (int16_t i = 0; i < 256; i++) {
84
4.09k
    if (zend_get_user_opcode_handler((uint8_t) i) != NULL) {
85
0
      PHP_MD5Update(&context, &i, sizeof i);
86
0
    }
87
4.09k
  }
88
89
16
  PHP_MD5Final(digest, &context);
90
16
  zend_bin2hex(zend_system_id, digest, sizeof digest);
91
16
  finalized = 1;
92
16
}