Documentation/translations/zh_CN/dev-tools/kmsan.rst
Source file repositories/reference/linux-study-clean/Documentation/translations/zh_CN/dev-tools/kmsan.rst
File Facts
- System
- Linux kernel
- Corpus path
Documentation/translations/zh_CN/dev-tools/kmsan.rst- Extension
.rst- Size
- 15118 bytes
- Lines
- 393
- Domain
- Support Tooling And Documentation
- Bucket
- Documentation
- Inferred role
- Support Tooling And Documentation: documentation
- Status
- atlas-only
Why This File Exists
Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.
- Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.
- Touches user memory; correctness depends on fault-safe copying and privilege boundary handling.
- Allocates kernel memory; connect allocation flags and lifetime to context constraints.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
- No C-style include directives detected by the generator.
Detected Declarations
struct kmsan_context_statestruct kmsan_contextstruct task_structstruct pagefunction combine
Annotated Snippet
struct kmsan_context_state {
char param_tls[KMSAN_PARAM_SIZE];
char retval_tls[KMSAN_RETVAL_SIZE];
char va_arg_tls[KMSAN_PARAM_SIZE];
char va_arg_origin_tls[KMSAN_PARAM_SIZE];
u64 va_arg_overflow_size_tls;
char param_origin_tls[KMSAN_PARAM_SIZE];
depot_stack_handle_t retval_origin_tls;
};
KMSAN 使用此结构体在插桩函数之间传递参数阴影和原始值(除非立刻通过
``CONFIG_KMSAN_CHECK_PARAM_RETVAL`` 检查参数)。
将未初始化的值传递给函数
~~~~~~~~~~~~~~~~~~~~~~~~
Clang 的 MemorySanitizer 插桩有一个选项 ``-fsanitize-memory-param-retval``,该
选项使编译器检查按值传递的函数参数,以及函数返回值。
该选项由 ``CONFIG_KMSAN_CHECK_PARAM_RETVAL`` 控制,默认启用以便 KMSAN 更早报告
未初始化的值。有关更多细节,请参考 `LKML discussion`_。
由于 LLVM 中的实现检查的方式(它们仅应用于标记为 ``noundef`` 的参数),并不是所
有参数都能保证被检查,因此我们不能放弃 ``kmsan_context_state`` 中的元数据存储
。
字符串函数
~~~~~~~~~~~
编译器将对 ``memcpy()``/``memmove()``/``memset()`` 的调用替换为以下函数。这些函
数在数据结构初始化或复制时也会被调用,确保阴影和原始值与数据一起复制::
void *__msan_memcpy(void *dst, void *src, uintptr_t n)
void *__msan_memmove(void *dst, void *src, uintptr_t n)
void *__msan_memset(void *dst, int c, uintptr_t n)
错误报告
~~~~~~~~
对于每个值的使用,编译器发出一个阴影检查,在值中毒的情况下调用
``__msan_warning()``::
void __msan_warning(u32 origin)
``__msan_warning()`` 使 KMSAN 运行时打印错误报告。
内联汇编插桩
~~~~~~~~~~~~
KMSAN 对每个内联汇编输出进行插桩,调用::
void __msan_instrument_asm_store(void *addr, uintptr_t size)
,该函数解除内存区域的污染。
这种方法可能会掩盖某些错误,但也有助于避免许多位操作、原子操作等中的假阳性。
有时传递给内联汇编的指针不指向有效内存。在这种情况下,它们在运行时被忽略。
运行时库
--------
代码位于 ``mm/kmsan/``。
每个任务 KMSAN 状态
~~~~~~~~~~~~~~~~~~~
每个 task_struct 都有一个关联的 KMSAN 任务状态,它保存 KMSAN
上下文(见上文)和一个每个任务计数器以禁止 KMSAN 报告::
Annotation
- Detected declarations: `struct kmsan_context_state`, `struct kmsan_context`, `struct task_struct`, `struct page`, `function combine`.
- Atlas domain: Support Tooling And Documentation / Documentation.
- Implementation status: atlas-only.
- This snippet crosses the user/kernel memory boundary; validate fault handling and access checks before translating the pattern.
Implementation Notes
- This generated page is the file-by-file coverage layer; curated subsystem chapters should link here when they synthesize a multi-file control flow.
- Core OS pages should be promoted from atlas-only to deep-reviewed when they explain data structures, invariants, locking, lifecycle, and C implementation snippets.
- Driver-family pages are intentionally pattern-oriented unless they are part of the selected PCIe/NVMe representative device path.