Understanding static
analysis
AmsterdamPHP 2018, The Netherlands
Agenda
What is static analysis
How does it work
How can you take advantage of it
Damien Seguy
CTO at Exakat
Static analysis engine for PHP
Ik ben een boterham
Code review
Reading on the IDE
Automated code
review
L'analyse statique, c'est quoi?
Manual
code review
Higher
abstraction
Simple review
Systématic
Synopsis
Convert PHP code into data
Add more knowledge
Query the internal database
Static analysis
Performences
Hosting
Sécurity
Migration 7.3
 
 

Tokenization
Memory
Source
Audits
An extra step
Opcode cache
Optimization
Text file
Tokens / syntax
Static analysis
Execution
PHP Tokens
token_get_all()
Comments, phpDoc, spaces
Delimiters : ' " () {} [] `
2 tokens out of 3 are ignored
[248] => Array
(
[0] => 382
[1] =>
[2] => 167
)
[249] => Array
(
[0] => 319
[1] => define
[2] => 167
)
[250] => (
[251] => Array
(
[0] => 323
[1] => 'EXT'
[2] => 167
)
[252] => ,
[253] => Array
(
[0] => 382
[1] =>
[2] => 167
)
<?php
//....
define('EXT', '.php');
AST
<?php
$a = 1;
echo foo($a);
function foo($b) {
  if ($b % 2) {
    return $b / 2;
  } else {
    return $b * 3 + 1;
  }
}
Abstract
syntactic
tree
Playing in the tree
Spot PHP features
Variable, Function,
Addition, Multiplication,
Ifthen, Return
Jouons dans l'arbre
Parameters, Variables
Properties, array variable,
object variable, static
property name, global
variable, static variable
Everything is a T_VARIABLE
Parameter counts
Local variable counts
0,00
25,00
50,00
75,00
100,00
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
Play in the tree
Local patterns
$a = 1 + $b;
$a = $a + 1;
$z = $a + $b + $d - $b + $e;
Assignat
ion
$a Addition
LEFT RIGHT
$b
RIGHT
1
LEFT
Motif
Analyse
Analyse
PHP is not a tree
Link definitions with usage
Definitions and usage
Function and functioncalls
class X and instantiations
define('A') or const A and A or A
$variable and their hidden definition
AST
<?php
$a = 1;
echo foo($a);
function foo($b) {
  if ($b % 2) {
    return $b / 2;
  } else {
    return $b * 3 + 1;
  }
}
Définitions et usages
Definitions and usage
Function definition
Parameters
Functioncall
Arguments
Context
foo($a)
DEFINITION
function
foo($b)
$a = 2 NEXT echo $a
Sequence
NEXT
BLOCK
Definitions and usage
Function definition
Parameters
Functioncall
Arguments
Context
foo($a[1])
DEFINITION
function
foo($b)
foo($b)
foo($a)
foo($a)
DEFINITION
DEFINITION
DEFINITION
Definitions and usage
Function definition
Parameters
Functioncall
Arguments
Context
foo($a[1])
DEFINITION
function
foo($b)
What is that?
One function call,
Multiple definition?
function
foo($b)
DEFINITION
foo($a)
function
foo($b)
function
foo($b)
function
foo($b)
DEFINITION
DEFINITION
DEFINITION
Dead Function
<?php
function morte($z) { }
Dead Function
A function without
outgoing DEFINITION
function
foo($b)
Dead Function
<?php
function morte($z) { }
Dead Function (hard)
<?php
function morte($z) { }
Dead Function (Hard)
The linear propagation of
death
function
foob($b)
function
fooa($b)
DEFINITION
Dead Function (very hard)
<?php
function morte($z) { }
Dead Function (very hard)
Fonctions récursives
function
foo($b)
foo($a)
BLOCK
DEFINITION
EXPRESSION
Sequence
Dead Function (hardest)
<?php
function morte($z) { }
Dead Function (hardest)
Recursive, level 2
Recursive, level 3
Recursive, level 4…
DEFINITION
function
foob($b)
function
fooa($b)
DEFINITION
Dead Function (hardest)
Recursive, level 2
Recursive, level 3
Recursive, level 4…
DEFINITION
function
foob($b)
function
fooa($b)
DEFINITION
function
fooc($b)
DEFINITION
Dead functions
No call to the function
Function called by dead functions
Recursive functions
Level 2+ recursive functions
What fresh hell is that?
Call to function without
definition ?
bar($a)
Functions without definition
PHP extensions
Components
Higher level of abstraction
Components
PHP Extensions
Code PHP
Identifying extensions
API
Functions, constants
Classes, interfaces, traits, namespaces
Configuration directives
Natives extensions, PECL, independent
Extension usage
Removed extensions
APC, posix
Added extensions
AST, libsodium
Extension evolution
Nouvelle classe, fonctions obsolètes, corrections…
APIExt
PHP
code
Recognizing extensions
Recognizing frameworks
Identify a component
Component
Versions
Classes
Méthods
Arguments
Identify a component
Code
repository
Versions
Static
analysis
Compatibility
phpJuicer
Compatibility
Check the API version
Check the correct usage of the API
Validate contextual usage :
error_reporting(E_ERROR | E_WARNING | E_PARSE)
Creating an analysis
Beyond the docs
Coach your users
Usage stats
Compatibility usage
Suggestions
Bedankt
@exakat / https://www.exakat.io/
Understanding static analysis   php amsterdam 2018

Understanding static analysis php amsterdam 2018