Skip to content

Commit 0176497

Browse files
author
Julien Pauli
committed
additions and fixes
1 parent 5974f39 commit 0176497

File tree

6 files changed

+33
-19
lines changed

6 files changed

+33
-19
lines changed

Book/index.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ This part concerns only the PHP 7 branch. It is under development.
3939
php7/extensions_design.rst
4040
php7/memory_management.rst
4141
php7/zend_engine.rst
42+
php7/debugging.rst
43+
4244
..
4345
php7/hashtables.rst
4446
php7/classes_objects.rst

Book/php7/debugging.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
Debugging with GDB
2+
==================
3+
4+
This chapter will introduce you with the GNU C debugger, aka GDB. When a crash happens, you usually have to find the
5+
guilty part in thousands of lines. You need tools for that, and GDB is the most commonly used debugger under Unix
6+
platforms. Here we'll give you an introduction to GDB and how to practice with it against the PHP source code.
7+
8+

Book/php7/extensions_design/php_functions.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ You'll pass to the function vector a declared vector of functions. Let's see tog
8585
static const zend_function_entry pib_functions[] =
8686
{
8787
PHP_FE(fahrenheit_to_celsius, NULL)
88-
}
88+
};
8989

9090
zend_module_entry pib_module_entry = {
9191
STANDARD_MODULE_HEADER,
@@ -175,7 +175,7 @@ Fortunately, you are once more provided some macros to abstract the hard job for
175175

176176
ZEND_BEGIN_ARG_INFO_EX(arginfo_fahrenheit_to_celsius, 0, 0, 1)
177177
ZEND_ARG_INFO(0, fahrenheit)
178-
ZEND_END_ARG_INFO();
178+
ZEND_END_ARG_INFO()
179179

180180
The code above details how to create an argument, but when we expand macros, we can feel some difficulty::
181181

Book/php7/extensions_design/php_lifecycle.rst

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -157,9 +157,10 @@ A request just showed in, and PHP is about to treat it here. In ``RINIT()``, you
157157
treat that precise request. PHP is a share-nothing architecture, and as-is, it provides
158158
:doc:`memory management <../memory_management>` facilities.
159159

160-
In ``RINIT()``, if you need to allocate dynamic memory, you'll use Zend Memory Manager. You will call for ``emalloc()``.
161-
Zend Memory Manager tracks the memory you allocate through it, and when the request shuts down, it will attempt to free
162-
the request-bound memory if you forgot to do so (you should not).
160+
In ``RINIT()``, if you need to allocate dynamic memory, you'll use
161+
:doc:`Zend Memory Manager <../memory_management/zend_memory_manager>`. You will call for ``emalloc()``.
162+
:doc:`Zend Memory Manager <../memory_management/zend_memory_manager>` tracks the memory you allocate through it, and
163+
when the request shuts down, it will attempt to free the request-bound memory if you forgot to do so (you should not).
163164

164165
You should not require persistent dynamic memory here, aka libc's ``malloc()`` or Zend's ``pemalloc()``. If you require
165166
persistent memory here, and forgets to free it, you'll create leaks that will stack as PHP treats more and more
@@ -265,19 +266,20 @@ Thoughts on PHP lifecycle
265266

266267
As you may have spotted, ``RINIT()`` and ``RSHUTDOWN()`` are especially crucial as they could get triggered thousands
267268
of times on your extension. If the PHP setup is about Web (not CLI), and has been configured so that it can treat an
268-
infinite number of requests, thus your RINIT()/RSHUTDOWN() couple will be called an infinite amount of time.
269+
infinite number of requests, thus your ``RINIT()/RSHUTDOWN()`` couple will be called an infinite amount of time.
269270

270271
We'd like to once more get your attention about memory management. The little tiny byte you'll eventually leak while
271272
treating a request (between ``RINIT()`` and ``RSHUTDOWN()``) will have dramatic consequences on fully loaded servers.
273+
That's why you are advised to use :doc:`Zend Memory Manager <../memory_management/zend_memory_manager>` for such
274+
allocations. PHP will forget and free the request memory at the end of every request as part of the share-nothing
275+
architecture, that's PHP's internal design.
272276

273277
Also, if you crash with a SIGSEGV signal (bad memory access), you crash the entire process. If the PHP setup was using
274278
threads as multi-processing engine, then you crash every other thread with you, and could even crash the webserver.
275279

276280
.. note:: The C language is not the PHP language. Using C, errors and mistakes in your program will highly probably
277281
lead to a program crash and termination.
278282

279-
.. todo: Add a chapter about debugging. Add a chapter about memory leak tracking.
280-
281283
Hooking by overwritting function pointers
282284
*****************************************
283285

Book/php7/internal_types/zend_resources.rst

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ The Resource type: zend_resource
33

44
Even though PHP could really get rid of the "resource" type, because
55
:doc:`custom object storage <./classes_objects/custom_object_storage>` allows to build a PHP representation of any
6-
abstract kind of data, that resource type still exists in PHP, and you may need to deal with it.
6+
abstract kind of data, that resource type still exists in the current version of PHP, and you may need to deal with it.
77

88
If you need to create resources, we really would like to push you not to, but instead use objects and their
99
:doc:`custom storage <./classes_objects/custom_object_storage>` management. Objects is the PHP type that can embed
@@ -41,7 +41,7 @@ The ``type`` is used to regroup resources of the same type together. This is abo
4141
how they are fetched back from their handle.
4242

4343
Finally, the ``ptr`` field in ``zend_resource`` is your abstract data. Remember resources are about storing an abstract
44-
data that cannot fit in any data type PHP can represent natively.
44+
data that cannot fit in any data type PHP can represent natively (but objects could, like we said earlier).
4545

4646
Resource types and resource destruction
4747
---------------------------------------
@@ -61,12 +61,13 @@ Destructors are grouped by types, so are resources themselves. You won't apply t
6161
There also exists two kinds of resources, here again differenciated about their lifetime.
6262

6363
* Classical resources, the most used ones, do not persist across several requests, their destructor is called at
64-
request shutdown
64+
request shutdown.
6565
* Persistent resources will persist across several requests and will only get destroyed when the PHP process dies.
6666

6767
.. note:: You may be interested by :doc:`the PHP lifecycle <../extensions_design/php_lifecycle>` chapter that shows you
68-
the different steps occuring in PHP's process life.
69-
68+
the different steps occuring in PHP's process life. Also, the
69+
:doc:`Zend Memory Manager chapter <../memory_management/zend_memory_manager>` may help in understanding
70+
concepts of persistent and request-bound memory allocations.
7071

7172
Playing with resources
7273
----------------------
@@ -145,6 +146,10 @@ connection") into the same resource table. Resource types have names, so that th
145146
debug statement (like a ``var_dump($my_resource)``), and they also are represented as an integer used internaly to
146147
fetch back the resource pointer from it, and to register a destructor with the resource type.
147148

149+
.. note:: Like you can see, if we would have used objects, those represent types by themselves, and there would'nt have
150+
to happen that step of fetching back a resource from its identifier verifying its type. Objects are
151+
self-describing types. But resources are still a valid data type for the current PHP version.
152+
148153
Reference counting resources
149154
----------------------------
150155

Book/php7/memory_management/memory_debugging.rst

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -367,13 +367,10 @@ Finally here is a last example to show a use-after-free scenario. This is also a
367367
which is as bad as bad-memory-access: it creates security flaws that can lead to very nasty behaviors.
368368
Obviously, valgrind can detect use-after-free. Here is one::
369369

370-
PHP_MINIT_FUNCTION(pib)
371-
{
372-
char *foo = strdup("foo");
373-
free(foo);
370+
char *foo = strdup("foo");
371+
free(foo);
374372

375-
memcpy(foo, "foo", sizeof("foo"));
376-
}
373+
memcpy(foo, "foo", sizeof("foo"));
377374

378375
Here again, a PHP scenario that has nothing to do with PHP but still. We free a pointer, and reuse it after. This is a
379376
big mistake. Let's ask valgrind::

0 commit comments

Comments
 (0)