未定义行为消毒(Undefined Behavior Sanitization)
文章目录
当运行其行为未被编程语言规范描述的代码时,就会发生未定义行为。像 C 和 C++ 这样的语言有相当多的未定义行为,以便编译器有机会为不同架构生成高效代码。当编译器遇到未定义行为时,可能会生成执行意外操作的代码,甚至可能什么都不做。幸运的是,GCC 可以生成代码来在运行时分析程序,并使用一个名为 ubsan 的库来捕获某些类型的未定义行为。ubsan 可以捕获运行时错误,例如解引用 NULL 或非规范地址、某些未定义的溢出错误、对越界数据进行移位或乘法以及其他错误。为业余爱好者的操作系统添加 ubsan 支持非常简单,只需定义几个在内核检测到未定义行为时被调用的钩子,并使用特殊标志重新编译即可。
GCC 的 Libsanitizer ubsan 实现
GCC 提供了一个名为 Libsanitizer 的库,其中包含 ubsan 针对用户态程序的钩子的 C++ 实现,位于 GCC 仓库的 libsanitizer/ubsan/ 目录 中。尝试将 libsanitizer 链接到内核可能会有问题,因此建议通过查看 GCC 的结构和函数定义并重写它们来实现自己的钩子。Sortix 和 Kernel of Truth 有很好的纯 C 实现作为这些钩子的示例。
使用 ubsan
编译带有 ubsan 支持的内核非常简单,只需将 -fsanitize=undefined 添加到提供给 GCC 的标志中。请注意,添加未定义行为检查会使内核体积变大,并导致大量低效的运行时检查。不过,使用 Makefile 可以轻松添加一个仅用于调试的伪目标,该目标构建带有调试信息和未定义行为检查的内核。要进行普通构建,请使用 make all,而要构建特殊的调试版本则使用 make debug
CFLAGS := -O2 -Werror -Wall ASFLAGS := -O2 -Werror -Wall CC := x86_64-elf-gcc debug: CFLAGS += -g -fsanitize=undefined debug: ASFLAGS += -g debug: all all: # Invoke GCC, GAS, etc. $(CC) -c myos.c -o myos.elf $(CFLAGS)
ubsan 数据结构
ubsan 定义了几个用于确定错误原因和位置的数据结构。其中最有用的一个是保存原始代码源位置的结构体。它有一个指向 C 字符串的指针,该字符串包含导致错误的文件名,以及错误发生的确切行号和列号。还有一个用于可能涉及未定义行为的类型的类型描述符,其中包含一个表示类型名称的 C 字符串。每种特定的行为类型,例如类型不匹配、溢出或越界错误,都有其自己的结构体类型,这些结构体类型总是以一个内嵌的源位置结构体开头。
struct source_location { const char *file; uint32_t line; uint32_t column; }; struct type_descriptor { uint16_t kind; uint16_t info; char name[]; }; struct type_mismatch_info { struct source_location location; struct type_descriptor *type; uintptr_t alignment; uint8_t type_check_kind; }; struct out_of_bounds_info { struct source_location location; struct type_descriptor left_type; struct type_descriptor right_type; };
示例钩子实现
一个特别有用的钩子叫做 __ubsan_handle_type_mismatch。在撰写本文时,此钩子处理三种不同类型的错误:NULL 指针访问、未对齐的内存访问,或通过数据大小不足的指针访问内存。这也能捕获 x86_64 上的非规范地址解引用。此函数接受两个参数:一个指向类型不匹配信息结构体的指针,以及一个指向原始数据的指针。我们可以检查数据以诊断错误,并将其记录给用户,然后触发内核 panic。
// Alignment must be a power of 2. #define is_aligned(value, alignment) !(value & (alignment - 1)) struct type_mismatch_info { struct source_location location; struct type_descriptor *type; uintptr_t alignment; uint8_t type_check_kind; }; const char *Type_Check_Kinds[] = { "load of", "store to", "reference binding to", "member access within", "member call on", "constructor call on", "downcast of", "downcast of", "upcast of", "cast to virtual base of", }; static void log_location(struct source_location *location) { logf("\tfile: %s\n\tline: %i\n\tcolumn: %i\n", location->file, location->line, location->column); } void __ubsan_handle_type_mismatch(struct type_mismatch_info *type_mismatch, uintptr_t pointer) { struct source_location *location = &type_mismatch->location; if (pointer == 0) { log("Null pointer access"); } else if (type_mismatch->alignment != 0 && is_aligned(pointer, type_mismatch->alignment)) { // Most useful on architectures with stricter memory alignment requirements, like ARM. log("Unaligned memory access"); } else { log("Insufficient size"); logf("%s address %p with insufficient space for object of type %s\n", Type_Check_Kinds[type_mismatch->type_check_kind], (void *)pointer, type_mismatch->type->name); } log_location(location); kernel_panic(); }
来源与翻译说明
本文根据原文快照翻译,保留原有代码、数值、技术限定及历史背景。原文中的命令与运行结果不代表本站已实际执行验证。
原文:Undefined Behavior Sanitization;离线修订号:28093。

评论
登录后参与技术讨论,分享你的经验与见解。
登录 / 注册暂无评论