2018-02-04 00:52:35 +01:00
|
|
|
<?php
|
2018-11-12 16:46:55 +01:00
|
|
|
namespace Psalm\Internal\Codebase;
|
2018-02-04 00:52:35 +01:00
|
|
|
|
2019-07-05 22:24:00 +02:00
|
|
|
use function array_shift;
|
|
|
|
use function assert;
|
|
|
|
use function count;
|
2019-12-28 01:06:09 +01:00
|
|
|
use function file_exists;
|
2019-06-15 22:10:48 +02:00
|
|
|
use PhpParser;
|
|
|
|
use Psalm\Codebase;
|
2019-02-07 18:25:57 +01:00
|
|
|
use Psalm\Internal\Analyzer\ProjectAnalyzer;
|
2019-06-15 22:10:48 +02:00
|
|
|
use Psalm\Internal\Analyzer\TypeAnalyzer;
|
2019-07-05 22:24:00 +02:00
|
|
|
use Psalm\Storage\FunctionLikeParameter;
|
2018-02-04 00:52:35 +01:00
|
|
|
use Psalm\Type;
|
2019-06-15 22:10:48 +02:00
|
|
|
use Psalm\Type\Atomic\TCallable;
|
2019-06-26 22:52:29 +02:00
|
|
|
use function strtolower;
|
|
|
|
use function substr;
|
2019-12-28 01:06:09 +01:00
|
|
|
use function version_compare;
|
2018-02-04 00:52:35 +01:00
|
|
|
|
2018-02-09 23:51:49 +01:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*
|
|
|
|
* Gets values from the call map array, which stores data about native functions and methods
|
|
|
|
*/
|
2018-02-04 00:52:35 +01:00
|
|
|
class CallMap
|
|
|
|
{
|
2019-02-07 18:25:57 +01:00
|
|
|
const PHP_MAJOR_VERSION = 7;
|
|
|
|
const PHP_MINOR_VERSION = 3;
|
2019-12-28 01:06:09 +01:00
|
|
|
const LOWEST_AVAILABLE_DELTA = 71;
|
2019-02-07 18:25:57 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var ?int
|
|
|
|
*/
|
|
|
|
private static $loaded_php_major_version = null;
|
|
|
|
/**
|
|
|
|
* @var ?int
|
|
|
|
*/
|
|
|
|
private static $loaded_php_minor_version = null;
|
|
|
|
|
2018-02-04 00:52:35 +01:00
|
|
|
/**
|
2019-12-27 16:34:51 +01:00
|
|
|
* @var array<array<int|string,string>>|null
|
2018-02-04 00:52:35 +01:00
|
|
|
*/
|
|
|
|
private static $call_map = null;
|
|
|
|
|
2019-06-15 22:10:48 +02:00
|
|
|
/**
|
|
|
|
* @var array<array<int, TCallable>>|null
|
|
|
|
*/
|
|
|
|
private static $call_map_callables = [];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $method_id
|
|
|
|
* @param array<int, PhpParser\Node\Arg> $args
|
|
|
|
*
|
|
|
|
* @return TCallable
|
|
|
|
*/
|
2019-11-25 17:44:54 +01:00
|
|
|
public static function getCallableFromCallMapById(
|
|
|
|
Codebase $codebase,
|
|
|
|
$method_id,
|
|
|
|
array $args,
|
|
|
|
?\Psalm\Internal\Provider\NodeDataProvider $nodes
|
|
|
|
) {
|
2019-06-15 22:10:48 +02:00
|
|
|
$possible_callables = self::getCallablesFromCallMap($method_id);
|
|
|
|
|
|
|
|
if ($possible_callables === null) {
|
|
|
|
throw new \UnexpectedValueException(
|
|
|
|
'Not expecting $function_param_options to be null for ' . $method_id
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-11-25 17:44:54 +01:00
|
|
|
return self::getMatchingCallableFromCallMapOptions(
|
|
|
|
$codebase,
|
|
|
|
$possible_callables,
|
|
|
|
$args,
|
|
|
|
$nodes
|
|
|
|
);
|
2019-06-15 22:10:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array<int, TCallable> $callables
|
|
|
|
* @param array<int, PhpParser\Node\Arg> $args
|
|
|
|
*
|
|
|
|
* @return TCallable
|
|
|
|
*/
|
|
|
|
public static function getMatchingCallableFromCallMapOptions(
|
|
|
|
Codebase $codebase,
|
|
|
|
array $callables,
|
2019-11-25 17:44:54 +01:00
|
|
|
array $args,
|
|
|
|
?\Psalm\NodeTypeProvider $nodes
|
2019-06-15 22:10:48 +02:00
|
|
|
) {
|
|
|
|
if (count($callables) === 1) {
|
|
|
|
return $callables[0];
|
|
|
|
}
|
|
|
|
|
2019-09-08 20:44:59 +02:00
|
|
|
$matching_param_count_callable = null;
|
|
|
|
|
2019-06-15 22:10:48 +02:00
|
|
|
foreach ($callables as $possible_callable) {
|
|
|
|
$possible_function_params = $possible_callable->params;
|
|
|
|
|
|
|
|
assert($possible_function_params !== null);
|
|
|
|
|
|
|
|
$all_args_match = true;
|
|
|
|
|
|
|
|
$last_param = count($possible_function_params)
|
|
|
|
? $possible_function_params[count($possible_function_params) - 1]
|
|
|
|
: null;
|
|
|
|
|
|
|
|
$mandatory_param_count = count($possible_function_params);
|
|
|
|
|
|
|
|
foreach ($possible_function_params as $i => $possible_function_param) {
|
|
|
|
if ($possible_function_param->is_optional) {
|
|
|
|
$mandatory_param_count = $i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($mandatory_param_count > count($args) && !($last_param && $last_param->is_variadic)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($args as $argument_offset => $arg) {
|
|
|
|
if ($argument_offset >= count($possible_function_params)) {
|
|
|
|
if (!$last_param || !$last_param->is_variadic) {
|
|
|
|
$all_args_match = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
$function_param = $last_param;
|
|
|
|
} else {
|
|
|
|
$function_param = $possible_function_params[$argument_offset];
|
|
|
|
}
|
|
|
|
|
|
|
|
$param_type = $function_param->type;
|
|
|
|
|
|
|
|
if (!$param_type) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-11-25 17:44:54 +01:00
|
|
|
$arg_type = null;
|
|
|
|
|
|
|
|
if (!$nodes
|
|
|
|
|| !($arg_type = $nodes->getType($arg->value))
|
|
|
|
) {
|
2019-06-15 22:10:48 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($arg_type->hasMixed()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($arg->unpack && !$function_param->is_variadic) {
|
|
|
|
if ($arg_type->hasArray()) {
|
2019-10-01 21:44:43 +02:00
|
|
|
/**
|
2019-11-11 16:11:42 +01:00
|
|
|
* @psalm-suppress PossiblyUndefinedStringArrayOffset
|
2019-10-11 02:16:43 +02:00
|
|
|
* @var Type\Atomic\TArray|Type\Atomic\ObjectLike|Type\Atomic\TList
|
2019-10-01 21:44:43 +02:00
|
|
|
*/
|
2020-01-04 18:20:26 +01:00
|
|
|
$array_atomic_type = $arg_type->getAtomicTypes()['array'];
|
2019-06-15 22:10:48 +02:00
|
|
|
|
|
|
|
if ($array_atomic_type instanceof Type\Atomic\ObjectLike) {
|
2019-10-11 02:16:43 +02:00
|
|
|
$arg_type = $array_atomic_type->getGenericValueType();
|
|
|
|
} elseif ($array_atomic_type instanceof Type\Atomic\TList) {
|
|
|
|
$arg_type = $array_atomic_type->type_param;
|
|
|
|
} else {
|
|
|
|
$arg_type = $array_atomic_type->type_params[1];
|
2019-06-15 22:10:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (TypeAnalyzer::isContainedBy(
|
|
|
|
$codebase,
|
|
|
|
$arg_type,
|
|
|
|
$param_type,
|
|
|
|
true,
|
|
|
|
true
|
|
|
|
)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$all_args_match = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2019-09-08 20:44:59 +02:00
|
|
|
if (count($args) === count($possible_function_params)) {
|
|
|
|
$matching_param_count_callable = $possible_callable;
|
|
|
|
}
|
|
|
|
|
2019-06-15 22:10:48 +02:00
|
|
|
if ($all_args_match) {
|
|
|
|
return $possible_callable;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-08 20:44:59 +02:00
|
|
|
if ($matching_param_count_callable) {
|
|
|
|
return $matching_param_count_callable;
|
|
|
|
}
|
|
|
|
|
2019-06-15 22:10:48 +02:00
|
|
|
// if we don't succeed in finding a match, set to the first possible and wait for issues below
|
|
|
|
return $callables[0];
|
|
|
|
}
|
|
|
|
|
2018-02-04 00:52:35 +01:00
|
|
|
/**
|
|
|
|
* @param string $function_id
|
|
|
|
*
|
|
|
|
* @return array|null
|
2019-06-15 22:10:48 +02:00
|
|
|
* @psalm-return array<int, TCallable>|null
|
2018-02-04 00:52:35 +01:00
|
|
|
*/
|
2019-06-15 22:10:48 +02:00
|
|
|
public static function getCallablesFromCallMap($function_id)
|
2018-02-04 00:52:35 +01:00
|
|
|
{
|
2020-02-15 02:54:26 +01:00
|
|
|
$call_map_key = strtolower($function_id);
|
|
|
|
|
|
|
|
if (isset(self::$call_map_callables[$call_map_key])) {
|
|
|
|
return self::$call_map_callables[$call_map_key];
|
2019-06-15 22:10:48 +02:00
|
|
|
}
|
|
|
|
|
2018-02-04 00:52:35 +01:00
|
|
|
$call_map = self::getCallMap();
|
|
|
|
|
|
|
|
if (!isset($call_map[$call_map_key])) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$call_map_functions = [];
|
|
|
|
$call_map_functions[] = $call_map[$call_map_key];
|
|
|
|
|
|
|
|
for ($i = 1; $i < 10; ++$i) {
|
|
|
|
if (!isset($call_map[$call_map_key . '\'' . $i])) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
$call_map_functions[] = $call_map[$call_map_key . '\'' . $i];
|
|
|
|
}
|
|
|
|
|
2019-06-15 22:10:48 +02:00
|
|
|
$possible_callables = [];
|
2018-02-04 00:52:35 +01:00
|
|
|
|
|
|
|
foreach ($call_map_functions as $call_map_function_args) {
|
2019-06-15 22:10:48 +02:00
|
|
|
$return_type_string = array_shift($call_map_function_args);
|
|
|
|
|
|
|
|
if (!$return_type_string) {
|
|
|
|
$return_type = Type::getMixed();
|
|
|
|
} else {
|
|
|
|
$return_type = Type::parseString($return_type_string);
|
|
|
|
}
|
2018-02-04 00:52:35 +01:00
|
|
|
|
2019-05-21 17:51:41 +02:00
|
|
|
$function_params = [];
|
2018-02-04 00:52:35 +01:00
|
|
|
|
2019-08-14 01:18:50 +02:00
|
|
|
$arg_offset = 0;
|
|
|
|
|
2018-02-04 00:52:35 +01:00
|
|
|
/** @var string $arg_name - key type changed with above array_shift */
|
|
|
|
foreach ($call_map_function_args as $arg_name => $arg_type) {
|
|
|
|
$by_reference = false;
|
|
|
|
$optional = false;
|
|
|
|
$variadic = false;
|
|
|
|
|
|
|
|
if ($arg_name[0] === '&') {
|
|
|
|
$arg_name = substr($arg_name, 1);
|
|
|
|
$by_reference = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (substr($arg_name, -1) === '=') {
|
|
|
|
$arg_name = substr($arg_name, 0, -1);
|
|
|
|
$optional = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (substr($arg_name, 0, 3) === '...') {
|
|
|
|
$arg_name = substr($arg_name, 3);
|
|
|
|
$variadic = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
$param_type = $arg_type
|
|
|
|
? Type::parseString($arg_type)
|
|
|
|
: Type::getMixed();
|
|
|
|
|
2019-05-21 17:51:41 +02:00
|
|
|
$function_param = new FunctionLikeParameter(
|
2018-02-04 00:52:35 +01:00
|
|
|
$arg_name,
|
|
|
|
$by_reference,
|
|
|
|
$param_type,
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
$optional,
|
|
|
|
false,
|
|
|
|
$variadic
|
|
|
|
);
|
2019-05-21 17:51:41 +02:00
|
|
|
|
2019-08-14 01:18:50 +02:00
|
|
|
if ($arg_offset === 0
|
2020-02-15 02:54:26 +01:00
|
|
|
&& ($call_map_key === 'exec'
|
|
|
|
|| $call_map_key === 'shell_exec'
|
|
|
|
|| $call_map_key === 'passthru'
|
|
|
|
|| $call_map_key === 'system'
|
|
|
|
|| $call_map_key === 'pcntl_exec'
|
|
|
|
|| $call_map_key === 'file_put_contents'
|
|
|
|
|| $call_map_key === 'fopen')
|
2019-08-14 01:18:50 +02:00
|
|
|
) {
|
|
|
|
$function_param->sink = Type\Union::TAINTED_INPUT_SHELL;
|
|
|
|
}
|
|
|
|
|
2019-10-18 21:28:15 +02:00
|
|
|
if ($arg_offset === 0
|
2020-02-15 02:54:26 +01:00
|
|
|
&& ($call_map_key === 'print_r')
|
2019-10-18 21:28:15 +02:00
|
|
|
) {
|
|
|
|
$function_param->sink = Type\Union::TAINTED_INPUT_HTML;
|
|
|
|
}
|
|
|
|
|
2019-05-21 17:51:41 +02:00
|
|
|
$function_param->signature_type = null;
|
|
|
|
|
|
|
|
$function_params[] = $function_param;
|
2019-08-14 01:18:50 +02:00
|
|
|
|
|
|
|
$arg_offset++;
|
2018-02-04 00:52:35 +01:00
|
|
|
}
|
|
|
|
|
2019-06-15 22:10:48 +02:00
|
|
|
$possible_callables[] = new TCallable('callable', $function_params, $return_type);
|
2018-02-04 00:52:35 +01:00
|
|
|
}
|
|
|
|
|
2020-02-15 02:54:26 +01:00
|
|
|
self::$call_map_callables[$call_map_key] = $possible_callables;
|
2018-02-04 15:22:24 +01:00
|
|
|
|
2019-06-15 22:10:48 +02:00
|
|
|
return $possible_callables;
|
2018-02-04 00:52:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the method/function call map
|
|
|
|
*
|
|
|
|
* @return array<string, array<int|string, string>>
|
|
|
|
* @psalm-suppress MixedInferredReturnType as the use of require buggers things up
|
2018-07-05 16:11:04 +02:00
|
|
|
* @psalm-suppress MixedTypeCoercion
|
2019-02-07 18:25:57 +01:00
|
|
|
* @psalm-suppress MixedReturnStatement
|
2018-02-04 00:52:35 +01:00
|
|
|
*/
|
|
|
|
public static function getCallMap()
|
|
|
|
{
|
2019-02-07 18:25:57 +01:00
|
|
|
$codebase = ProjectAnalyzer::getInstance()->getCodebase();
|
|
|
|
$analyzer_major_version = $codebase->php_major_version;
|
|
|
|
$analyzer_minor_version = $codebase->php_minor_version;
|
|
|
|
|
2019-12-28 01:06:09 +01:00
|
|
|
$analyzer_version = $analyzer_major_version . '.' . $analyzer_minor_version;
|
|
|
|
$current_version = self::PHP_MAJOR_VERSION . '.' . self::PHP_MINOR_VERSION;
|
|
|
|
|
|
|
|
$analyzer_version_int = (int) ($analyzer_major_version . $analyzer_minor_version);
|
|
|
|
$current_version_int = (int) (self::PHP_MAJOR_VERSION . self::PHP_MINOR_VERSION);
|
|
|
|
|
2019-02-07 18:25:57 +01:00
|
|
|
if (self::$call_map !== null
|
|
|
|
&& $analyzer_major_version === self::$loaded_php_major_version
|
|
|
|
&& $analyzer_minor_version === self::$loaded_php_minor_version
|
|
|
|
) {
|
2018-02-04 00:52:35 +01:00
|
|
|
return self::$call_map;
|
|
|
|
}
|
|
|
|
|
2018-02-22 00:59:31 +01:00
|
|
|
/** @var array<string, array<int|string, string>> */
|
2019-02-07 18:25:57 +01:00
|
|
|
$call_map = require(__DIR__ . '/../CallMap.php');
|
2018-02-04 00:52:35 +01:00
|
|
|
|
|
|
|
self::$call_map = [];
|
|
|
|
|
|
|
|
foreach ($call_map as $key => $value) {
|
|
|
|
$cased_key = strtolower($key);
|
|
|
|
self::$call_map[$cased_key] = $value;
|
|
|
|
}
|
|
|
|
|
2019-12-28 01:06:09 +01:00
|
|
|
if (version_compare($analyzer_version, $current_version, '<')) {
|
|
|
|
// the following assumes both minor and major versions a single digits
|
|
|
|
for ($i = $current_version_int; $i > $analyzer_version_int && $i >= self::LOWEST_AVAILABLE_DELTA; --$i) {
|
|
|
|
$delta_file = __DIR__ . '/../CallMap_' . $i . '_delta.php';
|
|
|
|
if (!file_exists($delta_file)) {
|
|
|
|
continue;
|
|
|
|
}
|
2019-02-07 18:25:57 +01:00
|
|
|
/**
|
|
|
|
* @var array{
|
|
|
|
* old: array<string, array<int|string, string>>,
|
|
|
|
* new: array<string, array<int|string, string>>
|
|
|
|
* }
|
|
|
|
* @psalm-suppress UnresolvableInclude
|
|
|
|
*/
|
2019-12-28 01:06:09 +01:00
|
|
|
$diff_call_map = require($delta_file);
|
2019-02-07 18:25:57 +01:00
|
|
|
|
|
|
|
foreach ($diff_call_map['new'] as $key => $_) {
|
|
|
|
$cased_key = strtolower($key);
|
|
|
|
unset(self::$call_map[$cased_key]);
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($diff_call_map['old'] as $key => $value) {
|
|
|
|
$cased_key = strtolower($key);
|
|
|
|
self::$call_map[$cased_key] = $value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
self::$loaded_php_major_version = $analyzer_major_version;
|
|
|
|
self::$loaded_php_minor_version = $analyzer_minor_version;
|
|
|
|
|
2018-02-04 00:52:35 +01:00
|
|
|
return self::$call_map;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $key
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public static function inCallMap($key)
|
|
|
|
{
|
|
|
|
return isset(self::getCallMap()[strtolower($key)]);
|
|
|
|
}
|
|
|
|
}
|