• Stars
    star
    811
  • Rank 56,215 (Top 2 %)
  • Language
    PHP
  • License
    MIT License
  • Created almost 12 years ago
  • Updated almost 6 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A PHP VM implementation in PHP

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

More Repositories

1

password_compat

Compatibility with the password_* functions that ship with PHP 5.5
PHP
2,151
star
2

RandomLib

A library for generating random numbers and strings
PHP
840
star
3

php-compiler

A compiler. For PHP
PHP
793
star
4

PhpGenerics

Here be dragons
PHP
530
star
5

filterus

A simple filtering library for PHP
PHP
454
star
6

PHP-PasswordLib

A library for generating and validating passwords
PHP
373
star
7

monad-php

A simple Monad library for PHP
PHP
296
star
8

php-cfg

A Control Flow Graph implementation in PHP
PHP
243
star
9

Tuli

A static analysis engine
PHP
168
star
10

phpvm

A PHP version manager for CLI PHP
PHP
150
star
11

PHP-Yacc

A PHP port of kmyacc
PHP
149
star
12

PHP-CryptLib

A Cryptography Library for PHP
PHP
144
star
13

FFIMe

A FFI Wrapper library and header parser!
PHP
136
star
14

SecurityLib

SecurityLib
PHP
126
star
15

Stauros

A fast XSS sanitization library for PHP
PHP
118
star
16

php-security-scanner

A static security scanner for PHP
PHP
97
star
17

Tari-PHP

A middleware proposal for PHP
PHP
78
star
18

password-policy

A password policy enforcer for PHP and JavaScript
PHP
77
star
19

php-ast-visualizer

An AST visualizer, for PHP
PHP
75
star
20

prerano

A new language for PHP
PHP
65
star
21

php-preprocessor

A PreProcessing library for PHP
PHP
49
star
22

ErrorExceptions

A library for converting core PHP errors into ErrorExceptions
PHP
43
star
23

PHP-BrainFuck

A brainfuck interpreter for PHP
PHP
42
star
24

php-c-parser

A C parser built in and for PHP (yes, it's a bad idea)...
PHP
40
star
25

random_compat

Compatibility library for proposed simplified random number generator
PHP
40
star
26

php-llvm

A "lightweight" wrapper around LLVM-C in native PHP
PHP
39
star
27

php-types

A PHP Type reconstruction library
PHP
36
star
28

php-compiler-toolkit

A compiler toolkit. For PHP (yes, I am creative at naming things)...
PHP
30
star
29

resume

Anthony Ferrara's Resume (CV)
30
star
30

php-math-parser

A Shunting-Yard Based Math Engine For PHP
PHP
29
star
31

Protocol-Lib

A library for runtime checking of protocols
PHP
27
star
32

php-optimizer

A CFG Optimizer for PHP
PHP
24
star
33

RequirePHP

A RequireJS clone in PHP - As a dependency Loader
PHP
22
star
34

ballandchain

A PHP implementation of BallAndChain
PHP
20
star
35

libgccffi

libgccffi interface for PHP, based on 7.4's FFI and FFIMe
PHP
19
star
36

MixinPHP

A test mixin library for super-happy-crazy-time
PHP
18
star
37

php-ndata

NData PECL extension for dealing with native data types
C
15
star
38

cpu_assembler

An assembler for my custom CPU
PHP
13
star
39

TrueObjectStore

What SPLObjectStorage Should Have Been
PHP
13
star
40

haas

Hugs, As A Service
HTML
13
star
41

programming-with-anthony

Scripts for the Programming With Anthony series on YouTube
11
star
42

php-object-symbolresolver

A linux object file (ELF) parser
PHP
10
star
43

password-bad-web-app

A bad web app, to demonstrate password hashing issues DO NOT USE!!!
PHP
10
star
44

quality-checker

PHP Quality Checker
PHP
10
star
45

blog.ircmaxell.com

blog.ircmaxell.com future site
Less
9
star
46

Primitives

A collection of primitive types for PHP
PHP
6
star
47

blog-ideas

6
star
48

cryptography-presentation-tnphp

Slides for the Cryptography Presentation done at TrueNorthPHP on Nov 2, 2012
JavaScript
6
star
49

Ircmaxell.com

PHP
5
star
50

ZPP

A PHP implementation of Zend-Parse-Parameters
PHP
5
star
51

XssBadWebApp

A Intentionally Vulnerable Bad Web Application With XSS Vulnerabilities - *DO NOT USE!!!*
PHP
5
star
52

CodeReviewSecurityRepo

Code Review for Security Repository Of Code To Review
PHP
5
star
53

password-advice

The website behind password-advice.com
4
star
54

SetLib

A Badly Named Playground
PHP
4
star
55

DontBeStupid-Presentation

A repo of the Don't Be Stupid, Grasp Solid presentation at NYPHP on 5-22-12
JavaScript
3
star
56

hashguesser

Hash guesser
JavaScript
3
star
57

jQuery.OOP

A pseudo-port of MooTools OOP to jQuery
2
star
58

Intervalometer

An intervalometer
Arduino
2
star
59

BehaviorTest

A Proof-Of-Concept behavioral testing app
PHP
2
star
60

password-hashing-mini-presentation

Password-Hashing-Mini-Presentation
JavaScript
2
star
61

PHPTest

A Unit Testing Framework for PHP
PHP
2
star
62

ITL

Some silly test programming language thingy
Ruby
1
star
63

solid-presentation-tnphp

Slides for the SOLID OO Design presentation at True North PHP on Nov 3, 2012
JavaScript
1
star
64

PreProcessor

A trivial attempt at a PHP preprocessor (DO NOT USE!!! Experimental ONLY!!!)
PHP
1
star
65

jsGoodies

Just some JS snipits I've found useful
JavaScript
1
star
66

8bit-cpu-v2

Ruby
1
star