Skip to content

Commit 7463784

Browse files
authored
Add TSan and UBSan to sanitize options (#2315)
* Add TSan and UBSan to sanitize options * Add TSan and UBSan flag tests * Add TSan and UBSan to docs * use "UndefinedBehavior" instead of "Undefined" for UBSan * add compiler support notes for sanitizers to docs * Register TSan and UBSan flags in init * Add TSan and UBSan to linker flags * Properly share GCC sanitizer tables with Clang
1 parent b5e6c10 commit 7463784

File tree

6 files changed

+71
-27
lines changed

6 files changed

+71
-27
lines changed

src/_premake_init.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -805,6 +805,8 @@
805805
allowed = {
806806
"Address",
807807
"Fuzzer", -- Visual Studio 2022+ only
808+
"Thread",
809+
"UndefinedBehavior",
808810
}
809811
}
810812

src/tools/clang.lua

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,9 @@
6666
unsignedchar = gcc.shared.unsignedchar,
6767
omitframepointer = gcc.shared.omitframepointer,
6868
compileas = gcc.shared.compileas,
69-
sanitize = gcc.shared.sanitize,
69+
sanitize = table.merge(gcc.shared.sanitize, {
70+
Fuzzer = "-fsanitize=fuzzer",
71+
}),
7072
visibility = gcc.shared.visibility,
7173
inlinesvisibility = gcc.shared.inlinesvisibility
7274
}
@@ -118,9 +120,6 @@
118120
--
119121

120122
clang.cxxflags = table.merge(gcc.cxxflags, {
121-
sanitize = {
122-
Fuzzer = "-fsanitize=fuzzer",
123-
},
124123
})
125124

126125
function clang.getcxxflags(cfg)
@@ -254,9 +253,9 @@
254253
end,
255254
},
256255
linker = gcc.ldflags.linker,
257-
sanitize = {
258-
Address = "-fsanitize=address",
259-
},
256+
sanitize = table.merge(gcc.ldflags.sanitize, {
257+
Fuzzer = "-fsanitize=fuzzer",
258+
}),
260259
system = {
261260
wii = "$(MACHDEP)",
262261
}

src/tools/gcc.lua

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,8 @@
147147
},
148148
sanitize = {
149149
Address = "-fsanitize=address",
150+
Thread = "-fsanitize=thread",
151+
UndefinedBehavior = "-fsanitize=undefined",
150152
},
151153
visibility = {
152154
Default = "-fvisibility=default",
@@ -494,6 +496,8 @@
494496
},
495497
sanitize = {
496498
Address = "-fsanitize=address",
499+
Thread = "-fsanitize=thread",
500+
UndefinedBehavior = "-fsanitize=undefined",
497501
},
498502
system = {
499503
wii = "$(MACHDEP)",

tests/tools/test_clang.lua

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -110,25 +110,45 @@
110110
-- Check handling of linker flag.
111111
--
112112

113-
function suite.ldflags_linker_lld()
114-
linker "LLD"
115-
prepare()
116-
test.contains("-fuse-ld=lld", clang.getldflags(cfg))
117-
end
113+
function suite.ldflags_linker_lld()
114+
linker "LLD"
115+
prepare()
116+
test.contains("-fuse-ld=lld", clang.getldflags(cfg))
117+
end
118118

119119
--
120120
-- Check the translation of CXXFLAGS.
121121
--
122122

123-
function suite.onSanitizeAddress()
124-
sanitize { "Address" }
125-
prepare()
126-
test.contains({ "-fsanitize=address" }, clang.getcxxflags(cfg))
127-
test.contains({ "-fsanitize=address" }, clang.getldflags(cfg))
128-
end
123+
function suite.onSanitizeAddress()
124+
sanitize { "Address" }
125+
prepare()
126+
test.contains({ "-fsanitize=address" }, clang.getcxxflags(cfg))
127+
test.contains({ "-fsanitize=address" }, clang.getcflags(cfg))
128+
test.contains({ "-fsanitize=address" }, clang.getldflags(cfg))
129+
end
130+
131+
function suite.cxxflags_onSanitizeFuzzer()
132+
sanitize { "Fuzzer" }
133+
prepare()
134+
test.contains({ "-fsanitize=fuzzer" }, clang.getcxxflags(cfg))
135+
test.contains({ "-fsanitize=fuzzer" }, clang.getcflags(cfg))
136+
test.contains({ "-fsanitize=fuzzer" }, clang.getldflags(cfg))
137+
end
138+
139+
function suite.cxxflags_onSanitizeThread()
140+
sanitize { "Thread" }
141+
prepare()
142+
test.contains({ "-fsanitize=thread" }, clang.getcxxflags(cfg))
143+
test.contains({ "-fsanitize=thread" }, clang.getcflags(cfg))
144+
test.contains({ "-fsanitize=thread" }, clang.getldflags(cfg))
145+
end
129146

130-
function suite.cxxflags_onSanitizeFuzzer()
131-
sanitize { "Fuzzer" }
132-
prepare()
133-
test.contains({ "-fsanitize=fuzzer" }, clang.getcxxflags(cfg))
134-
end
147+
-- UBSan
148+
function suite.cxxflags_onSanitizeUndefined()
149+
sanitize { "UndefinedBehavior" }
150+
prepare()
151+
test.contains({ "-fsanitize=undefined" }, clang.getcxxflags(cfg))
152+
test.contains({ "-fsanitize=undefined" }, clang.getcflags(cfg))
153+
test.contains({ "-fsanitize=undefined" }, clang.getldflags(cfg))
154+
end

tests/tools/test_gcc.lua

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,23 @@
393393
test.contains({ "-fsanitize=address" }, gcc.getldflags(cfg))
394394
end
395395

396+
function suite.cxxflags_onSanitizeThread()
397+
sanitize { "Thread" }
398+
prepare()
399+
test.contains({ "-fsanitize=thread" }, gcc.getcxxflags(cfg))
400+
test.contains({ "-fsanitize=thread" }, gcc.getcflags(cfg))
401+
test.contains({ "-fsanitize=thread" }, gcc.getldflags(cfg))
402+
end
403+
404+
-- UBSan
405+
function suite.cxxflags_onSanitizeUndefined()
406+
sanitize { "UndefinedBehavior" }
407+
prepare()
408+
test.contains({ "-fsanitize=undefined" }, gcc.getcxxflags(cfg))
409+
test.contains({ "-fsanitize=undefined" }, gcc.getcflags(cfg))
410+
test.contains({ "-fsanitize=undefined" }, gcc.getldflags(cfg))
411+
end
412+
396413
--
397414
-- Check the basic translation of LDFLAGS for a Posix system.
398415
--

website/docs/sanitize.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@ sanitize { "value_list" }
88

99
`value_list` specifies the desired `fsanitize` options to enable.
1010

11-
| Value | Description |
12-
|-------------|--------------------------------------------------------|
13-
| Address | Enables compiler support for AddressSanitizer. | Visual Studio support starts with 2019 16.9 |
14-
| Fuzzer | Enables support for LibFuzzer, a coverage-guided fuzzing library. | Visual Studio support starts with 2019 16.9 |
11+
| Value | Description | Notes |
12+
|-------------------|--------------------------------------------------------|---|
13+
| Address | Enables compiler support for AddressSanitizer (ASan). | Visual Studio support starts with 2019 16.9 |
14+
| Fuzzer | Enables support for LibFuzzer, a coverage-guided fuzzing library. | Unsupported with GCC. Visual Studio support starts with 2019 16.9 |
15+
| Thread | Enables compiler support for ThreadSanitizer (TSan). | GCC & Clang only |
16+
| UndefinedBehavior | Enables compiler support for UndefinedBehaviorSanitizer (UBSan). | GCC & Clang only |
1517

1618
### Applies To ###
1719

0 commit comments

Comments
 (0)