Skip to content

Commit 899f11d

Browse files
KFilipekbratpiorka
authored andcommitted
load DLLs only from safe load list
1 parent 2a0dc99 commit 899f11d

File tree

7 files changed

+165
-14
lines changed

7 files changed

+165
-14
lines changed

.github/scripts/check_dll_flags.ps1

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Copyright (C) 2023-2024 Intel Corporation
2+
# Under the Apache License v2.0 with LLVM Exceptions. See LICENSE.TXT.
3+
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4+
5+
# Invoke-CmdScript runs a command script and updates the current environment
6+
# with any flag changes set by the script.
7+
function Invoke-CmdScript {
8+
param(
9+
[String] $scriptName
10+
)
11+
$cmdLine = """$scriptName"" $args & set"
12+
& $Env:SystemRoot\system32\cmd.exe /c $cmdLine |
13+
select-string '^([^=]*)=(.*)$' | foreach-object {
14+
$varName = $_.Matches[0].Groups[1].Value
15+
$varValue = $_.Matches[0].Groups[2].Value
16+
set-item Env:$varName $varValue
17+
}
18+
}
19+
20+
# Get the path to vcvarsall.bat
21+
$vsPath = & "${env:ProgramFiles(x86)}\Microsoft Visual Studio\Installer\vswhere.exe" `
22+
-latest -products * -requires Microsoft.VisualStudio.Component.VC.Tools.x86.x64 `
23+
-property installationPath
24+
$vsPath = "$vsPath\VC\Auxiliary\Build"
25+
$vcvarsall = "${vsPath}\vcvarsall.bat"
26+
echo "Visual Studio path: $vsPath"
27+
echo "vcvarsall.bat path: $vcvarsall"
28+
29+
# Call vcvarsall.bat so we can run MSVC commands
30+
echo "Setting up MSVC environment..."
31+
Invoke-CmdScript "$vcvarsall" x86
32+
33+
# Get umf.dll configuration flags and check if DEPENDENTLOADFLAG is set to 0x2000
34+
$flags = & "${env:VCToolsInstallDir}\bin\Hostx64\x64\dumpbin.exe" /LOADCONFIG "$args/build/bin/Release/umf.dll"
35+
if (($flags | Where-Object { $_ -match '(\d+).*Dependent' } | ForEach-Object { $matches[1] } ) -eq "2000") {
36+
echo "The DEPENDENTLOADFLAG is correclty set to 0x2000."
37+
exit 0
38+
} else {
39+
echo "The DEPENDENTLOADFLAG is not correclty set"
40+
echo "$flags"
41+
exit 1
42+
}

.github/workflows/basic.yml

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,57 @@ jobs:
228228
--umf-version ${{env.UMF_VERSION}}
229229
${{ matrix.shared_library == 'ON' && '--shared-library' || ''}}
230230
231+
windows-build_hwloc:
232+
name: "Windows + static hwloc"
233+
env:
234+
VCPKG_PATH: "${{github.workspace}}/build/vcpkg/packages/tbb_x64-windows;${{github.workspace}}/build/vcpkg/packages/jemalloc_x64-windows"
235+
BUILD_DIR : "${{github.workspace}}/build/${{matrix.build_type}}"
236+
INSTL_DIR : "${{github.workspace}}/../install-dir"
237+
strategy:
238+
matrix:
239+
os: ['windows-2019', 'windows-2022']
240+
build_type: [Release]
241+
shared_library: ['ON']
242+
level_zero_provider: ['OFF']
243+
244+
runs-on: ${{matrix.os}}
245+
246+
steps:
247+
- name: Checkout
248+
uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1
249+
250+
- name: Initialize vcpkg
251+
uses: lukka/run-vcpkg@5e0cab206a5ea620130caf672fce3e4a6b5666a1 # v11.5
252+
with:
253+
vcpkgGitCommitId: 3dd44b931481d7a8e9ba412621fa810232b66289
254+
vcpkgDirectory: ${{github.workspace}}/build/vcpkg
255+
vcpkgJsonGlob: '**/vcpkg.json'
256+
257+
- name: Install dependencies
258+
run: vcpkg install
259+
shell: pwsh # Specifies PowerShell as the shell for running the script.
260+
261+
- name: Configure build
262+
run: >
263+
cmake
264+
-B ${{env.BUILD_DIR}}
265+
-DCMAKE_PREFIX_PATH="${{env.VCPKG_PATH}}"
266+
-DUMF_BUILD_SHARED_LIBRARY=${{matrix.shared_library}}
267+
-DUMF_FORMAT_CODE_STYLE=OFF
268+
-DUMF_DEVELOPER_MODE=ON
269+
-DUMF_BUILD_LIBUMF_POOL_DISJOINT=ON
270+
-DUMF_BUILD_LIBUMF_POOL_JEMALLOC=ON
271+
-DUMF_BUILD_LEVEL_ZERO_PROVIDER=${{matrix.level_zero_provider}}
272+
-DUMF_TESTS_FAIL_ON_SKIP=ON
273+
-DUMF_LINK_HWLOC_STATICALLY=ON
274+
275+
- name: Build UMF
276+
run: cmake --build ${{env.BUILD_DIR}} --config ${{matrix.build_type}} -j $Env:NUMBER_OF_PROCESSORS
277+
278+
- name: Run tests
279+
working-directory: ${{env.BUILD_DIR}}
280+
run: ctest -C ${{matrix.build_type}} --output-on-failure --test-dir test
281+
231282
macos-build:
232283
name: MacOS
233284
strategy:

.github/workflows/pr_push.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ jobs:
8282
run: >
8383
cmake
8484
-B ${{github.workspace}}/build
85+
-DCMAKE_VERBOSE_MAKEFILE=ON
8586
-DCMAKE_PREFIX_PATH="${{env.VCPKG_PATH}}"
8687
-DUMF_FORMAT_CODE_STYLE=OFF
8788
-DUMF_DEVELOPER_MODE=ON
@@ -91,6 +92,7 @@ jobs:
9192
-DUMF_BUILD_EXAMPLES=ON
9293
-DUMF_BUILD_LEVEL_ZERO_PROVIDER=ON
9394
-DUMF_TESTS_FAIL_ON_SKIP=ON
95+
-DUMF_BUILD_SHARED_LIBRARY=ON
9496
${{matrix.extra_build_options}}
9597
9698
- name: Configure CMake (simple)
@@ -114,6 +116,11 @@ jobs:
114116
working-directory: ${{github.workspace}}/build
115117
run: ctest --output-on-failure --test-dir test -C Release
116118

119+
- name: check /DEPENDENTLOADFLAG (Windows only)
120+
if: matrix.os == 'windows-latest'
121+
run: ${{github.workspace}}/.github/scripts/check_dll_flags.ps1 ${{github.workspace}}
122+
shell: pwsh
123+
117124
CodeStyle:
118125
name: Coding style
119126
runs-on: ubuntu-latest

CMakeLists.txt

Lines changed: 50 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ set(UMF_CMAKE_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
1414
list(APPEND CMAKE_MODULE_PATH "${UMF_CMAKE_SOURCE_DIR}/cmake")
1515
include(${UMF_CMAKE_SOURCE_DIR}/cmake/helpers.cmake)
1616

17+
include(CTest)
18+
include(CMakePackageConfigHelpers)
19+
include(GNUInstallDirs)
20+
find_package(PkgConfig)
21+
1722
# CMAKE_PROJECT_VERSION[_MAJOR|_MINOR|_PATCH] variables are set via 'project'
1823
# command. They cannot contain any "pre-release" part, though. We use custom
1924
# "UMF_SRC_VERSION" to store more accurate (source) version - this var should be
@@ -37,6 +42,8 @@ option(UMF_BUILD_BENCHMARKS "Build UMF benchmarks" OFF)
3742
option(UMF_BUILD_BENCHMARKS_MT "Build UMF multithreaded benchmarks" OFF)
3843
option(UMF_BUILD_EXAMPLES "Build UMF examples" ON)
3944
option(UMF_BUILD_GPU_EXAMPLES "Build UMF GPU examples" OFF)
45+
option(UMF_LINK_HWLOC_STATICALLY
46+
"Link UMF with HWLOC library statically (Windows+Release only)" OFF)
4047
option(UMF_DEVELOPER_MODE "Enable developer checks, treats warnings as errors"
4148
OFF)
4249
option(UMF_FORMAT_CODE_STYLE
@@ -83,6 +90,47 @@ else()
8390
message(FATAL_ERROR "Unknown OS type")
8491
endif()
8592

93+
if(NOT UMF_LINK_HWLOC_STATICALLY)
94+
pkg_check_modules(LIBHWLOC hwloc>=2.3.0)
95+
if(NOT LIBHWLOC_FOUND)
96+
find_package(LIBHWLOC 2.3.0 REQUIRED hwloc)
97+
endif()
98+
# add PATH to DLL on Windows
99+
set(DLL_PATH_LIST
100+
"${DLL_PATH_LIST};PATH=path_list_append:${LIBHWLOC_LIBRARY_DIRS}/../bin"
101+
)
102+
else()
103+
if(NOT WIN32)
104+
message(FATAL_ERROR "hwloc can be static linked only on Windows")
105+
endif()
106+
include(FetchContent)
107+
FetchContent_Declare(
108+
hwloc_targ
109+
GIT_REPOSITORY "https://github.com/open-mpi/hwloc.git"
110+
GIT_TAG hwloc-2.10.0
111+
FIND_PACKAGE_ARGS)
112+
113+
FetchContent_GetProperties(hwloc_targ)
114+
if(NOT hwloc_targ_POPULATED)
115+
FetchContent_MakeAvailable(hwloc_targ)
116+
endif()
117+
118+
set(HWLOC_ENABLE_TESTING OFF)
119+
set(HWLOC_SKIP_LSTOPO ON)
120+
set(HWLOC_SKIP_TOOLS ON)
121+
set(hwloc_cmake ${hwloc_targ_SOURCE_DIR}/contrib/windows-cmake)
122+
123+
add_subdirectory(${hwloc_cmake} ${hwloc_targ_SOURCE_DIR})
124+
125+
set(LIBHWLOC_INCLUDE_DIRS ${hwloc_targ_SOURCE_DIR}/include)
126+
set(LIBHWLOC_LIBRARY_DIRS ${hwloc_targ_SOURCE_DIR}/Release)
127+
set(LIBHWLOC_LIBRARIES ${LIBHWLOC_LIBRARY_DIRS}/hwloc.lib)
128+
129+
message(STATUS " LIBHWLOC_LIBRARIES = ${LIBHWLOC_LIBRARIES}")
130+
message(STATUS " LIBHWLOC_INCLUDE_DIRS = ${LIBHWLOC_INCLUDE_DIRS}")
131+
message(STATUS " LIBHWLOC_LIBRARY_DIRS = ${LIBHWLOC_LIBRARY_DIRS}")
132+
endif()
133+
86134
# This build type check is not possible on Windows when CMAKE_BUILD_TYPE is not
87135
# set, because in this case the build type is determined after a CMake
88136
# configuration is done (at the build time)
@@ -130,11 +178,6 @@ foreach(option_name ${OPTIONS_REQUIRING_CXX})
130178
endif()
131179
endforeach()
132180

133-
include(CTest)
134-
include(CMakePackageConfigHelpers)
135-
include(GNUInstallDirs)
136-
find_package(PkgConfig)
137-
138181
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
139182
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
140183
set(CMAKE_UMF_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
@@ -179,7 +222,8 @@ if(WINDOWS)
179222
add_compile_definitions(_CRT_SECURE_NO_WARNINGS)
180223
# set PATH to DLLs on Windows
181224
set(DLL_PATH_LIST
182-
"PATH=path_list_append:${PROJECT_BINARY_DIR}/bin/$<CONFIG>")
225+
"${DLL_PATH_LIST};PATH=path_list_append:${PROJECT_BINARY_DIR}/bin/$<CONFIG>"
226+
)
183227
# add path to the proxy lib DLL
184228
set(DLL_PATH_LIST
185229
"${DLL_PATH_LIST};PATH=path_list_append:${PROJECT_BINARY_DIR}/src/proxy_lib"
@@ -190,14 +234,6 @@ if(WINDOWS)
190234
)
191235
endif()
192236

193-
pkg_check_modules(LIBHWLOC hwloc>=2.3.0)
194-
if(NOT LIBHWLOC_FOUND)
195-
find_package(LIBHWLOC 2.3.0 REQUIRED hwloc)
196-
endif()
197-
# add PATH to DLL on Windows
198-
set(DLL_PATH_LIST
199-
"${DLL_PATH_LIST};PATH=path_list_append:${LIBHWLOC_LIBRARY_DIRS}/../bin")
200-
201237
pkg_check_modules(TBB tbb)
202238
if(NOT TBB_FOUND)
203239
find_package(TBB OPTIONAL_COMPONENTS tbb)

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ List of options provided by CMake:
126126
| USE_MSAN | Enable MemorySanitizer checks | ON/OFF | OFF |
127127
| USE_VALGRIND | Enable Valgrind instrumentation | ON/OFF | OFF |
128128
| USE_GCOV | Enable gcov support (Linux only) | ON/OFF | OFF |
129+
| UMF_LINK_HWLOC_STATICALLY | Link UMF with HWLOC library statically (Windows+Release only) | ON/OFF | OFF |
129130

130131
## Architecture: memory pools and providers
131132

cmake/helpers.cmake

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,12 +136,22 @@ function(add_umf_target_link_options name)
136136
endif()
137137
endif()
138138
elseif(MSVC)
139+
message(STATUS "CXX_COMPILER_ID: ${CXX_COMPILER_ID}")
140+
message(STATUS "CLANG_CXX_COMPILER_ID: ${CLANG_CXX_COMPILER_ID}")
141+
message(STATUS "CLANG_CXX_LINKER_ID: ${CLANG_CXX_LINKER_ID}")
142+
message(
143+
STATUS
144+
"CLANG_CXX_COMPILER_LINKER_ID: ${CLANG_CXX_COMPILER_LINKER_ID}")
139145
target_link_options(
140146
${name}
141147
PRIVATE
148+
# allow loading a DLL only from safe load list
149+
$<$<CXX_COMPILER_ID:MSVC>:/DEPENDENTLOADFLAG:0x2000>
142150
/DYNAMICBASE
143151
/HIGHENTROPYVA
144152
/NXCOMPAT)
153+
get_target_property(my_link_options ${name} LINK_OPTIONS)
154+
message(STATUS "Link options for my_executable: ${my_link_options}")
145155
endif()
146156
endfunction()
147157

src/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,10 @@ else()
131131
LIBS ${UMF_LIBS})
132132
endif()
133133

134+
if(UMF_LINK_HWLOC_STATICALLY)
135+
add_dependencies(umf hwloc)
136+
endif()
137+
134138
target_link_directories(umf PRIVATE ${UMF_PRIVATE_LIBRARY_DIRS})
135139

136140
target_compile_definitions(umf PRIVATE ${UMF_PRIVATE_COMPILE_DEFINITIONS})

0 commit comments

Comments
 (0)