add kconfirm
Subsystems (4)
v3 cover add kconfirm
Hi all,
kconfirm has shrunk a lot since v2!
Okay back to the RFC...
kconfirm is a tool to detect misusage of Kconfig. It detects dead code, constant conditions, and invalid (reverse) ranges. There are also optional checks to detect config options that select visible config options, and to check for dead links in the help texts.
Following this discussion:
https://lore.kernel.org/all/[email protected]/
in which Andrew brought up the possibility of moving kconfirm in-tree, I've prepared this RFC to do so. See also kconfirm's introduction to the mailing list:
https://lore.kernel.org/all/[email protected]/
False Alarms:
kconfirm aims for zero false-positives, which is currently true for the default checks (as far as I'm aware - but there are hundreds to go through); this is not really possible for dead link checks, as this depends on an internet connection, and we do not attempt to bypass bot blocks. For this reason, dead link checking is disabled by default, but I've provided an example below of how to enable it. Additionally, you can view my previous message to the mailing list with hand-verified dead links here:
https://lore.kernel.org/all/[email protected]/
Additionally, there is an optional check to detect config options that select visible config options, as requested by Jani during the review of the first version of this RFC:
https://lore.kernel.org/all/[email protected]/
Even after deduplicating across architectures, there are well over 1,000 instances of these select-visible cases, and I suspect that, despite the Kconfig documentation saying select-visible should be avoided, some exceptions will be made. So, I have left this check disabled by default, keeping in line with the goal of having a low-noise checker. If interested in using it, I have included an example below of how to enable this check.
Current State of Alarms:
On Linux v7.1-rc3 (which this RFC is based), there are 489 alarms coming from the default set of checks, and an additional 1,789 alarms if enabling the optional select-visible check. These counts are with deduplication across all architectures, a change that was made to the tool's CLI from RFC v1 to RFC v2. The last time I checked linux-next (next-20260427), there were 81 unique dead links.
The most critical check is the dead default statements, which has surfaced a few misconfiguration bugs (fortunately, just for kunit tests), see examples:
https://lore.kernel.org/all/[email protected]/
and:
https://lore.kernel.org/all/[email protected]/
But hopefully kconfirm can ease maintenance and we can prevent more of these from making it into the tree in the future.
Use it:
You can test out kconfirm with this patch series by compiling and running kconfirm like this:
make kconfirm
To enable the select-visible check:KCONFIRM_ARGS="--enable-check select_visible" make kconfirm
And to enable dead link checks in the help texts:KCONFIRM_ARGS="--enable-check dead_link" make kconfirm
kconfirm by default runs on the same architecture as the kernel build would; though additional architectures can be enabled by passing --enable-arch and the default architecture can be disabled using --disable-arch. Alarms are tagged with the affected architecture. For alarms that appear in multiple of the enabled architectures, they are deduplicated and tagged like: [X86] or [X86, ARM].
Dependencies will need to first be downloaded from crates.io by running the cargo vendor command in scripts/kconfirm/
Requested feedback:
1. I would like to know if anyone thinks that the select-visible check should be enabled by default.
2. I'm still hoping for some usage feedback!
Thanks,
Julian Braha
---
Changes since v2:
- Reduce Rust dependencies significantly (follows Demi's suggestions):
- from 6 direct dependencies to 1
- from 107 indirect dependencies to 4
- Replace ureq crate with usage of system libcurl (thanks Demi)
- Replace clap crate with FFI bindings to libc's getopt_long (also Demi)
- Remove crates env_logger, regex
- Switch from vendoring dependencies to requiring users to first download outside of Make (as suggested by Miguel)
- Various makefile improvements (as pointed out by Nicolas):
- Fix out-of-tree builds
- Only delete kconfirm artifacts with 'distclean' and 'mrproper'
- Add myself as maintainer of kconfirm (as discussed with Nicolas)
- Remove dedicated code license file (pointed out by Jani)
- Update documentation to explain tool setup
- Add hint to users to check documentation and download tool dependencies
- Address sashiko's many code-level and documentation suggestions:
- Follow the kernel's rust import style
- Fix a dead_range/duplicate_range alarm mixup
- Fix potential duplicates in default value style check
- Avoid panicking on errors
- Clarify parse failure check usage in documentation
- Fix typo in documentation
- Can now enable architectures and disable the default (host) architecture in the CLI
Link to v2:
https://lore.kernel.org/all/[email protected]/
Changes since v1:
- vendored dependencies instead of requiring an internet connection
- removed Cargo.lock
- replaced reqwest dependency with smaller ureq
- removed rustls, expect user to have openssl instead
- added select-visible check based on Jani's feature request
- added invalid (reverse) range check
- deduplicating alarms that appear for multiple architectures
- make clean no longer deletes kconfirm's build artifacts
- typo fixes in documentation
- added patch description for the main "add kconfirm" patch (patch 1/2)
Link to v1:
https://lore.kernel.org/all/[email protected]/
---
Julian Braha (3):
scripts: add kconfirm
Documentation: add kconfirm
MAINTAINERS: create entry for kconfirm
Documentation/dev-tools/index.rst | 1 + Documentation/dev-tools/kconfirm.rst | 222 ++++++ MAINTAINERS | 6 + Makefile | 15 +- scripts/Makefile | 2 +- scripts/kconfirm/.gitignore | 3 + scripts/kconfirm/Cargo.lock | 60 ++ scripts/kconfirm/Cargo.toml | 12 + scripts/kconfirm/Makefile | 14 + scripts/kconfirm/kconfirm-lib/Cargo.toml | 12 + scripts/kconfirm/kconfirm-lib/src/analyze.rs | 643 ++++++++++++++++ scripts/kconfirm/kconfirm-lib/src/checks.rs | 701 ++++++++++++++++++ scripts/kconfirm/kconfirm-lib/src/curl_ffi.rs | 182 +++++ .../kconfirm/kconfirm-lib/src/dead_links.rs | 138 ++++ scripts/kconfirm/kconfirm-lib/src/lib.rs | 62 ++ scripts/kconfirm/kconfirm-lib/src/output.rs | 111 +++ .../kconfirm/kconfirm-lib/src/symbol_table.rs | 223 ++++++ scripts/kconfirm/kconfirm-linux/Cargo.toml | 10 + .../kconfirm/kconfirm-linux/src/getopt_ffi.rs | 99 +++ scripts/kconfirm/kconfirm-linux/src/lib.rs | 78 ++ scripts/kconfirm/kconfirm-linux/src/main.rs | 192 +++++ 21 files changed, 2781 insertions(+), 5 deletions(-) create mode 100644 Documentation/dev-tools/kconfirm.rst create mode 100644 scripts/kconfirm/.gitignore create mode 100644 scripts/kconfirm/Cargo.lock create mode 100644 scripts/kconfirm/Cargo.toml create mode 100644 scripts/kconfirm/Makefile create mode 100644 scripts/kconfirm/kconfirm-lib/Cargo.toml create mode 100644 scripts/kconfirm/kconfirm-lib/src/analyze.rs create mode 100644 scripts/kconfirm/kconfirm-lib/src/checks.rs create mode 100644 scripts/kconfirm/kconfirm-lib/src/curl_ffi.rs create mode 100644 scripts/kconfirm/kconfirm-lib/src/dead_links.rs create mode 100644 scripts/kconfirm/kconfirm-lib/src/lib.rs create mode 100644 scripts/kconfirm/kconfirm-lib/src/output.rs create mode 100644 scripts/kconfirm/kconfirm-lib/src/symbol_table.rs create mode 100644 scripts/kconfirm/kconfirm-linux/Cargo.toml create mode 100644 scripts/kconfirm/kconfirm-linux/src/getopt_ffi.rs create mode 100644 scripts/kconfirm/kconfirm-linux/src/lib.rs create mode 100644 scripts/kconfirm/kconfirm-linux/src/main.rs
Signature · 1 line
2.53.0
RFC PATCH v3 1/3 scripts: add kconfirm
Add kconfirm into scripts/
kconfirm is a static analysis tool with various checks for Kconfig, and intended to have zero false alarms by default. These default checks currently include dead code, constant conditions, and invalid (reverse) ranges.
There are also optional checks for dead links in the help texts, and for config options that select visible config options.
Checks are performed on the same architecture as the kernel build, using a single thread. More architectures can be enabled by passing --enable-arch. Alarms are tagged using the architectures' config options, like so: [X86] if specific to x86, or [X86, ARM] if the alarm appears for both x86 and arm.
Each alarm gets a single line (deduplicated across architectures) and is formatted like this:
[<SEVERITY>] [<ARCH_1>, <ARCH_2>] config <OPTION_NAME>: <alarm message>
The tool source contains two Rust packages: kconfirm-lib and kconfirm-linux.
kconfirm-lib is the underlying library that analyzes Kconfig code, and formats alarms for usability. It analyzes the entire Linux Kconfig spec, including all architectures. This package exposes the symbol table that it constructs so that other tools can import this library, and make use of it for their own Kconfig analyses.
kconfirm-linux imports kconfirm-lib, and provides the CLI, which is intended for either manual usage, or integration with the Linux build system so that users can simply run make kconfirm from the root. kconfirm-linux also handles some of the specificities of how Kconfig is used in the Linux tree, in contrast to other open source software. E.g. the way that each architecture has its own Kconfig and Kconfig.debug files.
The tool's dependencies need to be downloaded from crates.io by running cargo vendor in scripts/kconfirm/ before building and running the tool.
--- Makefile | 15 +- scripts/Makefile | 2 +- scripts/kconfirm/.gitignore | 3 + scripts/kconfirm/Cargo.lock | 60 ++ scripts/kconfirm/Cargo.toml | 12 + scripts/kconfirm/Makefile | 14 + scripts/kconfirm/kconfirm-lib/Cargo.toml | 12 + scripts/kconfirm/kconfirm-lib/src/analyze.rs | 643 ++++++++++++++++ scripts/kconfirm/kconfirm-lib/src/checks.rs | 701 ++++++++++++++++++ scripts/kconfirm/kconfirm-lib/src/curl_ffi.rs | 182 +++++ .../kconfirm/kconfirm-lib/src/dead_links.rs | 138 ++++ scripts/kconfirm/kconfirm-lib/src/lib.rs | 62 ++ scripts/kconfirm/kconfirm-lib/src/output.rs | 111 +++ .../kconfirm/kconfirm-lib/src/symbol_table.rs | 223 ++++++ scripts/kconfirm/kconfirm-linux/Cargo.toml | 10 + .../kconfirm/kconfirm-linux/src/getopt_ffi.rs | 99 +++ scripts/kconfirm/kconfirm-linux/src/lib.rs | 78 ++ scripts/kconfirm/kconfirm-linux/src/main.rs | 192 +++++ 18 files changed, 2552 insertions(+), 5 deletions(-) create mode 100644 scripts/kconfirm/.gitignore create mode 100644 scripts/kconfirm/Cargo.lock create mode 100644 scripts/kconfirm/Cargo.toml create mode 100644 scripts/kconfirm/Makefile create mode 100644 scripts/kconfirm/kconfirm-lib/Cargo.toml create mode 100644 scripts/kconfirm/kconfirm-lib/src/analyze.rs create mode 100644 scripts/kconfirm/kconfirm-lib/src/checks.rs create mode 100644 scripts/kconfirm/kconfirm-lib/src/curl_ffi.rs create mode 100644 scripts/kconfirm/kconfirm-lib/src/dead_links.rs create mode 100644 scripts/kconfirm/kconfirm-lib/src/lib.rs create mode 100644 scripts/kconfirm/kconfirm-lib/src/output.rs create mode 100644 scripts/kconfirm/kconfirm-lib/src/symbol_table.rs create mode 100644 scripts/kconfirm/kconfirm-linux/Cargo.toml create mode 100644 scripts/kconfirm/kconfirm-linux/src/getopt_ffi.rs create mode 100644 scripts/kconfirm/kconfirm-linux/src/lib.rs create mode 100644 scripts/kconfirm/kconfirm-linux/src/main.rs
Makefile +11 -4
@@ -296,7 +296,7 @@ no-dot-config-targets := $(clean-targets) \ $(version_h) headers headers_% archheaders archscripts \ %asm-generic kernelversion %src-pkg dt_binding_check \ outputmakefile rustavailable rustfmt rustfmtcheck \ - run-command + run-command kconfirm no-sync-config-targets := $(no-dot-config-targets) %install modules_sign kernelrelease \ image_name single-targets := %.a %.i %.ko %.lds %.ll %.lst %.mod %.o %.rsi %.s %/
@@ -536,6 +536,7 @@ OBJDUMP = $(CROSS_COMPILE)objdump READELF = $(CROSS_COMPILE)readelf STRIP = $(CROSS_COMPILE)strip endif +CARGO = cargo RUSTC = rustc RUSTDOC = rustdoc RUSTFMT = rustfmt
@@ -633,7 +634,7 @@ export RUSTC_BOOTSTRAP := 1 export CLIPPY_CONF_DIR := $(srctree) export ARCH SRCARCH CONFIG_SHELL BASH HOSTCC KBUILD_HOSTCFLAGS CROSS_COMPILE LD CC HOSTPKG_CONFIG -export RUSTC RUSTDOC RUSTFMT RUSTC_OR_CLIPPY_QUIET RUSTC_OR_CLIPPY BINDGEN LLVM_LINK +export CARGO RUSTC RUSTDOC RUSTFMT RUSTC_OR_CLIPPY_QUIET RUSTC_OR_CLIPPY BINDGEN LLVM_LINK export HOSTRUSTC KBUILD_HOSTRUSTFLAGS export CPP AR NM STRIP OBJCOPY OBJDUMP READELF PAHOLE RESOLVE_BTFIDS LEX YACC AWK INSTALLKERNEL export PERL PYTHON3 CHECK CHECKFLAGS MAKE UTS_MACHINE HOSTCXX
@@ -1705,7 +1706,7 @@ MRPROPER_FILES += include/config include/generated \ vmlinux-gdb.py \ rpmbuild \ rust/libmacros.so rust/libmacros.dylib \ - rust/libpin_init_internal.so rust/libpin_init_internal.dylib + rust/libpin_init_internal.so rust/libpin_init_internal.dylib \ # clean - Delete most, but leave enough to build external modules #
@@ -2227,7 +2228,7 @@ endif # Scripts to check various things for consistency # --------------------------------------------------------------------------- -PHONY += includecheck versioncheck coccicheck +PHONY += includecheck versioncheck coccicheck kconfirm includecheck: find $(srctree)/* $(RCS_FIND_IGNORE) \
@@ -2242,6 +2243,12 @@ versioncheck: coccicheck: $(Q)$(BASH) $(srctree)/scripts/$@ + +kconfirm: + $(Q)$(MAKE) -C $(srctree)/scripts/kconfirm srctree=$(abs_srctree) SRCARCH=$(SRCARCH) || \ + (printf "\n kconfirm failed to compile and run. Have you set up its dependencies yet?\n See Documentation/dev-tools/kconfirm.rst\n\n" && false) +clean-dirs += scripts/kconfirm + PHONY += checkstack kernelrelease kernelversion image_name # UML needs a little special treatment here. It wants to use the host
scripts/Makefile +1 -1
@@ -66,4 +66,4 @@ subdir-$(CONFIG_SECURITY_SELINUX) += selinux subdir-$(CONFIG_SECURITY_IPE) += ipe # Let clean descend into subdirs -subdir- += basic dtc gdb kconfig mod +subdir- += basic dtc gdb kconfig kconfirm mod
scripts/kconfirm/.gitignore new file +3 -0
@@ -0,0 +1,3 @@ +# SPDX-License-Identifier: GPL-2.0-only +/target +/vendor
scripts/kconfirm/Cargo.lock new file +60 -0
60 lines not on this page · raw message
scripts/kconfirm/Cargo.toml new file +12 -0
@@ -0,0 +1,12 @@ +# SPDX-License-Identifier: GPL-2.0 +[workspace] +members = ["kconfirm-lib", "kconfirm-linux"] +resolver = "3" + +[workspace.package] +rust-version = "1.85.0" + +[workspace.dependencies] +nom-kconfig = { version = "0.11", default-features = false, features = [ + "display", +] }
scripts/kconfirm/Makefile new file +14 -0
@@ -0,0 +1,14 @@ +# SPDX-License-Identifier: GPL-2.0 +# kconfirm makefile + +TARGET := kconfirm + +# Extra arguments forwarded to kconfirm. +# Example: make kconfirm KCONFIRM_ARGS="--enable-check dead_links" +KCONFIRM_ARGS ?= + +$(TARGET): + $(CARGO) run --release --offline -p kconfirm-linux -- --linux-path $(srctree) --enable-arch $(SRCARCH) $(KCONFIRM_ARGS) + + +clean-files += target vendor
scripts/kconfirm/kconfirm-lib/Cargo.toml new file +12 -0
@@ -0,0 +1,12 @@ +# SPDX-License-Identifier: GPL-2.0 +[package] +name = "kconfirm-lib" +version = "0.10.0" +edition = "2024" +rust-version.workspace = true + +[dependencies] +nom-kconfig = { workspace = true } + +[features] +default = []
scripts/kconfirm/kconfirm-lib/src/analyze.rs new file +643 -0
643 lines not on this page · raw message
scripts/kconfirm/kconfirm-lib/src/checks.rs new file +701 -0
701 lines not on this page · raw message
scripts/kconfirm/kconfirm-lib/src/curl_ffi.rs new file +182 -0
182 lines not on this page · raw message
scripts/kconfirm/kconfirm-lib/src/dead_links.rs new file +138 -0
138 lines not on this page · raw message
scripts/kconfirm/kconfirm-lib/src/lib.rs new file +62 -0
62 lines not on this page · raw message
scripts/kconfirm/kconfirm-lib/src/output.rs new file +111 -0
111 lines not on this page · raw message
scripts/kconfirm/kconfirm-lib/src/symbol_table.rs new file +223 -0
223 lines not on this page · raw message
scripts/kconfirm/kconfirm-linux/Cargo.toml new file +10 -0
@@ -0,0 +1,10 @@ +# SPDX-License-Identifier: GPL-2.0 +[package] +name = "kconfirm-linux" +version = "0.10.0" +edition = "2024" +rust-version.workspace = true + +[dependencies] +kconfirm-lib = { path = "../kconfirm-lib" } +nom-kconfig = { workspace = true }
scripts/kconfirm/kconfirm-linux/src/getopt_ffi.rs new file +99 -0
99 lines not on this page · raw message
scripts/kconfirm/kconfirm-linux/src/lib.rs new file +78 -0
78 lines not on this page · raw message
scripts/kconfirm/kconfirm-linux/src/main.rs new file +192 -0
192 lines not on this page · raw message
Signature · 1 line
2.53.0
Re: [RFC PATCH v3 1/3] scripts: add kconfirm
Quoted 2,440 lines · Julian Braha
Add kconfirm into
scripts/kconfirm is a static analysis tool with various checks for
Kconfig, and intended to have zero false alarms by default. These default checks currently include dead code, constant conditions, and invalid (reverse) ranges.There are also optional checks for dead links in the help texts, and for config options that select visible config options.
Checks are performed on the same architecture as the kernel build, using a single thread. More architectures can be enabled by passing
--enable-arch. Alarms are tagged using the architectures' config options, like so: [X86] if specific to x86, or [X86, ARM] if the alarm appears for both x86 and arm.Each alarm gets a single line (deduplicated across architectures) and is formatted like this:
[<SEVERITY>] [<ARCH_1>, <ARCH_2>] config <OPTION_NAME>: <alarm message>The tool source contains two Rust packages: kconfirm-lib and kconfirm-linux.
kconfirm-lib is the underlying library that analyzes
Kconfigcode, and formats alarms for usability. It analyzes the entire LinuxKconfigspec, including all architectures. This package exposes the symbol table that it constructs so that other tools can import this library, and make use of it for their ownKconfiganalyses.kconfirm-linux imports kconfirm-lib, and provides the CLI, which is intended for either manual usage, or integration with the Linux build system so that users can simply run
make kconfirmfrom the root. kconfirm-linux also handles some of the specificities of howKconfigis used in the Linux tree, in contrast to other open source software. E.g. the way that each architecture has its ownKconfigandKconfig.debugfiles.The tool's dependencies need to be downloaded from crates.io by running
cargo vendorinscripts/kconfirm/before building and running the tool.Signed-off-by Julian Braha <[email protected]>--- Makefile | 15 +- scripts/Makefile | 2 +- scripts/kconfirm/.gitignore | 3 + scripts/kconfirm/Cargo.lock | 60 ++ scripts/kconfirm/Cargo.toml | 12 + scripts/kconfirm/Makefile | 14 + scripts/kconfirm/kconfirm-lib/Cargo.toml | 12 + scripts/kconfirm/kconfirm-lib/src/analyze.rs | 643 ++++++++++++++++ scripts/kconfirm/kconfirm-lib/src/checks.rs | 701 ++++++++++++++++++ scripts/kconfirm/kconfirm-lib/src/curl_ffi.rs | 182 +++++ .../kconfirm/kconfirm-lib/src/dead_links.rs | 138 ++++ scripts/kconfirm/kconfirm-lib/src/lib.rs | 62 ++ scripts/kconfirm/kconfirm-lib/src/output.rs | 111 +++ .../kconfirm/kconfirm-lib/src/symbol_table.rs | 223 ++++++ scripts/kconfirm/kconfirm-linux/Cargo.toml | 10 + .../kconfirm/kconfirm-linux/src/getopt_ffi.rs | 99 +++ scripts/kconfirm/kconfirm-linux/src/lib.rs | 78 ++ scripts/kconfirm/kconfirm-linux/src/main.rs | 192 +++++ 18 files changed, 2552 insertions(+), 5 deletions(-) create mode 100644 scripts/kconfirm/.gitignore create mode 100644 scripts/kconfirm/Cargo.lock create mode 100644 scripts/kconfirm/Cargo.toml create mode 100644 scripts/kconfirm/Makefile create mode 100644 scripts/kconfirm/kconfirm-lib/Cargo.toml create mode 100644 scripts/kconfirm/kconfirm-lib/src/analyze.rs create mode 100644 scripts/kconfirm/kconfirm-lib/src/checks.rs create mode 100644 scripts/kconfirm/kconfirm-lib/src/curl_ffi.rs create mode 100644 scripts/kconfirm/kconfirm-lib/src/dead_links.rs create mode 100644 scripts/kconfirm/kconfirm-lib/src/lib.rs create mode 100644 scripts/kconfirm/kconfirm-lib/src/output.rs create mode 100644 scripts/kconfirm/kconfirm-lib/src/symbol_table.rs create mode 100644 scripts/kconfirm/kconfirm-linux/Cargo.toml create mode 100644 scripts/kconfirm/kconfirm-linux/src/getopt_ffi.rs create mode 100644 scripts/kconfirm/kconfirm-linux/src/lib.rs create mode 100644 scripts/kconfirm/kconfirm-linux/src/main.rsMakefile +11
-4@@ -296,7 +296,7 @@ no-dot-config-targets := $(clean-targets) \ $(version_h) headers headers_% archheaders archscripts \ %asm-generic kernelversion %src-pkg dt_binding_check \ outputmakefile rustavailable rustfmt rustfmtcheck \ - run-command + run-command kconfirm no-sync-config-targets := $(no-dot-config-targets) %install modules_sign kernelrelease \ image_name single-targets := %.a %.i %.ko %.lds %.ll %.lst %.mod %.o %.rsi %.s %/@@ -536,6 +536,7 @@ OBJDUMP = $(CROSS_COMPILE)objdump READELF = $(CROSS_COMPILE)readelf STRIP = $(CROSS_COMPILE)strip endif +CARGO = cargo RUSTC = rustc RUSTDOC = rustdoc RUSTFMT = rustfmt@@ -633,7 +634,7 @@ export RUSTC_BOOTSTRAP := 1 export CLIPPY_CONF_DIR := $(srctree) export ARCH SRCARCH CONFIG_SHELL BASH HOSTCC KBUILD_HOSTCFLAGS CROSS_COMPILE LD CC HOSTPKG_CONFIG -export RUSTC RUSTDOC RUSTFMT RUSTC_OR_CLIPPY_QUIET RUSTC_OR_CLIPPY BINDGEN LLVM_LINK +export CARGO RUSTC RUSTDOC RUSTFMT RUSTC_OR_CLIPPY_QUIET RUSTC_OR_CLIPPY BINDGEN LLVM_LINK export HOSTRUSTC KBUILD_HOSTRUSTFLAGS export CPP AR NM STRIP OBJCOPY OBJDUMP READELF PAHOLE RESOLVE_BTFIDS LEX YACC AWK INSTALLKERNEL export PERL PYTHON3 CHECK CHECKFLAGS MAKE UTS_MACHINE HOSTCXX@@ -1705,7 +1706,7 @@ MRPROPER_FILES += include/config include/generated \ vmlinux-gdb.py \ rpmbuild \ rust/libmacros.so rust/libmacros.dylib \ - rust/libpin_init_internal.so rust/libpin_init_internal.dylib + rust/libpin_init_internal.so rust/libpin_init_internal.dylib \ # clean - Delete most, but leave enough to build external modules #@@ -2227,7 +2228,7 @@ endif # Scripts to check various things for consistency # --------------------------------------------------------------------------- -PHONY += includecheck versioncheck coccicheck +PHONY += includecheck versioncheck coccicheck kconfirm includecheck: find $(srctree)/* $(RCS_FIND_IGNORE) \@@ -2242,6 +2243,12 @@ versioncheck: coccicheck: $(Q)$(BASH) $(srctree)/scripts/$@ + +kconfirm: + $(Q)$(MAKE) -C $(srctree)/scripts/kconfirm srctree=$(abs_srctree) SRCARCH=$(SRCARCH) || \ + (printf "\n kconfirm failed to compile and run. Have you set up its dependencies yet?\n See Documentation/dev-tools/kconfirm.rst\n\n" && false) +clean-dirs += scripts/kconfirm + PHONY += checkstack kernelrelease kernelversion image_name # UML needs a little special treatment here. It wants to use the hostscripts/Makefile +1
-1@@ -66,4 +66,4 @@ subdir-$(CONFIG_SECURITY_SELINUX) += selinux subdir-$(CONFIG_SECURITY_IPE) += ipe # Let clean descend into subdirs -subdir- += basic dtc gdb kconfig mod +subdir- += basic dtc gdb kconfig kconfirm modscripts/kconfirm/.gitignore new file +3
-0@@ -0,0 +1,3 @@ +# SPDX-License-Identifier: GPL-2.0-only +/target +/vendorscripts/kconfirm/Cargo.lock new file +60
-0@@ -0,0 +1,60 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "bytecount" +version = "0.6.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "175812e0be2bccb6abe50bb8d566126198344f707e304f45c648fd8f2cc0365e" + +[[package]] +name = "kconfirm-lib" +version = "0.10.0" +dependencies = [ + "nom-kconfig", +] + +[[package]] +name = "kconfirm-linux" +version = "0.10.0" +dependencies = [ + "kconfirm-lib", + "nom-kconfig", +] + +[[package]] +name = "memchr" +version = "2.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f8ca58f447f06ed17d5fc4043ce1b10dd205e060fb3ce5b979b8ed8e59ff3f79" + +[[package]] +name = "nom" +version = "8.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "df9761775871bdef83bee530e60050f7e54b1105350d6884eb0fb4f46c2f9405" +dependencies = [ + "memchr", +] + +[[package]] +name = "nom-kconfig" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5a0220bb2c8e5ad29b06fe0f75a276affeb10c9504726bf46d81fef78d69b1e3" +dependencies = [ + "nom", + "nom_locate", +] + +[[package]] +name = "nom_locate" +version = "5.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b577e2d69827c4740cba2b52efaad1c4cc7c73042860b199710b3575c68438d" +dependencies = [ + "bytecount", + "memchr", + "nom", +]scripts/kconfirm/Cargo.toml new file +12
-0@@ -0,0 +1,12 @@ +# SPDX-License-Identifier: GPL-2.0 +[workspace] +members = ["kconfirm-lib", "kconfirm-linux"] +resolver = "3" + +[workspace.package] +rust-version = "1.85.0" + +[workspace.dependencies] +nom-kconfig = { version = "0.11", default-features = false, features = [ + "display", +] }scripts/kconfirm/Makefile new file +14
-0@@ -0,0 +1,14 @@ +# SPDX-License-Identifier: GPL-2.0 +# kconfirm makefile + +TARGET := kconfirm + +# Extra arguments forwarded to kconfirm. +# Example: make kconfirm KCONFIRM_ARGS="--enable-check dead_links" +KCONFIRM_ARGS ?= + +$(TARGET): + $(CARGO) run --release --offline -p kconfirm-linux -- --linux-path $(srctree) --enable-arch $(SRCARCH) $(KCONFIRM_ARGS) + + +clean-files += target vendorscripts/kconfirm/kconfirm-lib/Cargo.toml new file +12
-0@@ -0,0 +1,12 @@ +# SPDX-License-Identifier: GPL-2.0 +[package] +name = "kconfirm-lib" +version = "0.10.0" +edition = "2024" +rust-version.workspace = true + +[dependencies] +nom-kconfig = { workspace = true } + +[features] +default = []scripts/kconfirm/kconfirm-lib/src/analyze.rs new file +643
-0@@ -0,0 +1,643 @@ +// SPDX-License-Identifier: GPL-2.0-only +use crate ::AnalysisArgs ; +use crate ::Check ; +use crate ::SymbolTable ; +use crate ::dead_links; +use crate ::dead_links::LinkStatus ; +use crate ::dead_links::check_link; +use crate ::output::Finding ; +use crate ::output::Severity ; +use crate ::symbol_table::ChoiceData ; +use nom_kconfig::Attribute ::*; +use nom_kconfig::Entry ; +use nom_kconfig::attribute::DefaultAttribute ; +use nom_kconfig::attribute::Expression ; +use nom_kconfig::attribute::Imply ; +use nom_kconfig::attribute::Select ; +use nom_kconfig::attribute::r#type::Type ; +use nom_kconfig::entry::Choice ; +use nom_kconfig::entry::Config ; +use nom_kconfig::entry::If ; +use nom_kconfig::entry::Menu ; +use nom_kconfig::entry::Source ; +use std::collections::HashSet ; +use std::option::Option ; + +#[derive( Debug , Clone , Copy , PartialEq , Eq , Hash )] +enum FunctionalAttributes { +// only tracking the attributes that affect the semantics, e.g. not help texts +Dependencies , +Selects , +Implies , +Ranges , +Defaults , +} + +struct AttributeGroupingChecker { + current_group:Option <FunctionalAttributes >, + finished_groups:HashSet <FunctionalAttributes >, +} + +impl AttributeGroupingChecker { +fn new () ->Self { +Self { + current_group:None , + finished_groups:HashSet ::new (), + } + } + +// doesn't modify `findings` if the style check is disabled +fn check ( + &mut self, + group:FunctionalAttributes , + args: &AnalysisArgs , + findings: &mut Vec <Finding >, + symbol: &str , + arch: &String , + message:String , + ) { +if !args.is_enabled (Check ::UngroupedAttribute ) { +return ; + } + +match self.current_group { +// still contiguous +Some (current)if current == group => {} + +// start of group +None => { + self.current_group =Some (group); + } + +Some (current) => { +// the previous group finished + self.finished_groups.insert (current); + +// we've already finished this group, it's ungrouped +if self.finished_groups.contains (&group) { + findings.push (Finding { + severity:Severity ::Style , + check:Check ::UngroupedAttribute , + symbol:Some (symbol.to_string ()), + message, + arch: arch.to_owned (), + }); + } + +// switch to the new group + self.current_group =Some (group); + } + } + } +} + +struct DeadLinkChecker { + visited_links:HashSet <String >, +} + +impl DeadLinkChecker { +fn new () ->Self { +Self { + visited_links:HashSet ::new (), + } + } + +fn check_text ( + &mut self, + text: &str , + args: &AnalysisArgs , + findings: &mut Vec <Finding >, + symbol:Option <&str >, + arch: &String , + context: &str , + ) { +if !args.is_enabled (Check ::DeadLink ) { +return ; + } + +let links = dead_links::find_links (text); + +if links.is_empty () { +return ; + } + +for linkin links { +// avoid rechecking identical links +if !self.visited_links.insert (link.clone ()) { +continue ; + } + +let status =check_link (&link); +if status !=LinkStatus ::Ok && status !=LinkStatus ::ProbablyBlocked { + findings.push (Finding { + severity:Severity ::Warning , + check:Check ::DeadLink , + symbol: symbol.map (|s| s.to_string ()), + message:format! ( +"{} contains link {} with status {:?}" , + context, link, status + ), + arch: arch.to_owned (), + }); + } + } + } +} + +#[derive( Clone )] +pub struct Context { +pub arch:String , +pub definition_condition:Vec <Expression >, +pub visibility:Vec <Option <Expression >>, +pub dependencies:Vec <Expression >, +pub in_choice:bool , +} + +impl Context { +fn with_arch (arch:String ) ->Context { +Context { + arch, + definition_condition:vec! [], + visibility:vec! [], + dependencies:vec! [], + in_choice:false , + } + } + +fn child (&self) ->Self { + self.clone () + } + +fn with_dep (mut self, dep:Expression ) ->Self { + self.dependencies.push (dep); + self + } + +fn with_visibility (mut self, cond:Option <Expression >) ->Self { + self.visibility.push (cond); + self + } + +fn with_definition (mut self, cond:Expression ) ->Self { + self.definition_condition.push (cond); + self + } + +fn in_choice (mut self) ->Self { + self.in_choice =true ; + self + } +} + +fn recurse_entries ( + args: &AnalysisArgs , + symtab: &mut SymbolTable , + entries:Vec <Entry >, + ctx:Context , + findings: &mut Vec <Finding >, +) { +for entryin entries { +process_entry (args, symtab, entry, ctx.clone (), findings); + } +} + +pub fn analyze ( + args: &AnalysisArgs , + symtab: &mut SymbolTable , + arch:String , + entries:Vec <Entry >, +) ->Vec <Finding > { +let mut findings =Vec ::new (); + +let ctx =Context ::with_arch (arch); + +recurse_entries (args, symtab, entries, ctx, &mut findings); + + findings +} + +fn handle_config ( + args: &AnalysisArgs , + symtab: &mut SymbolTable , + entry:Config , + ctx: &Context , + findings: &mut Vec <Finding >, +) { +let config_symbol = entry.symbol; + +let mut child_ctx = ctx.child (); + +let mut config_type =None ; +let mut kconfig_dependencies =Vec ::new (); +let mut kconfig_selects:Vec <Select > =Vec ::new (); +let mut kconfig_implies:Vec <Imply > =Vec ::new (); +let mut kconfig_ranges =Vec ::new (); +let mut kconfig_defaults =Vec ::new (); +let mut found_prompt =false ; + +/* +* style check: ungrouped attributes +* - need to check that dependencies, selects, ranges, and defaults are each kept together. +*/ +let mut attribute_grouping_checker =AttributeGroupingChecker ::new (); +let mut dead_link_checker =DeadLinkChecker ::new (); +for attributein entry.attributes { +match attribute { +Type (kconfig_type) =>match kconfig_type.r#type.clone () { +// hybrid type definition and default +Type ::DefBool (db) => { +let default_attribute:DefaultAttribute =DefaultAttribute { + expression: db.clone (), + r#if: kconfig_type.clone ().r#if, + }; + + kconfig_defaults.push (default_attribute); + config_type =Some (kconfig_type); + +// NOTE: as a style, we prefer to keep the hybrid default-typedef with the standalone defaults + attribute_grouping_checker.check ( +FunctionalAttributes ::Defaults , + args, + findings, + &config_symbol, + &ctx.arch, +format! ("ungrouped default {}" , db), + ); + } +Type ::Bool (unconditional_prompt) => { +if unconditional_prompt.is_some () { + found_prompt =true ; + } + config_type =Some (kconfig_type); + } + +// hybrid type definition and default +Type ::DefTristate (dt) => { +// NOTE: as a style, we prefer to keep the hybrid default-typedef with the standalone defaults + attribute_grouping_checker.check ( +FunctionalAttributes ::Defaults , + args, + findings, + &config_symbol, + &ctx.arch, +format! ("ungrouped default {}" , &dt), + ); + +let default_attribute:DefaultAttribute =DefaultAttribute { + expression: dt, + r#if: kconfig_type.clone ().r#if, + }; + + kconfig_defaults.push (default_attribute); + config_type =Some (kconfig_type); + } +Type ::Tristate (unconditional_prompt) => { +if unconditional_prompt.is_some () { + found_prompt =true ; + } + + config_type =Some (kconfig_type.clone ()) + } +Type ::Hex (unconditional_prompt) => { +if unconditional_prompt.is_some () { + found_prompt =true ; + } + + config_type =Some (kconfig_type); + } +Type ::Int (unconditional_prompt) => { +if unconditional_prompt.is_some () { + found_prompt =true ; + } + config_type =Some (kconfig_type); + } +Type ::String (unconditional_prompt) => { +if unconditional_prompt.is_some () { + found_prompt =true ; + } + config_type =Some (kconfig_type); + } + }, +Default (default) => { + attribute_grouping_checker.check ( +FunctionalAttributes ::Defaults , + args, + findings, + &config_symbol, + &ctx.arch, +format! ("ungrouped default {}" , &default ), + ); + + kconfig_defaults.push (default); + } + +DependsOn (depends_on) => { + attribute_grouping_checker.check ( +FunctionalAttributes ::Dependencies , + args, + findings, + &config_symbol, + &ctx.arch, +format! ("ungrouped dependency {}" , &depends_on), + ); + + kconfig_dependencies.push (depends_on); + } +Select (select) => { + attribute_grouping_checker.check ( +FunctionalAttributes ::Selects , + args, + findings, + &config_symbol, + &ctx.arch, +format! ("ungrouped select {}" , &select), + ); + + kconfig_selects.push (select); + } +Imply (imply) => { + attribute_grouping_checker.check ( +FunctionalAttributes ::Implies , + args, + findings, + &config_symbol, + &ctx.arch, +format! ("ungrouped imply {}" , imply), + ); + + kconfig_implies.push (imply); + +// TODO: may be relevant for nonvisible config options when building an SMT model... + } +// NOTE: range bounds are inclusive +Range (r) => { + attribute_grouping_checker.check ( +FunctionalAttributes ::Ranges , + args, + findings, + &config_symbol, + &ctx.arch, +format! ("ungrouped range {}" , r), + ); + + kconfig_ranges.push (r); + } +Help (h) => { +// doing nothing for menu help right now + + dead_link_checker.check_text ( + &h, + args, + findings, +Some (&config_symbol), + &ctx.arch, +"help text" , + ); + } + +Modules => { +// the modules attribute designates this config option as the one that determines if the `m` state is available for tristates options. + +// just making a special note of this in the symtab for now... + symtab.modules_option =Some (config_symbol.clone ()); + } + +// the prompt's option `if` determines "visibility" +Prompt (prompt) => { +// TODO: once we have SMT solving, we can also check if the prompt condition is always true or never true (and therefore, effectively unconditional) + + found_prompt =true ; +if let Some (c) = prompt.r#if { + child_ctx = child_ctx.with_visibility (Some (c)); + } + } +Transitional => { +// doing nothing for transitional right now + } +Optional |Visible (_) |Requires (_) |Option (_) => { +eprintln! ("Error: unexpected attribute encountered: {:?}" , attribute); + +if cfg! (debug_assertions) { +panic! (); + } + } + } + } + +if !found_prompt { + child_ctx = child_ctx.with_visibility (None ); + } + +// there can be multiple entries that get merged. so we need to do the same for our symtab. +let kconfig_type = config_type.clone ().map (|c| c.r#type); + +// at the time of writing this, linux's kconfig only uses Bool inside Choice. +// however, the kconfig documentation doesn't specify whether or not this is guaranteed to be the case. +// we add this check to ensure that we don't cause undefined behavior in future linux versions if something changes... +if child_ctx.in_choice { +if let Some (kt) = &kconfig_type { +match kt { +Type ::Bool (_) |Type ::DefBool (_) => { +// expected in a choice... + } + + _ => { +// TODO: old versions of linux (like 5.4.4) have tristates in the choice +// - u-boot also currently has hex options in the choice! +eprintln! ( +"Error: found something unexpected in a choice-statement: {:?}" , + kt + ); + } + } + } + } + +// at the end, add the file's cur_dependencies to this var's invididual dependencies. + kconfig_dependencies.extend (child_ctx.dependencies.clone ()); + symtab.merge_insert_new_solved ( + config_symbol.clone (), + kconfig_type, + kconfig_dependencies, +//z3_dependency, + kconfig_ranges, + kconfig_defaults, + child_ctx.visibility.clone (), + child_ctx.arch.clone (), + child_ctx.definition_condition.clone (), +None , + kconfig_selects + .clone () + .into_iter () + .map (|sel| (sel.symbol, sel.r#if)) + .collect (), + kconfig_implies + .into_iter () + .map (|imply| (imply.symbol.to_string (), imply.r#if)) + .collect (), + ); +// TODO: file a github issue, imply can never imply a constant (this is technically parsing incorrectly) + +// TODO: when SMT solving, we may need to keep track of the implies the same way we keep track of selects, +// in cases when the implied config option is non-visible + +// need to add the select condition to the definedness condition if it exists +for selectin kconfig_selects { +match select.r#if { +None => symtab.merge_insert_new_solved ( + select.symbol, +None , +Vec ::new (), +Vec ::new (), +Vec ::new (), +Vec ::new (), + child_ctx.arch.clone (), + child_ctx.definition_condition.clone (), +Some ((config_symbol.clone (),None )), +Vec ::new (), +Vec ::new (), + ), +Some (select_condition) => { + symtab.merge_insert_new_solved ( + select.symbol, +None , +Vec ::new (), +Vec ::new (), +Vec ::new (), +Vec ::new (), + child_ctx.arch.clone (), + child_ctx.definition_condition.clone (), +Some ((config_symbol.clone (),Some (select_condition))), +Vec ::new (), +Vec ::new (), + ); + } + } + } +} + +fn handle_menu ( + args: &AnalysisArgs , + symtab: &mut SymbolTable , + entry:Menu , + ctx: &Context , + findings: &mut Vec <Finding >, +) { +// menus can set the visibility of their menu items + +let mut child_ctx = ctx.child (); + +for depin entry.depends_on { + child_ctx = child_ctx.with_dep (dep.clone ()); + child_ctx = child_ctx.with_visibility (Some (dep));// not a typo, the config options inside of a menu are only visible if the menu's dependencies are satisfied + } + +let nested_entries = entry.entries; + +recurse_entries (args, symtab, nested_entries, child_ctx.clone (), findings); +} + +fn handle_choice ( + args: &AnalysisArgs , + symtab: &mut SymbolTable , + entry:Choice , + ctx: &Context , + findings: &mut Vec <Finding >, +) { +let mut child_ctx = ctx.child (); + child_ctx = child_ctx.in_choice (); + +// we are going to add the dependencies of the choice to the dependencies of the entries. +// we start with the dependencies inherited from the file +let mut choice_visibility_condition =None ; +let mut defaults =Vec ::new (); +for attributein entry.options { +match attribute { +DependsOn (depends_on) => { + child_ctx = child_ctx.with_dep (depends_on); + } + +Default (default) => { + defaults.push (default); + } + +// the prompt's `if` determines visibility +Prompt (prompt) => { + choice_visibility_condition = prompt.r#if; +if let Some (i) = choice_visibility_condition.clone () { + child_ctx = child_ctx.with_visibility (Some (i)); + } + } + _ => { +// skip + } + } + } + +// all of the variables in the choice menu +//let mut contained_vars = Vec::with_capacity(c.entries.len()); +let nested_entries = entry.entries; + +recurse_entries (args, symtab, nested_entries, child_ctx.clone (), findings); + +let choice_data =ChoiceData { +//inner_vars: contained_vars, + arch: child_ctx.arch.clone (), + visibility: choice_visibility_condition, + dependencies: child_ctx.dependencies, + defaults, + }; + symtab.choices.push (choice_data); +} + +fn handle_if ( + args: &AnalysisArgs , + symtab: &mut SymbolTable , + entry:If , + ctx: &Context , + findings: &mut Vec <Finding >, +) { +let mut child_ctx = ctx.child (); + child_ctx = child_ctx.with_definition (entry.condition.clone ()); + child_ctx = child_ctx.with_dep (entry.condition); +let nested_entries = entry.entries; + +recurse_entries (args, symtab, nested_entries, child_ctx, findings); +} + +fn handle_source ( + args: &AnalysisArgs , + symtab: &mut SymbolTable , + entry:Source , + ctx: &Context , + findings: &mut Vec <Finding >, +) { +let sourced_kconfig = entry.kconfigs; + +for sourced_kconfigin sourced_kconfig { +recurse_entries (args, symtab, sourced_kconfig.entries, ctx.clone (), findings); + } +} + +pub fn process_entry ( + args: &AnalysisArgs , + symtab: &mut SymbolTable , + entry:Entry , + ctx:Context , + findings: &mut Vec <Finding >, +) { +// NOTE: in general, each handler should update the context as it encounters that construct. +// e.g. Context.in_choice() should be called at the start of handle_choice(), not right before call to process_entry() when a choice is found and process_entry is called +match entry { +Entry ::Config (c) |Entry ::MenuConfig (c) => { +handle_config (args, symtab, c, &ctx, findings); + } +Entry ::Menu (m) =>handle_menu (args, symtab, m, &ctx, findings), +Entry ::Choice (c) =>handle_choice (args, symtab, c, &ctx, findings), +Entry ::If (i) =>handle_if (args, symtab, i, &ctx, findings), +Entry ::Source (s) =>handle_source (args, symtab, s, &ctx, findings), +Entry ::Comment (_) => {} +Entry ::MainMenu (_) => {} + _ => {} + } +}scripts/kconfirm/kconfirm-lib/src/checks.rs new file +701
-0@@ -0,0 +1,701 @@ +// SPDX-License-Identifier: GPL-2.0-only +use crate ::output::Finding ; +use crate ::output::Severity ; +use crate ::symbol_table::AttributeDef ; +use crate ::symbol_table::TypeInfo ; +use nom_kconfig::attribute::Expression ; +use nom_kconfig::attribute::range::RangeBound ; +use std::collections::HashSet ; +use std::num::ParseIntError ; +use std::str::FromStr ; + +#[derive( Debug , Clone , Copy , PartialEq , Eq , Hash )] +pub enum Check { +FailedParse , +UngroupedAttribute ,// check for duplicate default values, and ungrouped attributes +DeadLink ,// check for dead links in the help texts +SelectVisible , +// need SMT solving before we can detect select-undefineds +//SelectUndefined, +DuplicateDependency , +DuplicateRange , +DeadRange , +DuplicateSelect , +DeadSelect , +DeadDefault , +ConstantCondition , +DuplicateDefault , +DuplicateDefaultValue , +DuplicateImply , +DeadImply , +ReverseRange , +} + +impl Check { +pub fn as_str (self) -> &'staticstr { +match self { +Check ::FailedParse =>"failed_parse" , +Check ::UngroupedAttribute =>"ungrouped_attribute" , +Check ::DeadLink =>"dead_link" , +Check ::SelectVisible =>"select_visible" , +Check ::DuplicateDependency =>"duplicate_dependency" , +Check ::DuplicateRange =>"duplicate_range" , +Check ::DeadRange =>"dead_range" , +Check ::DuplicateSelect =>"duplicate_select" , +Check ::DeadSelect =>"dead_select" , +Check ::DeadDefault =>"dead_default" , +Check ::ConstantCondition =>"constant_condition" , +Check ::DuplicateDefault =>"duplicate_default" , +Check ::DuplicateDefaultValue =>"duplicate_default_value" , +Check ::DuplicateImply =>"duplicate_imply" , +Check ::DeadImply =>"dead_imply" , +Check ::ReverseRange =>"reverse_range" , + } + } +} + +#[derive( Debug )] +pub struct ParseCheckError { +pub input:String , +} + +impl std::fmt::Display for ParseCheckError { +fn fmt (&self, f: &mut std::fmt::Formatter <'_>) -> std::fmt::Result { +write! (f,"unknown check '{}'" , self.input) + } +} + +impl std::error::Error for ParseCheckError {} + +impl FromStr for Check { +type Err =ParseCheckError ; + +fn from_str (name: &str ) ->Result <Self ,Self ::Err > { +match name { +"failed_parse" =>Ok (Check ::FailedParse ), +"ungrouped_attribute" =>Ok (Check ::UngroupedAttribute ), +"dead_link" =>Ok (Check ::DeadLink ), +"select_visible" =>Ok (Check ::SelectVisible ), +"duplicate_dependency" =>Ok (Check ::DuplicateDependency ), +"duplicate_range" =>Ok (Check ::DuplicateRange ), +"dead_range" =>Ok (Check ::DeadRange ), +"duplicate_select" =>Ok (Check ::DuplicateSelect ), +"dead_select" =>Ok (Check ::DeadSelect ), +"dead_default" =>Ok (Check ::DeadDefault ), +"constant_condition" =>Ok (Check ::ConstantCondition ), +"duplicate_default" =>Ok (Check ::DuplicateDefault ), +"duplicate_default_value" =>Ok (Check ::DuplicateDefaultValue ), +"duplicate_imply" =>Ok (Check ::DuplicateImply ), +"dead_imply" =>Ok (Check ::DeadImply ), +"reverse_range" =>Ok (Check ::ReverseRange ), + _ =>Err (ParseCheckError { + input: name.to_string (), + }), + } + } +} + +#[derive( Clone , Debug )] +pub struct AnalysisArgs { +// check for duplicate default values +pub enabled_checks:HashSet <Check >, +} + +impl AnalysisArgs { +pub fn is_enabled (&self, check:Check ) ->bool { + self.enabled_checks.contains (&check) + } +} + +// returns an Error if a hex range bound cannot be parsed as an u64 +pub fn check_reverse_ranges (arch: &String , var_symbol: &str , info: &AttributeDef ) ->Vec <Finding > { +let mut findings =Vec ::new (); + +for rangein &info.kconfig_ranges { +// returns an Error if a hex range bound cannot be parsed as an u64 +fn range_bound_to_int (range_bound: &RangeBound ) ->Result <i128 ,ParseIntError > { +match range_bound { +RangeBound ::Number (b) => { +return Ok (b.to_owned ()as i128 ); + } +RangeBound ::Hex (b_str) => { +let trimmed = b_str.trim_start_matches ("0x" ).trim_start_matches ("0X" ); + +return i128::from_str_radix (trimmed,16 ); + } +RangeBound ::Variable (_) => { +// for now, the caller is expected not to pass these cases. +unreachable! ("not handling variable ranges until SMT solving" ); + } +RangeBound ::Symbol (_) => { +// TODO: need SMT solving for this case +// for now, the caller is expected not to pass these cases. +unreachable! ("not handling CONFIG ranges until SMT solving" ); + } + } + } + +if matches! (range.lower_bound,RangeBound ::Symbol (_)) + ||matches! (range.upper_bound,RangeBound ::Symbol (_)) + { +// not handling these cases until SMT solving. +// don't return though, because we stil want to check the other ranges. +continue ; + } + +let maybe_lower_bound =range_bound_to_int (&range.lower_bound); +let maybe_upper_bound =range_bound_to_int (&range.upper_bound); + +match (maybe_lower_bound, maybe_upper_bound) { + (Ok (lower_bound),Ok (upper_bound)) => { +if lower_bound > upper_bound { +let message =format! ( +"reverse range {} for config option: {}, no value is valid" , + range.to_string (), + var_symbol, + ); + findings.push (Finding { + severity:Severity ::Warning , + check:Check ::ReverseRange , + symbol:Some (var_symbol.to_owned ()), + arch: arch.to_owned (), + message, + }); + } + } + (Result ::Err (_), _) | (_,Result ::Err (_)) => { +eprintln! ( +"Error: couldn't parse hex range bound as i128 for config option: {}" , + var_symbol + ); +// still want to check the other range bounds +continue ; + } + } + } + + findings +} + +pub fn check_constant_conditions ( + arch: &String , + var_symbol: &str , + info: &AttributeDef , +) ->Vec <Finding > { +let mut findings =Vec ::new (); +let default_conditions:Vec <&Expression > = info + .kconfig_defaults + .iter () + .filter_map (|conditional_default| conditional_default.r#if.as_ref ()) + .collect (); + +check_conditions ( + arch, + &mut findings, + &var_symbol, + &info.kconfig_dependencies, + default_conditions, +"default" , + ); + +let select_conditions:Vec <&Expression > = info + .selects + .iter () + .filter_map (|conditional_select| conditional_select.1 .as_ref ()) + .collect (); + +check_conditions ( + arch, + &mut findings, + var_symbol, + &info.kconfig_dependencies, + select_conditions, +"select" , + ); + +let imply_conditions:Vec <&Expression > = info + .implies + .iter () + .filter_map (|imp| imp.1 .as_ref ()) + .collect (); + +check_conditions ( + arch, + &mut findings, + var_symbol, + &info.kconfig_dependencies, + imply_conditions, +"imply" , + ); + +let range_conditions:Vec <&Expression > = info + .kconfig_ranges + .iter () + .filter_map (|conditional_range| conditional_range.r#if.as_ref ()) + .collect (); + +check_conditions ( + arch, + &mut findings, + var_symbol, + &info.kconfig_dependencies, + range_conditions, +"range" , + ); + +fn check_conditions ( + arch: &String , + findings: &mut Vec <Finding >, + symbol: &str , + kconfig_dependencies: &[Expression ], + attribute_conditions:Vec <&Expression >, + context: &str , + ) { +for attribute_conditionin attribute_conditions.into_iter () { +if kconfig_dependencies.contains (attribute_condition) { +let message =format! ( +"constant {} condition 'if {}' for config option: {}, this condition is a dependency and will always be true" , + context, + attribute_condition.to_string (), + symbol, + ); + findings.push (Finding { + severity:Severity ::Warning , + check:Check ::ConstantCondition , + symbol:Some (symbol.to_owned ()), + arch: arch.to_owned (), + message, + }); + } + } + } + findings +} + +pub fn check_variable_info ( + args: &AnalysisArgs , + var_symbol: &str , + arch_specific: &String , + info: &AttributeDef , +) ->Vec <Finding > { +let mut findings =Vec ::new (); + +if args.is_enabled (Check ::DuplicateDependency ) { + findings.extend (check_duplicate_dependencies ( + arch_specific, + var_symbol, + info, + )); + } + +if args.is_enabled (Check ::DuplicateImply ) { + findings.extend (check_duplicate_implies (arch_specific, var_symbol, info)); + } + +if args.is_enabled (Check ::DuplicateRange ) { + findings.extend (check_duplicate_ranges (arch_specific, var_symbol, info)); + } + +if args.is_enabled (Check ::DuplicateSelect ) { + findings.extend (check_duplicate_selects (arch_specific, var_symbol, info)); + } + +if args.is_enabled (Check ::ConstantCondition ) { + findings.extend (check_constant_conditions (arch_specific, var_symbol, info)); + } + +if args.is_enabled (Check ::DeadDefault ) + || args.is_enabled (Check ::DuplicateDefault ) + || args.is_enabled (Check ::DuplicateDefaultValue ) + { + findings.extend (check_defaults (arch_specific, var_symbol, info, args)); + } + +if args.is_enabled (Check ::ReverseRange ) { + findings.extend (check_reverse_ranges (arch_specific, var_symbol, info)); + } + + findings +} + +// TODO: also check if a config option in one arch unconditionally references a config option that only exists in another arch (need SMT for this first) +pub fn check_select_visible (var_symbol: &str , info: &TypeInfo ) ->Vec <Finding > { +let mut findings =Vec ::new (); + +// only interested in the options that are selected +if info.selected_by.is_empty () { +return Vec ::new (); + } + +for (selector, select_info)in &info.selected_by { +for (arch, _cond)in select_info { +// NOTE: we don't care if the select is conditional or unconditional, just the selectee's visibility + +// at this point, we know that `selector` unconditionally selects `var_symbol` +// now, we need to check if `var_symbol` is unconditionally visible + +let message =format! ( +"selects the visible {}; consider using 'depends on' or 'imply' instead" , + var_symbol + ); + +// match the architecture that the select happens under with the architecture of the unconditional visibility +match info.attribute_defs.get (arch) { +None => { +// not selected in this architecture + } +Some (cur_arch_attribute_def) => { +for (if_conditions, attributes)in cur_arch_attribute_def { +if if_conditions.is_empty () && attributes.visibility.is_empty () { +// empty visiblity means that it is unconditionally visible, within the current arch (assuming arch is not `None`) + + findings.push (Finding { + severity:Severity ::Warning , + check:Check ::SelectVisible , + symbol:Some (selector.to_owned ()), + message: message.clone (), + arch: arch.to_owned (), + }); + } + } + } + } + } + } + + findings +} + +fn is_duplicate <T :Eq + std::hash::Hash >(set: &mut HashSet <T >, key:T ) ->bool { + !set.insert (key) +} + +fn check_duplicate_dependencies ( + arch_specific: &String , + var_symbol: &str , + info: &AttributeDef , +) ->Vec <Finding > { +let mut findings =Vec ::new (); +let mut seen =HashSet ::new (); + +for depin &info.kconfig_dependencies { +if is_duplicate (&mut seen, dep.to_string ()) { +let message =format! ("duplicate dependency on {}" , dep.to_string ()); + findings.push (Finding { + severity:Severity ::Warning , + check:Check ::DuplicateDependency , + symbol:Some (var_symbol.to_owned ()), + message, + arch: arch_specific.to_owned (), + }); + } + } + + findings +} + +fn check_duplicate_implies (arch: &String , var_symbol: &str , info: &AttributeDef ) ->Vec <Finding > { +let mut findings =Vec ::new (); + +// symbols implied unconditionally +let mut unconditional:HashSet <String > =HashSet ::new (); + +// (symbol, condition) +let mut conditional:HashSet <(String ,String )> =HashSet ::new (); + +for impin &info.implies { +let imply_var = imp.0 .clone (); + +match &imp.1 { +Some (cond) => { +let cond_str = cond.to_string (); + +// duplicate conditional imply +if !conditional.insert ((imply_var.clone (), cond_str.clone ())) { + findings.push (Finding { + severity:Severity ::Warning , + check:Check ::DuplicateImply , + symbol:Some (var_symbol.to_owned ()), + message:format! ( +"duplicate imply of {:?} with condition {}" , + imp.0 , cond_str + ), + arch: arch.to_owned (), + }); + } + +// conditional imply is dead if unconditional exists +if unconditional.contains (&imply_var) { + findings.push (Finding { + severity:Severity ::Warning , + check:Check ::DeadImply , + symbol:Some (var_symbol.to_owned ()), + message:format! ("dead imply of {:?}" , imp), + arch: arch.to_owned (), + }); + } + } + +None => { +// duplicate unconditional imply +if !unconditional.insert (imply_var.clone ()) { + findings.push (Finding { + severity:Severity ::Warning , + check:Check ::DuplicateImply , + symbol:Some (var_symbol.to_owned ()), + message:format! ("duplicate imply of {:?}" , imp), + arch: arch.to_owned (), + }); + } + +// previous conditionals with same symbol are dead +for (sym, _)in &conditional { +if sym == &imply_var { + findings.push (Finding { + severity:Severity ::Warning , + check:Check ::DeadImply , + symbol:Some (var_symbol.to_owned ()), + message:format! ("dead imply of {:?}" , imp), + arch: arch.to_owned (), + }); + } + } + } + } + } + + findings +} + +fn check_duplicate_ranges (arch: &String , var_symbol: &str , info: &AttributeDef ) ->Vec <Finding > { +let mut findings =Vec ::new (); + +// unconditional ranges by bounds +let mut unconditional:HashSet <String > =HashSet ::new (); + +// (bounds, condition) +let mut conditional:HashSet <(String ,String )> =HashSet ::new (); + +for rangein &info.kconfig_ranges { +// uniquely identify the range bounds +let range_key =format! ("{} {}" , range.lower_bound, range.upper_bound); + +match &range.r#if { +Some (cond) => { +let cond_str = cond.to_string (); + +// duplicate conditional range +if !conditional.insert ((range_key.clone (), cond_str.clone ())) { + findings.push (Finding { + severity:Severity ::Warning , + check:Check ::DuplicateRange , + symbol:Some (var_symbol.to_owned ()), + message:format! ("duplicate range {:?} with condition {}" , range, cond_str), + arch: arch.to_owned (), + }); + } + +// conditional range is dead if unconditional exists +if unconditional.contains (&range_key) { + findings.push (Finding { + severity:Severity ::Warning , + check:Check ::DeadRange , + symbol:Some (var_symbol.to_owned ()), + message:format! ("dead range of {:?}" , range), + arch: arch.to_owned (), + }); + } + } + +None => { +// duplicate unconditional range +if !unconditional.insert (range_key.clone ()) { + findings.push (Finding { + severity:Severity ::Warning , + check:Check ::DeadRange , + symbol:Some (var_symbol.to_owned ()), + message:format! ("duplicate range {:?}" , range), + arch: arch.to_owned (), + }); + } + +// previous conditionals with same bounds are dead +for (bounds, _)in &conditional { +if bounds == &range_key { + findings.push (Finding { + severity:Severity ::Warning , + check:Check ::DeadRange , + symbol:Some (var_symbol.to_owned ()), + message:format! ("dead range of {:?}" , range), + arch: arch.to_owned (), + }); + } + } + } + } + } + + findings +} + +fn check_duplicate_selects (arch: &String , var_symbol: &str , info: &AttributeDef ) ->Vec <Finding > { +let mut findings =Vec ::new (); + +// symbols selected unconditionally +let mut unconditional:HashSet <String > =HashSet ::new (); + +// (symbol, condition) +let mut conditional:HashSet <(String ,String )> =HashSet ::new (); + +for selectin &info.selects { +let select_var = select.0 .clone (); + +match &select.1 { +Some (cond) => { +let cond_str = cond.to_string (); + +// duplicate conditional select +if !conditional.insert ((select_var.clone (), cond_str.clone ())) { + findings.push (Finding { + severity:Severity ::Warning , + check:Check ::DuplicateSelect , + symbol:Some (var_symbol.to_owned ()), + message:format! ( +"duplicate select of {:?} with condition {}" , + select.0 , cond_str + ), + arch: arch.to_owned (), + }); + } + +// conditional is dead if unconditional exists +if unconditional.contains (&select_var) { + findings.push (Finding { + severity:Severity ::Warning , + check:Check ::DeadSelect , + symbol:Some (var_symbol.to_owned ()), + message:format! ("dead select of {:?}" , select.0 ), + arch: arch.to_owned (), + }); + } + } + +None => { +// duplicate unconditional select +if !unconditional.insert (select_var.clone ()) { + findings.push (Finding { + severity:Severity ::Warning , + check:Check ::DuplicateSelect , + symbol:Some (var_symbol.to_owned ()), + message:format! ("duplicate select of {:?}" , select.0 ), + arch: arch.to_owned (), + }); + } + +// any previous conditional selects are now dead too +for (sym, _)in &conditional { +if sym == &select_var { + findings.push (Finding { + severity:Severity ::Warning , + check:Check ::DeadSelect , + symbol:Some (var_symbol.to_owned ()), + message:format! ("dead select of {:?}" , select.0 ), + arch: arch.to_owned (), + }); + } + } + } + } + } + + findings +} + +#[allow(clippy::collapsible_if)] +fn check_defaults ( + arch: &String , + var_symbol: &str , + info: &AttributeDef , + args: &AnalysisArgs , +) ->Vec <Finding > { +let mut findings =Vec ::new (); +let mut seen_conditions =HashSet ::new (); +let mut seen_values =HashSet ::new (); +let mut already_unconditional =false ; + +for defaultin &info.kconfig_defaults { +let val_str = default.expression.to_string (); + +let has_real_condition =match &default.r#if { +Some (cond) => { +let cond_str = cond.to_string (); + !cond_str.is_empty () + } +None =>false , + }; + +let is_value_dup =if has_real_condition { +is_duplicate (&mut seen_values, val_str.clone ()) + }else { +false + }; + +if already_unconditional && args.is_enabled (Check ::DeadDefault ) { + findings.push (Finding { + severity:Severity ::Warning , + check:Check ::DeadDefault , + symbol:Some (var_symbol.to_owned ()), + message:format! ("dead default of {}" , val_str), + arch: arch.to_owned (), + }); + } + +if args.is_enabled (Check ::DuplicateDefaultValue ) { +if default.r#if.is_some () && is_value_dup { + findings.push (Finding { + severity:Severity ::Style , + check:Check ::DuplicateDefaultValue , + symbol:Some (var_symbol.to_owned ()), + message:format! ( +"duplicate default value of {}; consider combining the conditions with a logical-or: ||" , + val_str + ), + arch: arch.to_owned (), + }); + } + } + +match &default.r#if { +Some (cond) => { +if is_duplicate (&mut seen_conditions, cond.to_string ()) { +if is_value_dup { +if args.is_enabled (Check ::DuplicateDefault ) { + findings.push (Finding { + severity:Severity ::Warning , + check:Check ::DuplicateDefault , + symbol:Some (var_symbol.to_owned ()), + message:format! ("duplicate default condition of {:?}" , cond), + arch: arch.to_owned (), + }); + } + }else { +if args.is_enabled (Check ::DeadDefault ) { + findings.push (Finding { + severity:Severity ::Warning , + check:Check ::DeadDefault , + symbol:Some (var_symbol.to_owned ()), + message:format! ("dead default of {}" , val_str), + arch: arch.to_owned (), + }); + } + } + } + } +None => { + already_unconditional =true ; + } + } + } + + findings +}scripts/kconfirm/kconfirm-lib/src/curl_ffi.rs new file +182
-0@@ -0,0 +1,182 @@ +// SPDX-License-Identifier: GPL-2.0-only +use core::ffi::c_void; +use std::ffi::CStr ; +use std::ffi::CString ; +use std::os::raw::c_char; +use std::os::raw::c_int; +use std::os::raw::c_long; +use std::sync::OnceLock ; + +static CURL_INIT :OnceLock <()> =OnceLock ::new (); + +#[repr( C )] +pub struct CURL { + _private: [u8 ;0 ], +} + +type CURLcode =c_int ; +type CURLoption =u32 ; +type CURLINFO =u32 ; + +const CURLE_OK :CURLcode =0 ; + +const CURL_GLOBAL_DEFAULT :c_long =3 ; + +const CURLOPT_URL :CURLoption =10002 ; +const CURLOPT_NOBODY :CURLoption =44 ; +const CURLOPT_TIMEOUT :CURLoption =13 ; +const CURLOPT_FOLLOWLOCATION :CURLoption =52 ; +const CURLOPT_USERAGENT :CURLoption =10018 ; +const CURLOPT_HEADERFUNCTION :CURLoption =20079 ; +const CURLOPT_HEADERDATA :CURLoption =10029 ; + +const CURLINFO_RESPONSE_CODE :CURLINFO =0x200002 ; + +#[link(name = "curl" )] +unsafe extern "C" {} + +unsafe extern "C" { +fn curl_global_init (flags:c_long ) ->CURLcode ; + +fn curl_easy_init () -> *mut CURL ; + +fn curl_easy_cleanup (handle: *mut CURL ); + +fn curl_easy_perform (handle: *mut CURL ) ->CURLcode ; + +fn curl_easy_strerror (code:CURLcode ) -> *const c_char ; + +fn curl_easy_setopt (handle: *mut CURL , option:CURLoption , ...) ->CURLcode ; + +fn curl_easy_getinfo (handle: *mut CURL , info:CURLINFO , ...) ->CURLcode ; +} + +fn init_curl () { +CURL_INIT .get_or_init (||unsafe { +curl_global_init (CURL_GLOBAL_DEFAULT ); + }); +} + +fn curl_error (code:CURLcode ) ->String { +unsafe { +let ptr =curl_easy_strerror (code); + +if ptr.is_null () { +return format! ("curl error {}" , code); + } + +CStr ::from_ptr (ptr).to_string_lossy ().into_owned () + } +} + +struct HeaderCapture { + location:Option <String >, +} + +extern "C" fn header_callback ( + buffer: *mut c_char , + size:usize , + nitems:usize , + userdata: *mut c_void , +) ->usize { +let total = size * nitems; + +unsafe { +let bytes = std::slice::from_raw_parts (bufferas *const u8 , total); + +if let Ok (header) = std::str::from_utf8 (bytes) { +let lower = header.to_ascii_lowercase (); + +if lower.starts_with ("location:" ) { +if let Some ((_, value)) = header.split_once (':' ) { +let capture = &mut *(userdataas *mut HeaderCapture ); + + capture.location =Some (value.trim ().to_string ()); + } + } + } + } + + total +} + +#[derive( Debug )] +pub struct HttpResponse { +pub response_code:u16 , +pub location:Option <String >, +} + +pub fn head_request (url: &str ) ->Result <HttpResponse ,String > { +init_curl (); + +unsafe { +let curl =curl_easy_init (); + +if curl.is_null () { +return Err ("curl_easy_init failed" .into ()); + } + +let url_c =match CString ::new (url) { +Ok (v) => v, +Err (_) => { +curl_easy_cleanup (curl); + +return Err ("invalid URL" .into ()); + } + }; + +let ua_c =CString ::new ("link-checker/1.0" ).unwrap (); + +let mut headers =HeaderCapture { location:None }; + +macro_rules! setopt { + ($opt:expr, $val:expr) => {{ +let rc =curl_easy_setopt (curl, $opt, $val); + +if rc !=CURLE_OK { +curl_easy_cleanup (curl); + +return Err (curl_error (rc)); + } + }}; + } + +setopt! (CURLOPT_URL , url_c.as_ptr ()); +setopt! (CURLOPT_NOBODY ,1 as c_long ); +setopt! (CURLOPT_TIMEOUT ,10 as c_long ); +setopt! (CURLOPT_FOLLOWLOCATION ,0 as c_long ); +setopt! (CURLOPT_USERAGENT , ua_c.as_ptr ()); + +setopt! ( +CURLOPT_HEADERFUNCTION , + header_callbackas extern "C" fn (_, _, _, _) ->_ + ); + +setopt! (CURLOPT_HEADERDATA , &mut headersas *mut _ as *mut c_void ); + +let rc =curl_easy_perform (curl); + +if rc !=CURLE_OK { +curl_easy_cleanup (curl); + +return Err (curl_error (rc)); + } + +let mut response_code:c_long =0 ; + +let rc =curl_easy_getinfo (curl,CURLINFO_RESPONSE_CODE , &mut response_code); + +if rc !=CURLE_OK { +curl_easy_cleanup (curl); + +return Err (curl_error (rc)); + } + +curl_easy_cleanup (curl); + +Ok (HttpResponse { + response_code: response_codeas u16 , + location: headers.location, + }) + } +}scripts/kconfirm/kconfirm-lib/src/dead_links.rs new file +138
-0@@ -0,0 +1,138 @@ +// SPDX-License-Identifier: GPL-2.0-only +use crate ::curl_ffi::head_request; +use std::collections::HashSet ; + +#[derive( PartialEq , Debug )] +pub enum LinkStatus { +Ok , +ProbablyBlocked , +Redirected (String ), +NotFound , +ServerError , +Unreachable (String ), +UnsupportedScheme (String ), +} + +pub fn check_link (url: &str ) ->LinkStatus { +if let Some (scheme) = url.split ("://" ).next () { +match scheme { +"http" |"https" =>return check_http (url), + +"git" |"ftp" => { +return LinkStatus ::UnsupportedScheme (scheme.into ()); + } + + _ => { +return LinkStatus ::UnsupportedScheme (scheme.into ()); + } + } + } + +LinkStatus ::Unreachable ("invalid URL" .into ()) +} + +fn check_http (url: &str ) ->LinkStatus { +let response =match head_request (url) { +Ok (r) => r, +Err (e) =>return LinkStatus ::Unreachable (e), + }; + +match response.response_code { +200 ..=299 =>LinkStatus ::Ok , + +301 |302 =>LinkStatus ::Redirected (response.location.unwrap_or_else (||"unknown" .into ())), + +403 |429 =>LinkStatus ::ProbablyBlocked , + +404 =>LinkStatus ::NotFound , + +500 ..=599 =>LinkStatus ::ServerError , + + _ =>LinkStatus ::ProbablyBlocked , + } +} + +pub fn find_links (text: &str ) ->Vec <String > { +fn is_scheme_char (c:u8 ) ->bool { + c.is_ascii_alphanumeric () ||matches! (c,b'+' |b'-' |b'.' ) + } + +fn is_url_terminator (c:u8 ) ->bool { + c.is_ascii_whitespace () + ||matches! ( + c, +b'"' |b'\'' |b'<' |b'>' |b'(' |b')' |b'[' |b']' |b'{' |b'}' + ) + } + +let bytes = text.as_bytes (); + +let mut links =Vec ::new (); +let mut seen =HashSet ::new (); + +let mut i =0 ; + +while i +3 < bytes.len () { +if bytes[i] ==b':' && bytes[i +1 ] ==b'/' && bytes[i +2 ] ==b'/' { +// walk backward to find scheme start +let mut start = i; + +while start >0 &&is_scheme_char (bytes[start -1 ]) { + start -=1 ; + } + +// require non-empty scheme +if start == i { + i +=3 ; +continue ; + } + +// first char must be alphabetic +if !bytes[start].is_ascii_alphabetic () { + i +=3 ; +continue ; + } + +// walk forward to url end +let mut end = i +3 ; + +while end < bytes.len () && !is_url_terminator (bytes[end]) { + end +=1 ; + } + +let mut url = &text[start..end]; + +// trim trailing punctuation + url = url.trim_end_matches (&['.' ,',' ,';' ,':' ,'!' ,'?' ][..]); + +// trim unmatched markdown +while let Some (last) = url.chars ().last () { +let trim =match last { +')' => url.matches ('(' ).count () < url.matches (')' ).count (), + +']' => url.matches ('[' ).count () < url.matches (']' ).count (), + +'}' => url.matches ('{' ).count () < url.matches ('}' ).count (), + + _ =>false , + }; + +if trim { + url = &url[..url.len () - last.len_utf8 ()]; + }else { +break ; + } + } + +if seen.insert (url) { + links.push (url.to_string ()); + } + + i = end; + }else { + i +=1 ; + } + } + + links +}scripts/kconfirm/kconfirm-lib/src/lib.rs new file +62
-0@@ -0,0 +1,62 @@ +// SPDX-License-Identifier: GPL-2.0-only +use analyze::analyze; +pub use checks::AnalysisArgs ; +pub use checks::Check ; +pub use checks::check_select_visible; +pub use checks::check_variable_info; +use nom_kconfig::Entry ; +use nom_kconfig::KconfigInput ; +use nom_kconfig::parse_kconfig; +use output::*; +use symbol_table::*; +mod analyze; +mod checks; +mod curl_ffi; +mod dead_links; +pub mod output; +pub mod symbol_table; + +pub fn check_kconfig ( + args:AnalysisArgs , + kconfig_files:Vec <(String ,KconfigInput )>, +) ->Vec <Finding > { +let mut findings =Vec ::new (); +let mut symbol_table =SymbolTable ::new (); + +for (arch_config_option, kconfig_file)in kconfig_files { +match parse_kconfig (kconfig_file) { +Ok (parsed) => { +let entries:Vec <Entry > = parsed.1 .entries; + findings.extend (analyze ( + &args, + &mut symbol_table, + arch_config_option, + entries, + )); + } +Err (e) => { + findings.push (Finding { + severity:Severity ::Fatal , + check:Check ::FailedParse , + symbol:None , + message:format! ("Failed to parse kconfig, error is: {}" , e), + arch: arch_config_option, + }); + } + } + } + +for (var_symbol, type_info)in &symbol_table.raw { +for (arch_specific, redefinitions)in &type_info.attribute_defs { +for (_definition_condition, info)in redefinitions { + findings.extend (check_variable_info (&args, var_symbol, arch_specific, info)); + } + } + +if args.is_enabled (Check ::SelectVisible ) { + findings.extend (check_select_visible (var_symbol, type_info)); + } + } + + findings +}scripts/kconfirm/kconfirm-lib/src/output.rs new file +111
-0@@ -0,0 +1,111 @@ +// SPDX-License-Identifier: GPL-2.0-only +use crate ::Check ; +use std::fmt; + +#[derive( Debug , PartialEq , Eq , PartialOrd , Ord )] +pub enum Severity { +Fatal , +Error ,// will be used for known bugs, e.g. unmet dependencies +Warning , +Style , +} + +#[derive( Debug )] +pub struct Finding { +pub severity:Severity , +pub check:Check , +pub symbol:Option <String >, +pub message:String , +pub arch:String , +} + +impl Finding { +fn fmt_with_arches (&self, f: &mut fmt::Formatter , arches: &[&str ]) -> fmt::Result { +let arch_part =if arches.is_empty () { +String ::new () + }else { +format! (" [{}]" , arches.join (", " )) + }; + +match &self.symbol { +Some (s) =>write! ( + f, +"{} [{}]{} config {}: {}" , + self.severity, + self.check.as_str (), + arch_part, + s, + self.message + ), +None =>write! ( + f, +"{} [{}]{} {}" , + self.severity, + self.check.as_str (), + arch_part, + self.message + ), + } + } +} + +impl fmt::Display for Finding { +fn fmt (&self, f: &mut fmt::Formatter ) -> fmt::Result { + self.fmt_with_arches (f, &[]) + } +} + +pub fn print_findings (mut findings:Vec <Finding >) { + findings.sort_by (|a, b| { + ( + &a.severity, + a.check.as_str (), + &a.symbol, + &a.message, + &a.arch, + ) + .cmp (&( + &b.severity, + b.check.as_str (), + &b.symbol, + &b.message, + &b.arch, + )) + }); + +for groupin findings.chunk_by (|a, b| { + a.severity == b.severity + && a.check.as_str () == b.check.as_str () + && a.symbol == b.symbol + && a.message == b.message + }) { +let head = &group[0 ]; + +let mut arches:Vec <&str > =Vec ::new (); +for fin group { +if arches.last () !=Some (&f.arch.as_str ()) { + arches.push (&f.arch); + } + } + +// Use a small wrapper so we can call our custom formatter via println! +struct Wrap <'a>(&'aFinding , &'a [&'astr ]); +impl fmt::Display for Wrap <'_> { +fn fmt (&self, f: &mut fmt::Formatter ) -> fmt::Result { + self.0 .fmt_with_arches (f, self.1 ) + } + } +println! ("{}" ,Wrap (head, &arches)); + } +} + +impl fmt::Display for Severity { +fn fmt (&self, f: &mut fmt::Formatter ) -> fmt::Result { +match self { +Severity ::Fatal =>write! (f,"FATAL " ), +Severity ::Error =>write! (f,"ERROR " ), +Severity ::Warning =>write! (f,"WARNING" ), +Severity ::Style =>write! (f,"STYLE " ), + } + } +}scripts/kconfirm/kconfirm-lib/src/symbol_table.rs new file +223
-0@@ -0,0 +1,223 @@ +// SPDX-License-Identifier: GPL-2.0-only +use nom_kconfig::attribute::DefaultAttribute ; +use nom_kconfig::attribute::Expression ; +use nom_kconfig::attribute::OrExpression ; +use nom_kconfig::attribute::Range ; +use nom_kconfig::attribute::r#type::Type ; +use std::collections::HashMap ; +use std::collections::hash_map; + +type KconfigSymbol =String ; +type Arch =String ; +type Cond =Option <Expression >; + +// NOTE: we cannot add these elements to the solver until we've processed all variables, +// because we need to know all of the selectors. +#[derive( Debug , Clone )] +pub struct TypeInfo { +pub kconfig_type:Option <Type >,// 'None' when we don't know the type (e.g. if it's a dangling reference) + +// maps the selector to an (ARCH, select_cond) +// - if the ARCH is None, then it's not arch-specific +// if the select_cond is None, then it's unconditional +pub selected_by:HashMap <KconfigSymbol ,Vec <(Arch ,Cond )>>,// .0 only selects it when .1 is true. + +// there is one of these per entry (each entry expected to have a different definedness condition) +// maps architecture option name (or none if not arch-specific) to: +// [([condition], config definition)] +// - NOTE: there can be multiple partial definitions under the same condition, or mutually-exclusive conditions, or a subset condition. +pub attribute_defs:HashMap <Arch ,Vec <(Vec <Expression >,AttributeDef )>>,// the innermost `Vec<Expression>` represents each nested condition that was reached (we will eventually need to AND them all) +} + +// everything is a vector because we may encounter multiple over time, +// so we won't know until the end what the condition is. +#[derive( Debug , Clone )] +pub struct AttributeDef { +pub kconfig_dependencies:Vec <OrExpression >, +pub kconfig_ranges:Vec <Range >, +pub kconfig_defaults:Vec <DefaultAttribute >, +pub visibility:Vec <Option <OrExpression >>, +pub selects:Vec <(KconfigSymbol ,Cond )>, +pub implies:Vec <(KconfigSymbol ,Cond )>, +} + +impl TypeInfo { +fn new_empty () ->Self { +Self { + kconfig_type:None , + selected_by:HashMap ::new (), + attribute_defs:HashMap ::new (), + } + } + +// TODO: we should consider having separate functions for: +// 1. merge-inserting a redef of attributes (NOTE: the type definition is actually part of the redef, but we aren't handling type-redefinitions for now) +// 2. selectors +fn insert ( + &mut self, + kconfig_type:Option <Type >, + raw_constraints:Vec <OrExpression >, + kconfig_ranges:Vec <Range >, + kconfig_defaults:Vec <DefaultAttribute >, + visibility:Vec <Option <OrExpression >>, + arch:String , + definition_condition:Vec <OrExpression >, + selected_by:Option <(KconfigSymbol ,Cond )>, + selects:Vec <(KconfigSymbol ,Cond )>, + implies:Vec <(KconfigSymbol ,Cond )>, + ) { +// type merge +match (&self.kconfig_type, &kconfig_type) { + (None ,Some (_)) => self.kconfig_type = kconfig_type.clone (), + (Some (_),Some (new))if Some (new) != self.kconfig_type.as_ref () => { +// TODO: not doing anything with redefined types yet. +// later, we will want to consider e.g. bool/def_bool the same type (and possibly int/hex?) but not bool/tristate, so we need to build out typechecking. + } + _ => {} + } + +// selected_by merge +if let Some (sb) = selected_by { +merge_selected_by (&mut self.selected_by, arch.clone (), sb); + } + +// variable_info merge: +// we only want to add an attribute redefinition if the things in the attribute def aren't empty +// (the visibility is just additional info to capture) +if (&kconfig_type).is_some ()// we need to ensure that we have an empty definition here if the config option had a type definition + || !raw_constraints.is_empty () + || !kconfig_ranges.is_empty () + || !kconfig_defaults.is_empty () + || !selects.is_empty () + || !implies.is_empty () + { +insert_variable_info ( + &mut self.attribute_defs, + arch, + definition_condition, +AttributeDef { + kconfig_dependencies: raw_constraints, + kconfig_ranges, + kconfig_defaults, + visibility, + selects, + implies, + }, + ); + } + } +} + +// the visibility and the dependencies will each need to be AND'd (separately) +// the defaults should each be handled separately. +pub struct ChoiceData { +//pub inner_vars: Vec<String>, +pub arch:Arch , +pub visibility:Cond , +pub dependencies:Vec <OrExpression >,// this is the menu's dependencies (and inherited dependencies from the file) +pub defaults:Vec <DefaultAttribute >,// these are each of the conditional defaults for the choice +} + +// NOTE: it might be better if TypeInfo is an enum with a single value, +// e.g. Unsolved(kconfig_raw) and Solved(z3_ast) +pub struct SymbolTable { +pub raw:HashMap <KconfigSymbol ,TypeInfo >, +pub choices:Vec <ChoiceData >, +pub modules_option:Option <KconfigSymbol >,// None until we find the modules attribute in exactly 1 config option +} + +impl SymbolTable { +pub fn new () ->Self { +SymbolTable { + raw:HashMap ::new (), + choices:Vec ::new (), + modules_option:None , + } + } + +pub fn from_parts ( + raw:HashMap <KconfigSymbol ,TypeInfo >, + choices:Vec <ChoiceData >, + modules_option:Option <KconfigSymbol >, + ) ->Self { +SymbolTable { + raw, + choices, + modules_option, + } + } + +pub fn merge_insert_new_solved ( + &mut self, + var:KconfigSymbol , + kconfig_type:Option <Type >, + raw_constraints:Vec <OrExpression >, + kconfig_ranges:Vec <Range >, + kconfig_defaults:Vec <DefaultAttribute >, + visibility:Vec <Option <OrExpression >>, + arch:Arch , + definition_condition:Vec <OrExpression >, + selected_by:Option <(KconfigSymbol ,Cond )>, + selects:Vec <(KconfigSymbol ,Cond )>, + implies:Vec <(KconfigSymbol ,Cond )>, + ) { +let entry = self.raw.entry (var.clone ()); + +match entry { + hash_map::Entry ::Vacant (v) => { +let mut t =TypeInfo ::new_empty (); + t.insert ( + kconfig_type, + raw_constraints, + kconfig_ranges, + kconfig_defaults, + visibility, + arch, + definition_condition, + selected_by, + selects, + implies, + ); + v.insert (t); + } + + hash_map::Entry ::Occupied (mut o) => { +let t = o.get_mut (); + + t.insert ( + kconfig_type, + raw_constraints, + kconfig_ranges, + kconfig_defaults, + visibility, + arch, + definition_condition, + selected_by, + selects, + implies, + ); + } + } + } +} + +fn merge_selected_by ( + map: &mut HashMap <String ,Vec <(Arch ,Cond )>>, + arch:Arch , + selected_by: (KconfigSymbol ,Cond ), +) { + map.entry (selected_by.0 ) + .or_default ()// empty vec + .push ((arch, selected_by.1 )); +} + +fn insert_variable_info ( + map: &mut HashMap <Arch ,Vec <(Vec <Expression >,AttributeDef )>>, + arch:Arch , + definition_condition:Vec <Expression >, + info:AttributeDef , +) { + map.entry (arch) + .or_default ()// empty vec + .push ((definition_condition, info)); +}scripts/kconfirm/kconfirm-linux/Cargo.toml new file +10
-0@@ -0,0 +1,10 @@ +# SPDX-License-Identifier: GPL-2.0 +[package] +name = "kconfirm-linux" +version = "0.10.0" +edition = "2024" +rust-version.workspace = true + +[dependencies] +kconfirm-lib = { path = "../kconfirm-lib" } +nom-kconfig = { workspace = true }scripts/kconfirm/kconfirm-linux/src/getopt_ffi.rs new file +36
-0@@ -0,0 +1,99 @@ +// SPDX-License-Identifier: GPL-2.0-only +use std::env; +use std::ffi::CStr ; +use std::ffi::CString ; +use std::os::raw::c_char; +use std::os::raw::c_int; +use std::ptr; + +pub const REQUIRED_ARGUMENT :c_int =1 ; + +#[repr( C )] +pub struct option { +pub name: *const c_char , +pub has_arg:c_int , +pub flag: *mut c_int , +pub val:c_int , +} + +#[link(name = "c" )] +unsafe extern "C" { +fn getopt_long ( + argc:c_int , + argv: *mut *mut c_char , + optstring: *const c_char , + longopts: *const option , + longindex: *mut c_int , + ) ->c_int ; + +static mut optarg: *mut c_char ; +static mut optind:c_int ; +} + +pub struct Getopt { + _cstrings:Vec <CString >, + argv:Vec <*mut c_char >, + argc:c_int ,
scripts/kconfirm/kconfirm-linux/src/getopt_ffi.rs line 46
Quoted 10 lines
scripts/kconfirm/kconfirm-linux/src/getopt_ffi.rs new file +10
-0+} + +impl Getopt { +pub fn new () ->Self { +let raw_args:Vec <String > = env::args ().collect (); + +let cstrings:Vec <CString > = raw_args + .iter () + .map (|s|CString ::new (s.as_str ()).unwrap ()) + .collect ();
This panics if the arguments aren't valid UTF-8. Using env::args_os() might be better. getopt_long() doesn't care if the arguments are valid UTF-8 or not.
Quoted 29 lines
scripts/kconfirm/kconfirm-linux/src/getopt_ffi.rs new file +29
-0+let mut argv:Vec <*mut c_char > = + cstrings.iter ().map (|s| s.as_ptr ()as *mut c_char ).collect (); + + argv.push (ptr::null_mut ()); + +let argc = (argv.len () -1 )as c_int ; + +Self { + _cstrings: cstrings, + argv, + argc, + } + } + +pub fn reset (&mut self) { +unsafe { + optind =1 ; + } + } + +pub fn next ( + &mut self, + optstring: &CStr , + longopts: &[option ], + ) ->Option <Result <(char ,Option <String >),String >> { +unsafe { +let c =getopt_long ( + self.argc, + self.argv.as_mut_ptr(),
scripts/kconfirm/kconfirm-linux/src/getopt_ffi.rs line 86
Quoted 10 lines
scripts/kconfirm/kconfirm-linux/src/getopt_ffi.rs new file +10
-0+ optstring.as_ptr(), + longopts.as_ptr(), + ptr::null_mut(), + ); + +if c == -1 { +return None ; + } + +if c =='?' as c_int {
if c == c_int::from(b'?') {Might be a bit clearer.
scripts/kconfirm/kconfirm-linux/src/getopt_ffi.rs line 87
Quoted 1 line
scripts/kconfirm/kconfirm-linux/src/getopt_ffi.rs new file +1
-0+return Some (Err ("invalid argument" .into ()));
You can just std::process::exit() here. getopt_long() will have printed an error message. This assumes that the
scripts/kconfirm/kconfirm-linux/src/getopt_ffi.rs line 94
Quoted 7 lines
scripts/kconfirm/kconfirm-linux/src/getopt_ffi.rs new file +7
-0+ } + +let arg =if optarg.is_null () { +None + }else { +Some (CStr ::from_ptr (optarg).to_string_lossy ().into_owned ()) + };
I'd prefer to return an OsString here.
scripts/kconfirm/kconfirm-linux/src/getopt_ffi.rs line 99
Quoted 4 lines
scripts/kconfirm/kconfirm-linux/src/getopt_ffi.rs new file +4
-0+Some (Ok ((cas u8 as char , arg))) + } + } +}
I think it is simpler to just inline all of this code into its single call-site. The safety of the code is obvious in context, and you can avoid checking for impossible errors. For instance, since all of the options have required arguments, it really is safe to dereference optarg without any null check.
Yes, this is C with Rust syntax, but it's *simple* C with Rust syntax.
As an aside, I don't know if you want to accept abbreviated long options. Most tools using getopt_long() allow them. If you don't, I can show you how to reject them.
Signature · 2 lines
Sincerely, Demi Marie Obenour (she/her/hers)
Re: [RFC PATCH v3 1/3] scripts: add kconfirm
Quoted 6 lines · Demi Marie Obenour
I think it is simpler to just inline all of this code into its single call-site. The safety of the code is obvious in context, and you can avoid checking for impossible errors. For instance, since all of the options have required arguments, it really is safe to dereference optarg without any null check.
If we are going to have unsafe code, then let's please build safe abstractions wherever possible, just like we do elsewhere. We should also write // SAFETY comments and enable the lints that catch that etc., just like elsewhere too.
(This is not to say we should use getopt instead of something like clap -- as soon as we start using cargo vendor, then it makes sense to at least consider having a set of vetted, well-known crates to write Rust tools in-tree, as I mentioned in v1.)
Cheers,
Miguel
Re: [RFC PATCH v3 1/3] scripts: add kconfirm
Quoted 14 lines · Miguel Ojeda
Quoted 6 lines · Demi Marie Obenour
I think it is simpler to just inline all of this code into its single call-site. The safety of the code is obvious in context, and you can avoid checking for impossible errors. For instance, since all of the options have required arguments, it really is safe to dereference optarg without any null check.
If we are going to have unsafe code, then let's please build safe abstractions wherever possible, just like we do elsewhere. We should also write
// SAFETYcomments and enable the lints that catch that etc., just like elsewhere too.
Quoted 4 lines
(This is not to say we should use
getoptinstead of something likeclap-- as soon as we start usingcargo vendor, then it makes sense to at least consider having a set of vetted, well-known crates to write Rust tools in-tree, as I mentioned in v1.)
I was hoping for Linux to avoid the Rust trend of downloading tons of third-party crates, with all the supply-chain risks that entails. Hence the idea of using getopt and system C libraries.
Signature · 2 lines
Sincerely, Demi Marie Obenour (she/her/hers)
Re: [RFC PATCH v3 1/3] scripts: add kconfirm
Quoted 3 lines · Demi Marie Obenour
I was hoping for Linux to avoid the Rust trend of downloading tons of third-party crates, with all the supply-chain risks that entails.
I completely agree -- it is why I said a well-known, vetted set of crates.
That is, we should decide on e.g. a single CLI arg parser, a single logger, etc. for most of our tools, and ideally they should be well-known crates (ideally already trusted via use in the compiler itself).
Moreover, they should be pinned with --locked or similar (like we already recommend for bindgen-cli), so that we only ever use something that matches the hash in the lockfile that would be committed in the tree.
Cheers,
Miguel
Re: [RFC PATCH v3 1/3] scripts: add kconfirm
Quoted 13 lines · Miguel Ojeda
Quoted 3 lines · Demi Marie Obenour
I was hoping for Linux to avoid the Rust trend of downloading tons of third-party crates, with all the supply-chain risks that entails.
I completely agree -- it is why I said a well-known, vetted set of crates.
That is, we should decide on e.g. a single CLI arg parser, a single logger, etc. for most of our tools, and ideally they should be well-known crates (ideally already trusted via use in the compiler itself).
Quoted 4 lines
Moreover, they should be pinned with
--lockedor similar (like we already recommend forbindgen-cli), so that we only ever use something that matches the hash in the lockfile that would be committed in the tree.
What about dependencies that are normally shipped by the distros along with the rust compiler? Would it be possible to allow a range of version that matches the ones that are present on common distros like we do with C libraries, or would that cause more problems than it solves?
Arnd
Re: [RFC PATCH v3 1/3] scripts: add kconfirm
Quoted 5 lines · Arnd Bergmann
What about dependencies that are normally shipped by the distros along with the rust compiler? Would it be possible to allow a range of version that matches the ones that are present on common distros like we do with C libraries, or would that cause more problems than it solves?
Hi Arnd,
Yes it's something that I would like to enable, though we first need to wait for all of kconfirm's dependencies to be available for these distributions.
I've filed a GitHub issue for this in the repo of 'nom-kconfig':
https://github.com/Mcdostone/nom-kconfig/issues/149#issuecomment-4480419622
Also note that the author of that library is CC'd on these emails:
Yann Prono <[email protected]>
I will need to do some testing once all is available, but as far as I can tell, this would not create many additional problems, though we would still need to provide crates.io as a source for distributions that do not package Rust libraries.
- Julian Braha
Gary Guo
reviewerRe: [RFC PATCH v3 1/3] scripts: add kconfirm
In reply to Demi Marie ObenourQuoted 15 lines · Demi Marie Obenour
Quoted 14 lines · Miguel Ojeda
Quoted 6 lines · Demi Marie Obenour
I think it is simpler to just inline all of this code into its single call-site. The safety of the code is obvious in context, and you can avoid checking for impossible errors. For instance, since all of the options have required arguments, it really is safe to dereference optarg without any null check.
If we are going to have unsafe code, then let's please build safe abstractions wherever possible, just like we do elsewhere. We should also write
// SAFETYcomments and enable the lints that catch that etc., just like elsewhere too.
Quoted 8 lines
Quoted 4 lines
(This is not to say we should use
getoptinstead of something likeclap-- as soon as we start usingcargo vendor, then it makes sense to at least consider having a set of vetted, well-known crates to write Rust tools in-tree, as I mentioned in v1.)I was hoping for Linux to avoid the Rust trend of downloading tons of third-party crates, with all the supply-chain risks that entails. Hence the idea of using getopt and system C libraries.
Well, there're quite a few libraries which are "third-party" but they're blessed and used by the Rust compiler itself (and often maintained by the same group of people behind the Rust project).
https://github.com/rust-lang/rust/blob/main/Cargo.lock
For example, I would trust clap to be used, and I think it's a huge developer-experience improvement compared to getopt.
I think it's not really reasonable to avoid all dependencies, after all, C tools also require a few dependencies, it's just that they're sourced from distro and not crates.io.
I think the only issue is that unlike C libraries, there's no unified ways for distros to ship these libraries (and some distros don't ship individual Rust crates as libraries at all). It used to be the case that you can enable the rustc_private feature and just link against whatever the version that the Rust compiler depends on, but rustc stopped you from doing that a while ago because they don't want people to depend on rustc's private dependencies.
If the concern is with supply chain security, there're mechanisms like cargo-vet
https://github.com/mozilla/cargo-vet which can be used to ensure dependencies to be audited. That plus the hash locking should be sufficient.
Best,
Gary
Re: [RFC PATCH v3 1/3] scripts: add kconfirm
In reply to Julian BrahaQuoted 2 lines · Julian Braha
+CARGO = cargo
Question to Kbuild: would it hurt to hardcore --offline here?
If someone within Make actually ever needs Cargo to fetch something, then they should be very explicit about it (in which case we could have another variable etc.).
Makefile MRPROPER_FILES += include/config include/generated \ · line 1709
Quoted 2 lines
Makefile +1
-1- rust/libpin_init_internal.so rust/libpin_init_internal.dylib + rust/libpin_init_internal.so rust/libpin_init_internal.dylib \
Spurious change?
Quoted 2 lines
+$(TARGET):
+ $(CARGO) run --release --offline -p kconfirm-linux -- --linux-path $(srctree) --enable-arch $(SRCARCH) $(KCONFIRM_ARGS)
This probably does not work in O= builds or in cases where the srctree is read-only (please see my other reply on the docs patch).
scripts/kconfirm/kconfirm-lib/src/analyze.rs line 2
Quoted 2 lines
scripts/kconfirm/kconfirm-lib/src/analyze.rs new file +2
-0+// SPDX-License-Identifier: GPL-2.0-only +use crate ::AnalysisArgs ;
This appears to use a quite different, custom Rust style. If this is going to be developed in-tree, then we should do our best to follow the guidelines:
https://docs.kernel.org/rust/coding-guidelines.html
For instance, a few key points are not followed here:
- Public items are not documented. When they are, they seem to use
comments instead of actual docs. Markdown is not used either.
- No examples, doctests or tests.
- No // SAFETY comments for unsafe code.
Quoted 5 lines
scripts/kconfirm/kconfirm-lib/src/curl_ffi.rs new file +5
-0+unsafe extern "C" { +fn curl_global_init (flags:c_long ) ->CURLcode ; + +fn curl_easy_init () -> *mut CURL ; +
scripts/kconfirm/kconfirm-lib/src/curl_ffi.rs line 52
Quoted 10 lines
scripts/kconfirm/kconfirm-lib/src/curl_ffi.rs new file +10
-0+fn curl_easy_cleanup (handle: *mut CURL ); + +fn curl_easy_perform (handle: *mut CURL ) ->CURLcode ; + +fn curl_easy_strerror (code:CURLcode ) -> *const c_char ; + +fn curl_easy_setopt (handle: *mut CURL , option:CURLoption , ...) ->CURLcode ; + +fn curl_easy_getinfo (handle: *mut CURL , info:CURLINFO , ...) ->CURLcode ; +}
I like minimizing dependencies, but since we require vendored dependencies anyway, then it may be simpler to use a common ("standard") Rust dependency for these things. Then we can all agree on a particular one and use that when the same need arises.
In fact, it seems like FFI here and in the other file is the only source of unsafe code, no? We could perhaps even forbid it otherwise.
If we truly want to minimize dependencies even if we have vendored ones, then we could call into the curl CLI instead, just like we e.g. call into bindgen instead of using its library.
Thanks!
Cheers,
Miguel
Re: [RFC PATCH v3 1/3] scripts: add kconfirm
Quoted 51 lines · Miguel Ojeda
Quoted 2 lines · Julian Braha
+CARGO = cargo
Question to
Kbuild: would it hurt to hardcore--offlinehere?If someone within Make actually ever needs Cargo to fetch something, then they should be very explicit about it (in which case we could have another variable etc.).
Makefile MRPROPER_FILES += include/config include/generated \ · line 1709
Quoted 2 lines
Makefile +1
-1- rust/libpin_init_internal.so rust/libpin_init_internal.dylib + rust/libpin_init_internal.so rust/libpin_init_internal.dylib \Spurious change?
Quoted 2 lines
+$(TARGET):
+ $(CARGO) run --release --offline -p kconfirm-linux -- --linux-path $(srctree) --enable-arch $(SRCARCH) $(KCONFIRM_ARGS)This probably does not work in
O=builds or in cases where thesrctreeis read-only (please see my other reply on the docs patch).scripts/kconfirm/kconfirm-lib/src/analyze.rs line 2
Quoted 2 lines
scripts/kconfirm/kconfirm-lib/src/analyze.rs new file +2
-0+// SPDX-License-Identifier: GPL-2.0-only +use crate ::AnalysisArgs ;This appears to use a quite different, custom Rust style. If this is going to be developed in-tree, then we should do our best to follow the guidelines:
https://docs.kernel.org/rust/coding-guidelines.html
For instance, a few key points are not followed here:
- Public items are not documented. When they are, they seem to use
comments instead of actual docs. Markdown is not used either.- No examples, doctests or tests.
- No
// SAFETYcomments for unsafe code.Quoted 12 lines
scripts/kconfirm/kconfirm-lib/src/curl_ffi.rs new file +12
-0+unsafe extern "C" { +fn curl_global_init (flags:c_long ) ->CURLcode ; + +fn curl_easy_init () -> *mut CURL ; + +fn curl_easy_cleanup (handle: *mut CURL ); + +fn curl_easy_perform (handle: *mut CURL ) ->CURLcode ; + +fn curl_easy_strerror (code:CURLcode ) -> *const c_char ; + +fn curl_easy_setopt (handle: *mut CURL , option:CURLoption , ...) ->CURLcode ;
Quoted 8 lines
scripts/kconfirm/kconfirm-lib/src/curl_ffi.rs line 52
Quoted 3 lines
scripts/kconfirm/kconfirm-lib/src/curl_ffi.rs new file +3
-0+ +fn curl_easy_getinfo (handle: *mut CURL , info:CURLINFO , ...) ->CURLcode ; +}I like minimizing dependencies, but since we require vendored dependencies anyway, then it may be simpler to use a common ("standard") Rust dependency for these things. Then we can all agree on a particular one and use that when the same need arises.
Using a ton of vendored dependencies would make for unreviewable patches. It's also the kind of thing that gives distro packagers headaches.
Quoted 3 lines
In fact, it seems like FFI here and in the other file is the only source of
unsafecode, no? We could perhaps evenforbidit otherwise.
I don't want to pull in a huge library like clap. A simple getopt_long implementation could also be an option.
Quoted 3 lines
If we truly want to minimize dependencies even if we have vendored ones, then we could call into the
curlCLI instead, just like we e.g. call intobindgeninstead of using its library.
I'm the one who suggested using FFI here and for command-line parsing. The command-line interface would also work.
Signature · 2 lines
Sincerely, Demi Marie Obenour (she/her/hers)
Re: [RFC PATCH v3 1/3] scripts: add kconfirm
Quoted 3 lines · Demi Marie Obenour
Using a ton of vendored dependencies would make for unreviewable patches.
I am referring to cargo vendor here, not to copying in-tree -- we already discussed that in previous versions.
Please note that I was arguing for avoiding actual vendoring in previous versions...
Quoted 2 lines
I'm the one who suggested using FFI here and for command-line parsing. The command-line interface would also work.
Yes, I suggested the CLI in v1, and then you mentioned the library in v2.
Cheers,
Miguel
Re: [RFC PATCH v3 1/3] scripts: add kconfirm
Quoted 8 lines · Miguel Ojeda
Quoted 3 lines · Demi Marie Obenour
Using a ton of vendored dependencies would make for unreviewable patches.
I am referring to
cargo vendorhere, not to copying in-tree -- we already discussed that in previous versions.
That's true. Some distros (Fedora, Debian) don't like that either, but that's a bigger ecosystem-wide concern.
Quoted 7 lines
Please note that I was arguing for avoiding actual vendoring in previous versions...
Quoted 2 lines
I'm the one who suggested using FFI here and for command-line parsing. The command-line interface would also work.
Yes, I suggested the CLI in v1, and then you mentioned the library in v2.
CLI would also work just as well.
Signature · 2 lines
Sincerely, Demi Marie Obenour (she/her/hers)
Re: [RFC PATCH v3 1/3] scripts: add kconfirm
Quoted 3 lines · Demi Marie Obenour
That's true. Some distros (Fedora, Debian) don't like that either, but that's a bigger ecosystem-wide concern.
Do you mean that distributions would like to package this tool on their own? If so, I don't see why packagers would be blocked.
Cheers,
Miguel
Nathan Chancellor
maintainerRe: [RFC PATCH v3 1/3] scripts: add kconfirm
In reply to Miguel OjedaQuoted 9 lines · Miguel Ojeda
Quoted 2 lines · Julian Braha
+CARGO = cargo
Question to
Kbuild: would it hurt to hardcore--offlinehere?If someone within Make actually ever needs Cargo to fetch something, then they should be very explicit about it (in which case we could have another variable etc.).
No, I don't think so. I think there would need to be a very compelling reason for connecting to the network during the build process. Although, we would need to handle someone passing CARGO via the make command line so that '--offline' does not get blown away.
Quoted 4 lines
Makefile MRPROPER_FILES += include/config include/generated \ · line 1709
Quoted 2 lines
Makefile +1
-1- rust/libpin_init_internal.so rust/libpin_init_internal.dylib + rust/libpin_init_internal.so rust/libpin_init_internal.dylib \Spurious change?
Maybe 'scripts/kconfirm' used to be here?
Another thing I just realized: scripts/kconfirm is going to mess with shell autocompletion for some people, as scripts/kc<tab> will currently always complete to scripts/kconfig. Not sure if that will be that big of a deal but I know Linus has complained about that in the past.
Quoted 5 lines
Quoted 2 lines
+$(TARGET):
+ $(CARGO) run --release --offline -p kconfirm-linux -- --linux-path $(srctree) --enable-arch $(SRCARCH) $(KCONFIRM_ARGS)This probably does not work in
O=builds or in cases where thesrctreeis read-only (please see my other reply on the docs patch).
Yeah, it seems like this wants something like '--target-dir $(obj)' or '--target-dir $(objtree)/scripts/kconfirm'? I don't find this to be particularly readable either (I am more used to "build then run" as two separate steps) but maybe that is because I am just not familiar with Rust projects.
Cheers,
Nathan
RFC PATCH v3 2/3 Documentation: add kconfirm
In reply to Julian BrahaAdd usage documentation and a brief description for kconfirm to Documentation/dev-tools/
--- Documentation/dev-tools/index.rst | 1 + Documentation/dev-tools/kconfirm.rst | 222 +++++++++++++++++++++++++++ 2 files changed, 223 insertions(+) create mode 100644 Documentation/dev-tools/kconfirm.rst
Documentation/dev-tools/index.rst +1 -0
@@ -40,3 +40,4 @@ Documentation/process/debugging/index.rst autofdo propeller container + kconfirm
Documentation/dev-tools/kconfirm.rst new file +222 -0
222 lines not on this page · raw message
Signature · 1 line
2.53.0
Re: [RFC PATCH v3 2/3] Documentation: add kconfirm
Documentation/dev-tools/kconfirm.rst line 43
Quoted 3 lines · Julian Braha
Documentation/dev-tools/kconfirm.rst new file +2
-0+kconfirm's Minimum Supported Rust Version (MSRV) is v1.85.0, because +it uses Rust edition 2024, and this is the earliest supported version.
Note: this means it will be the first code within the kernel tree using the new edition.
I think it is fine, since in general no one should be copying code from here to kernel code or vice versa.
(For context for others: code in one edition can have different behavior than in another edition, and thus it is risky to mix them up by mistake).
Documentation/dev-tools/kconfirm.rst line 50
Quoted 3 lines
Documentation/dev-tools/kconfirm.rst new file +3
-0+In ``scripts/kconfirm/`` run the following to download the dependencies:: + + cargo vendor
I am not sure how important this is for scripts/ and/or tools/ (Kbuild may have a policy), but this should probably handle O= builds.
In some cases, the source tree may even be read-only, i.e. we wouldn't be able to create target/ there.
Cheers,
Miguel
Nathan Chancellor
maintainerRe: [RFC PATCH v3 2/3] Documentation: add kconfirm
Quoted 11 lines · Miguel Ojeda
Documentation/dev-tools/kconfirm.rst line 50
Quoted 3 lines · Julian Braha
Documentation/dev-tools/kconfirm.rst new file +3
-0+In ``scripts/kconfirm/`` run the following to download the dependencies:: + + cargo vendorI am not sure how important this is for
scripts/and/ortools/(Kbuildmay have a policy), but this should probably handleO=builds.In some cases, the source tree may even be read-only, i.e. we wouldn't be able to create
target/there.
I guess this is kind of a weird/unique situation. I agree that the files generated by 'cargo run' should absolutely be contained within the build folder; at that point, $(srctree) could be read only and I would consider it rude not to respect the user's choice of build directory. For 'cargo vendor' however, I am not sure. They are source files and I would expect that running 'cargo vendor' would be more considered part of preparing the source tree, rather than the build one (so it should not be read only).
At the same time, it might be safer for dependency updates and internal consistency that they are confined to the build folder. I guess we would only want to remove them with a 'distclean', rather than 'mrproper' or 'clean', in that case, to avoid requiring users to constantly run 'cargo vendor'. It might be more ergonomic for this to be a Kbuild target ('kconfirmvendor'?) so that this could be handled automatically based on the user's build command.
Additionally, can we detect explicitly when dependencies are not properly vendored and error with a more helpful error message? The build command in patch 1 just throws up its hands when the build fails and asks if the dependencies have been set up but if we provided our own vendoring build target, we could add some canary that says we vendored successfully and if that is not present, error before even running the build and say "hey, you need to explicitly run this target before you build".
Signature · 2 lines
Cheers, Nathan
Re: [RFC PATCH v3 2/3] Documentation: add kconfirm
Quoted 9 lines · Nathan Chancellor
I guess this is kind of a weird/unique situation. I agree that the files generated by 'cargo run' should absolutely be contained within the build folder; at that point, $(srctree) could be read only and I would consider it rude not to respect the user's choice of build directory. For 'cargo vendor' however, I am not sure. They are source files and I would expect that running 'cargo vendor' would be more considered part of preparing the source tree, rather than the build one (so it should not be read only).
That would simplify things, yeah. We could always start there and see if someone needs it.
Quoted 7 lines
At the same time, it might be safer for dependency updates and internal consistency that they are confined to the build folder. I guess we would only want to remove them with a 'distclean', rather than 'mrproper' or 'clean', in that case, to avoid requiring users to constantly run 'cargo vendor'. It might be more ergonomic for this to be a
Kbuildtarget ('kconfirmvendor'?) so that this could be handled automatically based on the user's build command.
Yeah, it is a bit painful to not have the usual Kbuild variables/infrastructure around... On the other hand, it is a nice property to know that nothing called via make will ever connect (or need to connect) to the Internet.
Hmm... Perhaps a good middle ground would be having something in the name that makes it obvious it will connect, e.g. fetch like Git? Or, if people feel strongly about the property mentioned, then something like an environment variable that needs to be set to allow it (with a message printed about it if it is not set).
If this were allowed, i.e. if we are OK having things in make that fetch stuff and put it in the build folder (only in certain targets, of course), then we could actually think about doing more things that we didn't so far, such as other setup-like targets, e.g. preparing kernel.org toolchains, setting up a Rust toolchain via rustup (including bindgen etc.), and so on and so forth.
Quoted 8 lines
Additionally, can we detect explicitly when dependencies are not properly vendored and error with a more helpful error message? The build command in patch 1 just throws up its hands when the build fails and asks if the dependencies have been set up but if we provided our own vendoring build target, we could add some canary that says we vendored successfully and if that is not present, error before even running the build and say "hey, you need to explicitly run this target before you build".
+1, good error messages help a lot. Something like rustavailable that prints which particular thing is missing is great (that one even tries to warn about some problematic versions testing for bugs -- hopefully we don't need autoconf... :).
Cheers,
Miguel
RFC PATCH v3 3/3 MAINTAINERS: create entry for kconfirm
In reply to Julian BrahaAdd myself as maintainer of kconfirm.
--- MAINTAINERS | 6 ++++++ 1 file changed, 6 insertions(+)
MAINTAINERS +6 -0
@@ -13824,6 +13824,12 @@ F: Documentation/kbuild/kconfig* F: scripts/Kconfig.include F: scripts/kconfig/ +KCONFIRM +M: Julian Braha <[email protected]> +S: Maintained +F: Documentation/dev-tools/kconfirm.rst +F: scripts/kconfirm/ + KCORE M: Omar Sandoval <[email protected]> L: [email protected]
Signature · 1 line
2.53.0
Quoted 2 lines · Julian Braha
Dependencies will need to first be downloaded from crates.io by running the
cargo vendorcommand inscripts/kconfirm/
Ugh, sorry all. I didn't realize that the linux gitignore blocks all dot files.
So, the scripts/kconfirm/.cargo/config.toml wasn't checked in correctly due to this.
It should have contained the following:
``` [source.crates-io] replace-with = "vendored-sources" [source.vendored-sources] directory = "vendor" ```
And Nicolas, I think this was why v2 didn't compile for you.
- Julian Braha
Quoted 3 lines · Julian Braha
Hi all,
kconfirm has shrunk a lot since v2!
Thanks for dropping so many of the dependencies!
It might be able to shrink it further by using the existing C Kconfig parser. This has the advantage that it ensures kconfirm and Kconfig will interpret the Kconfig files the same way. I'm not sure if that would be too much of a change. That's up to you.
Signature · 2 lines
Sincerely, Demi Marie Obenour (she/her/hers)
Quoted 9 lines · Demi Marie Obenour
Quoted 3 lines
Hi all,
kconfirm has shrunk a lot since v2!
Thanks for dropping so many of the dependencies!
It might be able to shrink it further by using the existing C
Kconfigparser. This has the advantage that it ensures kconfirm andKconfigwill interpret theKconfigfiles the same way. I'm not sure if that would be too much of a change. That's up to you.
Hi Demi,
I did look into the in-tree parser after your suggestion in v2. What I discovered was that this parser performs too much evaluation of the kconfig code during parsing, making it unsuitable for purposes of static analysis:
The in-tree parser doesn't actually output a parse tree that we can traverse and analyze, which is how kconfirm currently works. Instead, semantic actions directly construct the symbol table *during parsing*, with that symbol table being different from ours. I think(?) this makes the overall process faster (which is great for real-world kernel builds), but for static analysis purposes, we really need to preserve as much of the underlying code as we can. For example, we don't even preprocess variables, because this allows us to analyze more regardless of the host and target. (Well, architecture is the one exception there because we need to resolve imports/"source".)
I do want to point out that Yann, the author of kconfirm's parsing library (nom-kconfig) is CC'd on these RFCs and has done an awesome job of supporting the parser and kconfirm's usage of it.
He also helped reduce the number of indirect dependencies pulled in by the parser following your feedback in RFC v2.
- Julian Braha