Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions runtime-common/stdlib/serialization/serialization-context.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,20 @@
#include <cstddef>
#include <cstdint>
#include <optional>
#include <string_view>

#include "common/mixin/not_copyable.h"
#include "common/php-functions.h"
#include "common/wrappers/string_view.h"
#include "runtime-common/core/runtime-core.h"
#include "runtime-common/core/utils/kphp-assert-core.h"
#include "runtime-light/core/reference-counter/reference-counter-functions.h"

namespace kphp::serde::details {

inline constexpr std::string_view JSON_ENCODE_BUFFER_OVERFLOW_EXCEPTION_MESSAGE{"json_encode buffer overflow"};

} // namespace kphp::serde::details

struct SerializationLibContext final : private vk::not_copyable {
string last_json_processor_error;
Expand All @@ -34,3 +44,15 @@ struct SerializationLibContext final : private vk::not_copyable {

static SerializationLibContext& get() noexcept;
};

struct SerializationLibConstants final : private vk::not_copyable {
string JSON_ENCODE_BUFFER_OVERFLOW_EXCEPTION_MESSAGE{kphp::serde::details::JSON_ENCODE_BUFFER_OVERFLOW_EXCEPTION_MESSAGE.data(),
kphp::serde::details::JSON_ENCODE_BUFFER_OVERFLOW_EXCEPTION_MESSAGE.size()};

SerializationLibConstants() noexcept {
php_assert((kphp::core::set_reference_counter_recursive(JSON_ENCODE_BUFFER_OVERFLOW_EXCEPTION_MESSAGE, ExtraRefCnt::for_global_const),
kphp::core::is_reference_counter_recursive(JSON_ENCODE_BUFFER_OVERFLOW_EXCEPTION_MESSAGE, ExtraRefCnt::for_global_const)));
}

static const SerializationLibConstants& get() noexcept;
};
2 changes: 2 additions & 0 deletions runtime-light/state/image-state.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "runtime-light/stdlib/file/file-system-state.h"
#include "runtime-light/stdlib/math/math-state.h"
#include "runtime-light/stdlib/rpc/rpc-client-state.h"
#include "runtime-light/stdlib/serialization/serialization-state.h"
#include "runtime-light/stdlib/string/string-state.h"
#include "runtime-light/stdlib/time/time-state.h"
#include "runtime-light/stdlib/visitors/shape-visitors.h"
Expand All @@ -42,6 +43,7 @@ struct ImageState final : private vk::not_copyable {
ShapeKeyDemangle shape_key_demangler;

FileSystemImageState file_system_image_state;
SerializationImageState serialization_image_state;
StringImageState string_image_state;
TimeImageState time_image_state;
MathImageState math_image_state;
Expand Down
13 changes: 8 additions & 5 deletions runtime-light/stdlib/serialization/json-functions.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@

#pragma once

#include "common/containers/final_action.h"
#include "runtime-common/core/runtime-core.h"
#include "runtime-common/stdlib/serialization/json-functions.h"
#include "runtime-light/stdlib/diagnostics/logs.h"
#include "runtime-light/stdlib/diagnostics/exception-functions.h"
#include "runtime-light/stdlib/diagnostics/exception-types.h"
#include "runtime-light/stdlib/serialization/serialization-state.h"

inline void f$set_json_log_on_timeout_mode([[maybe_unused]] bool enabled) noexcept {}

Expand All @@ -15,13 +18,13 @@ string f$vk_json_encode_safe(const T& v, bool simple_encode = true) noexcept {
auto& rt_ctx{RuntimeContext::get()};
rt_ctx.static_SB.clean();
rt_ctx.sb_lib_context.error_flag = STRING_BUFFER_ERROR_FLAG_ON;
impl_::JsonEncoder(0, simple_encode).encode(v, RuntimeContext::get().static_SB);
const auto finalizer{vk::finally([&rt_ctx] noexcept { rt_ctx.sb_lib_context.error_flag = STRING_BUFFER_ERROR_FLAG_OFF; })};

impl_::JsonEncoder(0, simple_encode).encode(v, rt_ctx.static_SB);
if (rt_ctx.sb_lib_context.error_flag == STRING_BUFFER_ERROR_FLAG_FAILED) [[unlikely]] {
rt_ctx.static_SB.clean();
rt_ctx.sb_lib_context.error_flag = STRING_BUFFER_ERROR_FLAG_OFF;
kphp::log::error("vk_json_encode_safe tried to throw exception but it unsupported in runtime light");
THROW_EXCEPTION(kphp::exception::make_throwable<C$Exception>(SerializationImageState::get().JSON_ENCODE_BUFFER_OVERFLOW_EXCEPTION_MESSAGE));
return {};
}
rt_ctx.sb_lib_context.error_flag = STRING_BUFFER_ERROR_FLAG_OFF;
return rt_ctx.static_SB.str();
}
5 changes: 5 additions & 0 deletions runtime-light/stdlib/serialization/serialization-state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,13 @@

#include "runtime-light/stdlib/serialization/serialization-state.h"

#include "runtime-light/state/image-state.h"
#include "runtime-light/state/instance-state.h"

SerializationInstanceState& SerializationInstanceState::get() noexcept {
return InstanceState::get().serialization_instance_state;
}

const SerializationImageState& SerializationImageState::get() noexcept {
return ImageState::get().serialization_image_state;
}
1 change: 1 addition & 0 deletions runtime-light/stdlib/serialization/serialization-state.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@
#include "runtime-common/stdlib/serialization/serialization-context.h"

using SerializationInstanceState = SerializationLibContext;
using SerializationImageState = SerializationLibConstants;
6 changes: 6 additions & 0 deletions runtime/serialize-context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,9 @@ static SerializationLibContext serialize_lib_context{};
SerializationLibContext& SerializationLibContext::get() noexcept {
return serialize_lib_context;
}

const static SerializationLibConstants serialize_lib_constants{};

const SerializationLibConstants& SerializationLibConstants::get() noexcept {
return serialize_lib_constants;
}
2 changes: 1 addition & 1 deletion tests/phpt/dl/499_vk_json_encode_safe.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@ok k2_skip
@ok
<?php

$s = str_repeat(".", (1 << 24) * 10);
Expand Down
Loading