SlideShare a Scribd company logo
Diving into
HHVM Extensions
James Titcumb
PHPNW Conference 2015
James Titcumb
www.jamestitcumb.com
www.roave.com
www.phphants.co.uk
www.phpsouthcoast.co.uk
@asgrim
Who is this guy?
First, some background...
source: Microsoft
Diving into HHVM Extensions (PHPNW Conference 2015)
Diving into HHVM Extensions (PHPNW Conference 2015)
Diving into HHVM Extensions (PHPNW Conference 2015)
How PHP works
PHP code
OpCache
Execute (VM)
Lexer + Parser
Compiler
How HHVM works
PHP code
OpCache
Execute (VM)
Lexer + Parser
Compiler
JIT
Execute (Native)
Hack features
● Return/parameter type hints
● ?nullable
● Collections (Vectors, Sets, Maps, etc.)
● Async functions
● Enums
● Generics
● Lambda expressions ==>
● XHP
● more stuff
The HHVM codebase
The Engine
hphp/parser
The Engine
hphp/parser
hphp/compiler
The Engine
hphp/parser
hphp/compiler
hphp/hhbbc
The Engine
hphp/parser
hphp/compiler
hphp/hhbbc
hphp/hhvm
The Engine
hphp/parser
hphp/compiler
hphp/hhbbc
hphp/hhvm
hphp/runtime/vm
The Engine
hphp/parser
hphp/compiler
hphp/hhbbc
hphp/hhvm
hphp/runtime/vm
hphp/runtime/vm/jit
The Extensions
hphp/runtime/ext
The Extensions
hphp/runtime/ext
hphp/system/php
Compile HHVM?
source: https://xkcd.com/303/
My First HHVM Extension™
config.cmake
HHVM_EXTENSION(calc ext_calc.cpp)
ext_calc.cpp
#include "hphp/runtime/ext/extension.h"
namespace HPHP {
static class CalcExtension : public Extension {
public:
CalcExtension(): Extension("calc") {}
virtual void moduleInit() {}
} s_calc_extension;
HHVM_GET_MODULE(calc);
} // namespace HPHP
ext_calc.cpp
#include "hphp/runtime/ext/extension.h"
namespace HPHP {
static class CalcExtension : public Extension {
public:
CalcExtension(): Extension("calc") {}
virtual void moduleInit() {}
} s_calc_extension;
HHVM_GET_MODULE(calc);
} // namespace HPHP
ext_calc.cpp
#include "hphp/runtime/ext/extension.h"
namespace HPHP {
static class CalcExtension : public Extension {
public:
CalcExtension(): Extension("calc") {}
virtual void moduleInit() {}
} s_calc_extension;
HHVM_GET_MODULE(calc);
} // namespace HPHP
ext_calc.cpp
#include "hphp/runtime/ext/extension.h"
namespace HPHP {
static class CalcExtension : public Extension {
public:
CalcExtension(): Extension("calc") {}
virtual void moduleInit() {}
} s_calc_extension;
HHVM_GET_MODULE(calc);
} // namespace HPHP
ext_calc.cpp
#include "hphp/runtime/ext/extension.h"
namespace HPHP {
static class CalcExtension : public Extension {
public:
CalcExtension(): Extension("calc") {}
virtual void moduleInit() {}
} s_calc_extension;
HHVM_GET_MODULE(calc);
} // namespace HPHP
… that’s it.
<?php
var_dump(extension_loaded('calc'));
test.php
Compile it.
(waaaat?!)
Compile & run the test
#!/bin/bash
hphpize
cmake . && make
/usr/bin/hhvm 
-d extension_dir=. 
-d hhvm.extensions[]=calc.so 
test.php
Compile & run the test
$ ./build.sh
bool(true)
$
Hack it!
source: http://goo.gl/kUAxBI
config.cmake
HHVM_EXTENSION(calc ext_calc.cpp)
HHVM_SYSTEMLIB(calc ext_calc.php)
ext_calc.cpp
#include "hphp/runtime/ext/extension.h"
namespace HPHP {
static class CalcExtension : public Extension {
public:
CalcExtension(): Extension("calc") {}
virtual void moduleInit() {
loadSystemlib();
}
} s_calc_extension;
HHVM_GET_MODULE(calc);
} // namespace HPHP
ext_calc.php
<?hh
function calc_sub(int $a, int $b): int {
return $a - $b;
}
… that’s it.
<?php
var_dump(extension_loaded('calc'));
var_dump(calc_sub(5, 3));
test.php
Compile & run the test
$ ./build.sh
bool(true)
int(2)
$
Sprinkle some C++ in
for good measure
ext_calc.cpp
// ... SNIP ...
virtual void moduleInit() {
HHVM_FE(calc_add);
loadSystemlib();
}
// ... SNIP ...
ext_calc.cpp
// ... SNIP ...
static int64_t HHVM_FUNCTION(calc_add, int64_t a, int64_t b) {
return a + b;
}
// ... SNIP ...
in php extensions...
PHP_FUNCTION(calc_add)
{
// ... SNIP ...
#ifndef FAST_ZPP
if (zend_parse_parameters(ZEND_NUM_ARGS(), "ll", &a, &b) == FAILURE) {
return;
}
#else
ZEND_PARSE_PARAMETERS_START(2, 2)
Z_PARAM_LONG(a)
Z_PARAM_LONG(b)
ZEND_PARSE_PARAMETERS_END();
#endif
// ... SNIP ...
ext_calc.php
<?hh
<<__Native>>
function calc_add(int $a, int $b): int;
function calc_sub(int $a, int $b): int {
return $a - $b;
}
… that’s it.
<?php
var_dump(extension_loaded('calc'));
var_dump(calc_sub(5, 3));
var_dump(calc_add(5, 3));
test.php
Compile & run the test
$ ./build.sh
bool(true)
int(2)
int(8)
$
Debugging?!
source: http://www.gnu.org/software/gdb/
Add debug mode
#!/bin/bash
hphpize
cmake 
-DCMAKE_C_FLAGS="-O0 -ggdb3" 
-DCMAKE_CXX_FLAGS="-O0 -ggdb3" 
-DCMAKE_BUILD_TYPE=Debug 
.
make
# ... SNIP ...
Run with gdb
$ gdb --args 
/usr/bin/hhvm 
-d extension_dir=. 
-d hhvm.extensions[]=calc.so 
test.php
GNU gdb (Ubuntu 7.9-1ubuntu1) 7.9
Copyright (C) 2015 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.
org/licenses/gpl.html>
--- snip ---
Reading symbols from /usr/bin/hhvm...done.
(gdb)
Breakpoints
(gdb) b ext_calc.cpp:6
No source file named ext_calc.cpp.
Make breakpoint pending on future shared library load? (y or [n]) y
Breakpoint 1 (ext_calc.cpp:6) pending.
(gdb)
Running
(gdb) r
Starting program: /usr/bin/hhvm -d extension_dir=. -d hhvm.extensions[]
=calc.so smoke.php
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
Breakpoint 2, HPHP::f_calc_add (a=5, b=3) at /home/james/workspace/hhvm-
calc/ext_calc.cpp:6
6 return a + b;
(gdb) p a
$1 = 5
(gdb) p b
$2 = 3
(gdb)
Handy commands
c continue / step out
n step over
s step into
p x print the value of a variable (x)
set x = y set a variable (x) to value (y)
bt print backtrace
q quit :)
When NOT to write extensions
When to write extensions
BUCKLE UP.
source: https://goo.gl/x7Srhe
Integrating OpenGL
into an HHVM extension
Don’t try this in
production!
srsly.
Extension
Browser
What I did
Diving into HHVM Extensions (PHPNW Conference 2015)
huh?!
Make it OOOOOOO
ext_foo.php
<?hh
<<__NativeData("Foo")>>
class Foo {
<<__Native>>
public function bar(): int;
}
C++ object
!==
PHP object
source: http://goo.gl/HORwLQ
HHVM Universe
<?php
$o = new Foo();
$o->bar();
PHP Land
class Foo {
public:
Foo() {}
~Foo() {}
int value = 5;
}
Planet C++
int64_t HHVM_METHOD(Foo,bar)
{
auto data =
Native::data<Foo>(this_);
return data->value;
}
Time Vortex!?!?!
C++ object !== PHP object
HHVM Universe
<?php
$o = new Foo();
$o->bar();
PHP Land
class Foo {
public:
Foo() {}
~Foo() {}
int value = 5;
}
Planet C++
int64_t HHVM_METHOD(Foo,bar)
{
auto data =
Native::data<Foo>(this_);
return data->value;
}
Time Vortex!?!?!
C++ object !== PHP object
HHVM Universe
<?php
$o = new Foo();
$o->bar();
PHP Land
class Foo {
public:
Foo() {}
~Foo() {}
int value = 5;
}
Planet C++
int64_t HHVM_METHOD(Foo,bar)
{
auto data =
Native::data<Foo>(this_);
return data->value;
}
Time Vortex!?!?!
C++ object !== PHP object
HHVM Universe
<?php
$o = new Foo();
$o->bar();
PHP Land
class Foo {
public:
Foo() {}
~Foo() {}
int value = 5;
}
Planet C++
int64_t HHVM_METHOD(Foo,bar)
{
auto data =
Native::data<Foo>(this_);
return data->value;
}
Time Vortex!?!?!
C++ object !== PHP object
this slide is intentionally left blank
ext_foo.php
<?hh
class Foo {
private int $value
public function bar(): int
{
return $this->value;
}
}
Demo?
Diving into HHVM Extensions (PHPNW Conference 2015)
source: http://goo.gl/7gWfNz
Resources
● OpenGL Tutorial
○ http://www.opengl-tutorial.org/
● HHVM Example Extension
○ https://github.com/hhvm/extension-example
● Sara Golemon - HHVM extension blog series
○ http://blog.golemon.com/2015/01/hhvm-extension-writing-part-iii.html
● Derick Rethans’ extension API cookbook
○ https://github.com/derickr/hhvm-hni-cookbook
● The official API documentation
○ https://github.com/facebook/hhvm/wiki/Extension%20API
● Journey of a Thousand Bytecodes
○ http://hhvm.com/blog/6323/the-journey-of-a-thousand-bytecodes
Any questions? :)
https://joind.in/15434
James Titcumb @asgrim

More Related Content

PPTX
Zephir - A Wind of Change for writing PHP extensions
PDF
Just-In-Time Compiler in PHP 8
PPTX
New in php 7
KEY
Zend Framework Study@Tokyo #2
PDF
The worst Ruby codes I’ve seen in my life - RubyKaigi 2015
PPT
Symfony2 Service Container: Inject me, my friend
KEY
PHPSpec BDD for PHP
PDF
IPC2010SE Doctrine2 Enterprise Persistence Layer for PHP
Zephir - A Wind of Change for writing PHP extensions
Just-In-Time Compiler in PHP 8
New in php 7
Zend Framework Study@Tokyo #2
The worst Ruby codes I’ve seen in my life - RubyKaigi 2015
Symfony2 Service Container: Inject me, my friend
PHPSpec BDD for PHP
IPC2010SE Doctrine2 Enterprise Persistence Layer for PHP

What's hot (20)

PDF
PHPSpec - the only Design Tool you need - 4Developers
PDF
PHP7 is coming
PPT
ZFConf 2010: Zend Framework & MVC, Model Implementation (Part 2, Dependency I...
PDF
Get your teeth into Plack
KEY
Zend Framework Study@Tokyo vol1
ODP
PHP Tips for certification - OdW13
PDF
Understanding PHP objects
KEY
Yapcasia2011 - Hello Embed Perl
PPTX
Php7 HHVM and co
PDF
Dependency Injection in PHP
PDF
Rich Model And Layered Architecture in SF2 Application
PDF
November Camp - Spec BDD with PHPSpec 2
PDF
PhpSpec 2.0 ilustrated by examples
ODP
Mastering Namespaces in PHP
PDF
Information security programming in ruby
ODP
The promise of asynchronous PHP
PDF
PHP traits, treat or threat?
PDF
Clear php reference
ODP
Building Web Services with Zend Framework (PHP Benelux meeting 20100713 Vliss...
PDF
A reviravolta do desenvolvimento web
PHPSpec - the only Design Tool you need - 4Developers
PHP7 is coming
ZFConf 2010: Zend Framework & MVC, Model Implementation (Part 2, Dependency I...
Get your teeth into Plack
Zend Framework Study@Tokyo vol1
PHP Tips for certification - OdW13
Understanding PHP objects
Yapcasia2011 - Hello Embed Perl
Php7 HHVM and co
Dependency Injection in PHP
Rich Model And Layered Architecture in SF2 Application
November Camp - Spec BDD with PHPSpec 2
PhpSpec 2.0 ilustrated by examples
Mastering Namespaces in PHP
Information security programming in ruby
The promise of asynchronous PHP
PHP traits, treat or threat?
Clear php reference
Building Web Services with Zend Framework (PHP Benelux meeting 20100713 Vliss...
A reviravolta do desenvolvimento web
Ad

Viewers also liked (15)

PDF
TDD: Team-Driven Development
PDF
Are you a good scout? - PHPNW15 Unconf
PDF
Composer the right way - SunshinePHP
PDF
Your code are my tests
ODP
My app is secure... I think
PDF
Secure Form Processing and Protection - Sunshine PHP 2015
PDF
QA for PHP projects
PPTX
Your Inner Sysadmin - Tutorial (SunshinePHP 2015)
PDF
Building Your API for Longevity
PDF
TDD with PhpSpec
PDF
Dockerize All The Things
PDF
Introduction to Continuous Integration with Jenkins
PDF
Driving Design through Examples
PDF
Hexagonal architecture message-oriented software design
PDF
Consequences of an Insightful Algorithm
TDD: Team-Driven Development
Are you a good scout? - PHPNW15 Unconf
Composer the right way - SunshinePHP
Your code are my tests
My app is secure... I think
Secure Form Processing and Protection - Sunshine PHP 2015
QA for PHP projects
Your Inner Sysadmin - Tutorial (SunshinePHP 2015)
Building Your API for Longevity
TDD with PhpSpec
Dockerize All The Things
Introduction to Continuous Integration with Jenkins
Driving Design through Examples
Hexagonal architecture message-oriented software design
Consequences of an Insightful Algorithm
Ad

Similar to Diving into HHVM Extensions (PHPNW Conference 2015) (20)

PDF
Diving into HHVM Extensions (php[tek] 2016)
PDF
實戰 Hhvm extension php conf 2014
ODP
Incredible Machine with Pipelines and Generators
PPT
Hacking with hhvm
PDF
Living With Legacy Code
PPT
3. build your own php extension ai ti aptech
PPT
07 build your-own_php_extension
PPT
Build your own PHP extension
PDF
InspiringCon14: ElePHPants on speed: Running TYPO3 Flow on HipHop VM
PPTX
Php7 hhvm and co
PPTX
PHP7 Presentation
ODP
Building and Incredible Machine with Pipelines and Generators in PHP (IPC Ber...
PPTX
Php.ppt
PDF
The new features of PHP 7 - Enrico Zimuel - Codemotion Milan 2016
PDF
The new features of PHP 7
PDF
PHP 8: Process & Fixing Insanity
ODP
PHP: The easiest language to learn.
ODP
The why and how of moving to PHP 5.5/5.6
PDF
Cli the other sapi pbc11
PDF
Cli the other SAPI confoo11
Diving into HHVM Extensions (php[tek] 2016)
實戰 Hhvm extension php conf 2014
Incredible Machine with Pipelines and Generators
Hacking with hhvm
Living With Legacy Code
3. build your own php extension ai ti aptech
07 build your-own_php_extension
Build your own PHP extension
InspiringCon14: ElePHPants on speed: Running TYPO3 Flow on HipHop VM
Php7 hhvm and co
PHP7 Presentation
Building and Incredible Machine with Pipelines and Generators in PHP (IPC Ber...
Php.ppt
The new features of PHP 7 - Enrico Zimuel - Codemotion Milan 2016
The new features of PHP 7
PHP 8: Process & Fixing Insanity
PHP: The easiest language to learn.
The why and how of moving to PHP 5.5/5.6
Cli the other sapi pbc11
Cli the other SAPI confoo11

More from James Titcumb (20)

PDF
Living the Best Life on a Legacy Project (phpday 2022).pdf
PDF
Tips for Tackling a Legacy Codebase (ScotlandPHP 2021)
PDF
Climbing the Abstract Syntax Tree (Midwest PHP 2020)
PDF
Best practices for crafting high quality PHP apps (Bulgaria 2019)
PDF
Climbing the Abstract Syntax Tree (php[world] 2019)
PDF
Best practices for crafting high quality PHP apps (php[world] 2019)
PDF
Crafting Quality PHP Applications (PHP Joburg Oct 2019)
PDF
Climbing the Abstract Syntax Tree (PHP Russia 2019)
PDF
Best practices for crafting high quality PHP apps - PHP UK 2019
PDF
Climbing the Abstract Syntax Tree (ScotlandPHP 2018)
PDF
Best practices for crafting high quality PHP apps (ScotlandPHP 2018)
PDF
Kicking off with Zend Expressive and Doctrine ORM (PHP South Africa 2018)
PDF
Best practices for crafting high quality PHP apps (PHP South Africa 2018)
PDF
Climbing the Abstract Syntax Tree (PHP Developer Days Dresden 2018)
PDF
Climbing the Abstract Syntax Tree (Southeast PHP 2018)
PDF
Crafting Quality PHP Applications (PHPkonf 2018)
PDF
Best practices for crafting high quality PHP apps (PHP Yorkshire 2018)
PDF
Crafting Quality PHP Applications: an overview (PHPSW March 2018)
PDF
Kicking off with Zend Expressive and Doctrine ORM (PHP MiNDS March 2018)
PDF
Climbing the Abstract Syntax Tree (PHP UK 2018)
Living the Best Life on a Legacy Project (phpday 2022).pdf
Tips for Tackling a Legacy Codebase (ScotlandPHP 2021)
Climbing the Abstract Syntax Tree (Midwest PHP 2020)
Best practices for crafting high quality PHP apps (Bulgaria 2019)
Climbing the Abstract Syntax Tree (php[world] 2019)
Best practices for crafting high quality PHP apps (php[world] 2019)
Crafting Quality PHP Applications (PHP Joburg Oct 2019)
Climbing the Abstract Syntax Tree (PHP Russia 2019)
Best practices for crafting high quality PHP apps - PHP UK 2019
Climbing the Abstract Syntax Tree (ScotlandPHP 2018)
Best practices for crafting high quality PHP apps (ScotlandPHP 2018)
Kicking off with Zend Expressive and Doctrine ORM (PHP South Africa 2018)
Best practices for crafting high quality PHP apps (PHP South Africa 2018)
Climbing the Abstract Syntax Tree (PHP Developer Days Dresden 2018)
Climbing the Abstract Syntax Tree (Southeast PHP 2018)
Crafting Quality PHP Applications (PHPkonf 2018)
Best practices for crafting high quality PHP apps (PHP Yorkshire 2018)
Crafting Quality PHP Applications: an overview (PHPSW March 2018)
Kicking off with Zend Expressive and Doctrine ORM (PHP MiNDS March 2018)
Climbing the Abstract Syntax Tree (PHP UK 2018)

Recently uploaded (20)

PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Modernizing your data center with Dell and AMD
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
cuic standard and advanced reporting.pdf
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Encapsulation theory and applications.pdf
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Empathic Computing: Creating Shared Understanding
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
PDF
Machine learning based COVID-19 study performance prediction
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Modernizing your data center with Dell and AMD
Advanced methodologies resolving dimensionality complications for autism neur...
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Building Integrated photovoltaic BIPV_UPV.pdf
Chapter 3 Spatial Domain Image Processing.pdf
cuic standard and advanced reporting.pdf
Reach Out and Touch Someone: Haptics and Empathic Computing
Encapsulation theory and applications.pdf
Dropbox Q2 2025 Financial Results & Investor Presentation
Spectral efficient network and resource selection model in 5G networks
Diabetes mellitus diagnosis method based random forest with bat algorithm
Empathic Computing: Creating Shared Understanding
Agricultural_Statistics_at_a_Glance_2022_0.pdf
“AI and Expert System Decision Support & Business Intelligence Systems”
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Mobile App Security Testing_ A Comprehensive Guide.pdf
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
Machine learning based COVID-19 study performance prediction
The Rise and Fall of 3GPP – Time for a Sabbatical?

Diving into HHVM Extensions (PHPNW Conference 2015)