Skip to content

Commit 1df1ea5

Browse files
committed
build(meson): add hardening flags for libgrovedb
1 parent 5086179 commit 1df1ea5

File tree

3 files changed

+92
-1
lines changed

3 files changed

+92
-1
lines changed

src/ffi/grovedb/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ meson compile -C builddir
5050

5151
- `-Dbuild_tests=[true|false]`: Build unit tests (default: `true`)
5252
- `-Dgrovedb_cxx_build_dir=<path>`: Path to `grovedb_cxx` build directory (auto-detected if empty, ignored if `use_rustdeps=true`)
53+
- `-Dharden_build=[true|false]`: Set hardening compiler, linker and preprocessor flags (default: `true`)
5354
- `-Drustdeps_build_dir=<path>`: Path to `librustdeps` build directory (auto-detected if empty)
5455
- `-Dshared_library=[true|false]`: Build shared library with pkg-config definitions (default: `true`, implicit with `build_tests`)
5556
- `-Dsuppress_external_warnings=[true|false]`: Suppress compiler warnings on external sources (default: `true`)

src/ffi/grovedb/meson.build

Lines changed: 85 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@ cxx_core_flags_list = [
3333
'-fstack-reuse=none', # https://gcc.gnu.org/bugzilla/show_bug.cgi?id=90348
3434
]
3535

36+
cxx_harden_flags_list = [
37+
'-fcf-protection=full',
38+
'-fstack-protector-all',
39+
'-Wstack-protector',
40+
]
41+
3642
cxx_warn_flags_list = [
3743
'-Wconditional-uninitialized',
3844
'-Wdate-time',
@@ -55,8 +61,33 @@ cxx_warn_flags_list = [
5561
'-Wvla',
5662
]
5763

64+
# C++ linker flags
65+
cxx_harden_lflags_darwin_list = [
66+
'-fixup_chains',
67+
]
68+
69+
cxx_harden_lflags_linux_list = [
70+
'-z,now',
71+
'-z,relro',
72+
'-z,separate-code',
73+
]
74+
75+
cxx_harden_lflags_win64_list = [
76+
'--dynamicbase',
77+
'--enable-reloc-section',
78+
'--high-entropy-va',
79+
'--nxcompat',
80+
]
81+
82+
# C++ preprocessor flags
83+
cxx_harden_release_preproc = [
84+
'-D_FORTIFY_SOURCE=3',
85+
'-U_FORTIFY_SOURCE',
86+
]
87+
5888
# Check compiler support for flags and apply them
5989
cxx_flags = []
90+
6091
foreach flag : cxx_core_flags_list + cxx_warn_flags_list
6192
if cxx.has_multi_arguments(['-Werror', flag])
6293
cxx_flags += flag
@@ -68,7 +99,59 @@ if cxx.has_multi_arguments(['-Werror', '-Wformat', '-Wformat-security'])
6899
cxx_flags += ['-Wformat', '-Wformat-security']
69100
endif
70101

71-
add_project_arguments(cxx_flags, language: 'cpp')
102+
harden_build = get_option('harden_build')
103+
if harden_build
104+
foreach flag : cxx_harden_flags_list
105+
if cxx.has_multi_arguments(['-Werror', flag])
106+
cxx_flags += flag
107+
endif
108+
endforeach
109+
110+
if host_machine.cpu_family() == 'aarch64'
111+
if cxx.has_multi_arguments('-Werror', '-mbranch-protection=bti')
112+
cxx_flags += '-mbranch-protection=bti'
113+
endif
114+
endif
115+
116+
# -fstack-clash-protection is a no-op on windows, see https://gcc.gnu.org/bugzilla/show_bug.cgi?id=90458
117+
if host_machine.system() != 'windows'
118+
if cxx.has_multi_arguments('-Werror', '-fstack-clash-protection')
119+
cxx_flags += '-fstack-clash-protection'
120+
endif
121+
endif
122+
endif
123+
124+
cxx_lflags = []
125+
126+
if harden_build
127+
cxx_harden_lflags = []
128+
if host_machine.system() == 'darwin'
129+
cxx_harden_lflags = cxx_harden_lflags_darwin_list
130+
elif host_machine.system() == 'windows'
131+
cxx_harden_lflags = cxx_harden_lflags_win64_list
132+
else
133+
cxx_harden_lflags = cxx_harden_lflags_linux_list
134+
endif
135+
foreach flag : cxx_harden_lflags
136+
flag = '-Wl,' + flag
137+
if cxx.has_multi_link_arguments(['-Werror', flag])
138+
cxx_lflags += flag
139+
endif
140+
endforeach
141+
endif
142+
143+
cxx_preproc = []
144+
145+
if harden_build
146+
if get_option('buildtype') != 'debug'
147+
foreach flag : cxx_harden_release_preproc
148+
cxx_preproc += flag
149+
endforeach
150+
endif
151+
endif
152+
153+
add_project_arguments(cxx_flags + cxx_preproc, language: 'cpp')
154+
add_project_link_arguments(cxx_lflags, language: 'cpp')
72155

73156
rustdeps_build_dir = get_option('rustdeps_build_dir')
74157
grovedb_cxx_build_dir = get_option('grovedb_cxx_build_dir')
@@ -259,6 +342,7 @@ install_headers(libgrovedb_headers, subdir: 'grovedb')
259342
summary({
260343
'C++ compiler': cxx.get_id(),
261344
'C++ flags': ' '.join(cxx_flags),
345+
'C++ linker flags': ' '.join(cxx_lflags),
262346
'C++ standard': get_option('cpp_std'),
263347
}, section: 'Compiler')
264348

src/ffi/grovedb/meson_options.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ option('grovedb_cxx_build_dir',
1010
description: 'Path to grovedb_cxx build directory (auto-detected if empty, ignored if use_rustdeps=true)'
1111
)
1212

13+
option('harden_build',
14+
type: 'boolean',
15+
value: true,
16+
description: 'Set hardening compiler, linker and preprocessor flags'
17+
)
18+
1319
option('rustdeps_build_dir',
1420
type: 'string',
1521
value: '',

0 commit comments

Comments
 (0)