Skip to content

embed development problem:Segmentation fault #17502

@KingBes

Description

@KingBes

Description

class.h

#include <sapi/embed/php_embed.h>

static zend_object_handlers class_object_handlers;

typedef struct my_php_class_t
{
    zend_object std;
} my_php_class_t;

PHP_METHOD(MyPhpClass, __construct)
{
    RETURN_NULL();
}

ZEND_BEGIN_ARG_INFO_EX(arginfo__construct, ZEND_SEND_BY_VAL, ZEND_RETURN_VALUE, 0)
ZEND_END_ARG_INFO()

PHP_METHOD(MyPhpClass, myMethod)
{
    long num; 

    ZEND_PARSE_PARAMETERS_START(1, 1)
    Z_PARAM_LONG(num)
    ZEND_PARSE_PARAMETERS_END();

    RETURN_LONG(num * 2);
}

ZEND_BEGIN_ARG_INFO_EX(arginfo_myMethod, ZEND_SEND_BY_VAL, ZEND_RETURN_VALUE, 1)
ZEND_ARG_TYPE_INFO(0, num, IS_LONG, 0)
ZEND_END_ARG_INFO()

static const zend_function_entry my_php_class[] = {
    PHP_ME(MyPhpClass, __construct, arginfo__construct, ZEND_ACC_PUBLIC | ZEND_ACC_CTOR)
        PHP_ME(MyPhpClass, myMethod, arginfo_myMethod, ZEND_ACC_PUBLIC)
            PHP_FE_END};

zend_object *class_new(zend_class_entry *ce)
{
    my_php_class_t *class = zend_object_alloc(sizeof(my_php_class_t), ce);

    zend_object_std_init(&class->std, ce);
    class->std.handlers = &class_object_handlers;
    return &class->std;
}

void register_class()
{
    zend_class_entry ce;
    INIT_CLASS_ENTRY(ce, "MyPhpClass", my_php_class);
    ce.create_object = class_new;
    // ce.default_object_handlers = &class_object_handlers;
    memcpy(&class_object_handlers, &std_object_handlers, sizeof(zend_object_handlers));
    class_object_handlers.offset = XtOffsetOf(my_php_class_t, std);
    zend_register_internal_class(&ce);
    printf("register_class ok\n");
}

main.c

#include "class.h"

int main(int argc, char **argv)
{
    PHP_EMBED_START_BLOCK(argc, argv)

    register_class();

    zend_file_handle file_handle;
    zend_stream_init_filename(&file_handle, "index.php");
    if (php_execute_script(&file_handle) == FAILURE)
    {
        php_printf("Failed to execute PHP script.\n");
    }
    PHP_EMBED_END_BLOCK()
    return 0;
}

index.php

<?php

$MyPhpClass = new MyPhpClass();

var_dump($MyPhpClass->myMethod(123));

result

Segmentation fault

PHP Version

8.3.15

Operating System

Linux version 5.15.146.1-microsoft-standard-WSL2

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions