Top Banner
Diving into HHVM Extensions James Titcumb
79

Diving into HHVM Extensions (php[tek] 2016)

Jan 26, 2017

Download

Technology

James Titcumb
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Diving into HHVM Extensions (php[tek] 2016)

Diving intoHHVM Extensions

James Titcumb

Page 2: Diving into HHVM Extensions (php[tek] 2016)
Page 4: Diving into HHVM Extensions (php[tek] 2016)

First, some background...

source: Microsoft

Page 5: Diving into HHVM Extensions (php[tek] 2016)
Page 6: Diving into HHVM Extensions (php[tek] 2016)
Page 7: Diving into HHVM Extensions (php[tek] 2016)
Page 8: Diving into HHVM Extensions (php[tek] 2016)

Hack features

● Return/parameter type hints● ?nullable● Collections (Vectors, Sets, Maps, etc.)● Async functions● Enums● Generics● Lambda expressions ==>● XHP● more stuff

@asgrim

Page 9: Diving into HHVM Extensions (php[tek] 2016)

How PHP works

PHP code

OpCache

Execute (VM)

Lexer + Parser

Compiler

@asgrim

Page 10: Diving into HHVM Extensions (php[tek] 2016)

How HHVM worksPHP code

OpCache

Execute (VM)

Lexer + Parser

Compiler

JIT

Execute (Native)

@asgrim

Page 11: Diving into HHVM Extensions (php[tek] 2016)

The HHVM codebase

@asgrim

Page 12: Diving into HHVM Extensions (php[tek] 2016)

The Engine

hphp/parser

@asgrim

Page 13: Diving into HHVM Extensions (php[tek] 2016)

The Engine

hphp/parserhphp/compiler

@asgrim

Page 14: Diving into HHVM Extensions (php[tek] 2016)

The Engine

hphp/parserhphp/compilerhphp/hhbbc

@asgrim

Page 15: Diving into HHVM Extensions (php[tek] 2016)

The Engine

hphp/parserhphp/compilerhphp/hhbbchphp/hhvm

@asgrim

Page 16: Diving into HHVM Extensions (php[tek] 2016)

The Engine

hphp/parserhphp/compilerhphp/hhbbchphp/hhvmhphp/runtime/vm

@asgrim

Page 17: Diving into HHVM Extensions (php[tek] 2016)

The Engine

hphp/parserhphp/compilerhphp/hhbbchphp/hhvmhphp/runtime/vmhphp/runtime/vm/jit

@asgrim

Page 18: Diving into HHVM Extensions (php[tek] 2016)

The Extensions

hphp/runtime/ext

@asgrim

Page 19: Diving into HHVM Extensions (php[tek] 2016)

The Extensions

hphp/runtime/exthphp/system/php

@asgrim

Page 20: Diving into HHVM Extensions (php[tek] 2016)

Compile HHVM?

@asgrim

Page 21: Diving into HHVM Extensions (php[tek] 2016)

source: https://xkcd.com/303/

@asgrim

Page 22: Diving into HHVM Extensions (php[tek] 2016)

My First HHVM Extension™

@asgrim

Page 23: Diving into HHVM Extensions (php[tek] 2016)

config.cmake

HHVM_EXTENSION(calc ext_calc.cpp)

@asgrim

Page 24: Diving into HHVM Extensions (php[tek] 2016)

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

Page 25: Diving into HHVM Extensions (php[tek] 2016)

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

Page 26: Diving into HHVM Extensions (php[tek] 2016)

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

Page 27: Diving into HHVM Extensions (php[tek] 2016)

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

Page 28: Diving into HHVM Extensions (php[tek] 2016)

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

Page 29: Diving into HHVM Extensions (php[tek] 2016)

… that’s it.<?php

var_dump(extension_loaded('calc'));

test.php

@asgrim

Page 30: Diving into HHVM Extensions (php[tek] 2016)

Compile it.(waaaat?!)

@asgrim

Page 31: Diving into HHVM Extensions (php[tek] 2016)

Compile & run the test#!/usr/bin/env bash

hphpize

cmake . && make

/usr/bin/hhvm \

-d extension_dir=. \

-d hhvm.extensions[]=calc.so \

test.php

@asgrim

Page 32: Diving into HHVM Extensions (php[tek] 2016)

Compile & run the test$ ./build.sh

bool(true)

$

@asgrim

Page 33: Diving into HHVM Extensions (php[tek] 2016)

Hack it!

@asgrim

Page 34: Diving into HHVM Extensions (php[tek] 2016)

source: http://goo.gl/kUAxBI

Page 35: Diving into HHVM Extensions (php[tek] 2016)

config.cmake

HHVM_EXTENSION(calc ext_calc.cpp)HHVM_SYSTEMLIB(calc ext_calc.php)

@asgrim

Page 36: Diving into HHVM Extensions (php[tek] 2016)

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

@asgrim

Page 37: Diving into HHVM Extensions (php[tek] 2016)

ext_calc.php<?hh

function calc_sub(int $a, int $b): int {

return $a - $b;

}

@asgrim

Page 38: Diving into HHVM Extensions (php[tek] 2016)

… that’s it.

<?php

var_dump(extension_loaded('calc'));

var_dump(calc_sub(5, 3));

test.php

@asgrim

Page 39: Diving into HHVM Extensions (php[tek] 2016)

Compile & run the test$ ./build.sh

bool(true)

int(2)

$

@asgrim

Page 40: Diving into HHVM Extensions (php[tek] 2016)

Sprinkle some C++ infor good measure

@asgrim

Page 41: Diving into HHVM Extensions (php[tek] 2016)

ext_calc.cpp// ... SNIP ...

virtual void moduleInit() {

HHVM_FE(calc_add);

loadSystemlib();

}

// ... SNIP ...

@asgrim

Page 42: Diving into HHVM Extensions (php[tek] 2016)

ext_calc.cpp// ... SNIP ...

static int64_t HHVM_FUNCTION(calc_add, int64_t a, int64_t b) {

return a + b;

}

// ... SNIP ...

@asgrim

Page 43: Diving into HHVM Extensions (php[tek] 2016)

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 ...

Page 44: Diving into HHVM Extensions (php[tek] 2016)

ext_calc.php<?hh

<<__Native>>

function calc_add(int $a, int $b): int;

function calc_sub(int $a, int $b): int {

return $a - $b;

}

@asgrim

Page 45: Diving into HHVM Extensions (php[tek] 2016)

… that’s it.

<?php

var_dump(extension_loaded('calc'));

var_dump(calc_sub(5, 3));

var_dump(calc_add(5, 3));

test.php

@asgrim

Page 46: Diving into HHVM Extensions (php[tek] 2016)

Compile & run the test$ ./build.sh

bool(true)

int(2)

int(8)

$

@asgrim

Page 47: Diving into HHVM Extensions (php[tek] 2016)

Debugging?!

@asgrim

Page 48: Diving into HHVM Extensions (php[tek] 2016)

source: http://www.gnu.org/software/gdb/

@asgrim

Page 49: Diving into HHVM Extensions (php[tek] 2016)

Add debug mode#!/bin/bash

hphpize

cmake \

-DCMAKE_C_FLAGS="-O0 -ggdb3" \

-DCMAKE_CXX_FLAGS="-O0 -ggdb3" \

-DCMAKE_BUILD_TYPE=Debug \

.

make

# ... SNIP ...

@asgrim

Page 50: Diving into HHVM Extensions (php[tek] 2016)

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)

Page 51: Diving into HHVM Extensions (php[tek] 2016)

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)

@asgrim

Page 52: Diving into HHVM Extensions (php[tek] 2016)

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)

Page 53: Diving into HHVM Extensions (php[tek] 2016)

Handy commandsc 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 :)

@asgrim

Page 54: Diving into HHVM Extensions (php[tek] 2016)

When NOT to write extensions

@asgrim

Page 55: Diving into HHVM Extensions (php[tek] 2016)

When to write extensions

@asgrim

Page 56: Diving into HHVM Extensions (php[tek] 2016)

BUCKLE UP.

@asgrim

Page 57: Diving into HHVM Extensions (php[tek] 2016)

source: https://goo.gl/x7Srhe

@asgrim

Page 58: Diving into HHVM Extensions (php[tek] 2016)

Integrating OpenGLinto an HHVM extension

@asgrim

Page 59: Diving into HHVM Extensions (php[tek] 2016)

Don’t try this in production!

@asgrim

Page 60: Diving into HHVM Extensions (php[tek] 2016)

srsly.

@asgrim

Page 61: Diving into HHVM Extensions (php[tek] 2016)

Extension

Browser

@asgrim

Page 62: Diving into HHVM Extensions (php[tek] 2016)

What I did

@asgrim

Page 63: Diving into HHVM Extensions (php[tek] 2016)

@asgrim

Page 64: Diving into HHVM Extensions (php[tek] 2016)

huh?!@asgrim

Page 65: Diving into HHVM Extensions (php[tek] 2016)

Make it OOOOOOO

@asgrim

Page 66: Diving into HHVM Extensions (php[tek] 2016)

ext_foo.php<?hh

<<__NativeData("Foo")>>

class Foo {

<<__Native>>

public function bar(): int;

}@asgrim

Page 67: Diving into HHVM Extensions (php[tek] 2016)

C++ object!==

PHP object

@asgrim

Page 68: Diving into HHVM Extensions (php[tek] 2016)

source: http://goo.gl/HORwLQ

Page 69: Diving into HHVM Extensions (php[tek] 2016)

HHVM Universe

<?php

$o = new Foo();$o->bar();

PHP Landclass 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

Page 70: Diving into HHVM Extensions (php[tek] 2016)

HHVM Universe

<?php

$o = new Foo();$o->bar();

PHP Landclass 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

Page 71: Diving into HHVM Extensions (php[tek] 2016)

HHVM Universe

<?php

$o = new Foo();$o->bar();

PHP Landclass 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

Page 72: Diving into HHVM Extensions (php[tek] 2016)

HHVM Universe

<?php

$o = new Foo();$o->bar();

PHP Landclass 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

Page 73: Diving into HHVM Extensions (php[tek] 2016)

this slide is intentionally left blank

@asgrim

Page 74: Diving into HHVM Extensions (php[tek] 2016)

ext_foo.php<?hh

class Foo {

private int $value

public function bar(): int

{

return $this->value;

}

}

@asgrim

Page 75: Diving into HHVM Extensions (php[tek] 2016)

Demo?

@asgrim

Page 77: Diving into HHVM Extensions (php[tek] 2016)

source: http://goo.gl/7gWfNz

Page 78: Diving into HHVM Extensions (php[tek] 2016)

● 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

Resources

@asgrim

Page 79: Diving into HHVM Extensions (php[tek] 2016)

Any questions? :)

https://joind.in/talk/a4320James Titcumb @asgrim