Skip to content

Commit

Permalink
K2: add wrappers for libc allocator functions (#1128)
Browse files Browse the repository at this point in the history
  • Loading branch information
apolyakov authored Oct 26, 2024
1 parent 16c5af2 commit 5061a7f
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 9 deletions.
16 changes: 9 additions & 7 deletions compiler/code-gen/files/shape-keys.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,19 @@ void ShapeKeys::compile(CodeGenerator &W) const {
W << ExternInclude{G->settings().runtime_headers.get()};

FunctionSignatureGenerator{W} << "void " << get_function_name() << "()" << BEGIN;
W << "std::unordered_map<std::int64_t, std::string_view> shape_keys_storage{" << NL;
// skip it in K2 mode for now
if (!G->is_output_mode_k2()) {
W << "std::unordered_map<std::int64_t, std::string_view> shape_keys_storage{" << NL;

for (const auto &[hash, key] : shape_keys_storage_) {
W << Indent{2} << "{" << hash << ", \"" << key.data() << "\"}," << NL << Indent{-2};
}
for (const auto &[hash, key] : shape_keys_storage_) {
W << Indent{2} << "{" << hash << ", \"" << key.data() << "\"}," << NL << Indent{-2};
}

W << "};" << NL << NL;
W << "};" << NL << NL;

W << "vk::singleton<ShapeKeyDemangle>::get().init(std::move(shape_keys_storage));" << NL;
W << "vk::singleton<ShapeKeyDemangle>::get().init(std::move(shape_keys_storage));" << NL;
}

W << END << NL << NL;

W << CloseFile{};
}
4 changes: 3 additions & 1 deletion compiler/make/objs-to-k2-component-target.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include <sstream>

#include "compiler/compiler-core.h"
#include "compiler/compiler-settings.h"
#include "compiler/make/target.h"

Expand All @@ -29,7 +30,8 @@ class Objs2K2ComponentTarget : public Target {
public:
std::string get_cmd() final {
std::stringstream ss;
ss << settings->cxx.get() << " -static-libgcc -stdlib=libc++ -static-libstdc++ -shared -o " << target() << " ";
ss << settings->cxx.get() << " -Wl,--wrap,malloc -Wl,--wrap,free, -Wl,--wrap,calloc -Wl,--wrap,realloc "
<< " -static-libgcc -stdlib=libc++ -static-libstdc++ -shared -o " << target() << " ";

for (size_t i = 0; i + 1 < deps.size(); ++i) {
ss << deps[i]->get_name() << " ";
Expand Down
3 changes: 2 additions & 1 deletion runtime-light/allocator/allocator.cmake
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
set(RUNTIME_LIGHT_ALLOCATOR_SRC allocator/runtime-light-allocator.cpp)
set(RUNTIME_LIGHT_ALLOCATOR_SRC allocator/runtime-light-allocator.cpp
allocator/libc-alloc-wrapper.cpp)
23 changes: 23 additions & 0 deletions runtime-light/allocator/libc-alloc-wrapper.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Compiler for PHP (aka KPHP)
// Copyright (c) 2024 LLC «V Kontakte»
// Distributed under the GPL v3 License, see LICENSE.notice.txt

#include <cstddef>

#include "runtime-common/core/utils/kphp-assert-core.h"

extern "C" void *__wrap_malloc([[maybe_unused]] size_t size) noexcept {
php_critical_error("unexpected use of malloc");
}

extern "C" void __wrap_free([[maybe_unused]] void *ptr) noexcept {
php_critical_error("unexpected use of free");
}

extern "C" void *__wrap_calloc([[maybe_unused]] size_t nmemb, [[maybe_unused]] size_t size) noexcept {
php_critical_error("unexpected use of calloc");
}

extern "C" void *__wrap_realloc([[maybe_unused]] void *ptr, [[maybe_unused]] size_t size) noexcept {
php_critical_error("unexpected use of realloc");
}

0 comments on commit 5061a7f

Please sign in to comment.