Skip to content

Commit d05502a

Browse files
committed
build(meson): add hardening flags for libgrovedb
1 parent 9438d81 commit d05502a

File tree

3 files changed

+95
-2
lines changed

3 files changed

+95
-2
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
- `-Dsuppress_external_warnings=[true|false]`: Suppress compiler warnings on external sources (default: `true`)
5556
- `-Duse_rustdeps=[true|false]`: Link against `librustdeps` instead of `libgrovedb_cxx` (default: `false`)

src/ffi/grovedb/meson.build

Lines changed: 88 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,18 @@ use_rustdeps = get_option('use_rustdeps')
2424
grovedb_cxx_build_dir = get_option('grovedb_cxx_build_dir')
2525
rustdeps_build_dir = get_option('rustdeps_build_dir')
2626

27-
# C++ compiler
2827
cxx = meson.get_compiler('cpp')
28+
29+
# C++ compiler flags
2930
cxx_core_flags_list = [
3031
'-fno-extended-identifiers',
3132
'-fstack-reuse=none', # https://gcc.gnu.org/bugzilla/show_bug.cgi?id=90348
3233
]
34+
cxx_harden_flags_list = [
35+
'-fcf-protection=full',
36+
'-fstack-protector-all',
37+
'-Wstack-protector',
38+
]
3339
cxx_warn_flags_list = [
3440
'-Wconditional-uninitialized',
3541
'-Wdate-time',
@@ -52,8 +58,31 @@ cxx_warn_flags_list = [
5258
'-Wvla',
5359
]
5460

61+
# C++ linker flags
62+
cxx_harden_lflags_darwin_list = [
63+
'-fixup_chains',
64+
]
65+
cxx_harden_lflags_linux_list = [
66+
'-z,now',
67+
'-z,relro',
68+
'-z,separate-code',
69+
]
70+
cxx_harden_lflags_win64_list = [
71+
'--dynamicbase',
72+
'--enable-reloc-section',
73+
'--high-entropy-va',
74+
'--nxcompat',
75+
]
76+
77+
# C++ preprocessor flags
78+
cxx_harden_release_preproc = [
79+
'-D_FORTIFY_SOURCE=3',
80+
'-U_FORTIFY_SOURCE',
81+
]
82+
5583
# Check compiler support for flags and apply them
5684
cxx_flags = []
85+
5786
foreach flag : cxx_core_flags_list + cxx_warn_flags_list
5887
if cxx.has_multi_arguments(['-Werror', flag])
5988
cxx_flags += flag
@@ -65,7 +94,64 @@ if cxx.has_multi_arguments(['-Werror', '-Wformat', '-Wformat-security'])
6594
cxx_flags += ['-Wformat', '-Wformat-security']
6695
endif
6796

68-
add_project_arguments(cxx_flags, language: 'cpp')
97+
harden_build = get_option('harden_build')
98+
if harden_build
99+
foreach flag : cxx_harden_flags_list
100+
if cxx.has_multi_arguments(['-Werror', flag])
101+
cxx_flags += flag
102+
endif
103+
endforeach
104+
105+
if host_machine.cpu_family() == 'aarch64'
106+
if cxx.has_multi_arguments('-Werror', '-mbranch-protection=bti')
107+
cxx_flags += '-mbranch-protection=bti'
108+
endif
109+
endif
110+
111+
# -fstack-clash-protection is a no-op on windows, see https://gcc.gnu.org/bugzilla/show_bug.cgi?id=90458
112+
if host_machine.system() != 'windows'
113+
if cxx.has_multi_arguments('-Werror', '-fstack-clash-protection')
114+
cxx_flags += '-fstack-clash-protection'
115+
endif
116+
endif
117+
endif
118+
119+
cxx_lflags = []
120+
121+
if harden_build
122+
cxx_harden_lflags = []
123+
if host_machine.system() == 'darwin'
124+
cxx_harden_lflags = cxx_harden_lflags_darwin_list
125+
elif host_machine.system() == 'windows'
126+
cxx_harden_lflags = cxx_harden_lflags_win64_list
127+
else
128+
cxx_harden_lflags = cxx_harden_lflags_linux_list
129+
endif
130+
foreach flag : cxx_harden_lflags
131+
flag = '-Wl,' + flag
132+
if cxx.has_multi_link_arguments(['-Werror', flag])
133+
cxx_lflags += flag
134+
endif
135+
endforeach
136+
137+
if cxx.has_multi_arguments('-Werror', '-fPIE') and cxx.has_multi_link_arguments('-pie')
138+
cxx_flags += '-fPIE'
139+
cxx_lflags += '-pie'
140+
endif
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')
69155

70156
# Check for threading support
71157
thread_dep = dependency('threads', required: true)

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)