Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file added .checkpatch-camelcase.git.
Empty file.
21 changes: 21 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# clang-format configuration for Linux kernel code
# Based on Linux kernel coding style
---
BasedOnStyle: LLVM
IndentWidth: 8
UseTab: Always
TabWidth: 8
BreakBeforeBraces: Linux
AllowShortIfStatementsOnASingleLine: false
AllowShortLoopsOnASingleLine: false
AllowShortFunctionsOnASingleLine: None
IndentCaseLabels: false
SortIncludes: false
AlignConsecutiveMacros: true
AlignConsecutiveAssignments: false
AlignTrailingComments: false
ColumnLimit: 80
MaxEmptyLinesToKeep: 1
KeepEmptyLinesAtTheStartOfBlocks: false
SpaceAfterCStyleCast: false
PointerAlignment: Right
13 changes: 13 additions & 0 deletions .cppcheck-suppressions
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Cppcheck suppressions for kernel module code
# Suppress warnings that are false positives or not applicable to kernel code

# Kernel-specific suppressions
missingIncludeSystem
unusedFunction:*test*.c
unmatchedSuppression

# Suppress preprocessor errors in kernel headers (configuration issues)
preprocessorErrorDirective:*/linux/bitops.h
preprocessorErrorDirective:*/linux/*.h
noValidConfiguration:src/elf_det.c
noValidConfiguration:src/elf_det.mod.c
3 changes: 3 additions & 0 deletions .devcontainer/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ RUN apt-get update && apt-get install -y \
bc \
libelf-dev \
libssl-dev \
clang-format \
sparse \
cppcheck \
&& rm -rf /var/lib/apt/lists/*

# Ensure ubuntu user exists and has sudo privileges
Expand Down
2 changes: 1 addition & 1 deletion .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"source=${localEnv:HOME}${localEnv:USERPROFILE}/.ssh,target=/home/ubuntu/.ssh,type=bind,consistency=cached"
],
"features": {},
"postCreateCommand": "bash -lc 'make --version && gcc --version'",
"postCreateCommand": "bash -c 'echo \"Setting up development environment...\" && make --version && gcc --version && clang-format --version && echo \"Installing Git hooks...\" && mkdir -p .git/hooks && if [ -f scripts/pre-commit.sh ]; then cp scripts/pre-commit.sh .git/hooks/pre-commit && chmod +x .git/hooks/pre-commit && echo \"✓ Git pre-commit hook installed\"; fi && echo \"✓ Dev environment ready!\"'",
"customizations": {
"vscode": {
"extensions": [
Expand Down
31 changes: 31 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# EditorConfig is awesome: https://EditorConfig.org

# top-most EditorConfig file
root = true

# Unix-style newlines with a newline ending every file
[*]
end_of_line = lf
insert_final_newline = true
charset = utf-8

# Kernel C source files
[*.{c,h}]
indent_style = tab
indent_size = 8
trim_trailing_whitespace = true

# Makefile
[Makefile]
indent_style = tab

# YAML files
[*.{yml,yaml}]
indent_style = space
indent_size = 2

# Markdown files
[*.md]
trim_trailing_whitespace = false
indent_style = space
indent_size = 2
49 changes: 45 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,51 @@ jobs:
name: kernel-module-build
path: build/
retention-days: 30

static-analysis:
name: Static Analysis and Code Quality
runs-on: ubuntu-24.04

steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Static analysis with sparse (if available)
- name: Install analysis tools
run: |
sudo apt-get install -y sparse || true
make clean
make module C=1 || echo "Sparse analysis completed with warnings"
sudo apt-get update
sudo apt-get install -y \
clang-format \
cppcheck \
sparse \
linux-headers-$(uname -r)

- name: Check code formatting
run: make format-check

- name: Run checkpatch (kernel coding style)
run: make checkpatch
continue-on-error: true

- name: Run cppcheck
run: make cppcheck
continue-on-error: true

- name: Run sparse analyzer
run: make sparse
continue-on-error: true

unit-tests:
name: Unit Tests
runs-on: ubuntu-24.04

steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Install build tools
run: |
sudo apt-get update
sudo apt-get install -y build-essential gcc

- name: Run unit tests
run: make unit
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,9 @@ core.*
# QEMU testing environment
scripts/qemu-env/
*~

# Static analysis output
*.log
cppcheck-build/
scan-build-*/
compile_commands.json
99 changes: 98 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ SRC_DIR := src
# Build directory for user program
BUILD_DIR := build

.PHONY: all clean module user install uninstall test help unit
.PHONY: all clean module user install uninstall test help unit check format checkpatch sparse cppcheck

# Default target
all: module user
Expand Down Expand Up @@ -77,17 +77,114 @@ clean:
rm -rf $(SRC_DIR)/.tmp_versions
@echo "Clean complete!"

# Static Analysis and Code Quality Checks

# Run all code quality checks
check: checkpatch sparse cppcheck
@echo ""
@echo "================================================"
@echo "All static analysis checks completed!"
@echo "================================================"

# Check kernel coding style with checkpatch.pl
checkpatch:
@echo "Running checkpatch.pl (kernel coding style)..."
@if [ -f /lib/modules/$(shell uname -r)/build/scripts/checkpatch.pl ]; then \
for file in $(SRC_DIR)/*.c $(SRC_DIR)/*.h; do \
case "$$file" in \
*.mod.c) ;; \
*) if [ -f "$$file" ]; then \
echo "Checking $$file..."; \
/lib/modules/$(shell uname -r)/build/scripts/checkpatch.pl --no-tree --strict --file $$file || true; \
fi ;; \
esac; \
done; \
else \
echo "checkpatch.pl not found. Install kernel sources."; \
fi

# Run sparse static analyzer
sparse:
@echo "Running sparse static analyzer..."
@if command -v sparse >/dev/null 2>&1; then \
$(MAKE) -C $(KDIR) M=$(PWD)/$(SRC_DIR) C=2 CF="-D__CHECK_ENDIAN__" modules || true; \
else \
echo "sparse not found. Install with: sudo apt-get install sparse"; \
fi

# Run cppcheck static analyzer
cppcheck:
@echo "Running cppcheck static analyzer..."
@if command -v cppcheck >/dev/null 2>&1; then \
cppcheck --enable=all --inconclusive --force \
--suppressions-list=.cppcheck-suppressions \
--inline-suppr \
-I$(SRC_DIR) \
-I/lib/modules/$(shell uname -r)/build/include \
--error-exitcode=0 \
$(SRC_DIR)/*.c 2>&1 | grep -v "Cppcheck cannot find" || true; \
else \
echo "cppcheck not found. Install with: sudo apt-get install cppcheck"; \
fi

# Format code with clang-format
format:
@echo "Formatting code with clang-format..."
@if command -v clang-format >/dev/null 2>&1; then \
for file in $(SRC_DIR)/*.c $(SRC_DIR)/*.h; do \
if [ -f "$$file" ]; then \
echo "Formatting $$file..."; \
clang-format -i $$file; \
fi \
done; \
echo "Code formatting complete!"; \
else \
echo "clang-format not found. Install with: sudo apt-get install clang-format"; \
fi

# Check if code is properly formatted (CI-friendly)
format-check:
@echo "Checking code formatting..."
@if command -v clang-format >/dev/null 2>&1; then \
UNFORMATTED=$$(for file in $(SRC_DIR)/*.c $(SRC_DIR)/*.h; do \
if [ -f "$$file" ]; then \
clang-format -output-replacements-xml $$file | grep -q "<replacement " && echo "$$file"; \
fi \
done); \
if [ -n "$$UNFORMATTED" ]; then \
echo "The following files need formatting:"; \
echo "$$UNFORMATTED"; \
exit 1; \
else \
echo "All files are properly formatted!"; \
fi; \
else \
echo "clang-format not found. Install with: sudo apt-get install clang-format"; \
exit 1; \
fi

# Help target
help:
@echo "Linux Process Information Kernel Module - Build Targets:"
@echo ""
@echo "Build Targets:"
@echo " make all - Build both kernel module and user program (default)"
@echo " make module - Build kernel module only"
@echo " make user - Build user program only"
@echo " make unit - Build and run function-level unit tests"
@echo " make install - Install kernel module (requires root)"
@echo " make uninstall - Remove kernel module (requires root)"
@echo " make test - Install module and run user program"
@echo " make clean - Remove all build artifacts"
@echo ""
@echo "Code Quality Targets:"
@echo " make check - Run all static analysis checks"
@echo " make checkpatch - Check kernel coding style with checkpatch.pl"
@echo " make sparse - Run sparse static analyzer"
@echo " make cppcheck - Run cppcheck static analyzer"
@echo " make format - Format code with clang-format"
@echo " make format-check - Check if code is properly formatted (CI)"
@echo ""
@echo " make help - Show this help message"
@echo ""
@echo "Note: Building the kernel module requires kernel headers to be installed."
Loading