PHP's two-pass compiler
On my way to Istanbul I was looking at Xdebug bug #422 . For some reason Xdebug was crashing while doing code-coverage analysis, in the part that analyses which code was dead (ie. opcodes that could never be reached). The crash occurred with a JMPZ (jump-if-zero) instruction, that suddenly saw a jump-to position of 572222864. That position resembles more a jump-address.
Xdebug uses the same branch analysis implementation as VLD so I used the latter tool to find out why it would crash. Unfortunately, it was working just all nice and fine with VLD. After digging around some more, I saw from the back trace that the crash in Xdebug only occurred when a user-defined error-handler was called while parsing a file. The latter gave me the insight of looking at which phase the compiler was in. I remembered that PHP has a two phase compiler. The first pass is quick and dirty, and only records the opcode line number to jump to. Xdebug however was expecting an memory address as jump target. Because a memory address is a much larger number than an opcode number—the latter usually not being much higher than a thousand—Xdebug was setting the "visited" flag in a part of memory that wasn't allocated. And writing to unallocated memory makes a process die with a segmentation fault.
The compiler in PHP is two-pass. During the first pass, it will find out to which opcode it needs to jump in the jump instructions. However, the PHP engine (and Xdebug) expects a memory address to jump to while executing your script. In the second pass, the compiler will then go over the generated opcodes and calculate the memory address to jump to from the jumps to opcode numbers. It will also do a few other things, such as collapsing sequential EXT_STMT opcodes, calling Zend extension's functions to finalize the opcode arrays—Xdebug uses this for caching whether an opcode array has been scanned already—and re-allocating the opcode array itself to save space.
Now, the thing is, that usually VLD and Xdebug kick in after the whole opcode array has been created, which includes running the second pass of the compiler. However, Xdebug also tries to analyze opcode arrays while executing them. In the case of a user defined error handler, that happens before the second pass has been run. Preventing the crash was therefore as easy as making sure that the compiler's second pass had been run while scanning the opcode arrays for executable code.
Life Line
Created a bench and a crossing
Created 2 benches and a crossing; Updated 5 cushions, a post_box, and a crossing
Created a crossing; Updated a cushion
If you were wondering whether the www.php.net & downloads.php.net services weren't responding very well in the last 6 hours — thousands of requests/sec to https://www.php.net/ 's root.
The server's load was 720, didn't die, but CDN connections to it timed out.
Now there is a caching strategy in place for a selected set of resources.
Updated a bench
Created 3 benches; Updated 10 benches
Updated a bench
Updated a bus_stop
Created a bench and a waste_basket; Updated 6 bus_stops and a crossing
Created 2 waste_baskets and a recycling; Updated 2 bicycle_parkings and a recycling
Updated a fast_food, a funeral_directors shop, and 2 other objects; Confirmed a fast_food and a hairdresser shop
Created an information; Updated 3 benches and 2 waste_baskets
Updated 2 benches and a waste_basket
Updated a bench
Created a waste_basket and an information
Created a waste_basket
I hiked 18.0km in 4h1m52s
I walked 1.4km in 17m19s
I walked 4.5km in 1h21m49s
I just made and ate, a bowl full of bacon fried Brussels Sprouts. Not under duress, and out of my own free will.
Added new residential building
Created a hairdresser shop; Confirmed a convenience shop and a dry_cleaning shop
Created a building_materials shop, a vacant shop, and 4 other objects; Confirmed a hairdresser shop, a cafe, and 2 other objects
I walked 8.3km in 1h33m44s



Shortlink
This article has a short URL available: https://drck.me/ptc-6za