diff --git a/assembler/BUILD.gn b/assembler/BUILD.gn index d9e3073566aeabf8fdde2a7b09c8437a9f51a683..9d02b3ae8a0c96e381b2f37ec4c38a91c48552b3 100644 --- a/assembler/BUILD.gn +++ b/assembler/BUILD.gn @@ -69,6 +69,9 @@ ohos_shared_library("libarkassembler") { relative_install_dir = "ark" } output_extension = "so" + if (is_mingw) { + output_extension = "dll" + } subsystem_name = "ark" part_name = "ark" } diff --git a/disassembler/BUILD.gn b/disassembler/BUILD.gn index d5f47e81ca79cef37cfaf1356929d28b47789135..aa057920ab8831c2099bd18dced7098995c95d9f 100644 --- a/disassembler/BUILD.gn +++ b/disassembler/BUILD.gn @@ -56,6 +56,9 @@ ohos_shared_library("arkdisassembler") { relative_install_dir = "ark" } output_extension = "so" + if (is_mingw) { + output_extension = "dll" + } subsystem_name = "ark" part_name = "ark" } diff --git a/dprof/daemon/main.cpp b/dprof/daemon/main.cpp index b7b2dfd9ea8c7e9d69fddc2246e376b884ac29bb..2b1ccc66ae713cce98e9b9dd29b7d68a6f16c7a2 100644 --- a/dprof/daemon/main.cpp +++ b/dprof/daemon/main.cpp @@ -30,7 +30,7 @@ #include "generated/daemon_options.h" namespace panda::dprof { -bool CheckVersion(const os::unix::UniqueFd &sock) +bool CheckVersion(const os::unique_fd::UniqueFd &sock) { // Get version ipc::Message msg; @@ -54,7 +54,7 @@ bool CheckVersion(const os::unix::UniqueFd &sock) return true; } -static std::unique_ptr ProcessingConnect(const os::unix::UniqueFd &sock) +static std::unique_ptr ProcessingConnect(const os::unique_fd::UniqueFd &sock) { if (!CheckVersion(sock)) { return nullptr; @@ -106,7 +106,7 @@ static std::unique_ptr ProcessingConnect(const os::unix::UniqueFd &sock class Worker { public: - void EnqueueClientSocket(os::unix::UniqueFd clientSock) + void EnqueueClientSocket(os::unique_fd::UniqueFd clientSock) { os::memory::LockHolder lock(queue_lock_); queue_.push(std::move(clientSock)); @@ -130,7 +130,7 @@ public: void DoRun(AppDataStorage *storage) { while (!done_) { - os::unix::UniqueFd clientSock; + os::unique_fd::UniqueFd clientSock; { os::memory::LockHolder lock(queue_lock_); while (queue_.empty() && !done_) { @@ -157,7 +157,7 @@ public: private: std::thread thread_; os::memory::Mutex queue_lock_; - std::queue queue_; + std::queue queue_; os::memory::ConditionVariable cond_ GUARDED_BY(queue_lock_); bool done_ = false; }; @@ -245,7 +245,7 @@ static int Main(panda::Span args) } // Create server socket - os::unix::UniqueFd sock(ipc::CreateUnixServerSocket(MAX_PENDING_CONNECTIONS_QUEUE)); + os::unique_fd::UniqueFd sock(ipc::CreateUnixServerSocket(MAX_PENDING_CONNECTIONS_QUEUE)); if (!sock.IsValid()) { LOG(FATAL, DPROF) << "Cannot create socket"; return -1; @@ -257,7 +257,7 @@ static int Main(panda::Span args) LOG(INFO, DPROF) << "Daemon is ready for connections"; // Main loop while (!g_done) { - os::unix::UniqueFd clientSock(::accept4(sock.Get(), nullptr, nullptr, SOCK_CLOEXEC)); + os::unique_fd::UniqueFd clientSock(::accept4(sock.Get(), nullptr, nullptr, SOCK_CLOEXEC)); if (!clientSock.IsValid()) { if (errno == EINTR) { continue; diff --git a/dprof/libdprof/dprof/ipc/ipc_unix_socket.cpp b/dprof/libdprof/dprof/ipc/ipc_unix_socket.cpp index 8055315d52893d4d1d463b2ae1be19972277bf87..e17c7306a783733fa1517ed3d83d4328fe42e6ef 100644 --- a/dprof/libdprof/dprof/ipc/ipc_unix_socket.cpp +++ b/dprof/libdprof/dprof/ipc/ipc_unix_socket.cpp @@ -15,7 +15,7 @@ #include "ipc_unix_socket.h" -#include "os/unix/failure_retry.h" +#include "os/failure_retry.h" #include "utils/logger.h" #include "securec.h" @@ -29,14 +29,14 @@ namespace panda::dprof::ipc { constexpr char SOCKET_NAME[] = "\0dprof.socket"; // NOLINT(modernize-avoid-c-arrays) static_assert(sizeof(SOCKET_NAME) <= sizeof(static_cast(nullptr)->sun_path), "Socket name too large"); -os::unix::UniqueFd CreateUnixServerSocket(int backlog) +os::unique_fd::UniqueFd CreateUnixServerSocket(int backlog) { - os::unix::UniqueFd sock(PANDA_FAILURE_RETRY(::socket(AF_UNIX, SOCK_STREAM, 0))); + os::unique_fd::UniqueFd sock(PANDA_FAILURE_RETRY(::socket(AF_UNIX, SOCK_STREAM, 0))); int opt = 1; if (PANDA_FAILURE_RETRY(::setsockopt(sock.Get(), SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt))) == -1) { PLOG(ERROR, DPROF) << "setsockopt() failed"; - return os::unix::UniqueFd(); + return os::unique_fd::UniqueFd(); } struct sockaddr_un serverAddr { @@ -53,23 +53,23 @@ os::unix::UniqueFd CreateUnixServerSocket(int backlog) if (PANDA_FAILURE_RETRY(::bind(sock.Get(), reinterpret_cast(&serverAddr), sizeof(serverAddr))) == -1) { PLOG(ERROR, DPROF) << "bind() failed"; - return os::unix::UniqueFd(); + return os::unique_fd::UniqueFd(); } if (::listen(sock.Get(), backlog) == -1) { PLOG(ERROR, DPROF) << "listen() failed"; - return os::unix::UniqueFd(); + return os::unique_fd::UniqueFd(); } return sock; } -os::unix::UniqueFd CreateUnixClientSocket() +os::unique_fd::UniqueFd CreateUnixClientSocket() { - os::unix::UniqueFd sock(PANDA_FAILURE_RETRY(::socket(AF_UNIX, SOCK_STREAM, 0))); + os::unique_fd::UniqueFd sock(PANDA_FAILURE_RETRY(::socket(AF_UNIX, SOCK_STREAM, 0))); if (!sock.IsValid()) { PLOG(ERROR, DPROF) << "socket() failed"; - return os::unix::UniqueFd(); + return os::unique_fd::UniqueFd(); } struct sockaddr_un serverAddr { @@ -86,7 +86,7 @@ os::unix::UniqueFd CreateUnixClientSocket() if (PANDA_FAILURE_RETRY( ::connect(sock.Get(), reinterpret_cast(&serverAddr), sizeof(serverAddr))) == -1) { PLOG(ERROR, DPROF) << "connect() failed"; - return os::unix::UniqueFd(); + return os::unique_fd::UniqueFd(); } return sock; diff --git a/dprof/libdprof/dprof/ipc/ipc_unix_socket.h b/dprof/libdprof/dprof/ipc/ipc_unix_socket.h index 17b5568089857256830466df4bbc906f48e243b8..2e7a1e6dbeeb7ea1d639163fb6c4be8c221d8a55 100644 --- a/dprof/libdprof/dprof/ipc/ipc_unix_socket.h +++ b/dprof/libdprof/dprof/ipc/ipc_unix_socket.h @@ -16,11 +16,11 @@ #ifndef PANDA_DPROF_LIBDPROF_DPROF_IPC_IPC_UNIX_SOCKET_H_ #define PANDA_DPROF_LIBDPROF_DPROF_IPC_IPC_UNIX_SOCKET_H_ -#include "os/unix/unique_fd.h" +#include "os/unique_fd.h" namespace panda::dprof::ipc { -os::unix::UniqueFd CreateUnixServerSocket(int backlog); -os::unix::UniqueFd CreateUnixClientSocket(); +os::unique_fd::UniqueFd CreateUnixServerSocket(int backlog); +os::unique_fd::UniqueFd CreateUnixClientSocket(); bool WaitDataTimeout(int fd, int timeoutMs); bool SendAll(int fd, const void *buf, int len); diff --git a/dprof/libdprof/dprof/profiling_data.cpp b/dprof/libdprof/dprof/profiling_data.cpp index 3953d2f5b586c71bddc74c3de44d1c6965b0485f..018c82599b7ddeda56262d800080d9afd94c2ef3 100644 --- a/dprof/libdprof/dprof/profiling_data.cpp +++ b/dprof/libdprof/dprof/profiling_data.cpp @@ -35,7 +35,7 @@ bool ProfilingData::SetFeatureDate(const std::string &featureName, std::vectorGetSize() << " at addr = " << std::hex << arena; arena->~Arena(); -#ifdef PANDA_TARGET_WINDOWS - // std::free can not work with _aligned_malloc. it will lead to some weird crash - _aligned_free(arena); -#else - std::free(arena); // NOLINT(cppcoreguidelines-no-malloc) -#endif - + os::mem::AlignedFree(arena); LOG_MALLOC_MEM_POOL(DEBUG) << "Free arena call finished"; } diff --git a/libpandabase/os/dfx_option.h b/libpandabase/os/dfx_option.h index 474b1cca4c8ce936c0107be3295829fd3e34b9c1..6cfa126c126882fc5876c51a272fd13dc88b9d4c 100644 --- a/libpandabase/os/dfx_option.h +++ b/libpandabase/os/dfx_option.h @@ -41,8 +41,9 @@ namespace panda::os::dfx_option { DFX_OPTION_ELEM(D, END_FLAG, "end-flag") #else // NOLINTNEXTLINE(cppcoreguidelines-macro-usage) -#define DFX_OPTION_LIST(D) \ - DFX_OPTION_ELEM(D, DFXLOG, "dfx-log") \ +#define DFX_OPTION_LIST(D) \ + DFX_OPTION_ELEM(D, REFERENCE_DUMP, "reference-dump") \ + DFX_OPTION_ELEM(D, DFXLOG, "dfx-log") \ DFX_OPTION_ELEM(D, END_FLAG, "end-flag") #endif // PANDA_TARGET_UNIX @@ -51,7 +52,6 @@ public: enum DfxOptionId : uint8_t { #ifdef PANDA_TARGET_UNIX COMPILER_NULLCHECK_ID, - REFERENCE_DUMP_ID, SIGNAL_CATCHER_ID, SIGNAL_HANDLER_ID, ARK_SIGQUIT_ID, @@ -59,6 +59,7 @@ public: ARK_SIGUSR2_ID, MOBILE_LOG_ID, #endif // PANDA_TARGET_UNIX + REFERENCE_DUMP_ID, DFXLOG_ID, END_FLAG_ID, }; diff --git a/libpandabase/os/unix/failure_retry.h b/libpandabase/os/failure_retry.h similarity index 83% rename from libpandabase/os/unix/failure_retry.h rename to libpandabase/os/failure_retry.h index 011f2fc7f9db57065a6c4e64b8a142a46495b48b..2d914c1cfd6473881c8ec84375f2510d2d7ee589 100644 --- a/libpandabase/os/unix/failure_retry.h +++ b/libpandabase/os/failure_retry.h @@ -16,8 +16,10 @@ #ifndef PANDA_LIBPANDABASE_OS_UNIX_FAILURE_RETRY_H_ #define PANDA_LIBPANDABASE_OS_UNIX_FAILURE_RETRY_H_ +#ifdef PANDA_TARGET_UNIX // Mac Os' libc doesn't have this macro #ifndef TEMP_FAILURE_RETRY +// NOLINTNEXTLINE(cppcoreguidelines-macro-usage) #define TEMP_FAILURE_RETRY(exp) \ (__extension__({ \ decltype(exp) _result; \ @@ -30,5 +32,11 @@ // NOLINTNEXTLINE(cppcoreguidelines-macro-usage) #define PANDA_FAILURE_RETRY(exp) (__extension__ TEMP_FAILURE_RETRY(exp)) +#elif PANDA_TARGET_WINDOWS +// Windows Os does not support TEMP_FAILURE_RETRY macro +#define PANDA_FAILURE_RETRY(exp) (exp) +#else +#error "Unsupported platform" +#endif // PANDA_TARGET_UNIX #endif // PANDA_LIBPANDABASE_OS_UNIX_FAILURE_RETRY_H_ diff --git a/libpandabase/os/filesystem.cpp b/libpandabase/os/filesystem.cpp index fe4c92f8f694776aaea56fec9b22684d22dc7be1..6c6bb124b38f4d93beb73d1cf698ef7726264a42 100644 --- a/libpandabase/os/filesystem.cpp +++ b/libpandabase/os/filesystem.cpp @@ -19,6 +19,9 @@ defined PANDA_TARGET_ARM64 #include #endif +#if defined(PANDA_TARGET_WINDOWS) +#include +#endif namespace panda::os { diff --git a/libpandabase/os/filesystem.h b/libpandabase/os/filesystem.h index 6e2ced6a789da8aacdc6651274a0b16f0ac6416a..2693a8aeb1b2e0ddb6792ac9b674cf260f939792 100644 --- a/libpandabase/os/filesystem.h +++ b/libpandabase/os/filesystem.h @@ -20,7 +20,6 @@ #include #if defined(PANDA_TARGET_WINDOWS) -#include #ifndef NAME_MAX constexpr size_t NAME_MAX = 255; #endif // NAME_MAX diff --git a/libpandabase/os/library_loader.h b/libpandabase/os/library_loader.h index 456fd85acef51ad9d1a8ad0162282a9fc6e780c8..e77ced28de0ebc9721d56afd5add8ab69e2e5540 100644 --- a/libpandabase/os/library_loader.h +++ b/libpandabase/os/library_loader.h @@ -18,6 +18,8 @@ #ifdef PANDA_TARGET_UNIX #include "os/unix/library_loader.h" +#elif PANDA_TARGET_WINDOWS +#include "os/windows/library_loader.h" #else #error "Unsupported platform" #endif @@ -28,15 +30,8 @@ #include namespace panda::os::library_loader { - -#ifdef PANDA_TARGET_UNIX -using LibraryHandle = panda::os::unix::library_loader::LibraryHandle; -#endif - Expected Load(std::string_view filename); - Expected ResolveSymbol(const LibraryHandle &handle, std::string_view name); - } // namespace panda::os::library_loader #endif // PANDA_LIBPANDABASE_OS_LIBRARY_LOADER_H_ diff --git a/libpandabase/os/mem.h b/libpandabase/os/mem.h index 88ee21ecd3b17dbff7811d55b06e8387d4256d7a..fa9bd61f084c837a87de2f9c5b5a80f8070f5317 100644 --- a/libpandabase/os/mem.h +++ b/libpandabase/os/mem.h @@ -40,6 +40,15 @@ namespace panda::os::mem { void MmapDeleter(std::byte *ptr, size_t size) noexcept; +/** + * \brief Make memory region \param mem with size \param size with protection flags \param prot + * @param mem Pointer to memory region (should be aligned to page size) + * @param size Size of memory region + * @param prot Memory protection flags, a combination of MMAP_PROT_XXX values + * @return Error object if any errors occur + */ +std::optional MakeMemWithProtFlag(void *mem, size_t size, int prot); + /** * \brief Make memory region \param mem with size \param size readable and executable * @param mem Pointer to memory region (should be aligned to page size) @@ -72,12 +81,20 @@ std::optional MakeMemReadOnly(void *mem, size_t size); uintptr_t AlignDownToPageSize(uintptr_t addr); /** + * \brief Allocated aligned memory with alignment \param alignment_in_bytes and + * with size \param size. Use AlignedFree to free this memory. * @param alignment_in_bytes - alignment in bytes * @param size - min required size in bytes * @return */ void *AlignedAlloc(size_t alignment_in_bytes, size_t size); +/** + * \brief Free memory, allocated by AlignedAlloc. + * @param mem - Pointer to memory, allocated by AlignedAlloc + */ +void AlignedFree(void *mem); + template class MapRange { public: @@ -348,7 +365,7 @@ inline void ReleasePages([[maybe_unused]] uintptr_t pages_start, [[maybe_unused] #ifdef PANDA_TARGET_UNIX madvise(ToVoidPtr(pages_start), pages_end - pages_start, MADV_DONTNEED); #else - UNREACHABLE(); + // On windows systems we can do nothing #endif } diff --git a/libpandabase/os/mem_hooks.h b/libpandabase/os/mem_hooks.h new file mode 100644 index 0000000000000000000000000000000000000000..f8d08d0ff2bc40c1511e536b0c926cb798ff433a --- /dev/null +++ b/libpandabase/os/mem_hooks.h @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef PANDA_LIBPANDABASE_OS_MEM_HOOKS_H_ +#define PANDA_LIBPANDABASE_OS_MEM_HOOKS_H_ + +#ifdef PANDA_TARGET_UNIX +#include "os/unix/mem_hooks.h" +#elif PANDA_TARGET_WINDOWS +#include "os/windows/mem_hooks.h" +#else +#error "Unsupported platform" +#endif // PANDA_TARGET_UNIX + +#endif // PANDA_LIBPANDABASE_OS_MEM_HOOKS_H_ diff --git a/libpandabase/os/unix/mutex.cpp b/libpandabase/os/mutex.cpp similarity index 99% rename from libpandabase/os/unix/mutex.cpp rename to libpandabase/os/mutex.cpp index b98ecad2b8c332285a290f1b083568f6c84eefda..f8cad440c7d8b6884104e67c9c40566d26132db9 100644 --- a/libpandabase/os/unix/mutex.cpp +++ b/libpandabase/os/mutex.cpp @@ -14,15 +14,12 @@ */ #include "mutex.h" - #include "utils/logger.h" #include - #include namespace panda::os::unix::memory { - const int64_t MILLISECONDS_PER_SEC = 1000; const int64_t NANOSECONDS_PER_MILLISEC = 1000000; const int64_t NANOSECONDS_PER_SEC = 1000000000; @@ -199,5 +196,4 @@ bool ConditionVariable::TimedWait(Mutex *mutex, uint64_t ms, uint64_t ns, bool i FatalIfError("pthread_cond_timedwait", rc); return false; } - } // namespace panda::os::unix::memory diff --git a/libpandabase/os/native_stack.h b/libpandabase/os/native_stack.h index 8b0ee2d366db1f4caa1b57261af345c5e5ab6450..694e145f0686234072bd7b77cd71de250212f633 100644 --- a/libpandabase/os/native_stack.h +++ b/libpandabase/os/native_stack.h @@ -16,17 +16,19 @@ #ifndef PANDA_LIBPANDABASE_OS_NATIVE_STACK_H_ #define PANDA_LIBPANDABASE_OS_NATIVE_STACK_H_ +#include "os/thread.h" #if defined(PANDA_TARGET_UNIX) #include "os/unix/native_stack.h" #endif // PANDA_TARGET_UNIX + #include #include #include // NOLINTNEXTLINE(modernize-deprecated-headers) namespace panda::os::native_stack { -const auto g_PandaThreadSigmask = pthread_sigmask; // NOLINT(readability-identifier-naming) #if defined(PANDA_TARGET_UNIX) +const auto g_PandaThreadSigmask = pthread_sigmask; // NOLINT(readability-identifier-naming) using DumpUnattachedThread = panda::os::unix::native_stack::DumpUnattachedThread; const auto DumpKernelStack = panda::os::unix::native_stack::DumpKernelStack; // NOLINT(readability-identifier-naming) const auto GetNativeThreadNameForFile = // NOLINT(readability-identifier-naming) diff --git a/libpandabase/os/sighook.h b/libpandabase/os/sighook.h new file mode 100644 index 0000000000000000000000000000000000000000..a0b9bce4557a2a5e386ccf592d368ae7b6fe3380 --- /dev/null +++ b/libpandabase/os/sighook.h @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef PANDA_LIBPANDABASE_OS_SIGHOOK_H_ +#define PANDA_LIBPANDABASE_OS_SIGHOOK_H_ + +#ifdef PANDA_TARGET_UNIX +#include "os/unix/sighook.h" +#else +#error "Unsupported platform" +#endif // PANDA_TARGET_UNIX + +#endif // PANDA_LIBPANDABASE_OS_SIGHOOK_H_ diff --git a/libpandabase/os/thread.h b/libpandabase/os/thread.h index 00010fe5c1fec7ea6a001a27344b207de29f86e3..3038f7faa94fea701dc006249a73d3e6372d5d83 100644 --- a/libpandabase/os/thread.h +++ b/libpandabase/os/thread.h @@ -22,6 +22,7 @@ #include #include #include +#include namespace panda::os::thread { @@ -108,7 +109,11 @@ static void *ProxyFunc(void *args) template native_handle_type ThreadStart(Func *func, Args... args) { +#ifdef PANDA_TARGET_UNIX native_handle_type tid; +#else + pthread_t tid; +#endif auto args_tuple = std::make_tuple(func, std::move(args)...); internal::SharedPtrStruct *ptr = nullptr; { @@ -124,7 +129,11 @@ native_handle_type ThreadStart(Func *func, Args... args) pthread_create(&tid, nullptr, &internal::ProxyFunc::value>, static_cast(ptr)); +#ifdef PANDA_TARGET_UNIX return tid; +#else + return reinterpret_cast(tid); +#endif } } // namespace panda::os::thread diff --git a/libpandabase/os/time.cpp b/libpandabase/os/time.cpp index 8c3173225c061658e7e1e0a8e746d0271fdc84cb..ac9d8f47f01046fb200220e69d6fe7bc34d0ebec 100644 --- a/libpandabase/os/time.cpp +++ b/libpandabase/os/time.cpp @@ -16,20 +16,27 @@ #include "os/time.h" namespace panda::os::time { -#if !defined(PANDA_TARGET_UNIX) +/** + * Return current time in nanoseconds + */ uint64_t GetClockTimeInMicro() { - return 0; + return GetClockTime(CLOCK_MONOTONIC); } +/** + * Return current time in milliseconds + */ uint64_t GetClockTimeInMilli() { - return 0; + return GetClockTime(CLOCK_MONOTONIC); } +/** + * Return thread CPU time in nanoseconds + */ uint64_t GetClockTimeInThreadCpuTime() { - return 0; + return GetClockTime(CLOCK_THREAD_CPUTIME_ID); } -#endif // PANDA_TARGET_UNIX } // namespace panda::os::time diff --git a/libpandabase/os/time.h b/libpandabase/os/time.h index 594dd88fc0b4b9952a23da77b22825e619efa6c6..d898fead7d758da64ebca3c30b1be4158dbf00eb 100644 --- a/libpandabase/os/time.h +++ b/libpandabase/os/time.h @@ -16,23 +16,20 @@ #ifndef PANDA_LIBPANDABASE_OS_TIME_H_ #define PANDA_LIBPANDABASE_OS_TIME_H_ -#if defined(PANDA_TARGET_UNIX) -#include "os/unix/time_unix.h" +#ifdef PANDA_TARGET_UNIX +#include "os/unix/time.h" +#elif PANDA_TARGET_WINDOWS +#include "os/windows/time.h" +#else +#error "Unsupported platform" #endif // PANDA_TARGET_UNIX + #include namespace panda::os::time { -#if defined(PANDA_TARGET_UNIX) -const auto GetClockTimeInMicro = panda::os::unix::time::GetClockTimeInMicro; // NOLINT(readability-identifier-naming) -const auto GetClockTimeInMilli = panda::os::unix::time::GetClockTimeInMilli; // NOLINT(readability-identifier-naming) -const auto GetClockTimeInThreadCpuTime = // NOLINT(readability-identifier-naming) - panda::os::unix::time::GetClockTimeInThreadCpuTime; -#else uint64_t GetClockTimeInMicro(); uint64_t GetClockTimeInMilli(); uint64_t GetClockTimeInThreadCpuTime(); -#endif // PANDA_TARGET_UNIX - } // namespace panda::os::time #endif // PANDA_LIBPANDABASE_OS_TIME_H_ diff --git a/libpandabase/os/unique_fd.h b/libpandabase/os/unique_fd.h new file mode 100644 index 0000000000000000000000000000000000000000..b3fe7d477b83462a4d1e371734ac578e21736f2d --- /dev/null +++ b/libpandabase/os/unique_fd.h @@ -0,0 +1,95 @@ +/* + * Copyright (c) 2021-2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef PANDA_LIBPANDABASE_OS_UNIQUE_FD_H_ +#define PANDA_LIBPANDABASE_OS_UNIQUE_FD_H_ + +#ifdef PANDA_TARGET_UNIX +#include "os/unix/unique_fd.h" +#elif PANDA_TARGET_WINDOWS +#include "os/windows/unique_fd.h" +#else +#error "Unsupported platform" +#endif // PANDA_TARGET_UNIX + +#include "os/failure_retry.h" +#include "utils/logger.h" + +#include + +namespace panda::os::unique_fd { +class UniqueFd { +public: + explicit UniqueFd(int fd = -1) noexcept + { + Reset(fd); + } + + UniqueFd(const UniqueFd &other_fd) = delete; + UniqueFd &operator=(const UniqueFd &other_fd) = delete; + + UniqueFd(UniqueFd &&other_fd) noexcept + { + Reset(other_fd.Release()); + } + + UniqueFd &operator=(UniqueFd &&other_fd) noexcept + { + Reset(other_fd.Release()); + return *this; + } + + ~UniqueFd() + { + Reset(); + } + + int Release() noexcept + { + int fd = fd_; + fd_ = -1; + return fd; + } + + void Reset(int new_fd = -1) + { + if (fd_ != -1) { + ASSERT(new_fd != fd_); + DefaultCloser(fd_); + } + fd_ = new_fd; + } + + int Get() const noexcept + { + return fd_; + } + + bool IsValid() const noexcept + { + return fd_ != -1; + } + +private: + static void DefaultCloser(int fd) + { + LOG_IF(PANDA_FAILURE_RETRY(::close(fd)) != 0, FATAL, COMMON) << "Incorrect fd: " << fd; + } + + int fd_ = -1; +}; +} // namespace panda::os::unique_fd + +#endif // PANDA_LIBPANDABASE_OS_UNIQUE_FD_H_ diff --git a/libpandabase/os/unix/exec.cpp b/libpandabase/os/unix/exec.cpp index 1bdd8e1bd2ef0f6c298482d317d2a0f278c3d7b2..aad9ae5ff6fe70b7b42a2883d8705a396fe45d92 100644 --- a/libpandabase/os/unix/exec.cpp +++ b/libpandabase/os/unix/exec.cpp @@ -17,7 +17,7 @@ #include #include -#include "os/unix/failure_retry.h" +#include "os/failure_retry.h" #include "sys/wait.h" namespace panda::os::exec { diff --git a/libpandabase/os/unix/library_loader.h b/libpandabase/os/unix/library_loader.h index 55cc70d0f59976418c98f6810bbb460b8cbe306c..a20f17d7737c977728936e795b85d57e82de8872 100644 --- a/libpandabase/os/unix/library_loader.h +++ b/libpandabase/os/unix/library_loader.h @@ -64,4 +64,8 @@ private: } // namespace panda::os::unix::library_loader +namespace panda::os::library_loader { +using LibraryHandle = panda::os::unix::library_loader::LibraryHandle; +} // namespace panda::os::library_loader + #endif // PANDA_LIBPANDABASE_OS_UNIX_LIBRARY_LOADER_H_ diff --git a/libpandabase/os/unix/mem.cpp b/libpandabase/os/unix/mem.cpp index 7b13ecf362d9b0a90548548119446084eb780586..e9f8e7b445b1e768c4621d9fe88ab34b40be8ae5 100644 --- a/libpandabase/os/unix/mem.cpp +++ b/libpandabase/os/unix/mem.cpp @@ -65,36 +65,30 @@ BytePtr MapExecuted(size_t size) return BytePtr(static_cast(result), (result == nullptr) ? 0 : size, MmapDeleter); } -std::optional MakeMemReadExec(void *mem, size_t size) +std::optional MakeMemWithProtFlag(void *mem, size_t size, int prot) { - // NOLINTNEXTLINE(hicpp-signed-bitwise) - int r = mprotect(mem, size, PROT_EXEC | PROT_READ); + int r = mprotect(mem, size, prot); if (r != 0) { return Error(errno); } return {}; } -std::optional MakeMemReadWrite(void *mem, size_t size) +std::optional MakeMemReadExec(void *mem, size_t size) { // NOLINTNEXTLINE(hicpp-signed-bitwise) - int r = mprotect(mem, size, PROT_WRITE | PROT_READ); - if (r != 0) { - return Error(errno); - } - - return {}; + return MakeMemWithProtFlag(mem, size, PROT_EXEC | PROT_READ); } -std::optional MakeMemReadOnly(void *mem, size_t size) +std::optional MakeMemReadWrite(void *mem, size_t size) { // NOLINTNEXTLINE(hicpp-signed-bitwise) - int r = mprotect(mem, size, PROT_READ); - if (r != 0) { - return Error(errno); - } + return MakeMemWithProtFlag(mem, size, PROT_WRITE | PROT_READ); +} - return {}; +std::optional MakeMemReadOnly(void *mem, size_t size) +{ + return MakeMemWithProtFlag(mem, size, PROT_READ); } uintptr_t AlignDownToPageSize(uintptr_t addr) @@ -121,6 +115,12 @@ void *AlignedAlloc(size_t alignment_in_bytes, size_t size) return ret; } +void AlignedFree(void *mem) +{ + // NOLINTNEXTLINE(cppcoreguidelines-no-malloc) + std::free(mem); +} + static uint32_t GetPageSizeFromOs() { // NOLINTNEXTLINE(google-runtime-int) diff --git a/runtime/mem/mem_hooks.cpp b/libpandabase/os/unix/mem_hooks.cpp similarity index 97% rename from runtime/mem/mem_hooks.cpp rename to libpandabase/os/unix/mem_hooks.cpp index f171b90a9ec188ac68cd126aebab38694cffe5f4..aabbe7110b6c25b2c6d1d8e74feeb4cc9c686d73 100644 --- a/runtime/mem/mem_hooks.cpp +++ b/libpandabase/os/unix/mem_hooks.cpp @@ -19,8 +19,7 @@ #include "mem_hooks.h" -namespace panda::mem { - +namespace panda::os::unix::mem_hooks { size_t PandaHooks::alloc_via_standard = 0; void *(*volatile PandaHooks::old_malloc_hook)(size_t, const void *) = nullptr; void *(*volatile PandaHooks::old_memalign_hook)(size_t, size_t, const void *) = nullptr; @@ -113,5 +112,4 @@ void PandaHooks::Disable() __free_hook = old_free_hook; #endif // __MUSL__ } - -} // namespace panda::mem +} // namespace panda::os::unix::mem_hooks diff --git a/runtime/mem/mem_hooks.h b/libpandabase/os/unix/mem_hooks.h similarity index 79% rename from runtime/mem/mem_hooks.h rename to libpandabase/os/unix/mem_hooks.h index ade78af9e6bfe0afd79fdd2f7fde65b8e5e12812..8a21884f5590811031874b2982a80aa10f28e1e0 100644 --- a/runtime/mem/mem_hooks.h +++ b/libpandabase/os/unix/mem_hooks.h @@ -13,13 +13,12 @@ * limitations under the License. */ -#ifndef PANDA_RUNTIME_MEM_MEM_HOOKS_H_ -#define PANDA_RUNTIME_MEM_MEM_HOOKS_H_ +#ifndef PANDA_LIBPANDABASE_OS_UNIX_MEM_HOOKS_H_ +#define PANDA_LIBPANDABASE_OS_UNIX_MEM_HOOKS_H_ #include "libpandabase/mem/mem.h" -namespace panda::mem { - +namespace panda::os::unix::mem_hooks { class PandaHooks { public: static void Enable(); @@ -48,7 +47,10 @@ private: static size_t alloc_via_standard; }; +} // namespace panda::os::unix::mem_hooks -} // namespace panda::mem +namespace panda::os::mem_hooks { +using PandaHooks = panda::os::unix::mem_hooks::PandaHooks; +} // namespace panda::os::mem_hooks -#endif // PANDA_RUNTIME_MEM_MEM_HOOKS_H_ +#endif // PANDA_LIBPANDABASE_OS_UNIX_MEM_HOOKS_H_ diff --git a/libpandabase/os/unix/pipe.cpp b/libpandabase/os/unix/pipe.cpp index efd173fbeaaaba2d58b2d60ed0395715b271172c..1fd2e788a46f464ba885e69a2253d53a0d0d63ce 100644 --- a/libpandabase/os/unix/pipe.cpp +++ b/libpandabase/os/unix/pipe.cpp @@ -15,7 +15,7 @@ #include "pipe.h" -#include "failure_retry.h" +#include "os/failure_retry.h" #include #include diff --git a/libpandabase/os/unix/pipe.h b/libpandabase/os/unix/pipe.h index 1c4158bd6daf3640868a44083fba384f76da449d..4467b5581dc7d1253c7b4a64123474a3c877de81 100644 --- a/libpandabase/os/unix/pipe.h +++ b/libpandabase/os/unix/pipe.h @@ -16,15 +16,17 @@ #ifndef PANDA_LIBPANDABASE_OS_UNIX_PIPE_H_ #define PANDA_LIBPANDABASE_OS_UNIX_PIPE_H_ -#include "unique_fd.h" #include "utils/expected.h" #include "os/error.h" +#include "os/unique_fd.h" #include #include namespace panda::os::unix { +using UniqueFd = panda::os::unique_fd::UniqueFd; + std::pair CreatePipe(); int SetFdNonblocking(const UniqueFd &fd); diff --git a/libpandabase/os/unix/sighooklib/sighook.cpp b/libpandabase/os/unix/sighook.cpp similarity index 99% rename from libpandabase/os/unix/sighooklib/sighook.cpp rename to libpandabase/os/unix/sighook.cpp index f408f06a626c153e69577e2fb23c398fd808c9ff..d19ea829900dcb17240c54f32fb4015887358f90 100644 --- a/libpandabase/os/unix/sighooklib/sighook.cpp +++ b/libpandabase/os/unix/sighook.cpp @@ -13,9 +13,6 @@ * limitations under the License. */ -#include "sighook.h" - -#include "utils/logger.h" #include #include // NOLINTNEXTLINE(modernize-deprecated-headers) #include // NOLINTNEXTLINE(modernize-deprecated-headers) @@ -24,6 +21,9 @@ #include // NOLINTNEXTLINE(modernize-deprecated-headers) #include +#include "utils/logger.h" +#include "os/sighook.h" + #include #include #include @@ -35,7 +35,6 @@ #include namespace panda { - static decltype(&sigaction) real_sigaction; static decltype(&sigprocmask) real_sigprocmask; static bool g_is_init_really {false}; @@ -495,5 +494,4 @@ void ClearSignalHooksHandlersArray() signal_hooks[i].ClearHookActionHandlers(); } } - } // namespace panda diff --git a/libpandabase/os/unix/sighooklib/sighook.h b/libpandabase/os/unix/sighook.h similarity index 90% rename from libpandabase/os/unix/sighooklib/sighook.h rename to libpandabase/os/unix/sighook.h index 096ea39bb0a856bb1eeecad10d4040f37e7f914b..63baff2d428047cda7ebc2591337490e97d5d7f3 100644 --- a/libpandabase/os/unix/sighooklib/sighook.h +++ b/libpandabase/os/unix/sighook.h @@ -13,14 +13,13 @@ * limitations under the License. */ -#ifndef PANDA_LIBPANDABASE_OS_UNIX_SIGHOOKLIB_SIGHOOK_H_ -#define PANDA_LIBPANDABASE_OS_UNIX_SIGHOOKLIB_SIGHOOK_H_ +#ifndef PANDA_LIBPANDABASE_OS_UNIX_SIGHOOK_H_ +#define PANDA_LIBPANDABASE_OS_UNIX_SIGHOOK_H_ #include // NOLINTNEXTLINE(modernize-deprecated-headers) #include // NOLINTNEXTLINE(modernize-deprecated-headers) namespace panda { - #if PANDA_TARGET_MACOS && !defined _NSIG #define _NSIG NSIG #endif @@ -49,7 +48,6 @@ struct SigchainAction { extern "C" void AddSpecialSignalHandlerFn(int signal, SigchainAction *sa); extern "C" void RemoveSpecialSignalHandlerFn(int signal, bool (*fn)(int, siginfo_t *, void *)); extern "C" void EnsureFrontOfChain(int signal); - } // namespace panda -#endif // PANDA_LIBPANDABASE_OS_UNIX_SIGHOOKLIB_SIGHOOK_H_ +#endif // PANDA_LIBPANDABASE_OS_UNIX_SIGHOOK_H_ diff --git a/libpandabase/os/unix/thread.cpp b/libpandabase/os/unix/thread.cpp index 87fc09a53b35ba83a7e29eb2274b6f532c1d9e9d..28d5f1929f7fdc9ab1d27f1a30e9f88bfb823ce0 100644 --- a/libpandabase/os/unix/thread.cpp +++ b/libpandabase/os/unix/thread.cpp @@ -23,7 +23,6 @@ #include namespace panda::os::thread { - ThreadId GetCurrentThreadId() { #if defined(HAVE_GETTID) @@ -33,30 +32,20 @@ ThreadId GetCurrentThreadId() uint64_t tid64; pthread_threadid_np(NULL, &tid64); return static_cast(tid64); -#elif defined(PANDA_TARGET_UNIX) +#else // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg) return static_cast(syscall(SYS_gettid)); -#else -#error "Unsupported platform" #endif } int SetPriority(int thread_id, int prio) { -#if defined(PANDA_TARGET_UNIX) return setpriority(PRIO_PROCESS, thread_id, prio); -#else -#error "Unsupported platform" -#endif } int GetPriority(int thread_id) { -#if defined(PANDA_TARGET_UNIX) return getpriority(PRIO_PROCESS, thread_id); -#else -#error "Unsupported platform" -#endif } int SetThreadName(native_handle_type pthread_id, const char *name) @@ -64,65 +53,38 @@ int SetThreadName(native_handle_type pthread_id, const char *name) ASSERT(pthread_id != 0); #if defined(PANDA_TARGET_MACOS) return pthread_setname_np(name); -#elif defined(PANDA_TARGET_UNIX) - return pthread_setname_np(pthread_id, name); #else -#error "Unsupported platform" + return pthread_setname_np(pthread_id, name); #endif } native_handle_type GetNativeHandle() { -#if defined(PANDA_TARGET_UNIX) return pthread_self(); -#else -#error "Unsupported platform" -#endif } void Yield() { -#if defined(PANDA_TARGET_UNIX) std::this_thread::yield(); -#else -#error "Unsupported platform" -#endif } void NativeSleep(unsigned int ms) { -#if defined(PANDA_TARGET_UNIX) std::this_thread::sleep_for(std::chrono::milliseconds(ms)); -#else -#error "Unsupported platform" -#endif } void ThreadDetach(native_handle_type pthread_id) { -#if defined(PANDA_TARGET_UNIX) pthread_detach(pthread_id); -#else -#error "Unsupported platform" -#endif } void ThreadExit(void *retval) { -#if defined(PANDA_TARGET_UNIX) pthread_exit(retval); -#else -#error "Unsupported platform" -#endif } void ThreadJoin(native_handle_type pthread_id, void **retval) { -#if defined(PANDA_TARGET_UNIX) pthread_join(pthread_id, retval); -#else -#error "Unsupported platform" -#endif } - } // namespace panda::os::thread diff --git a/libpandabase/os/unix/time_unix.cpp b/libpandabase/os/unix/time.h similarity index 56% rename from libpandabase/os/unix/time_unix.cpp rename to libpandabase/os/unix/time.h index 0e687cbb876821dd7e4f8acddf3eef466f54aacb..e568fec0653593882122e0957da39434f383ee7f 100644 --- a/libpandabase/os/unix/time_unix.cpp +++ b/libpandabase/os/unix/time.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021-2022 Huawei Device Co., Ltd. + * Copyright (c) 2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -13,11 +13,12 @@ * limitations under the License. */ -#include "time.h" // NOLINTNEXTLINE(modernize-deprecated-headers, hicpp-deprecated-headers) -#include +#ifndef PANDA_LIBPANDABASE_OS_UNIX_TIME_H_ +#define PANDA_LIBPANDABASE_OS_UNIX_TIME_H_ -namespace panda::os::unix::time { +#include +namespace panda::os::time { template static uint64_t GetClockTime(clockid_t clock) { @@ -28,29 +29,6 @@ static uint64_t GetClockTime(clockid_t clock) } return 0; } +} // namespace panda::os::time -/** - * Return current time in nanoseconds - */ -uint64_t GetClockTimeInMicro() -{ - return GetClockTime(CLOCK_MONOTONIC); -} - -/** - * Return current time in milliseconds - */ -uint64_t GetClockTimeInMilli() -{ - return GetClockTime(CLOCK_MONOTONIC); -} - -/** - * Return thread CPU time in nanoseconds - */ -uint64_t GetClockTimeInThreadCpuTime() -{ - return GetClockTime(CLOCK_THREAD_CPUTIME_ID); -} - -} // namespace panda::os::unix::time +#endif // PANDA_LIBPANDABASE_OS_UNIX_TIME_H_ diff --git a/libpandabase/os/unix/unique_fd.h b/libpandabase/os/unix/unique_fd.h index 3c3533247d3c066c27eccb395eb341bc61ab693e..e8a390b9a9be515e77d3525b153e8ed4c6ae579a 100644 --- a/libpandabase/os/unix/unique_fd.h +++ b/libpandabase/os/unix/unique_fd.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021-2022 Huawei Device Co., Ltd. + * Copyright (c) 2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -16,79 +16,13 @@ #ifndef PANDA_LIBPANDABASE_OS_UNIX_UNIQUE_FD_H_ #define PANDA_LIBPANDABASE_OS_UNIX_UNIQUE_FD_H_ -#include #include -#include "utils/logger.h" -#include "os/unix/failure_retry.h" - -namespace panda::os::unix { - -class UniqueFd { -public: - explicit UniqueFd(int fd = -1) noexcept - { - Reset(fd); - } - - UniqueFd(const UniqueFd &other_fd) = delete; - UniqueFd &operator=(const UniqueFd &other_fd) = delete; - - UniqueFd(UniqueFd &&other_fd) noexcept - { - Reset(other_fd.Release()); - } - - UniqueFd &operator=(UniqueFd &&other_fd) noexcept - { - Reset(other_fd.Release()); - return *this; - } - - ~UniqueFd() - { - Reset(); - } - - int Release() noexcept - { - int fd = fd_; - fd_ = -1; - return fd; - } - - void Reset(int new_fd = -1) - { - if (fd_ != -1) { - ASSERT(new_fd != fd_); - DefaultCloser(fd_); - } - fd_ = new_fd; - } - - int Get() const noexcept - { - return fd_; - } - - bool IsValid() const noexcept - { - return fd_ != -1; - } - -private: - static void DefaultCloser(int fd) - { - LOG_IF(PANDA_FAILURE_RETRY(::close(fd)) != 0, FATAL, COMMON) << "Incorrect fd: " << fd; - } - - int fd_ = -1; -}; +namespace panda::os::unique_fd { inline int DupCloexec(int fd) { return fcntl(fd, F_DUPFD_CLOEXEC, 0); } - -} // namespace panda::os::unix +} // namespace panda::os::unique_fd #endif // PANDA_LIBPANDABASE_OS_UNIX_UNIQUE_FD_H_ diff --git a/libpandabase/os/windows/library_loader.cpp b/libpandabase/os/windows/library_loader.cpp new file mode 100644 index 0000000000000000000000000000000000000000..24449ecd351e7fb72226a60f063f5656d03e91f9 --- /dev/null +++ b/libpandabase/os/windows/library_loader.cpp @@ -0,0 +1,51 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "os/library_loader.h" + +#include + +namespace panda::os::library_loader { +Expected Load(std::string_view filename) +{ + HMODULE module = LoadLibrary(filename.data()); + void *handle = reinterpret_cast(module); + if (handle != nullptr) { + return LibraryHandle(handle); + } + return Unexpected(Error(std::string("Failed to load library ") + filename.data() + std::string(", error code ") + + std::to_string(GetLastError()))); +} + +Expected ResolveSymbol(const LibraryHandle &handle, std::string_view name) +{ + HMODULE module = reinterpret_cast(handle.GetNativeHandle()); + void *p = reinterpret_cast(GetProcAddress(module, name.data())); + if (p != nullptr) { + return p; + } + return Unexpected(Error(std::string("Failed to resolve symbol ") + name.data() + std::string(", error code ") + + std::to_string(GetLastError()))); +} +} // namespace panda::os::library_loader + +namespace panda::os::windows::library_loader { +LibraryHandle::~LibraryHandle() +{ + if (handle_ != nullptr) { + FreeLibrary(reinterpret_cast(handle_)); + } +} +} // namespace panda::os::windows::library_loader diff --git a/libpandabase/os/windows/library_loader.h b/libpandabase/os/windows/library_loader.h new file mode 100644 index 0000000000000000000000000000000000000000..8d8efabb4e97bd8d353e453a6417e84faab295ee --- /dev/null +++ b/libpandabase/os/windows/library_loader.h @@ -0,0 +1,62 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef PANDA_LIBPANDABASE_OS_WINDOWS_LIBRARY_LOADER_H_ +#define PANDA_LIBPANDABASE_OS_WINDOWS_LIBRARY_LOADER_H_ + +#include "macros.h" + +namespace panda::os::windows::library_loader { +class LibraryHandle { +public: + explicit LibraryHandle(void *handle) : handle_(handle) {} + + LibraryHandle(LibraryHandle &&handle) noexcept + { + handle_ = handle.handle_; + handle.handle_ = nullptr; + } + + LibraryHandle &operator=(LibraryHandle &&handle) noexcept + { + handle_ = handle.handle_; + handle.handle_ = nullptr; + return *this; + } + + bool IsValid() const + { + return handle_ != nullptr; + } + + void *GetNativeHandle() const + { + return handle_; + } + + ~LibraryHandle(); + +private: + void *handle_; + + NO_COPY_SEMANTIC(LibraryHandle); +}; +} // namespace panda::os::windows::library_loader + +namespace panda::os::library_loader { +using LibraryHandle = panda::os::windows::library_loader::LibraryHandle; +} // namespace panda::os::library_loader + +#endif // PANDA_LIBPANDABASE_OS_WINDOWS_LIBRARY_LOADER_H_ diff --git a/libpandabase/os/windows/mem.cpp b/libpandabase/os/windows/mem.cpp index 850ce2384fc159989df73b3d89902048230fc817..e2195d58422aef4ac7fe65d49cf94b58f9053409 100644 --- a/libpandabase/os/windows/mem.cpp +++ b/libpandabase/os/windows/mem.cpp @@ -14,7 +14,6 @@ */ #include "os/mem.h" -#include "windows_mem.h" #include "utils/type_helpers.h" #include "utils/asan_interface.h" @@ -24,6 +23,7 @@ #include #include +#include #include #define MAP_FAILED (reinterpret_cast(-1)) @@ -51,7 +51,7 @@ static DWORD mem_protection_flags_for_page(const int prot) return flags; } -static DWORD mem_protection_flags_for_file(const int prot) +static DWORD mem_protection_flags_for_file(const int prot, const uint32_t map_flags) { DWORD flags = 0; @@ -59,6 +59,15 @@ static DWORD mem_protection_flags_for_file(const int prot) return flags; } + /* Notice that only single FILE_MAP_COPY flag can ensure a copy-on-write mapping which + * MMAP_FLAG_PRIVATE needs. It can't be bitwise OR'ed with FILE_MAP_ALL_ACCESS, FILE_MAP_READ + * or FILE_MAP_WRITE. Or else it will be converted to PAGE_READONLY or PAGE_READWRITE, and make + * the changes synced back to the original file. + */ + if ((map_flags & MMAP_FLAG_PRIVATE) != 0) { + return FILE_MAP_COPY; + } + if ((static_cast(prot) & MMAP_PROT_READ) != 0) { flags |= FILE_MAP_READ; } @@ -115,7 +124,7 @@ void *mmap([[maybe_unused]] void *addr, size_t len, int prot, uint32_t flags, in return MAP_FAILED; } - const auto prot_file = mem_protection_flags_for_file(prot); + const auto prot_file = mem_protection_flags_for_file(prot, flags); const auto file_off_low = mem_select_lower_bound(off); const auto file_off_high = mem_select_upper_bound(off); void *map = MapViewOfFile(fm, prot_file, file_off_high, file_off_low, len); @@ -158,6 +167,46 @@ BytePtr MapFile(file::File file, uint32_t prot, uint32_t flags, size_t size, siz return BytePtr(static_cast(result) + offset, size, MmapDeleter); } +BytePtr MapExecuted(size_t size) +{ + // By design caller should pass valid size, so don't do any additional checks except ones that + // mmap do itself + // NOLINTNEXTLINE(hicpp-signed-bitwise) + void *result = mmap(nullptr, size, MMAP_PROT_EXEC | MMAP_PROT_WRITE, MMAP_FLAG_SHARED | MMAP_FLAG_ANONYMOUS, -1, 0); + if (result == reinterpret_cast(-1)) { + result = nullptr; + } + + return BytePtr(static_cast(result), (result == nullptr) ? 0 : size, MmapDeleter); +} + +std::optional MakeMemWithProtFlag(void *mem, size_t size, int prot) +{ + PDWORD old = nullptr; + int r = VirtualProtect(mem, size, prot, old); + if (r != 0) { + return Error(GetLastError()); + } + return {}; +} + +std::optional MakeMemReadExec(void *mem, size_t size) +{ + // NOLINTNEXTLINE(hicpp-signed-bitwise) + return MakeMemWithProtFlag(mem, size, MMAP_PROT_EXEC | MMAP_PROT_READ); +} + +std::optional MakeMemReadWrite(void *mem, size_t size) +{ + // NOLINTNEXTLINE(hicpp-signed-bitwise) + return MakeMemWithProtFlag(mem, size, MMAP_PROT_WRITE | MMAP_PROT_READ); +} + +std::optional MakeMemReadOnly(void *mem, size_t size) +{ + return MakeMemWithProtFlag(mem, size, MMAP_PROT_READ); +} + uint32_t GetPageSize() { constexpr size_t PAGE_SIZE = 4096; @@ -194,19 +243,18 @@ void *MapRWAnonymousWithAlignmentRaw(size_t size, size_t aligment_in_bytes, bool uintptr_t aligned_mem = (allocated_mem & ~(aligment_in_bytes - 1U)) + ((allocated_mem % aligment_in_bytes) != 0U ? aligment_in_bytes : 0U); ASSERT(aligned_mem >= allocated_mem); - size_t unused_in_start = aligned_mem - allocated_mem; - ASSERT(unused_in_start <= aligment_in_bytes); - size_t unused_in_end = aligment_in_bytes - unused_in_start; - if (unused_in_start != 0) { - UnmapRaw(result, unused_in_start); - } - if (unused_in_end != 0) { - auto end_part = reinterpret_cast(aligned_mem + size); - UnmapRaw(end_part, unused_in_end); - } return reinterpret_cast(aligned_mem); } +uintptr_t AlignDownToPageSize(uintptr_t addr) +{ + SYSTEM_INFO sysInfo; + GetSystemInfo(&sysInfo); + const size_t SYS_PAGE_SIZE = sysInfo.dwPageSize; + addr &= ~(SYS_PAGE_SIZE - 1); + return addr; +} + void *AlignedAlloc(size_t alignment_in_bytes, size_t size) { size_t aligned_size = (size + alignment_in_bytes - 1) & ~(alignment_in_bytes - 1); @@ -217,6 +265,11 @@ void *AlignedAlloc(size_t alignment_in_bytes, size_t size) return ret; } +void AlignedFree(void *mem) +{ + _aligned_free(mem); +} + std::optional UnmapRaw(void *mem, size_t size) { ASAN_UNPOISON_MEMORY_REGION(mem, size); @@ -234,4 +287,9 @@ std::optional TagAnonymousMemory([[maybe_unused]] const void *mem, [[mayb return {}; } +size_t GetNativeBytesFromMallinfo() +{ + return DEFAULT_NATIVE_BYTES_FROM_MALLINFO; +} + } // namespace panda::os::mem diff --git a/libpandabase/os/windows/mem_hooks.cpp b/libpandabase/os/windows/mem_hooks.cpp new file mode 100644 index 0000000000000000000000000000000000000000..741f7a8959990021f061a31841cbc9af8ef03603 --- /dev/null +++ b/libpandabase/os/windows/mem_hooks.cpp @@ -0,0 +1,113 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "os/mem_hooks.h" + +#include +#include + +namespace panda::os::windows::mem_hooks { +volatile bool enable = false; +static bool first = true; + +static const char *GetAllocTypeName(int at) +{ + switch (at) { + case _HOOK_ALLOC: + return "_HOOK_ALLOC"; + case _HOOK_REALLOC: + return "_HOOK_REALLOC"; + case _HOOK_FREE: + return "_HOOK_FREE"; + default: + return "unknown AllocType"; + } +} + +static const char *GetBlockTypeName(int bt) +{ + switch (bt) { + case _CRT_BLOCK: + return "_CRT_BLOCK"; + case _NORMAL_BLOCK: + return "_NORMAL_BLOCK"; + case _FREE_BLOCK: + return "_FREE_BLOCK"; + default: + return "unknown BlockType"; + } +} + +int PandaHooks::PandaAllocHook(int alloctype, [[maybe_unused]] void *data, std::size_t size, int blocktype, + [[maybe_unused]] long request, const unsigned char *filename, int linenumber) +{ + if (!enable) { + return true; + } + + /* Ignoring internal allocations made by C run-time library functions, + * or else it may trap the program in an endless loop. + */ + if (blocktype == _CRT_BLOCK) { + return true; + } + + constexpr int ALIGN_SIZE = 32; + + if (first) { + std::cout << std::left << std::setfill(' ') << std::setw(ALIGN_SIZE) << "alloc type"; + std::cout << std::left << std::setfill(' ') << std::setw(ALIGN_SIZE) << "block type"; + std::cout << std::left << std::setfill(' ') << std::setw(ALIGN_SIZE) << "size"; + std::cout << std::left << std::setfill(' ') << std::setw(ALIGN_SIZE) << "filename"; + std::cout << std::left << std::setfill(' ') << std::setw(ALIGN_SIZE) << "linenumber" << std::endl; + first = false; + } + + const char* alloctype_name = GetAllocTypeName(alloctype); + const char* blocktype_name = GetBlockTypeName(blocktype); + + std::cout << std::left << std::setfill(' ') << std::setw(ALIGN_SIZE) << alloctype_name; + std::cout << std::left << std::setfill(' ') << std::setw(ALIGN_SIZE) << blocktype_name; + std::cout << std::left << std::setfill(' ') << std::setw(ALIGN_SIZE) << (int)size; + std::cout << std::left << std::setfill(' ') << std::setw(ALIGN_SIZE) << filename; + std::cout << std::left << std::setfill(' ') << std::setw(ALIGN_SIZE) << linenumber << std::endl; + + return true; +} + +/* static */ +void PandaHooks::Enable() +{ + enable = true; + + _CrtSetAllocHook(PandaAllocHook); + _CrtMemCheckpoint(&begin); + _CrtMemDumpAllObjectsSince(&begin); +} + +/* static */ +void PandaHooks::Disable() +{ + enable = false; + + _CrtMemCheckpoint(&end); + _CrtMemDumpAllObjectsSince(&end); + + if (_CrtMemDifference(&out, &begin, &end)) { + std::cerr << "Memory leak detected:" << std::endl; + _CrtDumpMemoryLeaks(); + } +} +} // namespace panda::os::windows::mem_hooks diff --git a/libpandabase/os/windows/mem_hooks.h b/libpandabase/os/windows/mem_hooks.h new file mode 100644 index 0000000000000000000000000000000000000000..e70387381316c246b12b8b264af04e3e3176cda9 --- /dev/null +++ b/libpandabase/os/windows/mem_hooks.h @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef PANDA_LIBPANDABASE_OS_WINDOWS_MEM_HOOKS_H_ +#define PANDA_LIBPANDABASE_OS_WINDOWS_MEM_HOOKS_H_ + +#include +#include + +namespace panda::os::windows::mem_hooks { +class PandaHooks { +public: + static void Enable(); + + static void Disable(); + +private: + /* + * "PandaAllocHook" is an allocation hook function, following a prototype described in + * https://docs.microsoft.com/en-us/visualstudio/debugger/allocation-hook-functions. + * Installed it using "_CrtSetAllocHook", then it will be called every time memory is + * allocated, reallocated, or freed. + */ + static int PandaAllocHook(int alloctype, void *data, std::size_t size, int blocktype, long request, + const unsigned char *filename, int linenumber); + + static _CrtMemState begin, end, out; +}; +} // namespace panda::os::windows::mem_hooks + +namespace panda::os::mem_hooks { +using PandaHooks = panda::os::windows::mem_hooks::PandaHooks; +} // namespace panda::os::mem_hooks + +#endif // PANDA_LIBPANDABASE_OS_WINDOWS_MEM_HOOKS_H_ diff --git a/libpandabase/os/windows/thread.cpp b/libpandabase/os/windows/thread.cpp index 40f3a56b31b6a1192d328474424472e5da6b33c5..1dca1e55597cf4b0608d208a1fd6704945e7dc9d 100644 --- a/libpandabase/os/windows/thread.cpp +++ b/libpandabase/os/windows/thread.cpp @@ -16,6 +16,7 @@ #include "os/thread.h" #include +#include namespace panda::os::thread { @@ -24,4 +25,35 @@ ThreadId GetCurrentThreadId() return static_cast(std::hash()(std::this_thread::get_id())); } +int SetPriority([[maybe_unused]] int thread_id, int prio) +{ + return SetThreadPriority(GetCurrentThread(), prio); +} + +int GetPriority([[maybe_unused]] int thread_id) +{ + return GetThreadPriority(GetCurrentThread()); +} + +int SetThreadName([[maybe_unused]] native_handle_type pthread_id, const char *name) +{ + ASSERT(pthread_id != 0); + return pthread_setname_np(pthread_self(), name); +} + +void Yield() +{ + std::this_thread::yield(); +} + +void NativeSleep(unsigned int ms) +{ + std::this_thread::sleep_for(std::chrono::milliseconds(ms)); +} + +void ThreadJoin(native_handle_type pthread_id, void **retval) +{ + pthread_join(reinterpret_cast(pthread_id), retval); +} + } // namespace panda::os::thread diff --git a/libpandabase/os/windows/time.h b/libpandabase/os/windows/time.h new file mode 100644 index 0000000000000000000000000000000000000000..35b8eb6a18099d24359fc06e760046291905073e --- /dev/null +++ b/libpandabase/os/windows/time.h @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef PANDA_LIBPANDABASE_OS_WINDOWS_TIME_H_ +#define PANDA_LIBPANDABASE_OS_WINDOWS_TIME_H_ + +#include +#include + +namespace panda::os::time { +template +static uint64_t GetClockTime([[maybe_unused]] clockid_t clock) +{ + struct timeval time = {0, 0}; + if (gettimeofday(&time, nullptr) != -1) { + auto duration = std::chrono::seconds {time.tv_sec} + std::chrono::microseconds {time.tv_usec}; + return std::chrono::duration_cast(duration).count(); + } + return 0; +} +} // namespace panda::os::time + +#endif // PANDA_LIBPANDABASE_OS_WINDOWS_TIME_H_ diff --git a/libpandabase/os/unix/time_unix.h b/libpandabase/os/windows/unique_fd.h similarity index 51% rename from libpandabase/os/unix/time_unix.h rename to libpandabase/os/windows/unique_fd.h index 956091182851c800481e56d48b55af77e5f21d10..b26c0c823f8389ed2f9f0870f4b83b1c7bb994bf 100644 --- a/libpandabase/os/unix/time_unix.h +++ b/libpandabase/os/windows/unique_fd.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021-2022 Huawei Device Co., Ltd. + * Copyright (c) 2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -13,27 +13,17 @@ * limitations under the License. */ -#ifndef PANDA_LIBPANDABASE_OS_UNIX_TIME_UNIX_H_ -#define PANDA_LIBPANDABASE_OS_UNIX_TIME_UNIX_H_ +#ifndef PANDA_LIBPANDABASE_OS_WINDOWS_UNIQUE_FD_H_ +#define PANDA_LIBPANDABASE_OS_WINDOWS_UNIQUE_FD_H_ -#include +#include "libpandabase/macros.h" -namespace panda::os::unix::time { +namespace panda::os::unique_fd { +inline int DupCloexec([[maybe_unused]] int fd) +{ + // Unsupported on windows platform + UNREACHABLE(); +} +} // namespace panda::os::unique_fd -/** - * Return current time in nanoseconds - */ -uint64_t GetClockTimeInMicro(); -/** - * Return current time in milliseconds - */ -uint64_t GetClockTimeInMilli(); - -/** - * Thread Cpu Time in nanoseconds - */ -uint64_t GetClockTimeInThreadCpuTime(); - -} // namespace panda::os::unix::time - -#endif // PANDA_LIBPANDABASE_OS_UNIX_TIME_UNIX_H_ +#endif // PANDA_LIBPANDABASE_OS_WINDOWS_UNIQUE_FD_H_ diff --git a/libpandabase/os/windows/windows_mem.h b/libpandabase/os/windows/windows_mem.h index 95a051d5c33b08badbb835a8d5371f88a6504861..9127555d3f315bae0203922ee303b4f7ea3d1a17 100644 --- a/libpandabase/os/windows/windows_mem.h +++ b/libpandabase/os/windows/windows_mem.h @@ -23,10 +23,15 @@ static constexpr uint32_t MMAP_PROT_READ = 1; static constexpr uint32_t MMAP_PROT_WRITE = 2; static constexpr uint32_t MMAP_PROT_EXEC = 4; +static constexpr uint32_t MMAP_FLAG_SHARED = 1; static constexpr uint32_t MMAP_FLAG_PRIVATE = 2; static constexpr uint32_t MMAP_FLAG_FIXED = 0x10; static constexpr uint32_t MMAP_FLAG_ANONYMOUS = 0x20; +void *mmap([[maybe_unused]] void *addr, size_t len, int prot, uint32_t flags, int fildes, off_t off); + +int munmap(void *addr, [[maybe_unused]] size_t len); + } // namespace panda::os::mem #endif // PANDA_LIBPANDABASE_OS_WINDOWS_WINDOWS_MEM_H_ diff --git a/libpandabase/tests/dfx_test.cpp b/libpandabase/tests/dfx_test.cpp index 7390dbe1045a2a33bba905249bada33fc850f255..c0dd9f131236be0e793aad619552eb6c149e7cd0 100644 --- a/libpandabase/tests/dfx_test.cpp +++ b/libpandabase/tests/dfx_test.cpp @@ -22,6 +22,43 @@ namespace panda::test { +void MapDfxOption(std::map &option_map, DfxOptionHandler::DfxOption option) +{ + switch (option) { +#ifdef PANDA_TARGET_UNIX + case DfxOptionHandler::COMPILER_NULLCHECK: + option_map[DfxOptionHandler::COMPILER_NULLCHECK] = 1; + break; + case DfxOptionHandler::SIGNAL_CATCHER: + option_map[DfxOptionHandler::SIGNAL_CATCHER] = 1; + break; + case DfxOptionHandler::SIGNAL_HANDLER: + option_map[DfxOptionHandler::SIGNAL_HANDLER] = 1; + break; + case DfxOptionHandler::ARK_SIGQUIT: + option_map[DfxOptionHandler::ARK_SIGQUIT] = 1; + break; + case DfxOptionHandler::ARK_SIGUSR1: + option_map[DfxOptionHandler::ARK_SIGUSR1] = 1; + break; + case DfxOptionHandler::ARK_SIGUSR2: + option_map[DfxOptionHandler::ARK_SIGUSR2] = 1; + break; + case DfxOptionHandler::MOBILE_LOG: + option_map[DfxOptionHandler::MOBILE_LOG] = 1; + break; +#endif // PANDA_TARGET_UNIX + case DfxOptionHandler::REFERENCE_DUMP: + option_map[DfxOptionHandler::REFERENCE_DUMP] = 1; + break; + case DfxOptionHandler::DFXLOG: + option_map[DfxOptionHandler::DFXLOG] = 0; + break; + default: + break; + } +} + TEST(DfxController, Initialization) { if (DfxController::IsInitialized()) { @@ -37,38 +74,8 @@ TEST(DfxController, Initialization) std::map option_map; for (auto option = DfxOptionHandler::DfxOption(0); option < DfxOptionHandler::END_FLAG; - option = DfxOptionHandler::DfxOption(option + 1)) { - switch (option) { - case DfxOptionHandler::COMPILER_NULLCHECK: - option_map[DfxOptionHandler::COMPILER_NULLCHECK] = 1; - break; - case DfxOptionHandler::REFERENCE_DUMP: - option_map[DfxOptionHandler::REFERENCE_DUMP] = 1; - break; - case DfxOptionHandler::SIGNAL_CATCHER: - option_map[DfxOptionHandler::SIGNAL_CATCHER] = 1; - break; - case DfxOptionHandler::SIGNAL_HANDLER: - option_map[DfxOptionHandler::SIGNAL_HANDLER] = 1; - break; - case DfxOptionHandler::ARK_SIGQUIT: - option_map[DfxOptionHandler::ARK_SIGQUIT] = 1; - break; - case DfxOptionHandler::ARK_SIGUSR1: - option_map[DfxOptionHandler::ARK_SIGUSR1] = 1; - break; - case DfxOptionHandler::ARK_SIGUSR2: - option_map[DfxOptionHandler::ARK_SIGUSR2] = 1; - break; - case DfxOptionHandler::MOBILE_LOG: - option_map[DfxOptionHandler::MOBILE_LOG] = 1; - break; - case DfxOptionHandler::DFXLOG: - option_map[DfxOptionHandler::DFXLOG] = 0; - break; - default: - break; - } + option = DfxOptionHandler::DfxOption(option + 1)) { + MapDfxOption(option_map, option); } DfxController::Initialize(option_map); @@ -117,17 +124,18 @@ TEST(DfxController, TestPrintDfxOptionValues) #ifdef PANDA_TARGET_UNIX std::string res = helpers::string::Format( "[TID %06x] E/dfx: DFX option: compiler-nullcheck, option values: 1\n" - "[TID %06x] E/dfx: DFX option: reference-dump, option values: 1\n" "[TID %06x] E/dfx: DFX option: signal-catcher, option values: 1\n" "[TID %06x] E/dfx: DFX option: signal-handler, option values: 1\n" "[TID %06x] E/dfx: DFX option: sigquit, option values: 1\n" "[TID %06x] E/dfx: DFX option: sigusr1, option values: 1\n" "[TID %06x] E/dfx: DFX option: sigusr2, option values: 1\n" "[TID %06x] E/dfx: DFX option: mobile-log, option values: 1\n" + "[TID %06x] E/dfx: DFX option: reference-dump, option values: 1\n" "[TID %06x] E/dfx: DFX option: dfx-log, option values: 0\n", tid, tid, tid, tid, tid, tid, tid, tid, tid, tid); #else - std::string res = helpers::string::Format("[TID %06x] E/dfx: DFX option: dfx-log, option values: 0\n", tid, tid); + std::string res = helpers::string::Format( + "[TID %06x] E/dfx: DFX option: dfx-log, option values: 0\n", tid, tid); #endif EXPECT_EQ(err, res); diff --git a/libpandabase/tests/hash_test.cpp b/libpandabase/tests/hash_test.cpp index b8b02fe3528c85e9d11595e2d2845a8bea2865b7..e67c1bfba6a79b76d34e2b1f967378540755da93 100644 --- a/libpandabase/tests/hash_test.cpp +++ b/libpandabase/tests/hash_test.cpp @@ -21,7 +21,6 @@ #include "mem/mem.h" #include "os/mem.h" #include "utils/asan_interface.h" -#include namespace panda { @@ -120,7 +119,8 @@ void HashTest::EndOfPageStringHashTest() const constexpr size_t ALLOC_SIZE = PAGE_SIZE * 2; void *mem = panda::os::mem::MapRWAnonymousRaw(ALLOC_SIZE); ASAN_UNPOISON_MEMORY_REGION(mem, ALLOC_SIZE); - mprotect(reinterpret_cast(reinterpret_cast(mem) + PAGE_SIZE), PAGE_SIZE, PROT_NONE); + panda::os::mem::MakeMemWithProtFlag( + reinterpret_cast(reinterpret_cast(mem) + PAGE_SIZE), PAGE_SIZE, PROT_NONE); char *string = reinterpret_cast((reinterpret_cast(mem) + PAGE_SIZE) - sizeof(char) * string_size); string[0] = 'O'; diff --git a/libpandabase/tests/mem_range_test.cpp b/libpandabase/tests/mem_range_test.cpp index ce0584c7ffa0d81380a58f8ccaa0d3e5ecdd9d90..726ab944e51466bccb938dbd83ab4ee4e21064e9 100644 --- a/libpandabase/tests/mem_range_test.cpp +++ b/libpandabase/tests/mem_range_test.cpp @@ -26,8 +26,8 @@ namespace panda::test::mem_range { constexpr uintptr_t MAX_PTR = std::numeric_limits::max(); -constexpr uint NUM_RANDOM_TESTS = 100; -constexpr uint NUM_ITER_PER_TEST = 1000; +constexpr uint64_t NUM_RANDOM_TESTS = 100; +constexpr uint64_t NUM_ITER_PER_TEST = 1000; constexpr uintptr_t RANDOM_AREA_SIZE = 100000; std::default_random_engine g_generator; @@ -133,13 +133,13 @@ TEST(MemRangeTest, IntersectTest) } // function to conduct num_iter random tests with addresses in given bounds -static void randomTestInBounds(uintptr_t from, uintptr_t to, uint num_iter = NUM_ITER_PER_TEST) +static void randomTestInBounds(uintptr_t from, uintptr_t to, uint64_t num_iter = NUM_ITER_PER_TEST) { ASSERT(from < to); panda::mem::MemRange mem_range_1(0, 1), mem_range_2(0, 1); // check intersection via cycle - for (uint iter = 0; iter < num_iter; iter++) { + for (uint64_t iter = 0; iter < num_iter; iter++) { mem_range_1 = randomMemRange(from, to); mem_range_2 = randomMemRange(from, to); @@ -196,7 +196,7 @@ TEST(MemRangeTest, RandomIntersectTest) // tests in random ranges uintptr_t position; - for (uint i = 0; i < NUM_RANDOM_TESTS; i++) { + for (uint64_t i = 0; i < NUM_RANDOM_TESTS; i++) { position = random_uintptr(); if (position > RANDOM_AREA_SIZE) { randomTestInBounds(position - RANDOM_AREA_SIZE, position); diff --git a/libpandabase/tests/mmap_fixed_test.cpp b/libpandabase/tests/mmap_fixed_test.cpp index 9889d60bc639e2c0bd9e931962b24f337612b645..5afe49de022855ff733242d5fb00833a41023292 100644 --- a/libpandabase/tests/mmap_fixed_test.cpp +++ b/libpandabase/tests/mmap_fixed_test.cpp @@ -14,12 +14,11 @@ */ #include "os/mem.h" #include "mem/mem.h" -#include #include "utils/asan_interface.h" #include "gtest/gtest.h" -namespace panda { +namespace panda::os::mem { class MMapFixedTest : public testing::Test { protected: @@ -45,8 +44,8 @@ TEST_F(MMapFixedTest, MMapAsanTest) uintptr_t end_addr = panda::os::mem::MMAP_FIXED_MAGIC_ADDR_FOR_ASAN; end_addr = AlignUp(end_addr, sizeof(uint64_t)); void *result = // NOLINTNEXTLINE(hicpp-signed-bitwise) - mmap(ToVoidPtr(cur_addr), MMAP_ALLOC_SIZE, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, -1, - 0); + mmap(ToVoidPtr(cur_addr), MMAP_ALLOC_SIZE, MMAP_PROT_READ | MMAP_PROT_WRITE, + MMAP_FLAG_PRIVATE | MMAP_FLAG_ANONYMOUS | MMAP_FLAG_FIXED, -1, 0); ASSERT_TRUE(result != nullptr); ASSERT_TRUE(ToUintPtr(result) == cur_addr); while (cur_addr < end_addr) { @@ -63,4 +62,4 @@ TEST_F(MMapFixedTest, MMapAsanTest) munmap(result, MMAP_ALLOC_SIZE); } -} // namespace panda +} // namespace panda::os::mem diff --git a/libpandabase/tests/mmap_mem_pool_test.cpp b/libpandabase/tests/mmap_mem_pool_test.cpp index dbe3e6516b108c0979fdcf4ef648571496f3dd44..d3f64371788da10c2cac734abb16fdb417d531da 100644 --- a/libpandabase/tests/mmap_mem_pool_test.cpp +++ b/libpandabase/tests/mmap_mem_pool_test.cpp @@ -13,7 +13,6 @@ * limitations under the License. */ #include "mem/mem.h" -#include #include "mem/mmap_mem_pool-inl.h" #include "gtest/gtest.h" diff --git a/libpandabase/tests/unique_fd_test.cpp b/libpandabase/tests/unique_fd_test.cpp index bf39749eead53cd83dd34b10f65902f4d7505874..79bbf1587dd33b6cd0b368aa077d3be79e417341 100644 --- a/libpandabase/tests/unique_fd_test.cpp +++ b/libpandabase/tests/unique_fd_test.cpp @@ -13,13 +13,13 @@ * limitations under the License. */ -#include "os/unix/unique_fd.h" +#include "os/unique_fd.h" #include #include #include -namespace panda::os::unix { +namespace panda::os::unique_fd { enum testValue { DEFAULT_VALUE = -1, STDIN_VALUE, STDOUT_VALUE, STDERR_VALUE }; @@ -132,4 +132,4 @@ TEST(UniqueFd, Reset) EXPECT_EQ(fd_d.Get(), dupDF.stferrValue); } -} // namespace panda::os::unix +} // namespace panda::os::unique_fd diff --git a/libpandafile/BUILD.gn b/libpandafile/BUILD.gn index 7668c90834e005d3d3590ecfce34bbc31841fa14..fda509742410239508e623691626791f1b9f7863 100644 --- a/libpandafile/BUILD.gn +++ b/libpandafile/BUILD.gn @@ -79,6 +79,9 @@ ohos_shared_library("libarkfile") { relative_install_dir = "ark" } output_extension = "so" + if (is_mingw) { + output_extension = "dll" + } subsystem_name = "ark" part_name = "ark" } diff --git a/libpandafile/file.cpp b/libpandafile/file.cpp index 972e6bfed14dd2da6227caa6a4ca7015bdbc3d01..df261bc6bb4065f61cebed8fa9c827df4f19c8d4 100644 --- a/libpandafile/file.cpp +++ b/libpandafile/file.cpp @@ -26,9 +26,7 @@ #include "utils/span.h" #include "zip_archive.h" #include "trace/trace.h" -#if !PANDA_TARGET_WINDOWS #include "securec.h" -#endif #include #include @@ -56,6 +54,30 @@ const std::array File::MAGIC {'P', 'A', 'N', 'D', 'A' // NOLINTNEXTLINE(readability-identifier-naming, modernize-avoid-c-arrays) const char *ANONMAPNAME_PERFIX = "panda-"; +os::file::Mode GetMode(panda_file::File::OpenMode open_mode) +{ + switch (open_mode) { + case File::READ_ONLY: { + return os::file::Mode::READONLY; + } + case File::READ_WRITE: { +#ifdef PANDA_TARGET_WINDOWS + return os::file::Mode::READWRITE; +#else + return os::file::Mode::READONLY; +#endif + } + case File::WRITE_ONLY: { + return os::file::Mode::WRITEONLY; + } + default: { + break; + } + } + + UNREACHABLE(); +} + static uint32_t GetProt(panda_file::File::OpenMode mode) { uint32_t prot = os::mem::MMAP_PROT_READ; @@ -417,7 +439,8 @@ inline std::string VersionToString(const std::array std::unique_ptr File::Open(std::string_view filename, OpenMode open_mode) { trace::ScopedTrace scoped_trace("Open panda file " + std::string(filename)); - os::file::File file = os::file::Open(filename, os::file::Mode::READONLY); + os::file::Mode mode = GetMode(open_mode); + os::file::File file = os::file::Open(filename, mode); if (!file.IsValid()) { PLOG(ERROR, PANDAFILE) << "Failed to open panda file '" << filename << "'"; diff --git a/libpandafile/file.h b/libpandafile/file.h index b3121d553e06163b913f448ae241949507a5cb54..004dcb7596b6f113793f4e39afe71988fb3e99f4 100644 --- a/libpandafile/file.h +++ b/libpandafile/file.h @@ -121,7 +121,7 @@ public: uint32_t offset_ {0}; }; - enum OpenMode { READ_ONLY, READ_WRITE }; + enum OpenMode { READ_ONLY, READ_WRITE, WRITE_ONLY }; StringData GetStringData(EntityId id) const; EntityId GetLiteralArraysId() const; diff --git a/libpandafile/file_writer.h b/libpandafile/file_writer.h index 45e2afbb4bd0aa02ed4c83cafc2e55ef3f5bcf54..ef8d39aec4de76a94d8f58e940a36f4b1bc15c72 100644 --- a/libpandafile/file_writer.h +++ b/libpandafile/file_writer.h @@ -20,9 +20,7 @@ #include "utils/span.h" #include "utils/type_helpers.h" #include "utils/leb128.h" -#if !PANDA_TARGET_WINDOWS #include "securec.h" -#endif #include #include diff --git a/libpandafile/panda_cache.h b/libpandafile/panda_cache.h index 420ddd7e60d3de2977d22b104b02cd88bb84d06c..edb0146b086361b29bf2a8df5091e3a08707d03f 100644 --- a/libpandafile/panda_cache.h +++ b/libpandafile/panda_cache.h @@ -77,8 +77,10 @@ public: return panda::helpers::math::PowerOfTwoTableSlot(id.GetOffset(), CLASS_CACHE_SIZE); } - inline Method *GetMethodFromCache(File::EntityId id) const + inline Method *GetMethodFromCache([[maybe_unused]] File::EntityId id) const { +// the os platform macro should be removed when "atomic" symbol is provided in mingw +#ifndef PANDA_TARGET_WINDOWS uint32_t index = GetMethodIndex(id); auto *pair_ptr = reinterpret_cast *>(reinterpret_cast(&(method_cache_[index]))); @@ -87,11 +89,14 @@ public: if (pair.id_ == id) { return pair.ptr_; } +#endif return nullptr; } - inline void SetMethodCache(File::EntityId id, Method *method) + inline void SetMethodCache([[maybe_unused]] File::EntityId id, [[maybe_unused]] Method *method) { +// the os platform macro should be removed when "atomic" symbol is provided in mingw +#ifndef PANDA_TARGET_WINDOWS MethodCachePair pair; pair.id_ = id; pair.ptr_ = method; @@ -100,10 +105,13 @@ public: reinterpret_cast *>(reinterpret_cast(&(method_cache_[index]))); TSAN_ANNOTATE_HAPPENS_BEFORE(pair_ptr); pair_ptr->store(pair, std::memory_order_release); +#endif } - inline Field *GetFieldFromCache(File::EntityId id) const + inline Field *GetFieldFromCache([[maybe_unused]] File::EntityId id) const { +// the os platform macro should be removed when "atomic" symbol is provided in mingw +#ifndef PANDA_TARGET_WINDOWS uint32_t index = GetFieldIndex(id); auto *pair_ptr = reinterpret_cast *>(reinterpret_cast(&(field_cache_[index]))); @@ -112,11 +120,14 @@ public: if (pair.id_ == id) { return pair.ptr_; } +#endif return nullptr; } - inline void SetFieldCache(File::EntityId id, Field *field) + inline void SetFieldCache([[maybe_unused]] File::EntityId id, [[maybe_unused]] Field *field) { +// the os platform macro should be removed when "atomic" symbol is provided in mingw +#ifndef PANDA_TARGET_WINDOWS uint32_t index = GetFieldIndex(id); auto *pair_ptr = reinterpret_cast *>(reinterpret_cast(&(field_cache_[index]))); @@ -125,10 +136,13 @@ public: pair.ptr_ = field; TSAN_ANNOTATE_HAPPENS_BEFORE(pair_ptr); pair_ptr->store(pair, std::memory_order_release); +#endif } - inline Class *GetClassFromCache(File::EntityId id) const + inline Class *GetClassFromCache([[maybe_unused]] File::EntityId id) const { +// the os platform macro should be removed when "atomic" symbol is provided in mingw +#ifndef PANDA_TARGET_WINDOWS uint32_t index = GetClassIndex(id); auto *pair_ptr = reinterpret_cast *>(reinterpret_cast(&(class_cache_[index]))); @@ -137,11 +151,14 @@ public: if (pair.id_ == id) { return pair.ptr_; } +#endif return nullptr; } - inline void SetClassCache(File::EntityId id, Class *clazz) + inline void SetClassCache([[maybe_unused]] File::EntityId id, [[maybe_unused]] Class *clazz) { +// the os platform macro should be removed when "atomic" symbol is provided in mingw +#ifndef PANDA_TARGET_WINDOWS ClassCachePair pair; pair.id_ = id; pair.ptr_ = clazz; @@ -150,11 +167,14 @@ public: reinterpret_cast *>(reinterpret_cast(&(class_cache_[index]))); TSAN_ANNOTATE_HAPPENS_BEFORE(pair_ptr); pair_ptr->store(pair, std::memory_order_release); +#endif } template - bool EnumerateCachedClasses(const Callback &cb) + bool EnumerateCachedClasses([[maybe_unused]] const Callback &cb) { +// the os platform macro should be removed when "atomic" symbol is provided in mingw +#ifndef PANDA_TARGET_WINDOWS for (uint32_t i = 0; i < CLASS_CACHE_SIZE; i++) { auto *pair_ptr = reinterpret_cast *>(reinterpret_cast(&(class_cache_[i]))); @@ -166,6 +186,7 @@ public: } } } +#endif return true; } diff --git a/libziparchive/BUILD.gn b/libziparchive/BUILD.gn index c180e409530a15376b10ae54d7c5b9b716aa9ea5..8f529069353223c9d52607fb10e6da3783199fab 100644 --- a/libziparchive/BUILD.gn +++ b/libziparchive/BUILD.gn @@ -53,6 +53,9 @@ ohos_shared_library("libarkziparchive") { relative_install_dir = "ark" } output_extension = "so" + if (is_mingw) { + output_extension = "dll" + } subsystem_name = "ark" part_name = "ark" } diff --git a/runtime/BUILD.gn b/runtime/BUILD.gn index 4f259c25a447cc52a59a52cc9107152622aedad9..c5637a16b9b4ef7c7345dc55a00ece8670ff4886 100644 --- a/runtime/BUILD.gn +++ b/runtime/BUILD.gn @@ -76,7 +76,6 @@ ohos_static_library("libarkruntime_static") { "class_linker_extension.cpp", "coretypes/array.cpp", "coretypes/string.cpp", - "dprofiler/dprofiler.cpp", "dyn_class_linker_extension.cpp", "entrypoints/entrypoints.cpp", "exceptions.cpp", @@ -118,7 +117,6 @@ ohos_static_library("libarkruntime_static") { "mem/heap_manager.cpp", "mem/heap_verifier.cpp", "mem/internal_allocator.cpp", - "mem/mem_hooks.cpp", "mem/mem_stats.cpp", "mem/mem_stats_additional_info.cpp", "mem/mem_stats_default.cpp", @@ -141,7 +139,6 @@ ohos_static_library("libarkruntime_static") { "panda_vm.cpp", "runtime.cpp", "runtime_helpers.cpp", - "signal_handler.cpp", "stack_walker.cpp", "string_table.cpp", "thread.cpp", @@ -157,6 +154,12 @@ ohos_static_library("libarkruntime_static") { "tooling/pt_thread.cpp", "vtable_builder.cpp", ] + if (!is_mingw) { + sources += [ + "dprofiler/dprofiler.cpp", + "signal_handler.cpp", + ] + } if (current_cpu == "arm") { sources += [ "arch/arm/interpreter_support.S", @@ -228,7 +231,6 @@ ohos_static_library("libarkruntime_static") { deps = [ ":arkruntime_header_deps", ":arkruntime_interpreter_impl", - "$ark_root/dprof:libdprof", "$ark_root/libpandabase:libarkbase", "$ark_root/libpandafile:libarkfile", "$ark_root/libpandafile:libarkfile_type_gen_h", @@ -237,6 +239,10 @@ ohos_static_library("libarkruntime_static") { sdk_libc_secshared_dep, ] + if (!is_mingw) { + deps += [ "$ark_root/dprof:libdprof" ] + } + if (is_standard_system) { cflags_cc = [ "-fvisibility=hidden" ] } diff --git a/runtime/CMakeLists.txt b/runtime/CMakeLists.txt index a5bb82fc2178999039e8cb720e55d0c2f9dd9f22..23c744dd22b6df62aa95b13375aebc2c35048b44 100644 --- a/runtime/CMakeLists.txt +++ b/runtime/CMakeLists.txt @@ -107,7 +107,6 @@ set(SOURCES mem/mem_stats.cpp mem/internal_allocator.cpp mem/panda_string.cpp - mem/mem_hooks.cpp mem/memory_manager.cpp mark_word.cpp method.cpp diff --git a/runtime/arch/amd64/interpreter_support.S b/runtime/arch/amd64/interpreter_support.S index 5e3a7888aa6a3ffb1de48f2000de09d1326662dc..b9867a9c7d2e32eceaba55b0561c051bf3ddd7be 100644 --- a/runtime/arch/amd64/interpreter_support.S +++ b/runtime/arch/amd64/interpreter_support.S @@ -13,7 +13,9 @@ * limitations under the License. */ +#ifndef PANDA_TARGET_WINDOWS .type ExecuteImplStub, @function +#endif .global ExecuteImplStub .balign 16 ExecuteImplStub: diff --git a/runtime/arch/asm_support.cpp b/runtime/arch/asm_support.cpp index 08690cd0c15ba05b94ed4c50825058f2315930c6..0cee17c74bd5d5774d80005141fc32a0f1947d37 100644 --- a/runtime/arch/asm_support.cpp +++ b/runtime/arch/asm_support.cpp @@ -38,7 +38,7 @@ static_assert(FRAME_SLOT_OFFSET == 80U); static_assert(FRAME_TAG_OFFSET == 88U); #endif -extern "C" ManagedThread *GetCurrentThread() +extern "C" ManagedThread *GetCurrentManagedThread() { return ManagedThread::GetCurrent(); } diff --git a/runtime/arch/asm_support.h b/runtime/arch/asm_support.h index 17c1ac3d82ecec6f084767e90a874749faa045d5..85c88b3ca1860599393ec3a440c2cb6c23d673af 100644 --- a/runtime/arch/asm_support.h +++ b/runtime/arch/asm_support.h @@ -37,6 +37,13 @@ // clang-format off +#ifndef PANDA_TARGET_WINDOWS +// NOLINTNEXTLINE(cppcoreguidelines-macro-usage) +#define TYPE_FUNCTION(name) .type name, %function +#else +#define TYPE_FUNCTION(name) +#endif + #ifndef NDEBUG // NOLINTNEXTLINE(cppcoreguidelines-macro-usage) diff --git a/runtime/bridge/arch/amd64/compiled_code_to_interpreter_bridge_amd64.S b/runtime/bridge/arch/amd64/compiled_code_to_interpreter_bridge_amd64.S index ac09c1abb0f59dff56d7f8ef7f942a5a55053dfc..f6638f6c207aaf4be7b13a166712964cd2fb31cf 100644 --- a/runtime/bridge/arch/amd64/compiled_code_to_interpreter_bridge_amd64.S +++ b/runtime/bridge/arch/amd64/compiled_code_to_interpreter_bridge_amd64.S @@ -30,7 +30,7 @@ .extern IncrementHotnessCounter .global CompiledCodeToInterpreterBridge -.type CompiledCodeToInterpreterBridge, %function +TYPE_FUNCTION(CompiledCodeToInterpreterBridge) CompiledCodeToInterpreterBridge: CFI_STARTPROC CFI_DEF_CFA(rsp, 8) diff --git a/runtime/bridge/arch/amd64/compiled_code_to_interpreter_bridge_dyn_amd64.S b/runtime/bridge/arch/amd64/compiled_code_to_interpreter_bridge_dyn_amd64.S index f01a1ded7a8c059dc386727d89b551a0fe136591..9056cb8a339cc012e3f0dbfd0ed53fb6bd5f31c2 100644 --- a/runtime/bridge/arch/amd64/compiled_code_to_interpreter_bridge_dyn_amd64.S +++ b/runtime/bridge/arch/amd64/compiled_code_to_interpreter_bridge_dyn_amd64.S @@ -29,7 +29,7 @@ // CompiledCodeToInterpreterBridgeDyn(Method* method, uint32_t num_args, int64_t func_obj, int64_t func_tag, int64_t arg_i, int64_t tag_i, ...) .global CompiledCodeToInterpreterBridgeDyn -.type CompiledCodeToInterpreterBridgeDyn, %function +TYPE_FUNCTION(CompiledCodeToInterpreterBridgeDyn) CompiledCodeToInterpreterBridgeDyn: CFI_STARTPROC CFI_DEF_CFA(rsp, 8) diff --git a/runtime/bridge/arch/amd64/compiled_code_to_runtime_bridge_amd64.S b/runtime/bridge/arch/amd64/compiled_code_to_runtime_bridge_amd64.S index ffcef6b6dc078547b79742c3db1a203e14442853..9b7166b7d467e6e27498a95f8d0052b062158696 100644 --- a/runtime/bridge/arch/amd64/compiled_code_to_runtime_bridge_amd64.S +++ b/runtime/bridge/arch/amd64/compiled_code_to_runtime_bridge_amd64.S @@ -17,7 +17,7 @@ .macro ENTRYPOINT name, entry, paramsnum .global \name -.type \name, %function +TYPE_FUNCTION(\name) \name: CFI_STARTPROC CFI_DEF_CFA(rsp, 8) @@ -65,7 +65,7 @@ #include "entrypoints_bridge_asm_macro.inl" .global AbstractMethodStub -.type AbstractMethodStub, %function +TYPE_FUNCTION(AbstractMethodStub) AbstractMethodStub: CFI_STARTPROC CFI_DEF_CFA(rsp, 8) diff --git a/runtime/bridge/arch/amd64/interpreter_to_compiled_code_bridge_amd64.S b/runtime/bridge/arch/amd64/interpreter_to_compiled_code_bridge_amd64.S index e706973469ea30d65c0cff6e02166b6912a38eef..5aaa7370fb91f3701eab7c38050c98f8f68c1fd8 100644 --- a/runtime/bridge/arch/amd64/interpreter_to_compiled_code_bridge_amd64.S +++ b/runtime/bridge/arch/amd64/interpreter_to_compiled_code_bridge_amd64.S @@ -261,7 +261,7 @@ // void InterpreterToCompiledCodeBridge(const BytecodeInstruction* insn, const Frame *iframe, const Method *method, ManagedThread* thread) .global InterpreterToCompiledCodeBridge -.type InterpreterToCompiledCodeBridge, %function +TYPE_FUNCTION(InterpreterToCompiledCodeBridge) InterpreterToCompiledCodeBridge: CFI_STARTPROC CFI_DEF_CFA(rsp, 8) @@ -450,7 +450,7 @@ InterpreterToCompiledCodeBridge: // DecodedTaggedValue InvokeCompiledCodeWithArguments(const int64_t* args, const Frame *iframe, const Method *method, ManagedThread* thread) .global InvokeCompiledCodeWithArgArray -.type InvokeCompiledCodeWithArgArray, %function +TYPE_FUNCTION(InvokeCompiledCodeWithArgArray) InvokeCompiledCodeWithArgArray: CFI_STARTPROC CFI_DEF_CFA(rsp, 8) diff --git a/runtime/bridge/arch/amd64/interpreter_to_compiled_code_bridge_dyn_amd64.S b/runtime/bridge/arch/amd64/interpreter_to_compiled_code_bridge_dyn_amd64.S index e85b501dd2ffeabe48f64c319a01aac9704d7442..f4d2ff8991a5aa7bf2d1acd893075a45181746bd 100644 --- a/runtime/bridge/arch/amd64/interpreter_to_compiled_code_bridge_dyn_amd64.S +++ b/runtime/bridge/arch/amd64/interpreter_to_compiled_code_bridge_dyn_amd64.S @@ -21,7 +21,7 @@ // const Method*, %rdx // ManagedThread* thread) %rcx .global InterpreterToCompiledCodeBridgeDyn -.type InterpreterToCompiledCodeBridgeDyn, %function +TYPE_FUNCTION(InterpreterToCompiledCodeBridgeDyn) InterpreterToCompiledCodeBridgeDyn: CFI_STARTPROC CFI_DEF_CFA(rsp, 8) @@ -125,7 +125,7 @@ InterpreterToCompiledCodeBridgeDyn: // const Method*, %rcx // ManagedThread* thread) %r8 .global InvokeCompiledCodeWithArgArrayDyn -.type InvokeCompiledCodeWithArgArrayDyn, %function +TYPE_FUNCTION(InvokeCompiledCodeWithArgArrayDyn) InvokeCompiledCodeWithArgArrayDyn: CFI_STARTPROC CFI_DEF_CFA(rsp, 8) diff --git a/runtime/dprofiler/dprofiler.h b/runtime/dprofiler/dprofiler.h index 0a2f2dc0f8c4c1d4ad3af3c6d576edd5b0be6e4a..bb1ea099280ad65b2fcde2effbd3dab2335ec21c 100644 --- a/runtime/dprofiler/dprofiler.h +++ b/runtime/dprofiler/dprofiler.h @@ -38,7 +38,15 @@ class RuntimeListener; */ class DProfiler final { public: +#ifdef PANDA_TARGET_UNIX DProfiler(std::string_view app_name, Runtime *runtime); +#else + DProfiler([[maybe_unused]] std::string_view app_name, [[maybe_unused]] Runtime *runtime) + { + // Unsupported on windows platform + UNREACHABLE(); + } +#endif // PANDA_TARGET_UNIX ~DProfiler() = default; /** diff --git a/runtime/include/runtime.h b/runtime/include/runtime.h index 3d9a494e1787b9e0bd43f2c35690f546e46b32b6..e915b585a92236a62e5e6ac0bf32932a8a5a8f2e 100644 --- a/runtime/include/runtime.h +++ b/runtime/include/runtime.h @@ -24,7 +24,6 @@ #include "libpandabase/mem/arena_allocator.h" #include "libpandabase/os/mutex.h" -#include "libpandabase/os/library_loader.h" #include "libpandabase/utils/expected.h" #include "libpandabase/utils/dfx.h" #include "libpandafile/file_items.h" @@ -34,7 +33,9 @@ #include "runtime/include/runtime_options.h" #include "runtime/include/gc_task.h" #include "runtime/include/tooling/debug_interface.h" +#ifndef PANDA_TARGET_WINDOWS #include "runtime/signal_handler.h" +#endif #include "runtime/mem/allocator_adapter.h" #include "runtime/mem/gc/gc.h" #include "runtime/mem/gc/gc_trigger.h" @@ -43,6 +44,7 @@ #include "runtime/string_table.h" #include "runtime/thread_manager.h" #include "verification/verification_options.h" +#include "libpandabase/os/library_loader.h" namespace panda { @@ -307,12 +309,12 @@ public: return is_stacktrace_; } +#ifndef PANDA_TARGET_WINDOWS SignalManager *GetSignalManager() { return signal_manager_; } - - Trace *CreateTrace(LanguageContext ctx, PandaUniquePtr trace_file, size_t buffer_size); +#endif void SetPtLangExt(tooling::PtLangExt *pt_lang_ext); @@ -377,7 +379,9 @@ private: PandaVM *panda_vm_ = nullptr; +#ifndef PANDA_TARGET_WINDOWS SignalManager *signal_manager_ {nullptr}; +#endif // Language context static constexpr size_t LANG_EXTENSIONS_COUNT = static_cast(panda_file::SourceLang::LAST) + 1; diff --git a/runtime/interpreter/interpreter-inl.h b/runtime/interpreter/interpreter-inl.h index 4f232464a0079e9a371f6eac471943387371787b..bc5d3561331ee9531ca70800f412f8fdf9c914c1 100644 --- a/runtime/interpreter/interpreter-inl.h +++ b/runtime/interpreter/interpreter-inl.h @@ -58,7 +58,6 @@ #include "runtime/interpreter/vregister_iterator.h" #include "runtime/jit/profiling_data.h" #include "runtime/mem/vm_handle.h" -#include "runtime/object_accessor-impl.cpp" #include "runtime/handle_base-inl.h" // ALWAYS_INLINE is mandatory attribute for handlers. There are cases which will be failed without it. diff --git a/runtime/mem/gc/bitmap.h b/runtime/mem/gc/bitmap.h index 8adb635a75e9dec8612d3029b9e67c344a2b9126..6c249eadc7bd64580204954c8f3bf64c031b1de2 100644 --- a/runtime/mem/gc/bitmap.h +++ b/runtime/mem/gc/bitmap.h @@ -16,10 +16,7 @@ #ifndef PANDA_RUNTIME_MEM_GC_BITMAP_H_ #define PANDA_RUNTIME_MEM_GC_BITMAP_H_ -// clash with mingw -#ifndef PANDA_TARGET_WINDOWS #include -#endif #include #include #include @@ -46,9 +43,7 @@ public: void ClearAllBits() { -#ifndef PANDA_TARGET_WINDOWS (void)memset_s(bitmap_.Data(), bitmap_.SizeBytes(), 0, bitmap_.SizeBytes()); -#endif } Span GetBitMap() @@ -242,13 +237,11 @@ protected: void SetWords([[maybe_unused]] size_t word_begin, [[maybe_unused]] size_t word_end) { ASSERT(word_begin <= word_end); -#ifndef PANDA_TARGET_WINDOWS if (UNLIKELY(word_begin == word_end)) { return; } (void)memset_s(&bitmap_[word_begin], (word_end - word_begin) * sizeof(BitmapWordType), ~static_cast(0), (word_end - word_begin) * sizeof(BitmapWordType)); -#endif } /** @@ -259,13 +252,11 @@ protected: void ClearWords([[maybe_unused]] size_t word_begin, [[maybe_unused]] size_t word_end) { ASSERT(word_begin <= word_end); -#ifndef PANDA_TARGET_WINDOWS if (UNLIKELY(word_begin == word_end)) { return; } (void)memset_s(&bitmap_[word_begin], (word_end - word_begin) * sizeof(BitmapWordType), static_cast(0), (word_end - word_begin) * sizeof(BitmapWordType)); -#endif } explicit Bitmap(BitmapWordType *bitmap, size_t bitsize) @@ -299,7 +290,7 @@ private: size_t GetBitIdxWithinWord(size_t bit_offset) const { CheckBitOffset(bit_offset); - constexpr auto BIT_INDEX_MASK = static_cast((1UL << LOG_BITSPERWORD) - 1); + constexpr auto BIT_INDEX_MASK = static_cast((1ULL << LOG_BITSPERWORD) - 1); return bit_offset & BIT_INDEX_MASK; } @@ -310,7 +301,7 @@ private: */ BitmapWordType GetBitMask(size_t bit_offset) const { - return 1UL << GetBitIdxWithinWord(bit_offset); + return 1ULL << GetBitIdxWithinWord(bit_offset); } /** diff --git a/runtime/mem/malloc-proxy-allocator-inl.h b/runtime/mem/malloc-proxy-allocator-inl.h index ca3e6d3455566ff5cdb7979a8baeca9231abd70c..f045576e9cf09475b42f7b01d8445b1c00faf2bd 100644 --- a/runtime/mem/malloc-proxy-allocator-inl.h +++ b/runtime/mem/malloc-proxy-allocator-inl.h @@ -47,8 +47,7 @@ void *MallocProxyAllocator::Alloc(size_t size, Alignment align) lock_.Lock(); } size_t alignment_in_bytes = GetAlignmentInBytes(align); - size_t aligned_size = (size + alignment_in_bytes - 1) & ~(alignment_in_bytes - 1); - void *ret = aligned_alloc(alignment_in_bytes, aligned_size); + void *ret = os::mem::AlignedAlloc(alignment_in_bytes, size); // NOLINTNEXTLINE(readability-braces-around-statements, bugprone-suspicious-semicolon) if constexpr (!DUMMY_ALLOC_CONFIG) { ASSERT(allocated_memory_.find(ret) == allocated_memory_.end()); @@ -72,7 +71,7 @@ void MallocProxyAllocator::Free(void *mem) if constexpr (!DUMMY_ALLOC_CONFIG) { lock_.Lock(); } - std::free(mem); // NOLINT(cppcoreguidelines-no-malloc) + os::mem::AlignedFree(mem); // NOLINTNEXTLINE(readability-braces-around-statements, bugprone-suspicious-semicolon) if constexpr (!DUMMY_ALLOC_CONFIG) { auto iterator = allocated_memory_.find(mem); diff --git a/runtime/mem/mem_stats.h b/runtime/mem/mem_stats.h index 37f9e80670c05ebbacf6f15f39a042f256e7c483..b9cdffe52518ff21309b113aeddb5cc74f02e327 100644 --- a/runtime/mem/mem_stats.h +++ b/runtime/mem/mem_stats.h @@ -120,7 +120,7 @@ private: duration min_pause_ = duration(0); duration max_pause_ = duration(0); duration sum_pause_ = duration(0); - uint pause_count_ = 0; + uint64_t pause_count_ = 0; // make groups of different parts of the VM (JIT, interpreter, etc) std::atomic_uint64_t objects_allocated_ = 0; diff --git a/runtime/mem/mem_stats_additional_info.cpp b/runtime/mem/mem_stats_additional_info.cpp index 4012f78f8155c8f8fc9351cad36c6b71bbae5bce..1a276c5cefbaf077dcb1126c702a77cbdf73315f 100644 --- a/runtime/mem/mem_stats_additional_info.cpp +++ b/runtime/mem/mem_stats_additional_info.cpp @@ -65,7 +65,7 @@ void MemStatsAdditionalInfo::RecordGCPhaseEnd() os::memory::LockHolder lk(phase_lock_); ASSERT(current_phase_ != GCPhase::GC_PHASE_LAST); - uint phase_index = ToIndex(current_phase_); + uint64_t phase_index = ToIndex(current_phase_); duration phase_time = clock::now() - phase_start_time_; if (phase_count_[phase_index] != 0) { min_phase_time_[phase_index] = std::min(min_phase_time_[phase_index], phase_time); diff --git a/runtime/mem/mem_stats_additional_info.h b/runtime/mem/mem_stats_additional_info.h index 041e5b63d077f2cbab1fe12e2ba6d465fb971583..6d721c76f76eef0443ff2bf377eb4d3382a0ccbd 100644 --- a/runtime/mem/mem_stats_additional_info.h +++ b/runtime/mem/mem_stats_additional_info.h @@ -57,7 +57,7 @@ private: std::array min_phase_time_ = {}; std::array max_phase_time_ = {}; std::array sum_phase_time_ = {}; - std::array phase_count_ = {}; + std::array phase_count_ = {}; os::memory::Mutex phase_lock_; }; diff --git a/runtime/runtime.cpp b/runtime/runtime.cpp index b7893d3319c6d70bb7afb3063564d531e1bc774e..624311dd8c1ec7aea2d8347b9c350517a1cec721 100644 --- a/runtime/runtime.cpp +++ b/runtime/runtime.cpp @@ -26,7 +26,7 @@ #include "libpandabase/events/events.h" #include "libpandabase/mem/mem_config.h" #include "libpandabase/mem/pool_manager.h" -#include "libpandabase/os/library_loader.h" +#include "libpandabase/os/mem_hooks.h" #include "libpandabase/os/native_stack.h" #include "libpandabase/os/thread.h" #include "libpandabase/utils/arena_containers.h" @@ -53,7 +53,6 @@ #include "runtime/mem/gc/stw-gc/stw-gc.h" #include "runtime/mem/gc/crossing_map_singleton.h" #include "runtime/mem/heap_manager.h" -#include "runtime/mem/mem_hooks.h" #include "runtime/mem/memory_manager.h" #include "runtime/mem/internal_allocator-inl.h" #include "runtime/core/core_class_linker_extension.h" @@ -407,14 +406,16 @@ Runtime::Runtime(const RuntimeOptions &options, mem::InternalAllocatorPtr intern // CODECHECK-NOLINTNEXTLINE(CPP_RULE_ID_SMARTPOINTER_INSTEADOF_ORIGINPOINTER) class_linker_ = new ClassLinker(internal_allocator_, std::move(extensions)); +#ifndef PANDA_TARGET_WINDOWS // CODECHECK-NOLINTNEXTLINE(CPP_RULE_ID_SMARTPOINTER_INSTEADOF_ORIGINPOINTER) signal_manager_ = new SignalManager(internal_allocator_); +#endif if (IsEnableMemoryHooks()) { // libbfd (which is used to get debug info from elf files) does a lot of allocations. // Don't track allocations in this case. if (!options_.IsSafepointBacktrace()) { - mem::PandaHooks::Enable(); + panda::os::mem_hooks::PandaHooks::Enable(); } } @@ -432,12 +433,14 @@ Runtime::~Runtime() panda::verifier::debug::DebugContext::Destroy(); if (IsEnableMemoryHooks()) { - mem::PandaHooks::Disable(); + panda::os::mem_hooks::PandaHooks::Disable(); } trace::ScopedTrace scoped_trace("Delete state"); +#ifndef PANDA_TARGET_WINDOWS signal_manager_->DeleteHandlersArray(); delete signal_manager_; +#endif delete class_linker_; if (dprofiler_ != nullptr) { internal_allocator_->Delete(dprofiler_); @@ -1174,11 +1177,4 @@ bool Runtime::SaveProfileInfo() const return save_profiling_info_; } -Trace *Runtime::CreateTrace([[maybe_unused]] LanguageContext ctx, - [[maybe_unused]] PandaUniquePtr trace_file, - [[maybe_unused]] size_t buffer_size) -{ - LOG(FATAL, RUNTIME) << "Method tracing isn't supported at the moment!"; - return nullptr; -} } // namespace panda diff --git a/runtime/signal_handler.cpp b/runtime/signal_handler.cpp index d4fd92ef67131b27dfdfc5cd91a7fd94c04e0355..838f35a44621056b7617fc69af925e3df8a717ff 100644 --- a/runtime/signal_handler.cpp +++ b/runtime/signal_handler.cpp @@ -22,9 +22,6 @@ #include "include/panda_vm.h" #include "include/thread.h" #include "include/stack_walker.h" -#if defined(PANDA_TARGET_UNIX) -#include "libpandabase/os/unix/sighooklib/sighook.h" -#endif // PANDA_TARGET_UNIX namespace panda { diff --git a/runtime/signal_handler.h b/runtime/signal_handler.h index 9d6fa0d5228b8c3ecff06b1cafb0820da447f505..8c850359b980015c97356e46be1ac1aa0c5d7557 100644 --- a/runtime/signal_handler.h +++ b/runtime/signal_handler.h @@ -22,6 +22,7 @@ #include #include #include "runtime/include/mem/panda_containers.h" +#include "libpandabase/os/sighook.h" namespace panda {