Skip to content

Commit 11160fb

Browse files
authored
fix: segmentation fault when registering multiple extensions (#2112)
This PR fixes a segmentation fault when using frankenphp.RegisterExtension with more than one extension. The issue was a type mismatch between Go and C: Go passes a slice of pointers, but the C code was treating it as a contiguous array of structs. This caused invalid memory access when iterating past the first element. I created a minimal reproduction repo here: [https://github.com/y-l-g/frankenphp-extensions-segfault-repro](https://www.google.com/url?sa=E&q=https%3A%2F%2Fgithub.com%2Fy-l-g%2Ffrankenphp-extensions-segfault-repro)
1 parent e0f01d1 commit 11160fb

File tree

3 files changed

+5
-5
lines changed

3 files changed

+5
-5
lines changed

ext.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ func registerExtensions() {
2323
}
2424

2525
registerOnce.Do(func() {
26-
C.register_extensions(extensions[0], C.int(len(extensions)))
26+
C.register_extensions((**C.zend_module_entry)(unsafe.Pointer(&extensions[0])), C.int(len(extensions)))
2727
extensions = nil
2828
})
2929
}

frankenphp.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1262,7 +1262,7 @@ int frankenphp_reset_opcache(void) {
12621262

12631263
int frankenphp_get_current_memory_limit() { return PG(memory_limit); }
12641264

1265-
static zend_module_entry *modules = NULL;
1265+
static zend_module_entry **modules = NULL;
12661266
static int modules_len = 0;
12671267
static int (*original_php_register_internal_extensions_func)(void) = NULL;
12681268

@@ -1273,7 +1273,7 @@ PHPAPI int register_internal_extensions(void) {
12731273
}
12741274

12751275
for (int i = 0; i < modules_len; i++) {
1276-
if (zend_register_internal_module(&modules[i]) == NULL) {
1276+
if (zend_register_internal_module(modules[i]) == NULL) {
12771277
return FAILURE;
12781278
}
12791279
}
@@ -1284,7 +1284,7 @@ PHPAPI int register_internal_extensions(void) {
12841284
return SUCCESS;
12851285
}
12861286

1287-
void register_extensions(zend_module_entry *m, int len) {
1287+
void register_extensions(zend_module_entry **m, int len) {
12881288
modules = m;
12891289
modules_len = len;
12901290

frankenphp.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,6 @@ void frankenphp_register_bulk(
7676
ht_key_value_pair auth_type, ht_key_value_pair remote_ident,
7777
ht_key_value_pair request_uri, ht_key_value_pair ssl_cipher);
7878

79-
void register_extensions(zend_module_entry *m, int len);
79+
void register_extensions(zend_module_entry **m, int len);
8080

8181
#endif

0 commit comments

Comments
 (0)