lib/ubsan.c
Source file repositories/reference/linux-study-clean/lib/ubsan.c
File Facts
- System
- Linux kernel
- Corpus path
lib/ubsan.c- Extension
.c- Size
- 14489 bytes
- Lines
- 562
- Domain
- Kernel Services
- Bucket
- lib
- Inferred role
- Kernel Services: exported/initcall integration point
- Status
- integration implementation candidate
Why This File Exists
Shared kernel service surface used by multiple subsystems, including helpers, cryptography, virtualization support, and async I/O infrastructure.
- Shared kernel service surface used by multiple subsystems, including helpers, cryptography, virtualization support, and async I/O infrastructure.
- Exports symbols or registers init work; inspect boot/module ordering and who consumes the exported contract.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
linux/bitops.hlinux/bug.hlinux/ctype.hlinux/init.hlinux/kernel.hlinux/types.hlinux/sched.hlinux/uaccess.hlinux/ubsan.hkunit/test-bug.hubsan.h
Detected Declarations
function Copyrightfunction was_reportedfunction suppress_reportfunction type_is_intfunction type_is_signedfunction type_bit_widthfunction is_inline_intfunction get_signed_valfunction val_is_negativefunction get_unsigned_valfunction val_to_stringfunction ubsan_prologuefunction ubsan_epiloguefunction handle_overflowfunction __ubsan_handle_add_overflowfunction __ubsan_handle_sub_overflowfunction __ubsan_handle_mul_overflowfunction __ubsan_handle_negate_overflowfunction __ubsan_handle_implicit_conversionfunction __ubsan_handle_divrem_overflowfunction handle_null_ptr_dereffunction handle_misaligned_accessfunction handle_object_size_mismatchfunction ubsan_type_mismatch_commonfunction __ubsan_handle_type_mismatchfunction __ubsan_handle_type_mismatch_v1function __ubsan_handle_out_of_boundsfunction __ubsan_handle_shift_out_of_boundsfunction __ubsan_handle_builtin_unreachablefunction __ubsan_handle_load_invalid_valuefunction __ubsan_handle_alignment_assumptionexport __ubsan_handle_add_overflowexport __ubsan_handle_sub_overflowexport __ubsan_handle_mul_overflowexport __ubsan_handle_negate_overflowexport __ubsan_handle_implicit_conversionexport __ubsan_handle_divrem_overflowexport __ubsan_handle_type_mismatchexport __ubsan_handle_type_mismatch_v1export __ubsan_handle_out_of_boundsexport __ubsan_handle_shift_out_of_boundsexport __ubsan_handle_builtin_unreachableexport __ubsan_handle_load_invalid_valueexport __ubsan_handle_alignment_assumption
Annotated Snippet
if (type_bit_width(type) == 128) {
#if defined(CONFIG_ARCH_SUPPORTS_INT128)
u_max val = get_unsigned_val(type, value);
scnprintf(str, size, "0x%08x%08x%08x%08x",
(u32)(val >> 96),
(u32)(val >> 64),
(u32)(val >> 32),
(u32)(val));
#else
WARN_ON(1);
#endif
} else if (type_is_signed(type)) {
scnprintf(str, size, "%lld",
(s64)get_signed_val(type, value));
} else {
scnprintf(str, size, "%llu",
(u64)get_unsigned_val(type, value));
}
}
}
static void ubsan_prologue(struct source_location *loc, const char *reason)
{
current->in_ubsan++;
pr_warn(CUT_HERE);
pr_err("UBSAN: %s in %s:%d:%d\n", reason, loc->file_name,
loc->line & LINE_MASK, loc->column & COLUMN_MASK);
kunit_fail_current_test("%s in %s", reason, loc->file_name);
}
static void ubsan_epilogue(void)
{
dump_stack();
pr_warn("---[ end trace ]---\n");
current->in_ubsan--;
check_panic_on_warn("UBSAN");
}
static void handle_overflow(struct overflow_data *data, void *lhs,
void *rhs, char op)
{
struct type_descriptor *type = data->type;
char lhs_val_str[VALUE_LENGTH];
char rhs_val_str[VALUE_LENGTH];
if (suppress_report(&data->location))
return;
ubsan_prologue(&data->location, type_is_signed(type) ?
"signed-integer-overflow" :
"unsigned-integer-overflow");
val_to_string(lhs_val_str, sizeof(lhs_val_str), type, lhs);
val_to_string(rhs_val_str, sizeof(rhs_val_str), type, rhs);
pr_err("%s %c %s cannot be represented in type %s\n",
lhs_val_str,
op,
rhs_val_str,
type->type_name);
ubsan_epilogue();
}
void __ubsan_handle_add_overflow(void *data,
void *lhs, void *rhs)
{
handle_overflow(data, lhs, rhs, '+');
}
EXPORT_SYMBOL(__ubsan_handle_add_overflow);
void __ubsan_handle_sub_overflow(void *data,
void *lhs, void *rhs)
{
handle_overflow(data, lhs, rhs, '-');
}
EXPORT_SYMBOL(__ubsan_handle_sub_overflow);
void __ubsan_handle_mul_overflow(void *data,
void *lhs, void *rhs)
{
handle_overflow(data, lhs, rhs, '*');
}
Annotation
- Immediate include surface: `linux/bitops.h`, `linux/bug.h`, `linux/ctype.h`, `linux/init.h`, `linux/kernel.h`, `linux/types.h`, `linux/sched.h`, `linux/uaccess.h`.
- Detected declarations: `function Copyright`, `function was_reported`, `function suppress_report`, `function type_is_int`, `function type_is_signed`, `function type_bit_width`, `function is_inline_int`, `function get_signed_val`, `function val_is_negative`, `function get_unsigned_val`.
- Atlas domain: Kernel Services / lib.
- Implementation status: integration implementation candidate.
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.