php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #78474 FFI::scope does not work in CLI SAPI
Submitted: 2019-08-29 08:00 UTC Modified: 2019-08-30 09:28 UTC
From: ojrask at gmail dot com Assigned: cmb (profile)
Status: Not a bug Package: Unknown/Other Function
PHP Version: 7.4.0beta4 OS: Ubuntu 18.04
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: ojrask at gmail dot com
New email:
PHP Version: OS:

 

 [2019-08-29 08:00 UTC] ojrask at gmail dot com
Description:
------------
When I attempt to use `\FFI::scope()` in CLI scripts (no preloading and all that jazz), I get an error saying

    PHP Fatal error:  Uncaught FFI\Exception: Failed loading scope 'FOO' in ...

The stack trace reveals no reasons. I would presume scopes are OK to be used in CLI SAPI, or then there is some documentation missing as to why it is not possible.

Test script:
---------------
<?php declare(strict_types = 1);

$header = <<<'HEA'
    #define FFI_SCOPE "FOO"
    #define FFI_LIB "libc.so.6"

    void printf(char *const str, ...);
    'HEA';

file_put_contents(__DIR__ . '/header.h', $header);

\FFI::load(__DIR__ . '/header.h');

$ffi = \FFI::scope('FOO');

$ffi->printf('Hello %s!', 'world');

Expected result:
----------------
Output should be

    Hello world!

Actual result:
--------------
Output is

    PHP Fatal error:  Uncaught FFI\Exception: Failed loading scope 'FOO' in .../hello-world.php:14
    Stack trace:
    #0 .../hello-world.php(14): FFI::scope()
    #1 {main}
      thrown in .../hello-world.php on line 14

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2019-08-29 08:15 UTC] cmb@php.net
-Status: Open +Status: Not a bug -Assigned To: +Assigned To: cmb
 [2019-08-29 08:15 UTC] cmb@php.net
From the docs[1]:

| Instantiates an FFI object with C declarations parsed during
| preloading.
 [2019-08-29 21:29 UTC] ojrask at gmail dot com
Okay, is there a specific reason why scopes cannot be used outside the preloading state?
 [2019-08-30 09:28 UTC] cmb@php.net
> Okay, is there a specific reason why scopes cannot be used
> outside the preloading state?

See <https://www.php.net/manual/en/class.ffi.php>.  TL;DR: scopes
are meant for performance optimization in combination with
preloading, and don't really make sense without it.
 
PHP Copyright © 2001-2022 The PHP Group
All rights reserved.
Last updated: Mon May 23 08:05:45 2022 UTC