A PHP VM implementation in PHP

Related tags

Code Analysis PHPPHP
Overview

PHPPHP

YO DAWG

A PHP VM implementation written in PHP.

This is a basic VM implemented in PHP using the AST generating parser developed by @nikic

To see what's supported so far, check out the opcodes.

Right now, functions (definitions and calls) are supported, if statements (with basic boolean operations), if statements, as are variables and some basic variable operations...

To see the current state of the implementation versus the Core Language Test Suite, check out the test results file.

Installation

To get the dependencies you need to use composer:

curl -s https://getcomposer.org/installer | php
php composer.phar install

Grab a cup of coffee while it runs, as it will fetch the entire PHP source code in the process which can take a while (~10 minutes).

Usage

Linux/OSX

Use the php.sh shell script to invoke php.php. This requires that php be in your system path.

./php.sh -r "var_dump('foo');"

Windows

Use the php.bat shell script to invoke php.php. This requires that php.exe be in your system path.

php.bat -r "var_dump('foo');"

Manual

You can run the implementation from the command line using the php.php file directly.

php php.php -r "var_dump('foo');"

Or with a file:

php php.php ../test.php

It only supports relative includes off the base file now (no include path parsing yet)...

Testing

run-tests

To run the test suite (which mostly fails right now), install composer using the --dev flag (to install the test suite along side php's source code). Then use the run-tests.(sh|bat) file to execute them.

Linux/OSX

./run-tests.sh

Windows

Before running tests on Windows, some lines must be removed or commented out from runtests.php (if installed via composer it will be located in vendor/php/php-src/runtests.php). Starting from line 185:

if (function_exists('is_executable') && !is_executable($php)) {
    error("invalid PHP executable specified by TEST_PHP_EXECUTABLE  = $php");
}

To run test suite simply type

run-tests.bat

Manual

If you choose not to install php's source code from composer, but have it elsewhere in your system path, you can use it directly.

php path/to/runtests.php -p path/to/php.sh path/to/tests

TODO:

Major things left to do:

  1. Implement references properly
  2. Implement Error Handling
  3. Implement classes and objects
  4. Implement Output Buffering.
  5. Refactor output control to use SAPI modules
  6. Implement true array hash tables (as opposed to the current reliance on the underlying hash table)
  7. Implement Error Handling
  8. Implement Exception Handling
  9. Implement parameter parsing for core functions

For The Love Of God, Why?

There are a number of reasons why I did this...

  1. It was something that I always wanted to do. For no particular reason other than I wanted to do it. I knew it was possible, but possible and doing it are two very different things.
  2. It was far easier than I thought. The time to the initial commit (basic working VM) was only about 6 hours of work. So it's not like I spent a year building it...
  3. It could be a useful education tool. For me learning the intricacies of the Zend VM better (I know it fairly well, but knowing and building give two different amounts of knowledge). But also for teaching others how the VM works. By giving a PHP implementation reference, hopefully more people can understand how the C implementation works (they both operate off the same generic implementation at this point).
  4. It can enable certain interesting things. For example, we could hypothetically build an Opcode optimizer in PHP which parses the generated opcodes and optimizes things (removing redundant opcodes, statically compiling static expressions, etc). Then, we could build a PECL extension that would render those optimized opcodes directly into APC cache (or some other opcode cache mechanism).
  5. It can be used to quickly mock up future functionality changes. Consider that it's easier to alter a PHP VM simply because you don't need to worry about memory management at all. So whipping up a POC for a significant feature should be a lot easier in PHP than C (at least for many non-full-time C developers).
  6. It can be used to actually debug PHP code without working knowledge of GDB (and the underlying C structures). I wouldn't recommend this, as the chances of us getting it working 100% the same as the C implementation are practically 0, but it's a concept.
  7. It could wind up becoming a full implementation (like PYPY). If we can compile the implementation using HipHop, and do some other lower-level tricks, there's a chance we could achieve performance somewhere near the C implementation. I doubt it, but it's possible. Especially if we add a JIT component (or a way of rendering out to machine code certain opcodes)...
  8. Why not?

About The Authors

Anthony Ferrara - @ircmaxell blog.ircmaxell.com

Nikita Popov - @nikita_ppv nikic.github.com

Comments
  • Apache and Opcode caches integration

    Apache and Opcode caches integration

    This is more of a question but there is a "question" tag in Github issues so:

    If it was possible to integrate PHPPHP to Apache (or any server) like PHP, and if it was possible for PHPPHP to generate opcodes like PHP opcodes, then PHPPHP could become a serious alternative to PHP right?

    Because without even having to compile PHPPHP to C (with HipHop for example), you could have equivalent performances if you cache the opcodes generated.

    Now, I've read the README and looked at some parts of the code, but I'm not sure if this project generates any opcode (or if it executes directly the parsed code), and if yes then is the opcode compatible with the PHP opcodes?

    And I confess I don't know the details of how Apache communicates with PHP (I know it can be a "module" of using fastCGI), but I guess this could be possible with PHPPHP.

    That would be extremely interesting to have PHP developed in PHP, because following the latest discussions on PHP internals, there aren't enough PHP devs, and it is mostly because it is developed in C.

    With PHPPHP, PHP users can participate to its development.

    opened by mnapoli 6
  • switch bug

    switch bug

    <?php
    $a=10;
    switch($a){
       case 10:
           echo "10\n";
       case 20:
           echo "20\n";
           break;
       default:
           echo "default\n";
    }
    

    expacted output

    10
    20
    

    but got

    10
    default
    

    here is a bug fix. ;)

    opened by RickySu 3
  • AUTOMATED: WhiteSpace Removal

    AUTOMATED: WhiteSpace Removal

    This is an automated request to remove excessive whitespace from source code. You can find more about this project and opt-out at whitespacestrippers.com

    opened by WhiteSpace-Strippers 3
  • composer install error

    composer install error

    Hello!I perform composer install has been card after this command Then I added the debug composer - VVV install see error messages below Please help to solve it. phpphp_error

    opened by wujunze 2
  • About the  timeout

    About the timeout

    Hi when i run the command

    php composer.phar install --dev

    there are some problems about that:

    [Symfony\Component\Process\Exception\RuntimeException]
    The process timed out.

    I just run this COMPOSER_PROCESS_TIMEOUT=4000 php composer.phar update

    But the problem still exist .

    I dont know WHY and HOW !!

    Thanks.

    opened by liuyu121 2
  • Concrete dependency versions

    Concrete dependency versions

    I have added concrete dependency versions and updated composer.lock. Version numbers based on last commits date to PHPPHP project, reflected to nikic/php-parser and php/php-src versions at approximately same date.

    Related PR https://github.com/ircmaxell/PHPPHP/pull/29

    opened by cystbear 1
  • fix php.sh to support double quotes

    fix php.sh to support double quotes

    The current script loses double quotes, meaning this will fail:

    $ ./php.sh -r 'var_dump("hello");'
    

    I was not sure if there was a reason for the -c subshell. Hence the PR. If that needs to stay, an alternate fix would be:

    PARAMS="${PARAMS} \"${PARAM//\"/\\\"}\""
    

    (from here, lol bash)

    opened by igorw 1
  • [WIP] Objects support

    [WIP] Objects support

    This adds very minimal objects supports

    What works:

    • declaring a class
    • instanciating
    • calling methods
    • stdClass
    • casting to object

    TODO:

    • get/set properties
    • default property values
    • $this
    • visibility, scope
    • extending
    • abstract methods, classes
    • interfaces
    • instanceof, get_class, ...
    • self, static, parent
    • magic methods
    • constructor
    • var_dump

    Maybe all this could be done incrementally in separate PRs

    opened by arnaud-lb 1
  • Fixed continue

    Fixed continue

    This fixes ContinueOp in some cases:

    • made it work when startOp offset is 0
    • re-pushes the StructureData in the statementStack after continue
    • changed the startOp offsets for the do and for loops
    opened by arnaud-lb 1
  • Fixed variable lookup when scope has changed

    Fixed variable lookup when scope has changed

    This fixes fetching the value of a Variable when the current scope is not the scope in which the Variable is declared.

    This fixes the following code:

    <?php
    
    function x ($x) {
        var_dump($x);
    }
    
    $y = 1;
    x($y);
    

    tests/lang/022.phpt now passes tests/lang/static_basic_001.phpt now breaks (but should be fixed separately; this is caused by compiled vars being shared between function calls)

    opened by arnaud-lb 1
  • Specialized assign dim

    Specialized assign dim

    Adds support for appending to array ($array[] =), and creating array by assigning to it ($a[] = or $a[0] = when $a is not set or null).

    (first commit it in #12)

    opened by arnaud-lb 0
  • Delete composer.lock

    Delete composer.lock

    [RuntimeException]
    The lock file does not contain require-dev information, run install with the --no-dev option or run update to install those packages.

    opened by ghost 2
  • $_GET/$_POST ?

    $_GET/$_POST ?

    I've been trying to get $_GET/$_POST support working for the entire day already, but I don't seem to be very lucky at this, unfortunately.

    It definitely doesn't seem to be supported by default, so I tried to integrate it myself. I've tried the following approaches, without much luck. Would you guys be able to tell me what's up?

    • Add/Dump $_GET/$_POST in Executor.php's execute function, at the point where $scope->symbolTable is assigned.
    • Adding array's/Zval::ptrFactory's/Zval:factory's/Zval::<other stuff> to $this->symbolTable in ExecutorGlobals.php's __construct.
    • Dumping data in OpLines/FetchGlobalVariables.php's execute handler, to receive a notification of reading a global.
    • Some other stuff..

    Other than that, I'm really amazed by how clean the PHP code is, very good job on that one! Hopefully globals will be supported soon.

    Also, I noticed that var_dump($GLOBALS); results in an infinite loop.

    Cheers, Jurriaan

    opened by jbremer 2
Owner
Anthony Ferrara
Anthony Ferrara
A PHP parser written in PHP

PHP Parser This is a PHP 5.2 to PHP 8.0 parser written in PHP. Its purpose is to simplify static code analysis and manipulation. Documentation for ver

Nikita Popov 15.9k Jan 3, 2023
PHP Architecture Tester - Easy to use architectural testing tool for PHP :heavy_check_mark:

Easy to use architecture testing tool for PHP Introduction ?? PHP Architecture Tester is a static analysis tool to verify architectural requirements.

Carlos A Sastre 765 Dec 30, 2022
Provides functionality that helps writing PHP code that has runtime-specific (PHP / HHVM) execution paths

sebastian/environment This component provides functionality that helps writing PHP code that has runtime-specific (PHP / HHVM) execution paths. Instal

Sebastian Bergmann 6.5k Jan 3, 2023
Search PHP source code for function & method calls, variables, and more from PHP.

Searching PHP source code made easy Search PHP source code for function & method calls, variable assignments, classes and more directly from PHP. Inst

Permafrost Software 22 Nov 24, 2022
A full-scale PHP sandbox class that utilizes PHP-Parser to prevent sandboxed code from running unsafe code

A full-scale PHP 7.4+ sandbox class that utilizes PHP-Parser to prevent sandboxed code from running unsafe code. It also utilizes FunctionParser to di

Corveda 192 Dec 10, 2022
A tool to automatically fix PHP Coding Standards issues

PHP Coding Standards Fixer The PHP Coding Standards Fixer (PHP CS Fixer) tool fixes your code to follow standards; whether you want to follow PHP codi

null 11.6k Jan 3, 2023
PHP_CodeSniffer tokenizes PHP files and detects violations of a defined set of coding standards.

About PHP_CodeSniffer is a set of two PHP scripts; the main phpcs script that tokenizes PHP, JavaScript and CSS files to detect violations of a define

Squiz Labs 9.9k Jan 4, 2023
PHP Static Analysis Tool - discover bugs in your code without running it!

PHPStan - PHP Static Analysis Tool PHPStan focuses on finding errors in your code without actually running it. It catches whole classes of bugs even b

PHPStan 11.6k Dec 30, 2022
Phan is a static analyzer for PHP. Phan prefers to avoid false-positives and attempts to prove incorrectness rather than correctness.

Phan is a static analyzer for PHP that prefers to minimize false-positives. Phan attempts to prove incorrectness rather than correctness. Phan looks f

null 5.4k Jan 7, 2023
A PHP code-quality tool

GrumPHP Sick and tired of defending code quality over and over again? GrumPHP will do it for you! This composer plugin will register some git hooks in

PHPro 3.9k Jan 1, 2023
Beautiful and understandable static analysis tool for PHP

PhpMetrics PhpMetrics provides metrics about PHP project and classes, with beautiful and readable HTML report. Documentation | Twitter | Contributing

PhpMetrics 2.3k Dec 22, 2022
A tool for quickly measuring the size of a PHP project.

PHPLOC phploc is a tool for quickly measuring the size and analyzing the structure of a PHP project. Installation This tool is distributed as a PHP Ar

Sebastian Bergmann 2.3k Jan 4, 2023
Copy/Paste Detector (CPD) for PHP code.

PHP Copy/Paste Detector (PHPCPD) phpcpd is a Copy/Paste Detector (CPD) for PHP code. Installation This tool is distributed as a PHP Archive (PHAR): $

Sebastian Bergmann 2.2k Jan 1, 2023
:crystal_ball: Better Reflection is a reflection API that aims to improve and provide more features than PHP's built-in reflection API.

Better Reflection Better Reflection is a reflection API that aims to improve and provide more features than PHP's built-in reflection API. Why is it b

Roave, LLC 1.1k Dec 15, 2022
A command line refactoring tool for PHP

PHP Refactoring Browser Note: This software is under development and in alpha state. Refactorings do not contain all necessary pre-conditions and migh

QafooLabs 562 Dec 30, 2022
Micro PHP benchmark library

Ubench Ubench is a PHP micro library for benchmark Installation Note: If you are looking for this package for laravel application then install it from

Jeremy Perret 554 Nov 25, 2022
Analyze PHP code with one command

PHPQA Analyze PHP code with one command. Requirements PHP >= 5.4.0 xsl extension for HTML reports Why? Every analyzer has different arguments and opti

edgedesign/phpqa 542 Dec 24, 2022
Performs advanced static analysis on PHP code

PHP Analyzer Please report bugs or feature requests via our website support system ? in bottom right or by emailing [email protected]. Contri

Continuous Inspection 443 Sep 23, 2022
The Exakat Engine : smart static analysis for PHP

Exakat The Exakat Engine is an automated code reviewing engine for PHP. Installation Installation with the phar Phar is the recommended installation p

Exakat 370 Dec 28, 2022