Skip to content

Commit

Permalink
support build under ubuntu 22 (VKCOM#673)
Browse files Browse the repository at this point in the history
* fix build: pthread

* fix build: stacksize

* fix build: suppress openssl's deprecated declarations warnings; fix const-correctness

* fix build: signature discrepancy

* fix build: link both ssl and crypto statically
  • Loading branch information
drdzyk authored Nov 15, 2022
1 parent 8c5a537 commit 1e04955
Show file tree
Hide file tree
Showing 9 changed files with 32 additions and 13 deletions.
1 change: 1 addition & 0 deletions cmake/init-compilation-flags.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ if(APPLE)
set(OPENSSL_ROOT_DIR "/usr/local/opt/openssl" CACHE INTERNAL "")
endif()

set(OPENSSL_USE_STATIC_LIBS TRUE)
find_package(OpenSSL REQUIRED)
include_directories(${OPENSSL_INCLUDE_DIR})

Expand Down
6 changes: 6 additions & 0 deletions common/crypto/aes256-generic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@

#include <openssl/aes.h>

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"

void crypto_generic_aes256_set_encrypt_key(vk_aes_ctx_t *vk_ctx, const uint8_t key[32]) {
AES_set_encrypt_key(key, AES256_KEY_BITS, &vk_ctx->u.key);
}
Expand Down Expand Up @@ -74,3 +77,6 @@ void crypto_generic_aes256_ctr_encrypt(vk_aes_ctx_t *vk_ctx, const uint8_t *in,
} while (i < l);
}
}

#pragma GCC diagnostic pop

4 changes: 2 additions & 2 deletions common/macos-ports.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,11 @@ inline int prctl(int, unsigned long) noexcept {

#define PTHREAD_MUTEX_ROBUST_NP 2

inline int pthread_mutexattr_setrobust_np(const pthread_mutexattr_t *, int) noexcept {
inline int pthread_mutexattr_setrobust(const pthread_mutexattr_t *, int) noexcept {
return 0;
}

inline int pthread_mutex_consistent_np(pthread_mutex_t *) noexcept {
inline int pthread_mutex_consistent(pthread_mutex_t *) noexcept {
return 0;
}

Expand Down
12 changes: 10 additions & 2 deletions compiler/compiler-settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,9 @@ void append_apple_options(std::string &cxx_flags, std::string &ld_flags) noexcep
#endif
}

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"

std::string calc_cxx_flags_sha256(vk::string_view cxx, vk::string_view cxx_flags_line) noexcept {
SHA256_CTX sha256;
SHA256_Init(&sha256);
Expand All @@ -177,6 +180,8 @@ std::string calc_cxx_flags_sha256(vk::string_view cxx, vk::string_view cxx_flags
return hash_str;
}

#pragma GCC diagnostic pop

} // namespace

void CxxFlags::init(const std::string &runtime_sha256, const std::string &cxx,
Expand Down Expand Up @@ -309,7 +314,7 @@ void CompilerSettings::init() {
ld_flags.value_ = extra_ld_flags.get();
append_curl(cxx_default_flags, ld_flags.value_);
append_apple_options(cxx_default_flags, ld_flags.value_);
std::vector<vk::string_view> external_static_libs{"pcre", "re2", "yaml-cpp", "h3", "ssl", "z", "zstd", "nghttp2", "kphp-timelib"};
std::vector<vk::string_view> external_static_libs{"pcre", "re2", "yaml-cpp", "h3", "z", "zstd", "nghttp2", "kphp-timelib"};

#ifdef KPHP_TIMELIB_LIB_DIR
ld_flags.value_ += " -L" KPHP_TIMELIB_LIB_DIR;
Expand All @@ -326,7 +331,7 @@ void CompilerSettings::init() {
ld_flags.value_ += " /opt/homebrew/lib/libucontext.a";
#endif

std::vector<vk::string_view> external_libs{"pthread", "crypto", "m", "dl"};
std::vector<vk::string_view> external_libs{"pthread", "m", "dl"};

#ifdef PDO_DRIVER_MYSQL
#ifdef PDO_LIBS_STATIC_LINKING
Expand All @@ -336,6 +341,9 @@ void CompilerSettings::init() {
#endif
#endif

external_static_libs.emplace_back("ssl");
external_static_libs.emplace_back("crypto");

#if defined(__APPLE__)
append_if_doesnt_contain(ld_flags.value_, external_static_libs, "-l");
auto flex_prefix = kphp_src_path.value_ + "objs/flex/lib";
Expand Down
2 changes: 1 addition & 1 deletion runtime/memory_usage.h
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,6 @@ int64_t f$estimate_memory_usage(const class_instance<T> &value) {

template<typename Int, typename>
array<int64_t> f$get_global_vars_memory_stats(Int lower_bound) {
array<int64_t> get_global_vars_memory_stats_impl(int64_t);
array<int64_t> get_global_vars_memory_stats_impl(int64_t) noexcept;
return get_global_vars_memory_stats_impl(lower_bound);
}
12 changes: 8 additions & 4 deletions runtime/openssl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,8 @@ static EVP_PKEY *openssl_get_public_evp(const string &key, bool &from_cache) {
return evp_pkey;
}

using RSA_ptr = vk::unique_ptr_with_delete_function<rsa_st, RSA_free>;

bool f$openssl_public_encrypt(const string &data, string &result, const string &key) {
bool from_cache = false;
dl::CriticalSectionSmartGuard critical_section;
Expand All @@ -372,8 +374,9 @@ bool f$openssl_public_encrypt(const string &data, string &result, const string &
int key_size = EVP_PKEY_size(pkey);
php_assert (PHP_BUF_LEN >= key_size);

if (RSA_public_encrypt((int)data.size(), reinterpret_cast <const unsigned char *> (data.c_str()),
reinterpret_cast <unsigned char *> (php_buf), EVP_PKEY_get0_RSA(pkey), RSA_PKCS1_PADDING) != key_size) {
RSA_ptr rsa{EVP_PKEY_get1_RSA(pkey)};
if (RSA_public_encrypt(static_cast<int>(data.size()), reinterpret_cast<const unsigned char *>(data.c_str()),
reinterpret_cast<unsigned char *>(php_buf), rsa.get(), RSA_PKCS1_PADDING) != key_size) {
if (!from_cache) {
EVP_PKEY_free(pkey);
}
Expand Down Expand Up @@ -423,8 +426,9 @@ bool f$openssl_private_decrypt(const string &data, string &result, const string
int key_size = EVP_PKEY_size(pkey);
php_assert (PHP_BUF_LEN >= key_size);

int len = RSA_private_decrypt((int)data.size(), reinterpret_cast <const unsigned char *> (data.c_str()),
reinterpret_cast <unsigned char *> (php_buf), EVP_PKEY_get0_RSA(pkey), RSA_PKCS1_PADDING);
RSA_ptr rsa{EVP_PKEY_get1_RSA(pkey)};
int len = RSA_private_decrypt(static_cast<int>(data.size()), reinterpret_cast<const unsigned char *>(data.c_str()),
reinterpret_cast<unsigned char *>(php_buf), rsa.get(), RSA_PKCS1_PADDING);
if (!from_cache) {
EVP_PKEY_free(pkey);
}
Expand Down
2 changes: 1 addition & 1 deletion runtime/runtime.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ target_link_libraries(kphp-full-runtime PUBLIC ${RUNTIME_LIBS})
set_target_properties(kphp-full-runtime PROPERTIES ARCHIVE_OUTPUT_DIRECTORY ${OBJS_DIR})

prepare_cross_platform_libs(RUNTIME_LINK_TEST_LIBS pcre nghttp2 kphp-timelib)
set(RUNTIME_LINK_TEST_LIBS vk::flex_data_static OpenSSL::SSL ${CURL_LIB} ${NUMA_LIB} ${RUNTIME_LINK_TEST_LIBS} ${EPOLL_SHIM_LIB} ${ICONV_LIB} ${RT_LIB})
set(RUNTIME_LINK_TEST_LIBS vk::flex_data_static ${CURL_LIB} OpenSSL::SSL ${NUMA_LIB} ${RUNTIME_LINK_TEST_LIBS} ${EPOLL_SHIM_LIB} ${ICONV_LIB} ${RT_LIB})

if (PDO_DRIVER_MYSQL)
list(APPEND RUNTIME_LINK_TEST_LIBS mysqlclient)
Expand Down
4 changes: 2 additions & 2 deletions server/php-master-restart.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ void init_mutex(pthread_mutex_t *mutex) {
int err;
err = pthread_mutexattr_init(&attr);
assert (err == 0 && "failed to init mutexattr");
err = pthread_mutexattr_setrobust_np(&attr, PTHREAD_MUTEX_ROBUST_NP);
err = pthread_mutexattr_setrobust(&attr, PTHREAD_MUTEX_ROBUST_NP);
assert (err == 0 && "failed to setrobust_np for mutex");
err = pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_SHARED);
assert (err == 0 && "failed to setpshared for mutex");
Expand Down Expand Up @@ -101,7 +101,7 @@ void shared_data_lock(shared_data_t *data) {
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);
err = pthread_mutex_consistent(&data->main_mutex);
assert (err == 0 && "failed to make mutex constistent_np");
} else {
assert (0 && "unknown mutex lock error");
Expand Down
2 changes: 1 addition & 1 deletion server/php-runner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -674,7 +674,7 @@ void check_stack_overflow() {

//C interface
void init_handlers() {
constexpr size_t SEGV_STACK_SIZE = MINSIGSTKSZ + 65536;
constexpr size_t SEGV_STACK_SIZE = 65536;
static std::array<char, SEGV_STACK_SIZE> buffer;

stack_t segv_stack;
Expand Down

0 comments on commit 1e04955

Please sign in to comment.