new package: kphp

This commit is contained in:
Tee KOBAYASHI 2022-01-13 21:07:28 +09:00 committed by Leonid Pliushch
parent 19563a614b
commit 3e71f82453
27 changed files with 780 additions and 0 deletions

59
packages/kphp/build.sh Normal file
View File

@ -0,0 +1,59 @@
TERMUX_PKG_HOMEPAGE=https://vkcom.github.io/kphp/
TERMUX_PKG_DESCRIPTION="A PHP compiler"
TERMUX_PKG_LICENSE="GPL-3.0"
TERMUX_PKG_MAINTAINER="@termux"
_COMMIT=b1b2cec0f0e1206e1c134830ebd1f28e21bbd330
TERMUX_PKG_VERSION=2021.12.30
TERMUX_PKG_SRCURL=https://github.com/VKCOM/kphp.git
TERMUX_PKG_GIT_BRANCH=master
TERMUX_PKG_DEPENDS="fmt, libc++, libcurl, libmsgpack-cxx, libre2, libuber-h3, libucontext, libyaml-cpp, openssl, pcre, zstd"
TERMUX_PKG_BUILD_DEPENDS="kphp-timelib"
TERMUX_PKG_EXTRA_CONFIGURE_ARGS="-DKPHP_TESTS=OFF"
TERMUX_PKG_BLACKLISTED_ARCHES="arm, i686"
termux_step_post_get_source() {
git fetch --unshallow
git checkout $_COMMIT
local version="$(git log -1 --format=%cs | sed 's/-/./g')"
if [ "$version" != "$TERMUX_PKG_VERSION" ]; then
echo -n "ERROR: The specified version \"$TERMUX_PKG_VERSION\""
echo " is different from what is expected to be: \"$version\""
return 1
fi
}
termux_step_pre_configure() {
_NEED_DUMMY_LIBPTHREAD_A=
_LIBPTHREAD_A=$TERMUX_PREFIX/lib/libpthread.a
if [ ! -e $_LIBPTHREAD_A ]; then
_NEED_DUMMY_LIBPTHREAD_A=true
echo '!<arch>' > $_LIBPTHREAD_A
fi
}
termux_step_post_configure() {
local f
if [ "$TERMUX_CMAKE_BUILD" == "Ninja" ]; then
f=build.ninja
else
f=CMakeFiles/kphp2cpp.dir/link.txt
fi
sed -i -e 's/-l:libyaml-cpp\.a/-lyaml-cpp/g' \
-e 's/-l:libre2\.a/-lre2/g' \
$f
local bin=$TERMUX_PKG_BUILDDIR/_prefix/bin
mkdir -p $bin
for exe in generate_unicode_utils prepare_unicode_data; do
$CC_FOR_BUILD $TERMUX_PKG_SRCDIR/common/unicode/${exe//_/-}.cpp \
-o ${bin}/${exe}
done
export PATH=$bin:$PATH
}
termux_step_post_make_install() {
if [ $_NEED_DUMMY_LIBPTHREAD_A ]; then
rm -f $_LIBPTHREAD_A
fi
}

View File

@ -0,0 +1,15 @@
--- a/cmake/init-compilation-flags.cmake
+++ b/cmake/init-compilation-flags.cmake
@@ -80,9 +80,11 @@
if(CMAKE_SYSTEM_PROCESSOR STREQUAL "x86_64")
add_compile_options(-march=sandybridge -fno-common)
add_link_options(-fno-common)
+elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL "aarch64")
+ add_compile_options(-march=armv8-a+crc+aes)
endif()
-add_compile_options(-Werror -Wall -Wextra -Wunused-function -Wfloat-conversion -Wno-sign-compare
+add_compile_options(-Wall -Wextra -Wunused-function -Wfloat-conversion -Wno-sign-compare
-Wuninitialized -Wno-redundant-move -Wno-missing-field-initializers)
if(NOT APPLE)

View File

@ -0,0 +1,25 @@
--- a/cmake/init-global-vars.cmake
+++ b/cmake/init-global-vars.cmake
@@ -7,12 +7,9 @@
set(GENERATED_DIR "${OBJS_DIR}/generated")
set(AUTO_DIR "${GENERATED_DIR}/auto")
-if(APPLE)
+if(TRUE)
set(CURL_LIB curl)
set(ICONV_LIB iconv)
-else()
- set(CURL_LIB /opt/curl7600/lib/libcurl.a)
- set(RT_LIB rt)
endif()
find_package(Git REQUIRED)
@@ -72,7 +69,7 @@
set(CMAKE_INSTALL_PREFIX "/." CACHE PATH "install prefix" FORCE)
endif()
-set(VK_INSTALL_DIR usr/share/vkontakte)
+set(VK_INSTALL_DIR share/vkontakte)
set(INSTALL_KPHP_SOURCE ${VK_INSTALL_DIR}/kphp_source)
set(CPACK_PACKAGING_INSTALL_PREFIX ${CMAKE_INSTALL_PREFIX})

View File

@ -0,0 +1,27 @@
--- a/common/algorithms/simd-int-to-string.h
+++ b/common/algorithms/simd-int-to-string.h
@@ -274,6 +274,9 @@
// todo anyone who wants to practice some low-level magic — welcome to implement a proper SIMD form with ARM intrinsics
#include <cstdint>
#include <cstdio>
+#ifdef __ANDROID__
+#include <inttypes.h>
+#endif
inline char *simd_uint32_to_string(uint32_t value, char *out_buffer) noexcept {
int n = sprintf(out_buffer, "%u", value);
@@ -286,12 +289,12 @@
}
inline char *simd_uint64_to_string(uint64_t value, char *out_buffer) {
- int n = sprintf(out_buffer, "%llu", value);
+ int n = sprintf(out_buffer, "%" PRIu64, value);
return out_buffer + n;
}
inline char *simd_int64_to_string(int64_t value, char *out_buffer) {
- int n = sprintf(out_buffer, "%lld", value);
+ int n = sprintf(out_buffer, "%" PRId64, value);
return out_buffer + n;
}

View File

@ -0,0 +1,12 @@
--- a/common/binlog/binlog-buffer-aio.cpp
+++ b/common/binlog/binlog-buffer-aio.cpp
@@ -2,7 +2,9 @@
// Copyright (c) 2020 LLC «V Kontakte»
// Distributed under the GPL v3 License, see LICENSE.notice.txt
+#ifndef __ANDROID__
#include <aio.h>
+#endif
#include <assert.h>
#include <cstdlib>
#include <errno.h>

View File

@ -0,0 +1,12 @@
--- a/common/binlog/binlog-buffer.h
+++ b/common/binlog/binlog-buffer.h
@@ -5,7 +5,9 @@
#ifndef __KDB_BINLOG_BUFFER_H__
#define __KDB_BINLOG_BUFFER_H__
+#ifndef __ANDROID__
#include <aio.h>
+#endif
#include <limits.h>
#include <functional>

View File

@ -0,0 +1,20 @@
--- a/common/crc32c_aarch64.cpp
+++ b/common/crc32c_aarch64.cpp
@@ -25,7 +25,7 @@
static uint32_t aarch64_native_crc(const void *buffer, long int len,
uint32_t crc) {
- const uint8_t *p = buffer;
+ const uint8_t *p = (const uint8_t *)buffer;
int64_t length = len;
while ((length -= sizeof(uint64_t)) >= 0) {
@@ -98,7 +98,7 @@
// k2=CRC(x^(SEGMENTBYTES*8))
const poly64_t k0 = 0x8d96551c, k1 = 0xbd6f81f8, k2 = 0xdcb17aa4;
- const uint8_t *p = buf;
+ const uint8_t *p = (const uint8_t *)buf;
while (length >= KBYTES) {
crc0 = crc;

View File

@ -0,0 +1,56 @@
--- a/common/crypto/aes256-aarch64.cpp
+++ b/common/crypto/aes256-aarch64.cpp
@@ -78,7 +78,7 @@
void crypto_aarch64_aes256_set_decrypt_key(vk_aes_ctx_t *vk_ctx, const uint8_t key[32]) {
crypto_aarch64_aes256_set_encrypt_key(vk_ctx, key);
- unsigned char *a = align16(&vk_ctx->u.ctx.a);
+ unsigned char *a = (unsigned char *)align16(&vk_ctx->u.ctx.a);
for (int i = 1; i <= 13; i++) {
asm volatile("mov x9, %[key] ;"
"ld1 {v0.16b}, [x9] ;"
@@ -97,7 +97,7 @@
asm volatile("mov x9, %[iv] ;" // move IV address in x9
"mov x10, %[out] ;" // move out address in x10
- "mov x11, %[size] ;" // move size value in x11
+ "mov w11, %w[size] ;" // move size value in x11
"mov x12, %[in] ;" // move plaintext address in x12
"mov x13, %[key] ;" // move key address in x13
"ld1 {v25.16b}, [x9] ;" // load IV to v0.16b
@@ -167,7 +167,7 @@
asm volatile("mov x9, %[iv] ;" // move IV address in x9
"mov x10, %[out] ;" // move out address in x10
- "mov x11, %[size] ;" // move size value in x11
+ "mov w11, %w[size] ;" // move size value in x11
"mov x12, %[in] ;" // move ciphertext address in x12
"mov x13, %[key] ;" // move key address in x13
"ld1 {v25.16b}, [x9] ;" // load IV to v25.16b
@@ -238,7 +238,7 @@
asm volatile("mov x9, %[iv] ;" // move IGE IV address in x9
"mov x10, %[out] ;" // move out address in x10
- "mov x11, %[size] ;" // move size value in x11
+ "mov w11, %w[size] ;" // move size value in x11
"mov x12, %[in] ;" // move plaintext address in x12
"mov x13, %[key] ;" // move key address in x13
"ld1 {v25.16b}, [x9], #16 ;" // load IGE IV Y to v25.16b
@@ -313,7 +313,7 @@
asm volatile("mov x9, %[iv] ;" // move IGE IV address in x9
"mov x10, %[out] ;" // move out address in x10
- "mov x11, %[size] ;" // move size value in x11
+ "mov w11, %w[size] ;" // move size value in x11
"mov x12, %[in] ;" // move cyphertext address in x12
"mov x13, %[key] ;" // move key address in x13
"ld1 {v25.16b}, [x9], #16 ;" // load IGE IV Y to v25.16b
@@ -439,7 +439,7 @@
"mov x10, %[in] ;" // move plaintext address in x10
"mov x11, %[key] ;" // move key address in x11
"mov x12, %[iv] ;" // move IV address in x12
- "mov x13, %[n] ;" // move n value in x11
+ "mov w13, %w[n] ;" // move n value in x11
"ld1 {v26.16b}, [x12] ;" // load IV to v0.16b
"eor v25.16b, v25.16b, v25.16b ;"
"mov w15, #1 ;"

View File

@ -0,0 +1,54 @@
--- a/common/dl-utils-lite.cpp
+++ b/common/dl-utils-lite.cpp
@@ -7,7 +7,9 @@
#include <array>
#include <cassert>
#include <cinttypes>
+#ifndef __ANDROID__
#include <execinfo.h>
+#endif
#include <fcntl.h>
#include <limits>
#include <stdarg.h>
@@ -23,6 +25,7 @@
#include "common/stats/provider.h"
#include "common/wrappers/pathname.h"
+#ifndef __ANDROID__
#if DL_DEBUG_MEM >= 1
# define MEM_POS {\
void *buffer[64]; \
@@ -34,6 +37,9 @@
#else
# define MEM_POS
#endif
+#else /* __ANDROID__ */
+# define MEM_POS
+#endif
static std::array<char, 1024> assert_message{{0}};
@@ -44,16 +50,23 @@
return static_cast<double>(T.tv_sec) + static_cast<double>(T.tv_nsec) * 1e-9;
}
+#ifdef __ANDROID__
+void dl_print_backtrace(void **, int) {
+}
+#else
void dl_print_backtrace(void **trace, int trace_size) {
write (2, "\n------- Stack Backtrace -------\n", 33);
backtrace_symbols_fd (trace, trace_size, 2);
write (2, "-------------------------------\n", 32);
}
+#endif
void dl_print_backtrace() {
+#ifndef __ANDROID__
void *buffer[64];
int nptrs = backtrace (buffer, 64);
dl_print_backtrace(buffer, nptrs);
+#endif
}
void dl_print_backtrace_gdb() {

View File

@ -0,0 +1,33 @@
--- a/common/fast-backtrace.cpp
+++ b/common/fast-backtrace.cpp
@@ -6,12 +6,19 @@
#include <algorithm>
#include <cassert>
+#ifndef __ANDROID__
#include <execinfo.h>
+#endif
#include "common/sanitizer.h"
char *stack_end;
+#ifdef __ANDROID__
+int fast_backtrace (void **, int) {
+ return 0;
+}
+#else
#if defined(__aarch64__) || defined(__APPLE__)
int fast_backtrace (void **buffer, int size) {
return backtrace(buffer, size);
@@ -52,8 +59,9 @@
#else
#error "Unsupported arch"
#endif
+#endif /* __ANDROID__ */
-#if defined(__APPLE__)
+#if defined(__APPLE__) || defined(__ANDROID__)
int fast_backtrace_without_recursions(void **, int) noexcept {
return 0;
}

View File

@ -0,0 +1,18 @@
--- a/common/resolver.cpp
+++ b/common/resolver.cpp
@@ -15,10 +15,14 @@
#include <sys/types.h>
#include <unistd.h>
+#ifdef __ANDROID__
+#include <sys/endian.h>
+#endif
+
#include "common/kprintf.h"
#include "common/options.h"
-#define HOSTS_FILE "/etc/hosts"
+#define HOSTS_FILE "@TERMUX_PREFIX@/etc/hosts"
#define MAX_HOSTS_SIZE (1L << 24)
int kdb_hosts_loaded;

View File

@ -0,0 +1,11 @@
--- a/common/server/crash-dump.cpp
+++ b/common/server/crash-dump.cpp
@@ -116,6 +116,8 @@
crash_dump_write_reg(LITERAL_WITH_LENGTH("R15=0x"), uc->uc_mcontext.gregs[REG_R15], buffer);
#elif defined(__aarch64__)
+ const auto *uc = static_cast<ucontext_t *>(ucontext);
+
crash_dump_write_reg(LITERAL_WITH_LENGTH("SP=0x"), uc->uc_mcontext.sp, buffer);
crash_dump_write_reg(LITERAL_WITH_LENGTH("PC=0x"), uc->uc_mcontext.pc, buffer);
crash_dump_write_reg(LITERAL_WITH_LENGTH("PSTATE=0x"), uc->uc_mcontext.pstate, buffer);

View File

@ -0,0 +1,44 @@
--- a/common/server/signals.cpp
+++ b/common/server/signals.cpp
@@ -7,7 +7,9 @@
#include <cerrno>
#include <cstdlib>
#include <cstring>
+#ifndef __ANDROID__
#include <execinfo.h>
+#endif
#include <fcntl.h>
#include <pthread.h>
#include <unistd.h>
@@ -42,9 +44,12 @@
}
extra_debug_handler_t extra_debug_handler;
+#ifndef __ANDROID__
static volatile int double_print_backtrace_guard;
+#endif
void print_backtrace() {
+#ifndef __ANDROID__
if (double_print_backtrace_guard) {
kwrite(STDERR_FILENO, "\n---Ignoring recursive print backtrace---\n", 42);
return;
@@ -65,6 +70,7 @@
extra_debug_handler = nullptr;
debug_handler();
}
+#endif /* __ANDROID__ */
}
static pthread_t debug_main_pthread_id;
@@ -224,6 +230,10 @@
}
+#ifdef __ANDROID__
+#define valloc(size) memalign(sysconf(_SC_PAGESIZE),size)
+#endif
+
void set_debug_handlers() {
stack_t stack;
int res = sigaltstack(nullptr, &stack);

View File

@ -0,0 +1,11 @@
--- a/compiler/compiler-settings.cpp
+++ b/compiler/compiler-settings.cpp
@@ -127,7 +127,7 @@
void append_curl(std::string &cxx_flags, std::string &ld_flags) noexcept {
if (!contains_lib(ld_flags, "curl")) {
-#if defined(__APPLE__)
+#if defined(__APPLE__) || defined(__ANDROID__)
static_cast<void>(cxx_flags);
ld_flags += " -lcurl";
#else

View File

@ -0,0 +1,11 @@
--- a/compiler/compiler.cpp
+++ b/compiler/compiler.cpp
@@ -119,7 +119,7 @@
std::string dest_path = G->settings().dest_dir.get();
std::stringstream ss;
- ss << "/tmp/" << std::hex << vk::std_hash(dest_path) << "_kphp_temp_lock";
+ ss << "@TERMUX_PREFIX@/tmp/" << std::hex << vk::std_hash(dest_path) << "_kphp_temp_lock";
locked_filename_ = ss.str();
fd_ = open(locked_filename_.c_str(), O_RDWR | O_CREAT, 0666);

View File

@ -0,0 +1,70 @@
--- a/runtime/allocator.cpp
+++ b/runtime/allocator.cpp
@@ -9,6 +9,36 @@
#include <dlfcn.h>
#include <unistd.h>
+static void *(* volatile malloc_hook_dummy)(size_t, const void *);
+static decltype(malloc_hook_dummy) *malloc_hook_ptr;
+static void *(* volatile realloc_hook_dummy)(void *, size_t, const void *);
+static decltype(realloc_hook_dummy) *realloc_hook_ptr;
+static void (* volatile free_hook_dummy)(void *, const void *);
+static decltype(free_hook_dummy) *free_hook_ptr;
+static void *(* volatile memalign_hook_dummy)(size_t, size_t, const void *);
+static decltype(memalign_hook_dummy) *memalign_hook_ptr;
+
+static void malloc_hook_init(void) {
+ static bool initialized = false;
+ if (initialized) return;
+ void *libc_handle = dlopen("libc.so", RTLD_NOW);
+ if (libc_handle != NULL) {
+ malloc_hook_ptr = (decltype(malloc_hook_ptr))dlsym(libc_handle, "__malloc_hook");
+ realloc_hook_ptr = (decltype(realloc_hook_ptr))dlsym(libc_handle, "__realloc_hook");
+ free_hook_ptr = (decltype(free_hook_ptr))dlsym(libc_handle, "__free_hook");
+ memalign_hook_ptr = (decltype(memalign_hook_ptr))dlsym(libc_handle, "__memalign_hook");
+ }
+ if (malloc_hook_ptr == NULL)
+ malloc_hook_ptr = &malloc_hook_dummy;
+ if (realloc_hook_ptr == NULL)
+ realloc_hook_ptr = &realloc_hook_dummy;
+ if (free_hook_ptr == NULL)
+ free_hook_ptr = &free_hook_dummy;
+ if (memalign_hook_ptr == NULL)
+ memalign_hook_ptr = &memalign_hook_dummy;
+ initialized = true;
+}
+
#include "common/algorithms/find.h"
#include "common/containers/final_action.h"
#include "common/macos-ports.h"
@@ -246,10 +276,11 @@
void switch_hooks(bool malloc_hooks_are_replaced_before) noexcept {
php_assert(malloc_hooks_are_replaced_before == malloc_hooks_are_replaced_);
CriticalSectionGuard critical_section;
- std::swap(malloc_hook_, __malloc_hook);
- std::swap(realloc_hook_, __realloc_hook);
- std::swap(memalign_hook_, __memalign_hook);
- std::swap(free_hook_, __free_hook);
+ malloc_hook_init();
+ std::swap(malloc_hook_, *malloc_hook_ptr);
+ std::swap(realloc_hook_, *realloc_hook_ptr);
+ std::swap(memalign_hook_, *memalign_hook_ptr);
+ std::swap(free_hook_, *free_hook_ptr);
malloc_hooks_are_replaced_ = !malloc_hooks_are_replaced_;
}
@@ -274,10 +305,10 @@
return script_allocator_free(ptr);
}} {}
- decltype(__malloc_hook) malloc_hook_{nullptr};
- decltype(__realloc_hook) realloc_hook_{nullptr};
- decltype(__memalign_hook) memalign_hook_{nullptr};
- decltype(__free_hook) free_hook_{nullptr};
+ decltype(malloc_hook_dummy) malloc_hook_{nullptr};
+ decltype(realloc_hook_dummy) realloc_hook_{nullptr};
+ decltype(memalign_hook_dummy) memalign_hook_{nullptr};
+ decltype(free_hook_dummy) free_hook_{nullptr};
bool malloc_hooks_are_replaced_{false};
};

View File

@ -0,0 +1,13 @@
--- a/runtime/files.cpp
+++ b/runtime/files.cpp
@@ -10,6 +10,10 @@
#include <sys/utsname.h>
#include <unistd.h>
+#ifdef __ANDROID__
+#define __xpg_basename __posix_basename
+#endif
+
#undef basename
#include "common/macos-ports.h"

View File

@ -0,0 +1,31 @@
--- a/runtime/kphp-backtrace.cpp
+++ b/runtime/kphp-backtrace.cpp
@@ -5,7 +5,9 @@
#include "runtime/kphp-backtrace.h"
#include <cxxabi.h>
+#ifndef __ANDROID__
#include <execinfo.h>
+#endif
#include "common/fast-backtrace.h"
#include "common/wrappers/string_view.h"
@@ -14,6 +16,10 @@
std::forward_list<char **> KphpBacktrace::last_used_symbols_;
+#ifdef __ANDROID__
+KphpBacktrace::KphpBacktrace(void **, int32_t) noexcept {
+}
+#else
KphpBacktrace::KphpBacktrace(void **raw_backtrace, int32_t size) noexcept {
dl::CriticalSectionGuard signal_critical_section;
if ((symbols_begin_ = backtrace_symbols(raw_backtrace, size))) {
@@ -21,6 +27,7 @@
last_used_symbols_.emplace_front(symbols_begin_);
}
}
+#endif
void KphpBacktrace::clear() noexcept {
dl::CriticalSectionGuard signal_critical_section;

View File

@ -0,0 +1,14 @@
--- a/runtime/openssl.cpp
+++ b/runtime/openssl.cpp
@@ -21,6 +21,11 @@
#include <sys/time.h>
#include <unistd.h>
+#ifdef __ANDROID__
+#include <netinet/in.h>
+#include <netinet/in6.h>
+#endif
+
#include "common/crc32.h"
#include "common/resolver.h"
#include "common/smart_ptrs/unique_ptr_with_delete_function.h"

View File

@ -0,0 +1,31 @@
--- a/runtime/php_assert.cpp
+++ b/runtime/php_assert.cpp
@@ -12,7 +12,9 @@
#include <csignal>
#include <ctime>
#include <cxxabi.h>
+#ifndef __ANDROID__
#include <execinfo.h>
+#endif
#include <unistd.h>
#include <sys/wait.h>
@@ -46,6 +48,10 @@
return &__start_run_scheduler_section <= address && address <= &__stop_run_scheduler_section;
};
+#ifdef __ANDROID__
+static void print_demangled_adresses(void **, int, int, bool) {
+}
+#else
static void print_demangled_adresses(void **buffer, int nptrs, int num_shift, bool allow_gdb) {
if (php_warning_level == 1) {
for (int i = 0; i < nptrs; i++) {
@@ -87,6 +93,7 @@
}
}
}
+#endif /* __ANDROID__ */
static void php_warning_impl(bool out_of_memory, int error_type, char const *message, va_list args) {
if (php_warning_level == 0 || php_disable_warnings) {

View File

@ -0,0 +1,13 @@
--- a/runtime/string_functions.cpp
+++ b/runtime/string_functions.cpp
@@ -7,6 +7,10 @@
#include <clocale>
#include <sys/types.h>
+#ifdef __ANDROID__
+#include <sys/endian.h>
+#endif
+
#include "common/macos-ports.h"
#include "common/unicode/unicode-utils.h"

View File

@ -0,0 +1,14 @@
--- a/runtime/udp.cpp
+++ b/runtime/udp.cpp
@@ -8,6 +8,11 @@
#include <poll.h>
#include <sys/socket.h>
+#ifdef __ANDROID__
+#include <netinet/in.h>
+#include <netinet/in6.h>
+#endif
+
#include "common/resolver.h"
#include "runtime/critical_section.h"

View File

@ -0,0 +1,29 @@
--- a/server/json-logger.cpp
+++ b/server/json-logger.cpp
@@ -4,7 +4,9 @@
#include <cstring>
#include <cinttypes>
+#ifndef __ANDROID__
#include <execinfo.h>
+#endif
#include <fcntl.h>
#include <unistd.h>
@@ -241,11 +243,16 @@
json_out_it->finish_json_and_flush(json_log_fd_);
}
+#ifdef __ANDROID__
+void JsonLogger::write_log_with_backtrace(vk::string_view, int) noexcept {
+}
+#else
void JsonLogger::write_log_with_backtrace(vk::string_view message, int type) noexcept {
std::array<void *, 64> trace{};
const int trace_size = backtrace(trace.data(), trace.size());
write_log(message, type, time(nullptr), trace.data(), trace_size, true);
}
+#endif
void JsonLogger::write_stack_overflow_log(int type) noexcept {
std::array<void *, 64> trace{};

View File

@ -0,0 +1,78 @@
--- a/server/php-master-restart.cpp
+++ b/server/php-master-restart.cpp
@@ -12,6 +12,48 @@
#include <sys/stat.h>
#include <unistd.h>
+#ifdef __ANDROID__
+#include <alloca.h>
+
+static int shm_open(const char *name, int oflag, mode_t mode) {
+ size_t namelen;
+ char *fname;
+ int fd;
+
+ /* Construct the filename. */
+ while (name[0] == '/') ++name;
+
+ if (name[0] == '\0') {
+ /* The name "/" is not supported. */
+ errno = EINVAL;
+ return -1;
+ }
+
+ namelen = strlen(name);
+ fname = (char *) alloca(sizeof("@TERMUX_PREFIX@/tmp/") - 1 + namelen + 1);
+ memcpy(fname, "@TERMUX_PREFIX@/tmp/", sizeof("@TERMUX_PREFIX@/tmp/") - 1);
+ memcpy(fname + sizeof("@TERMUX_PREFIX@/tmp/") - 1, name, namelen + 1);
+
+ fd = open(fname, oflag, mode);
+ if (fd != -1) {
+ /* We got a descriptor. Now set the FD_CLOEXEC bit. */
+ int flags = fcntl(fd, F_GETFD, 0);
+ flags |= FD_CLOEXEC;
+ flags = fcntl(fd, F_SETFD, flags);
+
+ if (flags == -1) {
+ /* Something went wrong. We cannot return the descriptor. */
+ int save_errno = errno;
+ close(fd);
+ fd = -1;
+ errno = save_errno;
+ }
+ }
+
+ return fd;
+}
+#endif /* __ANDROID__ */
+
#include "common/dl-utils-lite.h"
#include "common/kprintf.h"
#include "common/macos-ports.h"
@@ -29,8 +71,10 @@
int err;
err = pthread_mutexattr_init(&attr);
assert (err == 0 && "failed to init mutexattr");
+#ifndef __ANDROID__
err = pthread_mutexattr_setrobust_np(&attr, PTHREAD_MUTEX_ROBUST_NP);
assert (err == 0 && "failed to setrobust_np for mutex");
+#endif
err = pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_SHARED);
assert (err == 0 && "failed to setpshared for mutex");
@@ -98,11 +142,15 @@
void shared_data_lock(shared_data_t *data) {
int err = pthread_mutex_lock(&data->main_mutex);
if (err != 0) {
+#ifdef __ANDROID__
+ if (0) {
+#else
if (err == EOWNERDEAD) {
vkprintf(1, "owner of shared memory mutex is dead. trying to make mutex and memory consitent\n");
err = pthread_mutex_consistent_np(&data->main_mutex);
assert (err == 0 && "failed to make mutex constistent_np");
+#endif
} else {
assert (0 && "unknown mutex lock error");
}

View File

@ -0,0 +1,14 @@
--- a/server/php-master-restart.h
+++ b/server/php-master-restart.h
@@ -51,7 +51,11 @@
#if !defined(__APPLE__)
static constexpr size_t MASTER_DATA_T_SIZEOF = 272;
+#ifdef __ANDROID__
+static constexpr size_t SHARED_DATA_T_SIZEOF = 648;
+#else
static constexpr size_t SHARED_DATA_T_SIZEOF = 656;
+#endif
static_assert(sizeof(master_data_t) == MASTER_DATA_T_SIZEOF, "Layout of this struct must be the same in any KPHP version unless shared data magic is used, "
"otherwise restart won't work");

View File

@ -0,0 +1,44 @@
--- a/server/php-runner.cpp
+++ b/server/php-runner.cpp
@@ -9,11 +9,17 @@
#include <cstdlib>
#include <cstring>
#include <exception>
+#ifndef __ANDROID__
#include <execinfo.h>
+#endif
#include <sys/mman.h>
#include <sys/time.h>
#include <unistd.h>
+#ifdef __ANDROID__
+#define valloc(size) memalign(sysconf(_SC_PAGESIZE),size)
+#endif
+
#include "common/fast-backtrace.h"
#include "common/kernel-version.h"
#include "common/kprintf.h"
@@ -518,7 +524,11 @@
print_prologue(cur_time);
void *trace[64];
+#ifndef __ANDROID__
const int trace_size = backtrace(trace, 64);
+#else
+ const int trace_size = 0;
+#endif
void *addr = info->si_addr;
if (PHPScriptBase::is_running && PHPScriptBase::current_script->is_protected(static_cast<char *>(addr))) {
@@ -549,7 +559,11 @@
void sigabrt_handler(int) {
const int64_t cur_time = time(nullptr);
void *trace[64];
+#ifndef __ANDROID__
const int trace_size = backtrace(trace, 64);
+#else
+ const int trace_size = 0;
+#endif
vk::string_view msg{dl_get_assert_message()};
if (msg.empty()) {
msg = "SIGABRT terminating program";

View File

@ -0,0 +1,21 @@
--- a/server/ucontext-portable.h
+++ b/server/ucontext-portable.h
@@ -4,7 +4,7 @@
#pragma once
-#if defined(__x86_64__)
+#if defined(__x86_64__) && !defined(__ANDROID__)
// for x86, we just use makecontext(), ucontext_t and other native functions
#include <ucontext.h>
@@ -14,7 +14,8 @@
#define makecontext_portable makecontext
#define swapcontext_portable swapcontext
-#elif defined(__arm64__)
+#elif defined(__arm64__) || \
+ defined(__ANDROID__) && (defined(__aarch64__) || defined(__x86_64__))
// for M1, we can't use native makecontext() and others: they compile, but hang up when called
// instead, we require the ucontext library: https://github.com/kaniini/libucontext
// see the docs: https://vkcom.github.io/kphp/kphp-internals/developing-and-extending-kphp/compiling-kphp-from-sources.html