Coverage Report

Created: 2025-06-13 06:43

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