Skip to content
This repository was archived by the owner on Nov 8, 2023. It is now read-only.

Commit 37e90c8

Browse files
maurerTreehugger Robot
authored andcommitted
ANDROID: rust: Enable KCFI support when available
On newer clang, a flag is available which makes KCFI compatibility with rustc possible without significant contortions. This is Android specific because Rust KCFI is incomplete, and we are using the binder driver as a staging ground to fix it. This patch is intended to go upstream once the compiler behaves properly. Signed-off-by: Matthew Maurer <[email protected]> Bug: 316623888 Change-Id: I2118224e0e85424298c5315392917f2699f75af7
1 parent 6be7b55 commit 37e90c8

File tree

4 files changed

+16
-3
lines changed

4 files changed

+16
-3
lines changed

Makefile

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1001,8 +1001,19 @@ export CC_FLAGS_LTO
10011001
endif
10021002

10031003
ifdef CONFIG_CFI_CLANG
1004-
CC_FLAGS_CFI := -fsanitize=kcfi
1005-
KBUILD_CFLAGS += $(CC_FLAGS_CFI)
1004+
CC_FLAGS_CFI := -fsanitize=kcfi
1005+
ifdef CONFIG_RUST
1006+
# If Rust is enabled, this flag is required to support cross-language
1007+
# integer types.
1008+
# This addresses the problem that on e.g. i686, int != long, and Rust
1009+
# maps both to i32.
1010+
# See https://rcvalle.com/docs/rust-cfi-design-doc.pdf for details.
1011+
CC_FLAGS_CFI += -fsanitize-cfi-icall-experimental-normalize-integers
1012+
RS_FLAGS_CFI := -Zsanitizer=kcfi -Zsanitizer-cfi-normalize-integers
1013+
KBUILD_RUSTFLAGS += $(RS_FLAGS_CFI)
1014+
export RS_FLAGS_CFI
1015+
endif
1016+
KBUILD_CFLAGS += $(CC_FLAGS_CFI)
10061017
export CC_FLAGS_CFI
10071018
endif
10081019

init/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1888,6 +1888,7 @@ config RUST
18881888
depends on !GCC_PLUGINS
18891889
depends on !RANDSTRUCT
18901890
depends on !DEBUG_INFO_BTF || PAHOLE_HAS_LANG_EXCLUDE
1891+
depends on !CFI_CLANG || $(cc-option,-fsanitize=kcfi,-fsanitize-cfi-icall-experimental-normalize-integers)
18911892
select CONSTRUCTORS
18921893
help
18931894
Enables Rust support in the kernel.

rust/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,7 @@ $(obj)/bindings/bindings_helpers_generated.rs: $(src)/helpers.c FORCE
367367
quiet_cmd_exports = EXPORTS $@
368368
cmd_exports = \
369369
$(NM) -p --defined-only $< \
370-
| awk '/ (T|R|D) / {printf "EXPORT_SYMBOL_RUST_GPL(%s);\n",$$3}' > $@
370+
| awk '$$2~/(T|R|D)/ && $$3!~/__cfi/ {printf "EXPORT_SYMBOL_RUST_GPL(%s);\n",$$3}' > $@
371371

372372
$(obj)/exports_core_generated.h: $(obj)/core.o FORCE
373373
$(call if_changed,exports)

scripts/generate_rust_target.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ fn main() {
181181
}
182182
ts.push("features", features);
183183
ts.push("llvm-target", "x86_64-linux-gnu");
184+
ts.push("supported-sanitizers", ["kcfi"]);
184185
ts.push("target-pointer-width", "64");
185186
} else {
186187
panic!("Unsupported architecture");

0 commit comments

Comments
 (0)