-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCMakeLists.txt
More file actions
287 lines (247 loc) · 9.58 KB
/
CMakeLists.txt
File metadata and controls
287 lines (247 loc) · 9.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
cmake_minimum_required(VERSION 3.25)
project(compiler-hardening-tests
VERSION 1.0.0
DESCRIPTION "Testing and verification of compiler hardening options"
LANGUAGES CXX
)
# Set C++20 as the required standard
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
# Export compile commands for clang-tidy
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
# Enable testing
enable_testing()
# Set up cross-compilation emulator for Emscripten
# This allows CTest to execute .js files generated by Emscripten using Node.js
if(EMSCRIPTEN)
set(CMAKE_CROSSCOMPILING_EMULATOR "node" CACHE FILEPATH "Emulator to run Emscripten-generated JavaScript")
message(STATUS "Set CMAKE_CROSSCOMPILING_EMULATOR to 'node' for Emscripten tests")
endif()
# ============================================================================
# Compiler-Specific Hardening Options Function
# ============================================================================
# Function to apply compiler-specific hardening flags to a target
function(apply_hardening_flags target)
message(STATUS "Applying hardening flags to ${target}")
# ----------------------------------------------------------------------------
# Apple Clang Hardening Options
# ----------------------------------------------------------------------------
if(CMAKE_CXX_COMPILER_ID MATCHES "AppleClang")
message(STATUS " Compiler: Apple Clang")
target_compile_options(${target} PRIVATE
-Wall
-Warray-bounds-pointer-arithmetic
-Wconversion
-Werror=format-security
-Wextra
-Wformat
-Wformat=2
-Wimplicit-fallthrough
-Wshadow
-Wsign-conversion
-fPIE
-fstack-protector-strong
-pedantic-errors
)
# Debug-specific flags
target_compile_options(${target} PRIVATE
$<$<CONFIG:Debug>:
-D_LIBCPP_HARDENING_MODE=_LIBCPP_HARDENING_MODE_DEBUG
# -D_LIBCPP_ASSERTION_SEMANTIC=_LIBCPP_ASSERTION_SEMANTIC_OBSERVE
>
)
# Release-specific flags
target_compile_options(${target} PRIVATE
$<$<CONFIG:Release>:
-D_LIBCPP_HARDENING_MODE=_LIBCPP_HARDENING_MODE_FAST
# -D_LIBCPP_ASSERTION_SEMANTIC=_LIBCPP_ASSERTION_SEMANTIC_OBSERVE
-fno-delete-null-pointer-checks
-fno-strict-aliasing
-fno-strict-overflow
-ftrivial-auto-var-init=zero
>
)
# Architecture-specific options
if(CMAKE_SYSTEM_PROCESSOR MATCHES "x86_64")
target_compile_options(${target} PRIVATE
# x86-64 specific hardening for Apple Clang
)
elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "arm64")
target_compile_options(${target} PRIVATE
# ARM64 specific hardening for Apple Clang
)
endif()
# ----------------------------------------------------------------------------
# Clang Hardening Options
# ----------------------------------------------------------------------------
elseif(CMAKE_CXX_COMPILER_ID MATCHES "Clang" AND NOT CMAKE_CXX_COMPILER_ID MATCHES "AppleClang")
message(STATUS " Compiler: Clang")
target_compile_options(${target} PRIVATE
-Wall
-Wextra
-Wpedantic
)
# Debug-specific flags
target_compile_options(${target} PRIVATE
$<$<CONFIG:Debug>:
# Debug-specific options for Clang
>
)
# Release-specific flags
target_compile_options(${target} PRIVATE
$<$<CONFIG:Release>:
# Release-specific options for Clang
>
)
# Architecture-specific options
if(CMAKE_SYSTEM_PROCESSOR MATCHES "x86_64|AMD64")
target_compile_options(${target} PRIVATE
# x86-64 specific hardening for Clang
)
elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "aarch64|ARM64")
target_compile_options(${target} PRIVATE
# ARM64 specific hardening for Clang
)
endif()
# ----------------------------------------------------------------------------
# GCC Hardening Options
# ----------------------------------------------------------------------------
elseif(CMAKE_CXX_COMPILER_ID MATCHES "GNU")
message(STATUS " Compiler: GCC")
target_compile_options(${target} PRIVATE
-Wall
-Wextra
-Wpedantic
)
# Debug-specific flags
target_compile_options(${target} PRIVATE
$<$<CONFIG:Debug>:
# Debug-specific options for GCC
>
)
# Release-specific flags
target_compile_options(${target} PRIVATE
$<$<CONFIG:Release>:
# Release-specific options for GCC
>
)
# Architecture-specific options
if(CMAKE_SYSTEM_PROCESSOR MATCHES "x86_64|AMD64")
target_compile_options(${target} PRIVATE
# x86-64 specific hardening for GCC
)
elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "aarch64|ARM64")
target_compile_options(${target} PRIVATE
# ARM64 specific hardening for GCC
)
endif()
# ----------------------------------------------------------------------------
# MSVC Hardening Options
# ----------------------------------------------------------------------------
elseif(CMAKE_CXX_COMPILER_ID MATCHES "MSVC")
message(STATUS " Compiler: MSVC")
target_compile_options(${target} PRIVATE
/W4
)
# Debug-specific flags
target_compile_options(${target} PRIVATE
$<$<CONFIG:Debug>:
# Debug-specific options for MSVC
>
)
# Release-specific flags
target_compile_options(${target} PRIVATE
$<$<CONFIG:Release>:
# Release-specific options for MSVC
>
)
# Architecture-specific options
if(CMAKE_SYSTEM_PROCESSOR MATCHES "AMD64|x64")
target_compile_options(${target} PRIVATE
# x86-64 specific hardening for MSVC
)
elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "ARM64")
target_compile_options(${target} PRIVATE
# ARM64 specific hardening for MSVC
)
endif()
# ----------------------------------------------------------------------------
# Emscripten Hardening Options
# ----------------------------------------------------------------------------
elseif(EMSCRIPTEN)
message(STATUS " Compiler: Emscripten")
target_compile_options(${target} PRIVATE
-Wall
-Wextra
-Wpedantic
)
# Debug-specific flags
target_compile_options(${target} PRIVATE
$<$<CONFIG:Debug>:
# Debug-specific options for Emscripten
>
)
# Release-specific flags
target_compile_options(${target} PRIVATE
$<$<CONFIG:Release>:
# Release-specific options for Emscripten
>
)
# Set output file extension to .js (which will also generate .wasm)
set_target_properties(${target} PROPERTIES SUFFIX ".js")
target_link_options(${target} PRIVATE
# Target Node.js environment specifically
-sENVIRONMENT=node
# Embed wasm as base64 in JS to avoid fetch() URL issues with Node.js
-sSINGLE_FILE=1
-sNODEJS_CATCH_EXIT=0
-sNODEJS_CATCH_REJECTION=0
)
endif()
endfunction()
# ============================================================================
# Test Executables
# ============================================================================
# Report test - logs compiler and platform information
add_executable(test-report test/report.cpp)
apply_hardening_flags(test-report)
add_test(
NAME report
COMMAND test-report
)
set_tests_properties(report PROPERTIES
LABELS "info;always-pass"
)
# Hardening assertions test - validates libc++ hardening aborts on violations
add_executable(test-hardening-assertions test/test_observe_assertions.cpp)
apply_hardening_flags(test-hardening-assertions)
# Test expectations differ by platform
if(APPLE)
# On macOS, wrap in shell command that detects signal termination
# Exit code > 128 indicates process was killed by signal (128 + signal_number)
# SIGABRT = 6 → exit code 134
# SIGILL = 4 → exit code 132
add_test(
NAME hardening_assertions
COMMAND bash -c "\"$<TARGET_FILE:test-hardening-assertions>\"; exit $(( $? > 128 ? 0 : 1 ))"
)
set_tests_properties(hardening_assertions PROPERTIES
LABELS "macOS;hardening;expects-signal"
)
else()
# On other platforms, run normally (may or may not have hardening)
add_test(
NAME hardening_assertions
COMMAND test-hardening-assertions
)
set_tests_properties(hardening_assertions PROPERTIES
LABELS "hardening"
)
endif()
# ============================================================================
# Installation
# ============================================================================
install(TARGETS test-report test-hardening-assertions
RUNTIME DESTINATION bin
)