if (ffi->symbols) {
sym = zend_hash_find_ptr(ffi->symbols, var_name);
- if (sym && sym->kind != ZEND_FFI_SYM_VAR && sym->kind != ZEND_FFI_SYM_CONST) {
+ if (sym && sym->kind != ZEND_FFI_SYM_VAR && sym->kind != ZEND_FFI_SYM_CONST && sym->kind != ZEND_FFI_SYM_FUNC) {
sym = NULL;
}
}
if (sym->kind == ZEND_FFI_SYM_VAR) {
zend_ffi_cdata_to_zval(NULL, sym->addr, ZEND_FFI_TYPE(sym->type), read_type, rv, (zend_ffi_flags)sym->is_const, 0);
+ } else if (sym->kind == ZEND_FFI_SYM_FUNC) {
+ zend_ffi_cdata *cdata;
+ zend_ffi_type *new_type = emalloc(sizeof(zend_ffi_type));
+
+ new_type->kind = ZEND_FFI_TYPE_POINTER;
+ new_type->attr = 0;
+ new_type->size = sizeof(void*);
+ new_type->align = _Alignof(void*);
+ new_type->pointer.type = ZEND_FFI_TYPE(sym->type);
+
+ cdata = emalloc(sizeof(zend_ffi_cdata));
+ zend_ffi_object_init(&cdata->std, zend_ffi_cdata_ce);
+ cdata->std.handlers = &zend_ffi_cdata_handlers;
+ cdata->type = ZEND_FFI_TYPE_MAKE_OWNED(new_type);
+ cdata->flags = ZEND_FFI_FLAG_CONST;
+ cdata->ptr_holder = sym->addr;
+ cdata->ptr = &cdata->ptr_holder;
+ ZVAL_OBJ(rv, &cdata->std);
} else {
ZVAL_LONG(rv, sym->value);
}
--- /dev/null
+--TEST--
+FFI 101: PHP symbols (function address)
+--SKIPIF--
+<?php require_once('skipif.inc'); ?>
+<?php
+try {
+ FFI::cdef("extern void *zend_printf;");
+} catch (Throwable $e) {
+ die('skip PHP symbols not available');
+}
+?>
+--INI--
+ffi.enable=1
+--FILE--
+<?php
+// Check if target supports "fastcall" calling convention
+try {
+ FFI::cdef("extern size_t __attribute__((fastcall)) (*zend_printf)(const char *format);");
+ $fastcall = "__attribute__((fastcall)) ";
+} catch (Throwable $e) {
+ $fastcall = "";
+}
+$zend = FFI::cdef("
+ const char *get_zend_version(void);
+ //char *get_zend_version(void);
+ extern size_t (*zend_printf)(const char *format, ...);
+
+ unsigned long $fastcall zend_hash_func(const char *str, size_t len);
+
+ void $fastcall zend_str_tolower(char *str, size_t length);
+
+");
+$f = $zend->get_zend_version;
+var_dump(trim(explode("\n",$f())[0]));
+//var_dump(trim(FFI::string($zend->get_zend_version())));
+var_dump($zend->zend_printf);
+var_dump(($zend->zend_printf)("Hello %s!\n", "World"));
+
+$f = $zend->zend_hash_func;
+var_dump($f("file", strlen("file")));
+
+$str = $zend->new("char[16]");
+FFI::memcpy($str, "Hello World!", strlen("Hello World!"));
+$f = $zend->zend_str_tolower;
+$f($str, strlen("Hello World!"));
+var_dump(FFI::string($str));
+
+?>
+--EXPECTF--
+string(%d) "Zend Engine %s"
+object(FFI\CData:uint%d_t(*)())#%d (1) {
+ [0]=>
+ object(FFI\CData:uint%d_t())#%d (0) {
+ }
+}
+Hello World!
+int(13)
+int(%i)
+string(12) "hello world!"