From 6e8a43ac1404c697f3aba81275057a78c38877ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Domagoj=20=C5=A0ari=C4=87?= Date: Thu, 8 Aug 2024 19:31:53 +0200 Subject: [PATCH 1/3] Clang-CL fixes. --- include/psi/vm/detail/nt.hpp | 26 ++---------- include/psi/vm/handles/handle.win32.hpp | 4 +- src/detail/nt.cpp | 55 +++++++++++++++++++++++++ src/mapped_view/mapped_view.cpp | 2 +- src/mapped_view/mapped_view.posix.cpp | 2 +- src/mapped_view/mapped_view.win32.cpp | 2 +- 6 files changed, 64 insertions(+), 27 deletions(-) create mode 100644 src/detail/nt.cpp diff --git a/include/psi/vm/detail/nt.hpp b/include/psi/vm/detail/nt.hpp index 365f540..b4f57df 100644 --- a/include/psi/vm/detail/nt.hpp +++ b/include/psi/vm/detail/nt.hpp @@ -13,26 +13,18 @@ /// For more information, see http://www.boost.org /// //////////////////////////////////////////////////////////////////////////////// -/// http://undocumented.ntinternals.net -/// https://www.i.u-tokyo.ac.jp/edu/training/ss/lecture/new-documents/Lectures/02-VirtualMemory/VirtualMemory.pdf -/// http://ultradefrag.sourceforge.net/doc/man/Windows%20NT(2000)%20Native%20API%20Reference.pdf -/// https://webdiis.unizar.es/~spd/pub/windows/ntdll.htm -/// https://technet.microsoft.com/en-us/sysinternals/bb896657.aspx WinObj -/// https://www.osronline.com/article.cfm?id=91 -/// http://downloads.securityfocus.com/vulnerabilities/exploits/20940-Ivan.c -//////////////////////////////////////////////////////////////////////////////// //------------------------------------------------------------------------------ #pragma once -#include #include #ifndef _WIN32_WINNT # define _WIN32_WINNT 0x0A00 // _WIN32_WINNT_WIN10 #endif // _WIN32_WINNT +#include #include -#pragma comment( lib, "ntdll.lib" ) +#include //------------------------------------------------------------------------------ namespace psi::vm::nt { @@ -50,18 +42,8 @@ inline auto const current_process{ reinterpret_cast( std::intptr_t{ -1 } namespace detail { - inline HMODULE const ntdll{ ::GetModuleHandleW( L"ntdll.dll" ) }; - - inline BOOST_ATTRIBUTES( BOOST_COLD, BOOST_RESTRICTED_FUNCTION_L3, BOOST_RESTRICTED_FUNCTION_RETURN ) - ::PROC BOOST_CC_REG get_nt_proc( char const * const proc_name ) noexcept - { - BOOST_ASSERT( current_process == ::GetCurrentProcess() ); // for lack of a better place for this sanity check - - BOOST_ASSERT( ntdll ); - auto const result{ ::GetProcAddress( ntdll, proc_name ) }; - BOOST_ASSERT( result ); - return result; - } + BOOST_ATTRIBUTES( BOOST_COLD, BOOST_RESTRICTED_FUNCTION_L3, BOOST_RESTRICTED_FUNCTION_RETURN ) + ::PROC get_nt_proc( char const * proc_name ) noexcept; template ProcPtr get_nt_proc( char const * const proc_name ) diff --git a/include/psi/vm/handles/handle.win32.hpp b/include/psi/vm/handles/handle.win32.hpp index 2fa321d..57adadd 100644 --- a/include/psi/vm/handles/handle.win32.hpp +++ b/include/psi/vm/handles/handle.win32.hpp @@ -36,7 +36,7 @@ struct handle_traits inline static native_t const invalid_value{ boost::winapi::invalid_handle_value }; //...mrmlj...or nullptr eg. for CreateFileMapping - static BOOST_ATTRIBUTES( BOOST_MINSIZE, BOOST_RESTRICTED_FUNCTION_L2, BOOST_EXCEPTIONLESS ) + static BOOST_ATTRIBUTES( BOOST_MINSIZE, BOOST_RESTRICTED_FUNCTION_L1, BOOST_EXCEPTIONLESS ) void close( native_t const native_handle ) { BOOST_VERIFY @@ -46,7 +46,7 @@ struct handle_traits ); } - static BOOST_ATTRIBUTES( BOOST_MINSIZE, BOOST_RESTRICTED_FUNCTION_L2, BOOST_EXCEPTIONLESS ) + static BOOST_ATTRIBUTES( BOOST_MINSIZE, BOOST_RESTRICTED_FUNCTION_L1, BOOST_EXCEPTIONLESS ) native_t copy( native_t native_handle ); // TODO }; // handle_traits diff --git a/src/detail/nt.cpp b/src/detail/nt.cpp new file mode 100644 index 0000000..4a39346 --- /dev/null +++ b/src/detail/nt.cpp @@ -0,0 +1,55 @@ +//////////////////////////////////////////////////////////////////////////////// +/// +/// Copyright (c) Domagoj Saric 2015 - 2024. +/// +/// Use, modification and distribution is subject to the +/// Boost Software License, Version 1.0. +/// (See accompanying file LICENSE_1_0.txt or copy at +/// http://www.boost.org/LICENSE_1_0.txt) +/// +/// For more information, see http://www.boost.org +/// +//////////////////////////////////////////////////////////////////////////////// +/// http://undocumented.ntinternals.net +/// https://www.i.u-tokyo.ac.jp/edu/training/ss/lecture/new-documents/Lectures/02-VirtualMemory/VirtualMemory.pdf +/// http://ultradefrag.sourceforge.net/doc/man/Windows%20NT(2000)%20Native%20API%20Reference.pdf +/// https://webdiis.unizar.es/~spd/pub/windows/ntdll.htm +/// https://technet.microsoft.com/en-us/sysinternals/bb896657.aspx WinObj +/// https://www.osronline.com/article.cfm?id=91 +/// http://downloads.securityfocus.com/vulnerabilities/exploits/20940-Ivan.c +//////////////////////////////////////////////////////////////////////////////// +//------------------------------------------------------------------------------ +#include + +#include + +#pragma comment( lib, "ntdll.lib" ) +//------------------------------------------------------------------------------ +namespace psi::vm::nt +{ +//------------------------------------------------------------------------------ + +namespace detail +{ +#ifdef __clang__ // initialization order fiasco wrkrnd + HMODULE ntdll; + [[ gnu::constructor( 101 ) ]] + void init_ntdll_handle() noexcept { ntdll = ::GetModuleHandleW( L"ntdll.dll" ); } +#else + HMODULE const ntdll{ ::GetModuleHandleW( L"ntdll.dll" ) }; +#endif + + BOOST_ATTRIBUTES( BOOST_COLD, BOOST_RESTRICTED_FUNCTION_L3, BOOST_RESTRICTED_FUNCTION_RETURN ) + ::PROC get_nt_proc( char const * const proc_name ) noexcept + { + BOOST_ASSERT( current_process == ::GetCurrentProcess() ); // for lack of a better place for this sanity check + BOOST_ASSUME( ntdll ); + auto const result{ ::GetProcAddress( ntdll, proc_name ) }; + BOOST_ASSUME( result ); + return result; + } +} // namespace detail + +//------------------------------------------------------------------------------ +} // psi::vm::nt +//------------------------------------------------------------------------------ diff --git a/src/mapped_view/mapped_view.cpp b/src/mapped_view/mapped_view.cpp index e1edd1b..475f47c 100644 --- a/src/mapped_view/mapped_view.cpp +++ b/src/mapped_view/mapped_view.cpp @@ -31,7 +31,7 @@ map ) noexcept; BOOST_ATTRIBUTES( BOOST_EXCEPTIONLESS, BOOST_RESTRICTED_FUNCTION_L1 ) -void BOOST_CC_REG unmap( mapped_span view ) noexcept; +void unmap( mapped_span view ) noexcept; void unmap_partial( mapped_span range ) noexcept; //------------------------------------------------------------------------------ diff --git a/src/mapped_view/mapped_view.posix.cpp b/src/mapped_view/mapped_view.posix.cpp index 6f7d658..c712ec5 100644 --- a/src/mapped_view/mapped_view.posix.cpp +++ b/src/mapped_view/mapped_view.posix.cpp @@ -106,7 +106,7 @@ map } BOOST_ATTRIBUTES( BOOST_MINSIZE, BOOST_EXCEPTIONLESS ) -void BOOST_CC_REG unmap( mapped_span const view ) noexcept +void unmap( mapped_span const view ) noexcept { [[ maybe_unused ]] auto munmap_result{ ::munmap( view.data(), view.size() ) }; #ifndef __EMSCRIPTEN__ diff --git a/src/mapped_view/mapped_view.win32.cpp b/src/mapped_view/mapped_view.win32.cpp index 90b5eab..5bbac94 100644 --- a/src/mapped_view/mapped_view.win32.cpp +++ b/src/mapped_view/mapped_view.win32.cpp @@ -100,7 +100,7 @@ namespace } // anonymous namespace BOOST_ATTRIBUTES( BOOST_MINSIZE, BOOST_EXCEPTIONLESS, BOOST_RESTRICTED_FUNCTION_L1 ) -void BOOST_CC_REG unmap( mapped_span const view ) noexcept +void unmap( mapped_span const view ) noexcept { BOOST_ASSERT_MSG ( // greater-or-equal because of the inability of Windows to do a proper unmap_partial From 9957183cb3bf9aa02d33fd951e3dcc0c0e43fd4d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Domagoj=20=C5=A0ari=C4=87?= Date: Thu, 8 Aug 2024 19:33:21 +0200 Subject: [PATCH 2/3] Removed bogus or superfluous function ('restrict level') attributes. --- include/psi/vm/flags/flags.win32.hpp | 2 +- include/psi/vm/handles/handle.posix.hpp | 4 ++-- include/psi/vm/mappable_objects/shared_memory/android/mem.hpp | 2 +- src/flags/flags.win32.cpp | 2 +- src/mappable_objects/shared_memory/mem.posix.cpp | 2 +- 5 files changed, 6 insertions(+), 6 deletions(-) diff --git a/include/psi/vm/flags/flags.win32.hpp b/include/psi/vm/flags/flags.win32.hpp index b476e64..98d8b0b 100644 --- a/include/psi/vm/flags/flags.win32.hpp +++ b/include/psi/vm/flags/flags.win32.hpp @@ -83,7 +83,7 @@ namespace detail std::uint8_t release() const noexcept { return --const_cast( member ); } }; // struct dynamic_sd - BOOST_ATTRIBUTES( BOOST_EXCEPTIONLESS, BOOST_RESTRICTED_FUNCTION_L3, BOOST_RESTRICTED_FUNCTION_RETURN ) + BOOST_ATTRIBUTES( BOOST_EXCEPTIONLESS, BOOST_RESTRICTED_FUNCTION_RETURN ) dynamic_sd const * BOOST_CC_REG make_sd( scope_privileges ); } // namespace detail diff --git a/include/psi/vm/handles/handle.posix.hpp b/include/psi/vm/handles/handle.posix.hpp index 0a1ea00..b5a870f 100644 --- a/include/psi/vm/handles/handle.posix.hpp +++ b/include/psi/vm/handles/handle.posix.hpp @@ -45,7 +45,7 @@ struct handle_traits static native_t constexpr invalid_value = -1; - static BOOST_ATTRIBUTES( BOOST_MINSIZE, BOOST_RESTRICTED_FUNCTION_L1, BOOST_EXCEPTIONLESS ) + static BOOST_ATTRIBUTES( BOOST_MINSIZE, BOOST_EXCEPTIONLESS ) void close( native_t const native_handle ) noexcept { BOOST_VERIFY @@ -58,7 +58,7 @@ struct handle_traits ); } - static BOOST_ATTRIBUTES( BOOST_MINSIZE, BOOST_RESTRICTED_FUNCTION_L1, BOOST_EXCEPTIONLESS ) + static BOOST_ATTRIBUTES( BOOST_MINSIZE, BOOST_EXCEPTIONLESS ) native_t copy( native_t native_handle ); // TODO }; // handle_traits diff --git a/include/psi/vm/mappable_objects/shared_memory/android/mem.hpp b/include/psi/vm/mappable_objects/shared_memory/android/mem.hpp index b81e04c..489fc24 100644 --- a/include/psi/vm/mappable_objects/shared_memory/android/mem.hpp +++ b/include/psi/vm/mappable_objects/shared_memory/android/mem.hpp @@ -69,7 +69,7 @@ namespace detail std::memcpy( prefixed_name + shm_prefix.size(), name, name_length + 1 ); } - BOOST_ATTRIBUTES( BOOST_MINSIZE, BOOST_RESTRICTED_FUNCTION_L1, BOOST_EXCEPTIONLESS, BOOST_WARN_UNUSED_RESULT ) + BOOST_ATTRIBUTES( BOOST_MINSIZE, BOOST_EXCEPTIONLESS, BOOST_WARN_UNUSED_RESULT ) file_handle::reference BOOST_CC_REG shm_open ( char const * const name, diff --git a/src/flags/flags.win32.cpp b/src/flags/flags.win32.cpp index cfcfd9b..13c0116 100644 --- a/src/flags/flags.win32.cpp +++ b/src/flags/flags.win32.cpp @@ -116,7 +116,7 @@ namespace detail return make_sd( &ea, 1 ); } - BOOST_ATTRIBUTES( BOOST_EXCEPTIONLESS, BOOST_RESTRICTED_FUNCTION_L3, BOOST_RESTRICTED_FUNCTION_RETURN ) + BOOST_ATTRIBUTES( BOOST_EXCEPTIONLESS, BOOST_RESTRICTED_FUNCTION_RETURN ) dynamic_sd const * BOOST_CC_REG make_sd( scope_privileges const permissions ) { EXPLICIT_ACCESSW entries[ permissions.size() ]; diff --git a/src/mappable_objects/shared_memory/mem.posix.cpp b/src/mappable_objects/shared_memory/mem.posix.cpp index bd0f819..94e0aeb 100644 --- a/src/mappable_objects/shared_memory/mem.posix.cpp +++ b/src/mappable_objects/shared_memory/mem.posix.cpp @@ -227,7 +227,7 @@ namespace detail return result; } - BOOST_ATTRIBUTES( BOOST_MINSIZE, BOOST_RESTRICTED_FUNCTION_L2, BOOST_EXCEPTIONLESS, BOOST_WARN_UNUSED_RESULT ) + BOOST_ATTRIBUTES( BOOST_MINSIZE, BOOST_EXCEPTIONLESS, BOOST_WARN_UNUSED_RESULT ) bool named_semaphore::semop( short const opcode, bool const nowait /*= false*/ ) noexcept { // http://linux.die.net/man/2/semop From 81eaab477142e98cebac90daabbc1383ef911c71 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Domagoj=20=C5=A0ari=C4=87?= Date: Thu, 8 Aug 2024 19:56:57 +0200 Subject: [PATCH 3/3] Windows: fixed build errors and initialization-order-fiasco crashes. --- src/detail/nt.cpp | 4 +++- vm.cmake | 1 + 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/src/detail/nt.cpp b/src/detail/nt.cpp index 4a39346..bc691e9 100644 --- a/src/detail/nt.cpp +++ b/src/detail/nt.cpp @@ -31,11 +31,13 @@ namespace psi::vm::nt namespace detail { -#ifdef __clang__ // initialization order fiasco wrkrnd + // initialization order fiasco wrkrnds +#if defined( __clang__ ) || defined( __GNUC__ ) // clang-cl does not define __GNUC__ HMODULE ntdll; [[ gnu::constructor( 101 ) ]] void init_ntdll_handle() noexcept { ntdll = ::GetModuleHandleW( L"ntdll.dll" ); } #else + #pragma init_seg( lib ) HMODULE const ntdll{ ::GetModuleHandleW( L"ntdll.dll" ) }; #endif diff --git a/vm.cmake b/vm.cmake index 52fdb25..43a8593 100644 --- a/vm.cmake +++ b/vm.cmake @@ -27,6 +27,7 @@ if ( WIN32 ) set( excluded_impl posix ) else() set( excluded_impl win32 ) + set_source_files_properties( ${CMAKE_CURRENT_LIST_DIR}/src/detail/nt.cpp PROPERTIES HEADER_FILE_ONLY true ) endif() foreach( source ${vm_sources} ) if ( ${source} MATCHES ${excluded_impl} )