Exemplo n.º 1
0
function ast_node_type($file, $node, $namespace)
{
    global $namespace_map;
    if ($node instanceof \ast\Node) {
        switch ($node->kind) {
            case \ast\AST_NAME:
                $result = qualified_name($file, $node, $namespace);
                break;
            case \ast\AST_TYPE:
                if ($node->flags == \ast\flags\TYPE_CALLABLE) {
                    $result = 'callable';
                } else {
                    if ($node->flags == \ast\flags\TYPE_ARRAY) {
                        $result = 'array';
                    } else {
                        assert(false, "Unknown type: {$node->flags}");
                    }
                }
                break;
            default:
                Log::err(Log::EFATAL, "ast_node_type: unknown node type: " . \ast\get_kind_name($node->kind));
                break;
        }
    } else {
        $result = (string) $node;
    }
    return $result;
}
Exemplo n.º 2
0
Arquivo: util.php Projeto: nikic/phan
function find_class_name(string $file, $node, string $namespace, $current_class, $current_scope, &$static_call_ok = false) : string
{
    global $classes, $scope;
    if (!$node instanceof \ast\Node) {
        return $node;
    }
    $class_name = $return = '';
    switch ($node->kind) {
        case \ast\AST_NEW:
        case \ast\AST_STATIC_CALL:
        case \ast\AST_CLASS_CONST:
            if ($node->children[0]->kind == \ast\AST_NAME) {
                $class_name = $node->children[0]->children[0];
                if ($class_name == 'self' || $class_name == 'static' || $class_name == 'parent') {
                    if (!$current_class) {
                        Log::err(Log::ESTATIC, "Cannot access {$class_name}:: when no class scope is active", $file, $node->lineno);
                        return '';
                    }
                    if ($class_name == 'static') {
                        $class_name = $current_class['name'];
                    } else {
                        if ($class_name == 'self') {
                            if ($current_scope != 'global') {
                                list($class_name, ) = explode('::', $current_scope);
                            } else {
                                $class_name = $current_class['name'];
                            }
                        } else {
                            if ($class_name == 'parent') {
                                $class_name = $current_class['parent'];
                            }
                        }
                    }
                    $static_call_ok = true;
                } else {
                    $class_name = qualified_name($file, $node->children[0], $namespace);
                }
            }
            break;
        case \ast\AST_INSTANCEOF:
            if ($node->children[1]->kind == \ast\AST_NAME) {
                $class_name = qualified_name($file, $node->children[1], $namespace);
            }
            break;
        case \ast\AST_METHOD_CALL:
        case \ast\AST_PROP:
            if ($node->children[0]->kind == \ast\AST_VAR) {
                if (!$node->children[0]->children[0] instanceof \ast\Node) {
                    // $var->method()
                    if ($node->children[0]->children[0] == 'this') {
                        if (!$current_class) {
                            Log::err(Log::ESTATIC, 'Using $this when not in object context', $file, $node->lineno);
                            return '';
                        }
                    }
                    if (empty($scope[$current_scope]['vars'][$node->children[0]->children[0]])) {
                        // Got lost, couldn't find the variable in the current scope
                        // If it really isn't defined, it will be caught by the undefined var error
                        return '';
                    }
                    $call = $scope[$current_scope]['vars'][$node->children[0]->children[0]]['type'];
                    // Hack - loop through the possible types of the var and assume first found class is correct
                    foreach (explode('|', nongenerics($call)) as $class_name) {
                        if (!empty($classes[strtolower($class_name)])) {
                            break;
                        }
                    }
                    if (empty($class_name)) {
                        return '';
                    }
                    $class_name = $classes[strtolower($class_name)]['name'] ?? $class_name;
                }
            } else {
                if ($node->children[0]->kind == \ast\AST_PROP) {
                    $prop = $node->children[0];
                    if ($prop->children[0]->kind == \ast\AST_VAR && !$prop->children[0]->children[0] instanceof \ast\Node) {
                        // $var->prop->method()
                        $var = $prop->children[0];
                        if ($var->children[0] == 'this') {
                            if (!$current_class) {
                                Log::err(Log::ESTATIC, 'Using $this when not in object context', $file, $node->lineno);
                                return '';
                            }
                            if (!$prop->children[1] instanceof \ast\Node) {
                                if (!empty($current_class['properties'][$prop->children[1]])) {
                                    $prop = $current_class['properties'][$prop->children[1]];
                                    foreach (explode('|', nongenerics($prop['type'])) as $class_name) {
                                        if (!empty($classes[strtolower($class_name)])) {
                                            break;
                                        }
                                    }
                                    if (empty($class_name)) {
                                        return '';
                                    }
                                    $class_name = $classes[strtolower($class_name)]['name'] ?? $class_name;
                                }
                            } else {
                                // $this->$prop->method() - too dynamic, give up
                                return '';
                            }
                        }
                    }
                }
            }
            break;
    }
    if ($class_name) {
        $lc = strtolower($class_name);
        switch ($node->kind) {
            case \ast\AST_NEW:
                if (empty($classes[$lc])) {
                    if (!is_native_type(type_map($class_name))) {
                        Log::err(Log::EUNDEF, "Trying to instantiate undeclared class {$class_name}", $file, $node->lineno);
                    }
                } else {
                    if ($classes[$lc]['flags'] & \ast\flags\CLASS_ABSTRACT) {
                        if (!is_native_type(type_map($class_name))) {
                            if ($current_scope != 'global') {
                                list($scope_class, ) = explode('::', $current_scope);
                            } else {
                                $scope_class = '';
                            }
                            $lsc = strtolower($scope_class);
                            if ($lc != $lsc || $lc == $lsc && strtolower($current_class['name']) != $lsc) {
                                Log::err(Log::ETYPE, "Cannot instantiate abstract class {$class_name}", $file, $node->lineno);
                            }
                        }
                    } else {
                        if ($classes[$lc]['flags'] & \ast\flags\CLASS_INTERFACE) {
                            if (!is_native_type(type_map($class_name))) {
                                Log::err(Log::ETYPE, "Cannot instantiate interface {$class_name}", $file, $node->lineno);
                            }
                        } else {
                            $return = $class_name;
                        }
                    }
                }
                break;
            case \ast\AST_INSTANCEOF:
                if (empty($classes[$lc])) {
                    if (!is_native_type(type_map($class_name))) {
                        Log::err(Log::EUNDEF, "instanceof called on undeclared class {$class_name}", $file, $node->lineno);
                    }
                } else {
                    $return = $class_name;
                }
                break;
            case \ast\AST_CLASS_CONST:
                if (empty($classes[$lc])) {
                    if (!is_native_type(type_map($class_name))) {
                        Log::err(Log::EUNDEF, "can't access constant from undeclared class {$class_name}", $file, $node->lineno);
                    }
                } else {
                    $return = $class_name;
                }
                break;
            case \ast\AST_STATIC_CALL:
                if (empty($classes[$lc])) {
                    if (!is_native_type(type_map($class_name))) {
                        Log::err(Log::EUNDEF, "static call to undeclared class {$class_name}", $file, $node->lineno);
                    }
                } else {
                    $return = $class_name;
                }
                break;
            case \ast\AST_METHOD_CALL:
                if (empty($classes[$lc])) {
                    if (!is_native_type(type_map($class_name))) {
                        Log::err(Log::EUNDEF, "call to method on undeclared class {$class_name}", $file, $node->lineno);
                    }
                } else {
                    $return = $class_name;
                }
                break;
            case \ast\AST_PROP:
                if (empty($classes[$lc])) {
                    if (!is_native_type(type_map($class_name))) {
                        Log::err(Log::EUNDEF, "can't access property from undeclared class {$class_name}", $file, $node->lineno);
                    }
                } else {
                    $return = $class_name;
                }
                break;
        }
    }
    return $return;
}
Exemplo n.º 3
0
Arquivo: pass1.php Projeto: nikic/phan
function node_namelist($file, $node, $namespace)
{
    $result = [];
    if ($node instanceof \ast\Node) {
        foreach ($node->children as $name_node) {
            $result[] = qualified_name($file, $name_node, $namespace);
        }
    }
    return $result;
}
Exemplo n.º 4
0
function node_type($file, $namespace, $node, $current_scope, $current_class, &$taint = null, $check_var_exists = true)
{
    global $classes, $functions, $scope, $namespace_map, $internal_arginfo;
    if (!$node instanceof \ast\Node) {
        if ($node === null) {
            return '';
        }
        return type_map(gettype($node));
    } else {
        if ($node->kind == \ast\AST_ARRAY) {
            if (!empty($node->children) && $node->children[0] instanceof \ast\Node && $node->children[0]->kind == \ast\AST_ARRAY_ELEM) {
                // Check the first 5 (completely arbitrary) elements and assume the rest are the same type
                $etypes = [];
                for ($i = 0; $i < 5; $i++) {
                    if (empty($node->children[$i])) {
                        break;
                    }
                    if ($node->children[$i]->children[0] instanceof \ast\Node) {
                        $etypes[] = node_type($file, $namespace, $node->children[$i]->children[0], $current_scope, $current_class, $temp_taint);
                    } else {
                        $etypes[] = type_map(gettype($node->children[$i]->children[0]));
                    }
                }
                $types = array_unique($etypes);
                if (count($types) == 1 && !empty($types[0])) {
                    return mkgenerics($types[0]);
                }
            }
            return 'array';
        } else {
            if ($node->kind == \ast\AST_BINARY_OP) {
                $taint = var_taint_check($file, $node, $current_scope);
                switch ($node->flags) {
                    // Always a string from a concat
                    case \ast\flags\BINARY_CONCAT:
                        $temp_taint = false;
                        node_type($file, $namespace, $node->children[0], $current_scope, $current_class, $temp_taint);
                        if ($temp_taint) {
                            $taint = true;
                            return 'string';
                        }
                        node_type($file, $namespace, $node->children[1], $current_scope, $current_class, $temp_taint);
                        if ($temp_taint) {
                            $taint = true;
                        }
                        return 'string';
                        break;
                        // Boolean unless invalid operands
                    // Boolean unless invalid operands
                    case \ast\flags\BINARY_IS_IDENTICAL:
                    case \ast\flags\BINARY_IS_NOT_IDENTICAL:
                    case \ast\flags\BINARY_IS_EQUAL:
                    case \ast\flags\BINARY_IS_NOT_EQUAL:
                    case \ast\flags\BINARY_IS_SMALLER:
                    case \ast\flags\BINARY_IS_SMALLER_OR_EQUAL:
                        $taint = false;
                        return 'bool';
                        break;
                        // Add is special because you can add arrays
                    // Add is special because you can add arrays
                    case \ast\flags\BINARY_ADD:
                        $temp = node_type($file, $namespace, $node->children[0], $current_scope, $current_class);
                        if (!$temp) {
                            $left = '';
                        } else {
                            $left = type_map($temp);
                        }
                        $temp = node_type($file, $namespace, $node->children[1], $current_scope, $current_class);
                        if (!$temp) {
                            $right = '';
                        } else {
                            $right = type_map($temp);
                        }
                        if ($left == 'array' && $right == 'array') {
                            return 'array';
                        } else {
                            if ($left == 'array' && !type_check($right, 'array')) {
                                Log::err(Log::ETYPE, "invalid operator: left operand is array and right is not", $file, $node->lineno);
                                return '';
                            } else {
                                if ($right == 'array' && !type_check($left, 'array')) {
                                    Log::err(Log::ETYPE, "invalid operator: right operand is array and left is not", $file, $node->lineno);
                                    return '';
                                } else {
                                    if ($left == 'int' && $right == 'int') {
                                        return 'int';
                                    } else {
                                        if ($left == 'float' || $right == 'float') {
                                            return 'float';
                                        } else {
                                            if ($left == 'array' || $right == 'array') {
                                                // If it is a '+' and we know one side is an array and the other is unknown, assume array
                                                return 'array';
                                            }
                                        }
                                    }
                                }
                            }
                        }
                        return 'int|float';
                        $taint = false;
                        break;
                        // Everything else should be an int/float
                    // Everything else should be an int/float
                    default:
                        $temp = node_type($file, $namespace, $node->children[0], $current_scope, $current_class);
                        if (!$temp) {
                            $left = '';
                        } else {
                            $left = type_map($temp);
                        }
                        $temp = node_type($file, $namespace, $node->children[1], $current_scope, $current_class);
                        if (!$temp) {
                            $right = '';
                        } else {
                            $right = type_map($temp);
                        }
                        if ($left == 'array' || $right == 'array') {
                            Log::err(Log::ETYPE, "invalid array operator", $file, $node->lineno);
                            return '';
                        } else {
                            if ($left == 'int' && $right == 'int') {
                                return 'int';
                            } else {
                                if ($left == 'float' || $right == 'float') {
                                    return 'float';
                                }
                            }
                        }
                        return 'int|float';
                        $taint = false;
                        break;
                }
            } else {
                if ($node->kind == \ast\AST_CAST) {
                    $taint = var_taint_check($file, $node->children[0], $current_scope);
                    switch ($node->flags) {
                        case \ast\flags\TYPE_NULL:
                            return 'null';
                            break;
                        case \ast\flags\TYPE_BOOL:
                            $taint = false;
                            return 'bool';
                            break;
                        case \ast\flags\TYPE_LONG:
                            $taint = false;
                            return 'int';
                            break;
                        case \ast\flags\TYPE_DOUBLE:
                            $taint = false;
                            return 'float';
                            break;
                        case \ast\flags\TYPE_STRING:
                            return 'string';
                            break;
                        case \ast\flags\TYPE_ARRAY:
                            return 'array';
                            break;
                        case \ast\flags\TYPE_OBJECT:
                            return 'object';
                            break;
                        default:
                            Log::err(Log::EFATAL, "Unknown type (" . $node->flags . ") in cast");
                    }
                } else {
                    if ($node->kind == \ast\AST_NEW) {
                        $class_name = find_class_name($file, $node, $namespace, $current_class, $current_scope);
                        if ($class_name) {
                            return $classes[strtolower($class_name)]['type'];
                        }
                        return 'object';
                    } else {
                        if ($node->kind == \ast\AST_DIM) {
                            $taint = var_taint_check($file, $node->children[0], $current_scope);
                            // TODO: Do something smart with array elements
                            return '';
                        } else {
                            if ($node->kind == \ast\AST_VAR) {
                                return var_type($file, $node, $current_scope, $taint, $check_var_exists);
                            } else {
                                if ($node->kind == \ast\AST_ENCAPS_LIST) {
                                    foreach ($node->children as $encap) {
                                        if ($encap instanceof \ast\Node) {
                                            if (var_taint_check($file, $encap, $current_scope)) {
                                                $taint = true;
                                            }
                                        }
                                    }
                                    return "string";
                                } else {
                                    if ($node->kind == \ast\AST_CONST) {
                                        if ($node->children[0]->kind == \ast\AST_NAME) {
                                            if (defined($node->children[0]->children[0])) {
                                                return type_map(gettype(constant($node->children[0]->children[0])));
                                            } else {
                                                // Todo: user-defined constant
                                            }
                                        }
                                    } else {
                                        if ($node->kind == \ast\AST_CLASS_CONST) {
                                            if ($node->children[1] == 'class') {
                                                return 'string';
                                            }
                                            // class name fetch
                                            $class_name = find_class_name($file, $node, $namespace, $current_class, $current_scope);
                                            if (!$class_name) {
                                                return '';
                                            }
                                            $ltemp = strtolower($class_name);
                                            while ($ltemp && !array_key_exists($node->children[1], $classes[$ltemp]['constants'])) {
                                                $ltemp = strtolower($classes[$ltemp]['parent']);
                                                if (empty($classes[$ltemp])) {
                                                    return '';
                                                }
                                                // undeclared class - will be caught elsewhere
                                            }
                                            if (!$ltemp || !array_key_exists($node->children[1], $classes[$ltemp]['constants'])) {
                                                Log::err(Log::EUNDEF, "can't access undeclared constant {$class_name}::{$node->children[1]}", $file, $node->lineno);
                                                return '';
                                            }
                                            return $classes[$ltemp]['constants'][$node->children[1]]['type'];
                                        } else {
                                            if ($node->kind == \ast\AST_PROP) {
                                                if ($node->children[0]->kind == \ast\AST_VAR) {
                                                    $class_name = find_class_name($file, $node, $namespace, $current_class, $current_scope);
                                                    if ($class_name && !$node->children[1] instanceof \ast\Node) {
                                                        if (empty($classes[strtolower($class_name)]['properties'][$node->children[1]])) {
                                                            return '';
                                                        }
                                                        return $classes[strtolower($class_name)]['properties'][$node->children[1]]['type'];
                                                    }
                                                }
                                            } else {
                                                if ($node->kind == \ast\AST_STATIC_PROP) {
                                                    if ($node->children[0]->kind == \ast\AST_NAME) {
                                                        $class_name = qualified_name($file, $node->children[0], $namespace);
                                                        if ($class_name && !$node->children[1] instanceof \ast\Node) {
                                                            if (empty($classes[strtolower($class_name)]['properties'][$node->children[1]])) {
                                                                return '';
                                                            }
                                                            return $classes[strtolower($class_name)]['properties'][$node->children[1]]['type'];
                                                        }
                                                    }
                                                } else {
                                                    if ($node->kind == \ast\AST_CALL) {
                                                        if ($node->children[0]->kind == \ast\AST_NAME) {
                                                            $func_name = $node->children[0]->children[0];
                                                            if ($node->children[0]->flags & \ast\flags\NAME_NOT_FQ) {
                                                                $func = $namespace_map[T_FUNCTION][$file][strtolower($namespace . $func_name)] ?? $namespace_map[T_FUNCTION][$file][strtolower($func_name)] ?? $functions[strtolower($namespace . $func_name)] ?? $functions[strtolower($func_name)] ?? null;
                                                            } else {
                                                                $func = $functions[strtolower($func_name)] ?? null;
                                                            }
                                                            if ($func['file'] == 'internal' && empty($func['ret'])) {
                                                                if (!empty($internal_arginfo[$func_name])) {
                                                                    return $internal_arginfo[$func_name][0] ?? '';
                                                                }
                                                            } else {
                                                                return $func['ret'] ?? '';
                                                            }
                                                        } else {
                                                            // TODO: Handle $func() and other cases that get here
                                                        }
                                                    } else {
                                                        if ($node->kind == \ast\AST_STATIC_CALL) {
                                                            $class_name = find_class_name($file, $node, $namespace, $current_class, $current_scope);
                                                            $method = find_method($class_name, $node->children[1]);
                                                            if ($method) {
                                                                return $method['ret'] ?? '';
                                                            }
                                                        } else {
                                                            if ($node->kind == \ast\AST_METHOD_CALL) {
                                                                $class_name = find_class_name($file, $node, $namespace, $current_class, $current_scope);
                                                                if ($class_name) {
                                                                    $method_name = $node->children[1];
                                                                    $method = find_method($class_name, $method_name);
                                                                    if ($method === false) {
                                                                        Log::err(Log::EUNDEF, "call to undeclared method {$class_name}->{$method_name}()", $file, $node->lineno);
                                                                    } else {
                                                                        if ($method != 'dynamic') {
                                                                            return $method['ret'];
                                                                        }
                                                                    }
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    return '';
}