1

I am trying to build an extension for PHP. After following Sara Golemon's book I have a basic extension which I can compile as a shared module and, in addition, I can compile it statically along PHP itself.

Now I want to modify the PHP interpreter in order to intercept particular internal function invocations and communicate these calls to my extension. I want to do this only when my extension is statically compiled with PHP---the interpreter build process should otherwise generate an unmodified PHP binary. My understanding is that I should use the C preprocessor. However, to achieve my goal I need a preprocessor flag that will only be raised when PHP is configured to compile with my extension (i.e. ./configure --enable-myextension). Unfortunately, I cannot find such a flag nor one seems to be set by the configure script.

I should say here that I have tried setting preprossessor flags within my extension's code but this will not work. My extension is first touched late in the build process (i.e. roughly after the core of the interpreter) and the flags I set there are not active when the bulk of interpreter code is being compiled.

Any thoughts? Do the above sound reasonable?

Shiplu Mokaddim
  • 56,364
  • 17
  • 141
  • 187
Yiannis
  • 880
  • 1
  • 7
  • 14

1 Answers1

0

My understanding is that I should use the C preprocessor.

Nope, you don't need that.

I need a preprocessor flag that will only be raised when PHP is configured to compile with my extension

Why would you want that? It would basically limit the functionality of your extension artificially, although it's possible to hook function calls no matter how your extension is compiled.

Do the above sound reasonable?

In my opinion, it's not reasonable. Please have a look at how AOP hooks function calls: https://github.com/AOP-PHP/AOP

If you need to hook more than just function calls, you need to reach down at the lowest level, the opcodes, by using zend_set_user_opcode_handler(). Please use lxr.php.net or similar tools (fgrep, etc) to find out where and how such handlers are used. I know laruence was working hard on an interesting extension last year here: http://svn.php.net/viewvc/pecl/taint/trunk/taint.c?view=markup so I would take that as the most "up to date" way of doing things as a reference, if anything has changed in the meanwhile.

Flavius
  • 13,566
  • 13
  • 80
  • 126
  • Thanks for the pointer to the AOP-PHP extension! Last time that I checked no such thing existed. I looked at its code. To be able to intercept function invocations, it hijacks a method in the Zend interpreter (zend_vm_execute.h:execute) that implements function invocations. It's a very clean/nice solution. However, it seems limited to support join points around function calls only. Can I use this technique, for example, to intercept string operations which do not involve function calls (e.g. via === or >)? If you don't know, is there any documentation for the Zend engine internals? – Yiannis Jan 30 '13 at 18:41
  • @Papajohn Unfortunately there's not lots of documentation beside the code itself, but the internals guys do usually help (they've helped me as I help you now in return for their investment in me). Please see my updated answer, I hope it helps. Whenever you have questions, feel free to ask (I am watching the zend-engine tag and I'll give my best if I know the answer). Thanks for the interesting questions and good luck! – Flavius Jan 30 '13 at 19:03
  • Hey hey hey, taint tracking (albeit with a cool twist) is exactly what I am trying to implement! Thanks A LOT for the second great pointer! – Yiannis Jan 30 '13 at 19:15
  • @Papajohn Glad I could help. Then you don't have to start from scratch, get in touch with laruence and send patches for whatever you're missing. Also, it would be good to take a slow look over the existing extensions in PECL, it kind of helps to gain a better overview of the ecosystem and where PHP is heading. – Flavius Jan 30 '13 at 19:21