Skip to content

Commit

Permalink
add stabs for unsupported whitelisted functions (#1078)
Browse files Browse the repository at this point in the history
Add new @kphp-extern-func-info tag `generate-stub` to ask compiler to generate body of function with call to php_critical_error

Also add some template stubs in runtime headers
  • Loading branch information
astrophysik authored Aug 29, 2024
1 parent 6e4dcaf commit 5747f48
Show file tree
Hide file tree
Showing 39 changed files with 1,970 additions and 23 deletions.
12 changes: 9 additions & 3 deletions builtin-functions/kphp-light/functions.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
<?php

define('TODO', -1);
define('TODO_OVERLOAD', -1);

define('PHP_INT_MAX', 9223372036854775807);
define('PHP_INT_MIN', -9223372036854775808);
define('PHP_INT_SIZE', 8);
define('PHP_EOL', "\n");


/** @var mixed $_SERVER */
global $_SERVER;
/** @var mixed $_GET */
Expand Down Expand Up @@ -63,9 +68,6 @@ function is_bool ($v ::: mixed) ::: bool;
function is_int ($v ::: mixed) ::: bool;
function is_integer ($v ::: mixed) ::: bool;
function is_long ($v ::: mixed) ::: bool;
function is_finite ($v ::: float) ::: bool;
function is_infinite ($v ::: float) ::: bool;
function is_nan ($v ::: float) ::: bool;
function is_float ($v ::: mixed) ::: bool;
function is_double ($v ::: mixed) ::: bool;
function is_real ($v ::: mixed) ::: bool;
Expand Down Expand Up @@ -210,3 +212,7 @@ function set_timer(int $timeout, callable():void $callback) ::: void;

// builtin that allows to store objects inside a mixed
function to_mixed(object $instance) ::: mixed;

// === Unsupported =======================================================================================

require_once __DIR__ . '/unsupported-functions.txt';
22 changes: 22 additions & 0 deletions builtin-functions/kphp-light/unsupported-functions.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

require_once __DIR__ . '/unsupported/arrays.txt';
require_once __DIR__ . '/unsupported/crypto.txt';
require_once __DIR__ . '/unsupported/curl.txt';
require_once __DIR__ . '/unsupported/error.txt';
require_once __DIR__ . '/unsupported/file.txt';
require_once __DIR__ . '/unsupported/fork.txt';
require_once __DIR__ . '/unsupported/hash.txt';
require_once __DIR__ . '/unsupported/job-worker.txt';
require_once __DIR__ . '/unsupported/kml.txt';
require_once __DIR__ . '/unsupported/kphp-toggles.txt';
require_once __DIR__ . '/unsupported/kphp-tracing.txt';
require_once __DIR__ . '/unsupported/kphp_internal.txt';
require_once __DIR__ . '/unsupported/math.txt';
require_once __DIR__ . '/unsupported/regex.txt';
require_once __DIR__ . '/unsupported/rpc.txt';
require_once __DIR__ . '/unsupported/serialize.txt';
require_once __DIR__ . '/unsupported/server.txt';
require_once __DIR__ . '/unsupported/string.txt';
require_once __DIR__ . '/unsupported/time.txt';
require_once __DIR__ . '/unsupported/vkext.txt';
117 changes: 117 additions & 0 deletions builtin-functions/kphp-light/unsupported/arrays.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
<?php

function create_vector ($n ::: int, $x ::: any) ::: ^2[];

function getKeyByPos ($a ::: array, $n ::: int) ::: mixed;
function getValueByPos ($a ::: array, $n ::: int) ::: ^1[*];

function array_first_key ($a ::: array) ::: mixed;
function array_key_first($a ::: array): mixed;
function array_first_value ($a ::: array) ::: ^1[*];
function array_last_key ($a ::: array) ::: mixed;
function array_key_last($a ::: array): mixed;
function array_last_value ($a ::: array) ::: ^1[*];
function array_swap_int_keys (&$a ::: array, $idx1 ::: int, $idx2 ::: int) ::: void;

function array_chunk ($a ::: array, $chunk_size ::: int, $preserve_keys ::: bool = false) ::: ^1[];

function array_splice (&$a ::: array, $offset ::: int, $length ::: int, $replacement ::: array = array()) ::: ^1;
/** @kphp-extern-func-info cpp_template_call */
function array_merge ($a1 ::: array, $a2 ::: array = array(), $a3 ::: array = array(),
$a4 ::: array = array(), $a5 ::: array = array(), $a6 ::: array = array(),
$a7 ::: array = array(), $a8 ::: array = array(), $a9 ::: array = array(),
$a10 ::: array = array(), $a11 ::: array = array(), $a12 ::: array = array())
::: ^1 | ^2 | ^3 | ^4 | ^5 | ^6 | ^7 | ^8 | ^9 | ^10 | ^11 | ^12;
/**
* @kphp-extern-func-info cpp_template_call cpp_variadic_call
* @kphp-pure-function
*/
function array_merge_recursive (...$a) ::: mixed[];
function array_merge_into (&$a ::: array, $another_array ::: array) ::: void;
/** @kphp-extern-func-info cpp_template_call */
function array_merge_spread ($a1 ::: array, $a2 ::: array = array(), $a3 ::: array = array(),
$a4 ::: array = array(), $a5 ::: array = array(), $a6 ::: array = array(),
$a7 ::: array = array(), $a8 ::: array = array(), $a9 ::: array = array(),
$a10 ::: array = array(), $a11 ::: array = array(), $a12 ::: array = array())
::: ^1 | ^2 | ^3 | ^4 | ^5 | ^6 | ^7 | ^8 | ^9 | ^10 | ^11 | ^12;
/** @kphp-extern-func-info cpp_template_call */
function array_replace ($base_array ::: array,
$replacements_1 ::: array = array(), $replacements_2 ::: array = array(), $replacements_3 ::: array = array(),
$replacements_4 ::: array = array(), $replacements_5 ::: array = array(), $replacements_6 ::: array = array(),
$replacements_7 ::: array = array(), $replacements_8 ::: array = array(), $replacements_9 ::: array = array(),
$replacements_10 ::: array = array(), $replacements_11 ::: array = array())
::: ^1 | ^2 | ^3 | ^4 | ^5 | ^6 | ^7 | ^8 | ^9 | ^10 | ^11 | ^12;
function array_intersect_key ($a1 ::: array, $a2 ::: array) ::: ^1;
function array_intersect ($a1 ::: array, $a2 ::: array) ::: ^1;
function array_intersect_assoc ($a1 ::: array, $a2 ::: array, $a3 ::: array = TODO) ::: ^1;
function array_diff_key ($a1 ::: array, $a2 ::: array) ::: ^1;
function array_diff ($a1 ::: array, $a2 ::: array, $a3 ::: array = TODO) ::: ^1;
function array_diff_assoc ($a1 ::: array, $a2 ::: array, $a3 ::: array = TODO) ::: ^1;
function array_reverse ($a ::: array, $preserve_keys ::: bool = false) ::: ^1;
function array_shift (&$a ::: array) ::: ^1[*];
function array_unshift (&$a ::: array, $val ::: any) ::: int;
function array_key_exists ($v ::: any, $a ::: array) ::: bool;
function array_search ($val ::: any, $a ::: array, $strict ::: bool = false) ::: mixed;
function array_find ($val ::: array, callable(^1[*] $x):bool $callback) ::: tuple(mixed, ^1[*]);
function array_rand ($a ::: array, $num ::: int = 1) ::: mixed;
function array_keys ($a ::: array) ::: mixed[];
function array_keys_as_strings ($a ::: array) ::: string[];
function array_keys_as_ints ($a ::: array) ::: int[];
function array_values ($a ::: array) ::: ^1;
function array_unique ($a ::: array, int $flags = SORT_STRING) ::: ^1;
function array_count_values ($a ::: array) ::: int[];
function array_flip ($a ::: array) ::: mixed[];
function in_array ($value ::: any, $a ::: array, $strict ::: bool = false) ::: bool;
function array_fill ($start_index ::: int, $num ::: int, $value ::: any) ::: ^3[];
function array_fill_keys ($a ::: array, $value ::: any) ::: ^2[];
function array_combine ($keys ::: array, $values ::: array) ::: ^2;
function range ($from, $to, $step ::: int = 1) ::: mixed[];//TODO
function array_push (&$a ::: array, $val2 ::: any, $val3 ::: any = TODO, $val4 ::: any = TODO, $val5 ::: any = TODO, $val6 ::: any = TODO) ::: int;
function array_pop (&$a ::: array) ::: ^1[*];
function array_sum ($a ::: array) ::: float;//TODO
function array_slice ($a ::: array, $offset ::: int, $length = null, $preserve_keys ::: bool = false) ::: ^1;
/** @kphp-extern-func-info cpp_template_call */
function array_pad ($a ::: array, $size ::: int, $value ::: any) ::: array< ^1[*] | ^3 >;
function array_column ($a ::: array, $column_key, $index_key = null) ::: array< ^1[*][*] > | false;
function array_unset (&$a ::: array, any $key) ::: ^1[*];

function array_filter ($a ::: array, callable(^1[*] $x):bool $callback = TODO) ::: ^1;
function array_filter_by_key ($a ::: array, callable(mixed $key):bool $callback) ::: ^1;
function array_map (callable(^2[*] $x):any $callback, $a ::: array) ::: ^1() [];
/** @kphp-extern-func-info cpp_template_call */
function array_reduce ($a ::: array, callable(^3 | ^2() $carry, ^1[*] $item):any $callback, $initial ::: any) ::: ^2() | ^3;
function array_reserve (&$a ::: array, $int_size ::: int, $string_size ::: int, $make_vector_if_possible ::: bool) ::: void;
function array_reserve_vector (&$a ::: array, $size ::: int) ::: void;
function array_reserve_map_int_keys (&$a ::: array, $size ::: int) ::: void;
function array_reserve_map_string_keys (&$a ::: array, $size ::: int) ::: void;
function array_reserve_from (&$a ::: array, $base ::: array) ::: void;
function array_is_vector ($a ::: array) ::: bool;
function array_is_list ($a ::: array) ::: bool;

define('SORT_REGULAR', 0);
define('SORT_NUMERIC', 1);
define('SORT_STRING', 2);

function asort (&$a ::: array, $flag ::: int = SORT_REGULAR) ::: void;
function arsort (&$a ::: array, $flag ::: int = SORT_REGULAR) ::: void;
function ksort (&$a ::: array, $flag ::: int = SORT_REGULAR) ::: void;
function krsort (&$a ::: array, $flag ::: int = SORT_REGULAR) ::: void;
function natsort (&$a ::: array) ::: void;
function rsort (&$a ::: array, $flag ::: int = SORT_REGULAR) ::: void;
function shuffle (&$a ::: array) ::: void;
function sort (&$a ::: array, $flag ::: int = SORT_REGULAR) ::: void;
function uasort (&$a ::: array, callable(^1[*] $x, ^1[*] $y):int $callback) ::: void;
function uksort (&$a ::: array, callable(mixed $x, mixed $y):int $callback) ::: void;
function usort (&$a ::: array, callable(^1[*] $x, ^1[*] $y):int $callback) ::: void;


/** @kphp-extern-func-info cpp_template_call */
function vk_dot_product ($a ::: array, $b ::: array) ::: ^1[*] | ^2[*];


function to_array_debug(any $instance, bool $with_class_names = false) ::: mixed[];

function implode ($s ::: string, $v ::: array) ::: string;

/** @kphp-extern-func-info generate-stub */
function explode ($delimiter ::: string, $str ::: string, $limit ::: int = PHP_INT_MAX) ::: string[];
46 changes: 46 additions & 0 deletions builtin-functions/kphp-light/unsupported/crypto.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php


define('X509_PURPOSE_SSL_CLIENT' , 1);
define('X509_PURPOSE_SSL_SERVER' , 2);
define('X509_PURPOSE_NS_SSL_SERVER' , 3);
define('X509_PURPOSE_SMIME_SIGN' , 4);
define('X509_PURPOSE_SMIME_ENCRYPT' , 5);
define('X509_PURPOSE_CRL_SIGN' , 6);
define('X509_PURPOSE_ANY' , 7);
define('X509_PURPOSE_OCSP_HELPER' , 8);
define('X509_PURPOSE_TIMESTAMP_SIGN', 9);

define('OPENSSL_ALGO_SHA1' , 1);
define('OPENSSL_ALGO_MD5' , 2);
define('OPENSSL_ALGO_MD4' , 3);
define('OPENSSL_ALGO_MD2' , 4);
define('OPENSSL_ALGO_DSS1' , 5);
define('OPENSSL_ALGO_SHA224', 6);
define('OPENSSL_ALGO_SHA256', 7);
define('OPENSSL_ALGO_SHA384', 8);
define('OPENSSL_ALGO_SHA512', 9);
define('OPENSSL_ALGO_RMD160', 10);

define('OPENSSL_RAW_DATA', 1);
define('OPENSSL_ZERO_PADDING', 2);
define('OPENSSL_DONT_ZERO_PAD_KEY', 4);

/** @kphp-extern-func-info generate-stub */
function openssl_public_encrypt ($data ::: string, &$result ::: mixed, $key ::: string) ::: bool;
/** @kphp-extern-func-info generate-stub */
function openssl_private_decrypt ($data ::: string, &$result ::: mixed, $key ::: string) ::: bool;
/** @kphp-extern-func-info generate-stub */
function openssl_pkey_get_private ($key ::: string, $passphrase ::: string = '') ::: string | false;
/** @kphp-extern-func-info generate-stub */
function openssl_pkey_get_public ($key ::: string) ::: string | false;
/** @kphp-extern-func-info generate-stub */
function openssl_sign ($data ::: string, &$signature ::: string, $priv_key_id ::: string, $signature_alg ::: int = 1) ::: bool;
/** @kphp-extern-func-info generate-stub */
function openssl_verify ($data ::: string, $signature ::: string, $pub_key_id ::: string, $signature_alg ::: int = 1) ::: int;
/** @kphp-extern-func-info generate-stub */
function openssl_random_pseudo_bytes ($length ::: int) ::: string | false;
/** @kphp-extern-func-info generate-stub */
function openssl_x509_parse ($x509cert ::: string, $shortnames ::: bool = true) ::: mixed[] | false;
/** @kphp-extern-func-info generate-stub */
function openssl_x509_verify ($x509cert ::: string, $public_key ::: string) ::: int;
45 changes: 45 additions & 0 deletions builtin-functions/kphp-light/unsupported/curl.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

/** @kphp-extern-func-info generate-stub */
function curl_init ($url ::: string = "") ::: int;
/** @kphp-extern-func-info generate-stub */
function curl_reset ($curl_handle ::: int) ::: void;
/** @kphp-extern-func-info generate-stub */
function curl_setopt ($curl_handle ::: int, $option ::: int, $value ::: mixed) ::: bool;
/** @kphp-extern-func-info generate-stub */
function curl_setopt_array ($curl_handle ::: int, $options ::: array) ::: bool;
/** @kphp-extern-func-info generate-stub */
function curl_exec ($curl_handle ::: int) ::: mixed;
/** @kphp-extern-func-info generate-stub */
function curl_getinfo ($curl_handle ::: int, $option ::: int = 0) ::: mixed;
/** @kphp-extern-func-info generate-stub */
function curl_error ($curl_handle ::: int) ::: string;
/** @kphp-extern-func-info generate-stub */
function curl_errno ($curl_handle ::: int) ::: int;
/** @kphp-extern-func-info generate-stub */
function curl_close ($curl_handle ::: int) ::: void;
/** @kphp-extern-func-info generate-stub */
function curl_exec_concurrently($curl_handle ::: int, $timeout ::: float = 1.0): string|false;

/** @kphp-extern-func-info generate-stub */
function curl_multi_init () ::: int;
/** @kphp-extern-func-info generate-stub */
function curl_multi_add_handle ($multi_handle ::: int, $curl_handle ::: int) ::: int|false;
/** @kphp-extern-func-info generate-stub */
function curl_multi_getcontent ($curl_handle ::: int ) ::: string|false|null;
/** @kphp-extern-func-info generate-stub */
function curl_multi_setopt ($multi_handle ::: int, $option ::: int, $value ::: int) ::: bool;
/** @kphp-extern-func-info generate-stub */
function curl_multi_exec ($multi_handle ::: int, &$still_running ::: int) ::: int|false;
/** @kphp-extern-func-info generate-stub */
function curl_multi_select ($multi_handle ::: int, $timeout ::: float = 1.0) ::: int|false;
/** @kphp-extern-func-info generate-stub */
function curl_multi_info_read ($multi_handle ::: int, &$msgs_in_queue ::: int = TODO) ::: int[]|false;
/** @kphp-extern-func-info generate-stub */
function curl_multi_remove_handle ($multi_handle ::: int, $curl_handle ::: int) ::: int|false;
/** @kphp-extern-func-info generate-stub */
function curl_multi_errno ($multi_handle ::: int) ::: int|false;
/** @kphp-extern-func-info generate-stub */
function curl_multi_close ($multi_handle ::: int) ::: void;
/** @kphp-extern-func-info generate-stub */
function curl_multi_strerror ($errornum ::: int) ::: string|null;
61 changes: 61 additions & 0 deletions builtin-functions/kphp-light/unsupported/error.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

interface Throwable {
public function getMessage () ::: string;
public function getCode () ::: int;
public function getFile () ::: string;
public function getLine () ::: int;
public function getTrace () ::: string[][];
public function getTraceAsString () ::: string;
}

class Exception implements Throwable {
protected $message = '';
protected $code = 0;
/** @var string */
protected $file;
/** @var int */
protected $line;

public function __construct($message ::: string = '', $code ::: int = 0) ::: Exception;
final public function getMessage () ::: string;
final public function getCode () ::: int;
final public function getFile () ::: string;
final public function getLine () ::: int;
final public function getTrace () ::: string[][];
final public function getTraceAsString () ::: string;

final private function __clone() ::: void;
}


class Error implements Throwable {
protected $message = '';
protected $code = 0;
/** @var string */
protected $file;
/** @var int */
protected $line;

public function __construct($message ::: string = '', $code ::: int = 0) ::: Error;
final public function getMessage () ::: string;
final public function getCode () ::: int;
final public function getFile () ::: string;
final public function getLine () ::: int;
final public function getTrace () ::: string[][];
final public function getTraceAsString () ::: string;

final private function __clone() ::: void;
}

/** @kphp-extern-func-info generate-stub */
function err ($file ::: string, $line ::: int, $code ::: string, $desc ::: string = '') ::: Exception;
/** @kphp-extern-func-info generate-stub */
function error_get_last() ::: mixed;
/** @kphp-extern-func-info generate-stub */
function error_reporting ($e ::: int = TODO) ::: int;
/** @kphp-extern-func-info generate-stub */
function kphp_backtrace($pretty ::: bool = true) ::: string[];
/** @kphp-extern-func-info generate-stub */
function kphp_set_context_on_error(mixed[] $tags, mixed $extra_info, string $env = "") ::: void;

Loading

0 comments on commit 5747f48

Please sign in to comment.