SlideShare a Scribd company logo
Diving into
HHVM Extensions
James Titcumb
BrnoPHP 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 (Brno PHP Conference 2015)
Diving into HHVM Extensions (Brno PHP Conference 2015)
Diving into HHVM Extensions (Brno PHP 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 (Brno PHP 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 (Brno PHP 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/16263
James Titcumb @asgrim

More Related Content

PDF
TVM VTA (TSIM)
ODP
Отладка в GDB
PDF
C++ Programming - 2nd Study
PDF
All I know about rsc.io/c2go
PDF
C++ Programming - 1st Study
PPTX
Understand more about C
PPT
Perl Intro 4 Debugger
TVM VTA (TSIM)
Отладка в GDB
C++ Programming - 2nd Study
All I know about rsc.io/c2go
C++ Programming - 1st Study
Understand more about C
Perl Intro 4 Debugger

What's hot (20)

DOCX
C++ file
PDF
Powered by Python - PyCon Germany 2016
PDF
2018 cosup-delete unused python code safely - english
PDF
Defcon 23 - Daniel Selifonov - drinking from LETHE
PDF
Job Queue in Golang
PPTX
A new way to develop with WordPress!
PDF
"A 1,500 line (!!) switch statement powers your Python!" - Allison Kaptur, !!...
PDF
Byterun, a Python bytecode interpreter - Allison Kaptur at NYCPython
PPT
C introduction by piyushkumar
PPTX
C - ISRO
PDF
One definition rule - что это такое, и как с этим жить
PPTX
Getting started with open cv in raspberry pi
PPT
Introduction to cython
PDF
Diving into byte code optimization in python
PDF
第1回PHP拡張勉強会
PDF
Linux shell script-1
PPTX
Ensemble: A DSL for Concurrency, Adaptability, and Distribution
PDF
Virus lab
C++ file
Powered by Python - PyCon Germany 2016
2018 cosup-delete unused python code safely - english
Defcon 23 - Daniel Selifonov - drinking from LETHE
Job Queue in Golang
A new way to develop with WordPress!
"A 1,500 line (!!) switch statement powers your Python!" - Allison Kaptur, !!...
Byterun, a Python bytecode interpreter - Allison Kaptur at NYCPython
C introduction by piyushkumar
C - ISRO
One definition rule - что это такое, и как с этим жить
Getting started with open cv in raspberry pi
Introduction to cython
Diving into byte code optimization in python
第1回PHP拡張勉強会
Linux shell script-1
Ensemble: A DSL for Concurrency, Adaptability, and Distribution
Virus lab
Ad

Similar to Diving into HHVM Extensions (Brno PHP Conference 2015) (20)

PDF
Diving into HHVM Extensions (php[tek] 2016)
PPTX
Php extensions
PDF
Php7 extensions workshop
PPTX
Php extensions
PDF
實戰 Hhvm extension php conf 2014
PPTX
Php extensions
PDF
HHVM and Hack: A quick introduction
PDF
Php extensions workshop
PDF
PHP 7 – What changed internally? (Forum PHP 2015)
PDF
HHVM on AArch64 - BUD17-400K1
PDF
PHP to Hack at Slack
PPTX
From PHP to Hack as a Stack and Back
PDF
What the HACK is HHVM?
PPT
Hacking with hhvm
PDF
Building Custom PHP Extensions
PPTX
Extending php (7), the basics
PDF
Create your own PHP extension, step by step - phpDay 2012 Verona
PDF
Php 7 evolution
PPTX
HHVM: Efficient and Scalable PHP/Hack Execution / Guilherme Ottoni (Facebook)
PPTX
Php Extensions for Dummies
Diving into HHVM Extensions (php[tek] 2016)
Php extensions
Php7 extensions workshop
Php extensions
實戰 Hhvm extension php conf 2014
Php extensions
HHVM and Hack: A quick introduction
Php extensions workshop
PHP 7 – What changed internally? (Forum PHP 2015)
HHVM on AArch64 - BUD17-400K1
PHP to Hack at Slack
From PHP to Hack as a Stack and Back
What the HACK is HHVM?
Hacking with hhvm
Building Custom PHP Extensions
Extending php (7), the basics
Create your own PHP extension, step by step - phpDay 2012 Verona
Php 7 evolution
HHVM: Efficient and Scalable PHP/Hack Execution / Guilherme Ottoni (Facebook)
Php Extensions for Dummies
Ad

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
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
Electronic commerce courselecture one. Pdf
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PPTX
Cloud computing and distributed systems.
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
cuic standard and advanced reporting.pdf
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Encapsulation theory and applications.pdf
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
CIFDAQ's Market Insight: SEC Turns Pro Crypto
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PPTX
MYSQL Presentation for SQL database connectivity
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Per capita expenditure prediction using model stacking based on satellite ima...
Reach Out and Touch Someone: Haptics and Empathic Computing
Encapsulation_ Review paper, used for researhc scholars
Review of recent advances in non-invasive hemoglobin estimation
Electronic commerce courselecture one. Pdf
Unlocking AI with Model Context Protocol (MCP)
Diabetes mellitus diagnosis method based random forest with bat algorithm
Cloud computing and distributed systems.
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
cuic standard and advanced reporting.pdf
Advanced methodologies resolving dimensionality complications for autism neur...
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Encapsulation theory and applications.pdf
Chapter 3 Spatial Domain Image Processing.pdf
CIFDAQ's Market Insight: SEC Turns Pro Crypto
The AUB Centre for AI in Media Proposal.docx
Spectral efficient network and resource selection model in 5G networks
Agricultural_Statistics_at_a_Glance_2022_0.pdf
MYSQL Presentation for SQL database connectivity
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...

Diving into HHVM Extensions (Brno PHP Conference 2015)