ush.it - a beautiful place

Architecture detection by PHP anomaly

August 22, 2007 at 1:09 am - Filed under Hacks, Language EN - 595 words, reading time ~1 minutes - Permalink - Comments

Sometimes it's right to enjoy a more relaxed entry.

php -r 'echo ((123234234234234%134==-14)?32:64)."bit";'
echo -e "php: `php -r 'echo (123234234234234%134);'`\npython: `python -c 'print 123234234234234%134;'`"

For those who didn't get it: the % operand must always return positive numbers, in this case PHP on 32bit architectures returns -14. On amd64 and other 64 bit systems it works as respected, while on MAC, regardless if it's INTEL(x86_64) or PPC, it returns -1. Don't ask me why : ) Test on your computer it's 100% reliable!

An advanced version is:

php -r '$a=(123234234234234%134);if($a==78)$r="amd64";if($a==-14)$r="x86";if($a==-1)$r="x86_64/ppc";echo $r;'

Want to add some signatures, mail me or add a comment? With the help of some friends I collected these informations:

$ php -r 'echo (123234234234234%134);'
-14

PHP 4.4.8_pre20070816-pl0-gentoo with Suhosin-Patch 0.9.6 (cli) (built: XXX)
Copyright (c) 1997-2007 The PHP Group
Zend Engine v1.3.0, Copyright (c) 1998-2004 Zend Technologies
with Suhosin v0.9.17, Copyright (c) 2002-2006, by Hardened-PHP Project
x86, detected as 32bit

$ php -r 'echo (123234234234234%134);'
-14

PHP 5.2.4_pre200708051230-pl2-gentoo (cli) (built: Aug XXX)
Copyright (c) 1997-2007 The PHP Group
Zend Engine v2.2.0, Copyright (c) 1998-2007 Zend Technologies
x86, detected as 32bit

$ php -r 'echo (123234234234234%134);'
78

PHP 5.2.3-1+b1 (cli) (built: XXX)
Copyright (c) 1997-2007 The PHP Group
Zend Engine v2.2.0, Copyright (c) 1998-2007 Zend Technologies
amd64, detected as 64bit

$ php -r 'echo (123234234234234%134);'
-14

PHP 5.2.1 (cli) (built: XXX)
Copyright (c) 1997-2007 The PHP Group
Zend Engine v2.2.0, Copyright (c) 1998-2007 Zend Technologies
x86, detected as 32bit

$ php -r 'echo (123234234234234%134);'
-14

PHP 5.2.0-8+etch7 (cli) (built: XXX)
Copyright (c) 1997-2006 The PHP Group
Zend Engine v2.2.0, Copyright (c) 1998-2006 Zend Technologies
x86, detected as 32bit

$ php -r 'echo (123234234234234%134);'
-1

PHP 4.4.7 (cli) (built: Jul 10 2007 10:54:32)
Copyright (c) 1997-2007 The PHP Group
Zend Engine v1.3.0, Copyright (c) 1998-2004 Zend Technologies
x86_64 (INTEL core duo), detected as mac

Do you have a system with an architecture != that x86, x86_64, amd64 and PPC? Just run the following script and post the results using the comments :) Don't forget to specify the PHP banner (php -v) and the arch!

php -r 'echo (123234234234234%134);'

Update: this is a known issue, http://bugs.php.net/bug.php?id=39610 and http://www.php.net/manual/en/language.operators.arithmetic.php.

Operands of modulus are converted to integers (by stripping the decimal part) before processing.
Note: Remainder $a % $b is negative for negative $a.

$ php -r 'echo PHP_INT_MAX;'
2147483647

It the PHP_INT_MAX constant is missing you can use the following script (found on php-general and modified for faster execution) to calculate it:

set_time_limit(0);
$s = 2100000000;
$i=$s;
$c=0;
while(is_int($i)) {
 $i=$i+100;
 $c++;
}
$t=$s+($c-1)*100;
while(is_int($t)) $t++;
echo $t;

Since this is a feature of PHP the architecture detection will probably work on future versions too.

THP USH Wisec DigitalBullets