Skip to content

Commit

Permalink
Fix objects in mixed in light runtime (#1075)
Browse files Browse the repository at this point in the history
Fix UB with reaching unreachable builtin when do var_dump() when mixed stores object and some fixes
  • Loading branch information
mkornaukhov03 authored Aug 21, 2024
1 parent 081ee6b commit a5ae8f4
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 6 deletions.
2 changes: 1 addition & 1 deletion builtin-functions/kphp-light/functions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ function json_decode ($v ::: string, $assoc ::: bool = false) ::: mixed;
// === Misc =======================================================================================

/** @kphp-extern-func-info cpp_template_call */
function instance_cast(object $instance, $to_type ::: string) ::: instance<^2>;
function instance_cast(any $instance, $to_type ::: string) ::: instance<^2>;

function make_clone ($x ::: any) ::: ^1;

Expand Down
1 change: 1 addition & 0 deletions runtime-core/runtime-core.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,4 @@ endif()

prepend(KPHP_CORE_SRC ${RUNTIME_CORE_DIR}/ "${KPHP_CORE_SRC}")
vk_add_library(runtime-core OBJECT ${KPHP_CORE_SRC})
target_compile_options(runtime-core PUBLIC -fPIC)
1 change: 1 addition & 0 deletions runtime-light/runtime-light.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ set_target_properties(runtime-light PROPERTIES LIBRARY_OUTPUT_DIRECTORY
${BASE_DIR}/objs)
target_compile_options(runtime-light PUBLIC -stdlib=libc++)
target_link_options(runtime-light PUBLIC -stdlib=libc++ -static-libstdc++)
target_compile_options(runtime-light PUBLIC -fPIC)

vk_add_library(kphp-light-runtime STATIC)
target_link_libraries(
Expand Down
15 changes: 15 additions & 0 deletions runtime-light/stdlib/variable-handling.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ void do_print_r(const mixed &v, int depth) {
*coub << shift << ")\n";
break;
}
case mixed::type::OBJECT: {
php_warning("print_r used on object");
*coub << v.as_object()->get_class();
break;
}
default:
__builtin_unreachable();
}
Expand Down Expand Up @@ -97,6 +102,12 @@ static void do_var_dump(const mixed &v, int depth) {
*coub << shift << "}";
break;
}
case mixed::type::OBJECT: {
php_warning("var_dump used on object");
auto s = string(v.as_object()->get_class(), static_cast<string::size_type>(strlen(v.as_object()->get_class())));
*coub << shift << "string(" << static_cast<int>(s.size()) << ") \"" << s << '"';
break;
}
default:
__builtin_unreachable();
}
Expand Down Expand Up @@ -176,6 +187,10 @@ static void do_var_export(const mixed &v, int depth, char endc = 0) {
*coub << shift << ")";
break;
}
case mixed::type::OBJECT: {
*coub << shift << v.get_type_or_class_name();
break;
}
default:
__builtin_unreachable();
}
Expand Down
6 changes: 3 additions & 3 deletions tests/kphp_tester.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,12 @@ def make_kphp_once_runner(self, use_nocc, cxx_name, k2_bin):
k2_bin=k2_bin
)

def set_up_env_for_k2(self):
def set_up_env_for_k2(self, cxx_name="clang++"):
self.env_vars["KPHP_MODE"] = "k2-component"
self.env_vars["KPHP_USER_BINARY_PATH"] = "component.so"
self.env_vars["KPHP_ENABLE_GLOBAL_VARS_MEMORY_STATS"] = "0"
self.env_vars["KPHP_PROFILER"] = "0"
self.env_vars["KPHP_CXX"] = "clang++"
self.env_vars["KPHP_CXX"] = cxx_name
self.env_vars["KPHP_K2_COMPONENT_IS_ONESHOT"] = "1"
self.env_vars["KPHP_FORCE_LINK_RUNTIME"] = "1"

Expand Down Expand Up @@ -352,7 +352,7 @@ def run_test(use_nocc, cxx_name, k2_bin, test: TestFile):
runner = test.make_kphp_once_runner(use_nocc, cxx_name, k2_bin)
runner.remove_artifacts_dir()
if k2_bin is not None:
test.set_up_env_for_k2()
test.set_up_env_for_k2(cxx_name)

if k2_bin is not None and not test.is_available_for_k2():
test_result = TestResult.k2_skipped(test)
Expand Down
2 changes: 1 addition & 1 deletion tests/phpt/mixed/non_primitive/008_forks.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@ok
@ok k2_skip
<?php
require_once 'kphp_tester_include.php';

Expand Down
2 changes: 1 addition & 1 deletion tests/phpt/mixed/non_primitive/014_class_as_int.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@ok
@ok k2_skip
<?php
require_once 'kphp_tester_include.php';

Expand Down
21 changes: 21 additions & 0 deletions tests/phpt/mixed/non_primitive/016_var_dump.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
@ok
<?php
require_once 'kphp_tester_include.php';

class A {
public int $field;
}

/** @param $x mixed */
function print_mixed($x) {
#ifndef KPHP
echo "string(1) \"A\"\n";
return;
#endif
var_dump($x);
}

/** @var mixed */
$x = to_mixed(new A);

print_mixed($x);

0 comments on commit a5ae8f4

Please sign in to comment.