ubsan.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #include <kernel/system.h>
  2. #include <kernel/types.h>
  3. #include <kernel/fs.h>
  4. #include <kernel/printf.h>
  5. #include <kernel/ubsan.h>
  6. #include <va_list.h>
  7. #define EARLY_LOG_DEVICE 0x3F8
  8. static uint32_t _ubsan_log_write(fs_node_t *node, uint32_t offset, uint32_t size, uint8_t *buffer) {
  9. for (unsigned int i = 0; i < size; ++i) {
  10. outportb(EARLY_LOG_DEVICE, buffer[i]);
  11. }
  12. return size;
  13. }
  14. static fs_node_t _ubsan_log = { .write = &_ubsan_log_write };
  15. void ubsan_debug(struct SourceLocation * location) {
  16. fprintf(&_ubsan_log, "[ubsan] %s:%d:%dc - ", location->file_name, location->line, location->column);
  17. }
  18. void __ubsan_handle_add_overflow(struct OverflowData * data, unsigned long lhs, unsigned long rhs) {
  19. ubsan_debug(&data->location);
  20. fprintf(&_ubsan_log, "Overflow in add: %d %d\n", lhs, rhs);
  21. }
  22. void __ubsan_handle_sub_overflow(struct OverflowData * data, unsigned long lhs, unsigned long rhs) {
  23. ubsan_debug(&data->location);
  24. fprintf(&_ubsan_log, "Overflow in sub: %d %d\n", lhs, rhs);
  25. }
  26. void __ubsan_handle_mul_overflow(struct OverflowData * data, unsigned long lhs, unsigned long rhs) {
  27. ubsan_debug(&data->location);
  28. fprintf(&_ubsan_log, "Overflow in mul: %d %d\n", lhs, rhs);
  29. }
  30. void __ubsan_handle_divrem_overflow(struct OverflowData * data, unsigned long lhs, unsigned long rhs) {
  31. ubsan_debug(&data->location);
  32. fprintf(&_ubsan_log, "Overflow in divrem: %d %d\n", lhs, rhs);
  33. }
  34. void __ubsan_handle_negate_overflow(struct OverflowData * data, unsigned long old) {
  35. ubsan_debug(&data->location);
  36. fprintf(&_ubsan_log, "Overflow in negate: %d\n", old);
  37. }
  38. void __ubsan_handle_builtin_unreachable(struct UnreachableData * data) {
  39. ubsan_debug(&data->location);
  40. fprintf(&_ubsan_log, "called __builtin_unreachable()\n");
  41. }
  42. void __ubsan_handle_out_of_bounds(struct OutOfBoundsData * data, unsigned long index) {
  43. ubsan_debug(&data->location);
  44. fprintf(&_ubsan_log, "out of bounds array reference at %s[%d]\n", data->array_type->type_name, index);
  45. }
  46. void __ubsan_handle_shift_out_of_bounds(struct ShiftOutOfBoundsData * data, unsigned long lhs, unsigned long rhs) {
  47. ubsan_debug(&data->location);
  48. fprintf(&_ubsan_log, "shift is out of bounds: %d %d\n", lhs, rhs);
  49. }
  50. #define IS_ALIGNED(a, b) (((a) & ((__typeof__(a))(b)-1)) == 0)
  51. void __ubsan_handle_type_mismatch(struct TypeMismatchData * data, unsigned long ptr) {
  52. return; /* Unaligned reads are valid on x86, and we have some very ugly code where this goes poorly. */
  53. ubsan_debug(&data->location);
  54. if (data->alignment && !IS_ALIGNED(ptr, data->alignment)) {
  55. fprintf(&_ubsan_log, "bad alignment in read at 0x%x (wanted %d)\n", ptr, data->alignment);
  56. } else {
  57. fprintf(&_ubsan_log, "type mismatch in reference at 0x%x\n", ptr);
  58. }
  59. }
  60. void __ubsan_handle_vla_bound_not_positive(struct VLABoundData * data, unsigned long bound) {
  61. ubsan_debug(&data->location);
  62. fprintf(&_ubsan_log, "vla bound not positive: %d\n", bound);
  63. }