Fix libcontext arm64 build and purge winfiber

The winfiber implementation ran into memory leak issues as it didnt exit the stack jumps correctly
and lets not mislead anyone by keeping it
This commit is contained in:
Marek Roszko 2023-04-10 18:07:10 -04:00
parent 32eb1ef77e
commit 726b137fed
4 changed files with 27 additions and 125 deletions

View File

@ -116,11 +116,6 @@ cmake_dependent_option( KICAD_WIN32_DPI_AWARE
OFF "WIN32"
OFF )
cmake_dependent_option( KICAD_WIN32_CONTEXT_WINFIBER
"Use win32 fibers for libcontext"
OFF "WIN32"
OFF )
cmake_dependent_option( KICAD_WIN32_VERIFY_CODESIGN
"When enabled, verifies the code signing signature of certain DLLs loaded during runtime."
OFF "WIN32"
@ -269,18 +264,9 @@ message( STATUS "KiCad install dir: <${DEFAULT_INSTALL_PATH}>" )
include( PerformFeatureChecks )
perform_feature_checks()
# Setup the compiler warnings
include( ${KICAD_CMAKE_MODULE_PATH}/Warnings.cmake )
add_compile_definitions( $<$<BOOL:${KICAD_WIN32_CONTEXT_WINFIBER}>:LIBCONTEXT_USE_WINFIBER> )
if( KICAD_WIN32_CONTEXT_WINFIBER )
set(LIBCONTEXT_USE_WINFIBER true)
else()
set(LIBCONTEXT_USE_WINFIBER false)
endif()
if( WIN32 )
# define UNICODE and_UNICODE definition on Windows. KiCad uses unicode.
# Both definitions are required

View File

@ -14,36 +14,33 @@ list(APPEND LIBCONTEXT_SOURCES
)
if( MSVC )
if( NOT LIBCONTEXT_USE_WINFIBER )
include( MSVCAssemblyHelper )
# we need our assembly helper until cmake 2.26.1 becomes standard on MSVC
include( MSVCAssemblyHelper )
if ( KICAD_BUILD_ARCH_X86 )
list(APPEND LIBCONTEXT_ASM_SOURCES
${CMAKE_CURRENT_SOURCE_DIR}/make_i386_ms_pe_masm.asm
${CMAKE_CURRENT_SOURCE_DIR}/jump_i386_ms_pe_masm.asm
)
elseif( KICAD_BUILD_ARCH_X64 )
list(APPEND LIBCONTEXT_ASM_SOURCES
${CMAKE_CURRENT_SOURCE_DIR}/make_x86_64_ms_pe_masm.asm
${CMAKE_CURRENT_SOURCE_DIR}/jump_x86_64_ms_pe_masm.asm
)
elseif( KICAD_BUILD_ARCH_ARM64 )
list(APPEND LIBCONTEXT_ASM_SOURCES
${CMAKE_CURRENT_SOURCE_DIR}/make_arm64_aapcs_pe_armasm.asm
${CMAKE_CURRENT_SOURCE_DIR}/jump_arm64_aapcs_pe_armasm.asm
)
endif()
if ( KICAD_BUILD_ARCH_X86 )
list(APPEND LIBCONTEXT_ASM_SOURCES
${CMAKE_CURRENT_SOURCE_DIR}/make_i386_ms_pe_masm.asm
${CMAKE_CURRENT_SOURCE_DIR}/jump_i386_ms_pe_masm.asm
)
elseif( KICAD_BUILD_ARCH_X64 )
list(APPEND LIBCONTEXT_ASM_SOURCES
${CMAKE_CURRENT_SOURCE_DIR}/make_x86_64_ms_pe_masm.asm
${CMAKE_CURRENT_SOURCE_DIR}/jump_x86_64_ms_pe_masm.asm
)
elseif( KICAD_BUILD_ARCH_ARM64 )
list(APPEND LIBCONTEXT_ASM_SOURCES
${CMAKE_CURRENT_SOURCE_DIR}/make_arm64_aapcs_pe_armasm.asm
${CMAKE_CURRENT_SOURCE_DIR}/jump_arm64_aapcs_pe_armasm.asm
)
endif()
if( KICAD_BUILD_ARCH_ARM64 )
# ARM64 needs to use the compile_asm workaround
compile_asm( TARGET libcontext ASM_FILES ${LIBCONTEXT_ASM_SOURCES} OUTPUT_OBJECTS ASM_SOURCES_OBJECTS )
if( KICAD_BUILD_ARCH_ARM64 )
# ARM64 needs to use the compile_asm workaround
compile_asm( TARGET libcontext ASM_FILES ${LIBCONTEXT_ASM_SOURCES} OUTPUT_OBJECTS ASM_SOURCES_OBJECTS )
list(APPEND LIBCONTEXT_SOURCES ${ASM_SOURCES_OBJECTS})
else()
list(APPEND LIBCONTEXT_SOURCES ${LIBCONTEXT_ASM_SOURCES})
endif()
list(APPEND LIBCONTEXT_SOURCES ${ASM_SOURCES_OBJECTS})
else()
add_compile_definitions(LIBCONTEXT_USE_WINFIBER)
list(APPEND LIBCONTEXT_SOURCES ${LIBCONTEXT_ASM_SOURCES})
endif()
endif()

View File

@ -1552,83 +1552,8 @@ __asm (
);
#endif
#if defined(LIBCONTEXT_USE_WINFIBER) && (defined(LIBCONTEXT_PLATFORM_msvc_x86_64) || defined(LIBCONTEXT_PLATFORM_msvc_i386))
#include <map>
#ifdef __cplusplus
extern "C" {
#endif
#include <windows.h>
namespace libcontext
{
static int threadHasFibers = 0;
struct FiberData
{
intptr_t inValue;
intptr_t outValue;
void(*entry)(intptr_t);
};
static std::map<fcontext_t, FiberData> fiberParams;
static void fiberEntry(LPVOID params)
{
auto ctx = (fcontext_t) GetCurrentFiber();
auto& d = fiberParams[ctx];
d.entry(d.inValue);
}
fcontext_t LIBCONTEXT_CALL_CONVENTION make_fcontext(void* sp, size_t size, void(*fn)(intptr_t))
{
if (!threadHasFibers)
{
ConvertThreadToFiberEx( nullptr, FIBER_FLAG_FLOAT_SWITCH );
threadHasFibers = 1;
}
fcontext_t ctx = CreateFiberEx( size - 1, size, FIBER_FLAG_FLOAT_SWITCH, (LPFIBER_START_ROUTINE) fiberEntry, nullptr );
fiberParams[ctx].entry = fn;
return ctx;
}
intptr_t LIBCONTEXT_CALL_CONVENTION jump_fcontext( fcontext_t* ofc, fcontext_t nfc,
intptr_t vp, bool preserve_fpu )
{
*ofc = GetCurrentFiber();
fiberParams[nfc].inValue = vp;
fiberParams[nfc].outValue = vp;
SwitchToFiber(nfc);
return fiberParams[*ofc].outValue;
}
void LIBCONTEXT_CALL_CONVENTION release_fcontext( fcontext_t ctx )
{
if( ctx == nullptr )
return;
if( fiberParams.find( ctx ) != fiberParams.end() )
{
fiberParams.erase( ctx );
}
DeleteFiber( ctx );
}
}; // namespace libcontext
#ifdef __cplusplus
};
#endif
#else // defined(LIBCONTEXT_USE_WINFIBER) && (defined(LIBCONTEXT_PLATFORM_msvc_x86_64) || defined(LIBCONTEXT_PLATFORM_msvc_i386))
#if( defined( LIBCONTEXT_PLATFORM_msvc_x86_64 ) || defined( LIBCONTEXT_PLATFORM_msvc_i386 ) \
|| defined( LIBCONTEXT_PLATFORM_msvc_arm64 ) )
#ifdef __cplusplus
extern "C" {

View File

@ -88,16 +88,14 @@
#endif
#elif defined (_MSC_VER )
#if defined( LIBCONTEXT_USE_WINFIBER )
#define LIBCONTEXT_HAS_OWN_STACK
#endif
#define LIBCONTEXT_CALL_CONVENTION __cdecl
#if defined(_WIN64)
#define LIBCONTEXT_PLATFORM_msvc_x86_64
#elif defined(_WIN32)
#define LIBCONTEXT_PLATFORM_msvc_i386
#elif defined( _M_ARM64 )
#define LIBCONTEXT_PLATFORM_msvc_arm64
#endif
#endif
@ -105,10 +103,6 @@
namespace libcontext {
#endif
#if defined(_WIN32_WCE)
typedef int intptr_t;
#endif
typedef void* fcontext_t;
#ifdef __cplusplus