Skip to content

Commit

Permalink
Merge pull request #12 from psiha/fix/clang_cl_bad_codegen
Browse files Browse the repository at this point in the history
Fix/clang cl bad codegen
  • Loading branch information
psiha authored Aug 8, 2024
2 parents 6b8f244 + 81eaab4 commit b49c329
Show file tree
Hide file tree
Showing 12 changed files with 73 additions and 33 deletions.
26 changes: 4 additions & 22 deletions include/psi/vm/detail/nt.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <boost/assert.hpp>
#include <boost/config_ex.hpp>

#ifndef _WIN32_WINNT
# define _WIN32_WINNT 0x0A00 // _WIN32_WINNT_WIN10
#endif // _WIN32_WINNT
#include <windows.h>
#include <winternl.h>

#pragma comment( lib, "ntdll.lib" )
#include <cstdint>
//------------------------------------------------------------------------------
namespace psi::vm::nt
{
Expand All @@ -50,18 +42,8 @@ inline auto const current_process{ reinterpret_cast<HANDLE>( 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 <typename ProcPtr>
ProcPtr get_nt_proc( char const * const proc_name )
Expand Down
2 changes: 1 addition & 1 deletion include/psi/vm/flags/flags.win32.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ namespace detail
std::uint8_t release() const noexcept { return --const_cast<std::uint8_t &>( 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

Expand Down
4 changes: 2 additions & 2 deletions include/psi/vm/handles/handle.posix.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down
4 changes: 2 additions & 2 deletions include/psi/vm/handles/handle.win32.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
57 changes: 57 additions & 0 deletions src/detail/nt.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
////////////////////////////////////////////////////////////////////////////////
///
/// 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 <psi/vm/detail/nt.hpp>

#include <boost/assert.hpp>

#pragma comment( lib, "ntdll.lib" )
//------------------------------------------------------------------------------
namespace psi::vm::nt
{
//------------------------------------------------------------------------------

namespace detail
{
// 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

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
//------------------------------------------------------------------------------
2 changes: 1 addition & 1 deletion src/flags/flags.win32.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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() ];
Expand Down
2 changes: 1 addition & 1 deletion src/mappable_objects/shared_memory/mem.posix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/mapped_view/mapped_view.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
//------------------------------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion src/mapped_view/mapped_view.posix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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__
Expand Down
2 changes: 1 addition & 1 deletion src/mapped_view/mapped_view.win32.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions vm.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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} )
Expand Down

0 comments on commit b49c329

Please sign in to comment.