Skip to content

Commit a8efea5

Browse files
Add support for ionCube-style loading and enhance module lifecycle management in opdump
1 parent ca1f545 commit a8efea5

2 files changed

Lines changed: 87 additions & 4 deletions

File tree

‎README.md‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,31 @@ Then run a smoke test:
174174
PHP_BIN=php8.4 php/tests/run-rung1.sh
175175
```
176176

177+
For ionCube-style loading and a `php -v` banner entry, load the same shared
178+
object as a Zend extension:
179+
180+
```bash
181+
php8.4 -n -d zend_extension=/var/www/bytecode/php/src/modules/opdump.so -v
182+
```
183+
184+
Expected shape:
185+
186+
```text
187+
Zend Engine v4.4.23, Copyright (c) Zend Technologies
188+
with Bytecode PHP Loader v0.0.1-phase0, Copyright (c) 2026 MegaMind Technologies LTD, by MegaMind Technologies LTD
189+
```
190+
191+
Runtime bytecode loading works through the Zend-extension path too:
192+
193+
```bash
194+
BYTECODE_KEY="$KEY" \
195+
OPDUMP_MODE=load-tree \
196+
OPDUMP_MAP="$OUT/bytecode.map" \
197+
php8.4 \
198+
-d zend_extension=/var/www/bytecode/php/src/modules/opdump.so \
199+
-f /path/to/app/public/index.php
200+
```
201+
177202
On macOS:
178203

179204
```bash

‎php/src/opdump.c‎

Lines changed: 62 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@
1818

1919
#include "php.h"
2020
#include "zend_compile.h"
21+
#ifndef ZEND_EXT_API
22+
# define ZEND_EXT_API ZEND_DLEXPORT
23+
#endif
24+
#include "zend_extensions.h"
2125
#include "zend_vm.h"
2226
#include "zend_string.h"
2327
#include <openssl/evp.h>
@@ -90,6 +94,7 @@ static void opdump_unlock_mem(void *p, size_t len)
9094
}
9195

9296
static zend_op_array *(*opdump_orig_compile_file)(zend_file_handle *file_handle, int type);
97+
static bool opdump_hooks_started = false;
9398

9499
static void opdump_init_run_time_cache(zend_op_array *op_array)
95100
{
@@ -1887,10 +1892,11 @@ static zend_op_array *opdump_load_tree_compile_file(zend_file_handle *file_handl
18871892
return opdump_orig_compile_file(file_handle, type);
18881893
}
18891894

1890-
/* ---- module lifecycle ---- */
1891-
1892-
PHP_MINIT_FUNCTION(opdump)
1895+
static int opdump_startup_hooks(void)
18931896
{
1897+
if (opdump_hooks_started) {
1898+
return SUCCESS;
1899+
}
18941900
opdump_disable_core_dumps();
18951901
opdump_orig_compile_file = zend_compile_file;
18961902
const char *mode = getenv("OPDUMP_MODE");
@@ -1909,20 +1915,43 @@ PHP_MINIT_FUNCTION(opdump)
19091915
zend_compile_file = opdump_load_tree_compile_file;
19101916
}
19111917
}
1918+
opdump_hooks_started = true;
19121919
return SUCCESS;
19131920
}
19141921

1922+
static void opdump_shutdown_hooks(void)
1923+
{
1924+
if (opdump_hooks_started && opdump_orig_compile_file) {
1925+
zend_compile_file = opdump_orig_compile_file;
1926+
opdump_orig_compile_file = NULL;
1927+
opdump_hooks_started = false;
1928+
}
1929+
}
1930+
19151931
/* Cleanses the cached license DEK (if license mode ever unwrapped one) once
19161932
* per process, rather than immediately after each derivation -- it has to
19171933
* survive for the whole request/process to avoid re-doing an RSA-OAEP
19181934
* unwrap per file in load-tree mode (see opdump_license_resolve_ikm). */
1919-
PHP_MSHUTDOWN_FUNCTION(opdump)
1935+
static void opdump_clean_license_cache(void)
19201936
{
19211937
if (opdump_license_ikm_ready) {
19221938
opdump_unlock_mem(opdump_license_ikm_cache, sizeof(opdump_license_ikm_cache));
19231939
OPENSSL_cleanse(opdump_license_ikm_cache, sizeof(opdump_license_ikm_cache));
19241940
opdump_license_ikm_ready = false;
19251941
}
1942+
}
1943+
1944+
/* ---- module lifecycle ---- */
1945+
1946+
PHP_MINIT_FUNCTION(opdump)
1947+
{
1948+
return opdump_startup_hooks();
1949+
}
1950+
1951+
PHP_MSHUTDOWN_FUNCTION(opdump)
1952+
{
1953+
opdump_shutdown_hooks();
1954+
opdump_clean_license_cache();
19261955
return SUCCESS;
19271956
}
19281957

@@ -1940,3 +1969,32 @@ zend_module_entry opdump_module_entry = {
19401969
#ifdef COMPILE_DL_OPDUMP
19411970
ZEND_GET_MODULE(opdump)
19421971
#endif
1972+
1973+
/* Zend-extension entry point. Loading the same .so with
1974+
* `zend_extension=opdump.so` makes PHP list it beside OPcache/ionCube in
1975+
* `php -v`, while reusing the same compile-file hook implementation above. */
1976+
static int opdump_zend_startup(zend_extension *extension)
1977+
{
1978+
(void) extension;
1979+
return opdump_startup_hooks();
1980+
}
1981+
1982+
static void opdump_zend_shutdown(zend_extension *extension)
1983+
{
1984+
(void) extension;
1985+
opdump_shutdown_hooks();
1986+
opdump_clean_license_cache();
1987+
}
1988+
1989+
ZEND_EXTENSION();
1990+
1991+
ZEND_EXT_API zend_extension zend_extension_entry = {
1992+
.name = "Bytecode PHP Loader",
1993+
.version = "0.0.1-phase0",
1994+
.author = "MegaMind Technologies LTD",
1995+
.URL = "https://github.com/Mastermegamind/bytecode",
1996+
.copyright = "Copyright (c) 2026 MegaMind Technologies LTD",
1997+
.startup = opdump_zend_startup,
1998+
.shutdown = opdump_zend_shutdown,
1999+
.resource_number = -1
2000+
};

0 commit comments

Comments
 (0)