1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
|
/*
* SPDX-License-Identifier: BSD-2-Clause
*
* Copyright (c) 2018 The NetBSD Foundation, Inc.
*
* Author: Marcos Oduardo <[email protected]>
*/
#ifdef UBSAN_ENABLED
#include <sbi/sbi_types.h>
#include <sbi/sbi_console.h>
/* Undefined Behavior specific defines and structures defined by the compiler ABI */
#define KIND_INTEGER 0
#define KIND_FLOAT 1
#define KIND_UNKNOWN UINT16_MAX
volatile unsigned long sbi_ubsan_report_count;
struct CSourceLocation {
char *mFilename;
uint32_t mLine;
uint32_t mColumn;
};
struct CTypeDescriptor {
uint16_t mTypeKind;
uint16_t mTypeInfo;
uint8_t mTypeName[1];
};
struct COverflowData {
struct CSourceLocation mLocation;
struct CTypeDescriptor *mType;
};
struct CUnreachableData {
struct CSourceLocation mLocation;
};
struct CCFICheckFailData {
uint8_t mCheckKind;
struct CSourceLocation mLocation;
struct CTypeDescriptor *mType;
};
struct CDynamicTypeCacheMissData {
struct CSourceLocation mLocation;
struct CTypeDescriptor *mType;
void *mTypeInfo;
uint8_t mTypeCheckKind;
};
struct CFunctionTypeMismatchData {
struct CSourceLocation mLocation;
struct CTypeDescriptor *mType;
};
struct CInvalidBuiltinData {
struct CSourceLocation mLocation;
uint8_t mKind;
};
struct CInvalidValueData {
struct CSourceLocation mLocation;
struct CTypeDescriptor *mType;
};
struct CNonNullArgData {
struct CSourceLocation mLocation;
struct CSourceLocation mAttributeLocation;
int mArgIndex;
};
struct CNonNullReturnData {
struct CSourceLocation mAttributeLocation;
};
struct COutOfBoundsData {
struct CSourceLocation mLocation;
struct CTypeDescriptor *mArrayType;
struct CTypeDescriptor *mIndexType;
};
struct CPointerOverflowData {
struct CSourceLocation mLocation;
};
struct CShiftOutOfBoundsData {
struct CSourceLocation mLocation;
struct CTypeDescriptor *mLHSType;
struct CTypeDescriptor *mRHSType;
};
struct CTypeMismatchData {
struct CSourceLocation mLocation;
struct CTypeDescriptor *mType;
unsigned long mLogAlignment;
uint8_t mTypeCheckKind;
};
struct CTypeMismatchData_v1 {
struct CSourceLocation mLocation;
struct CTypeDescriptor *mType;
uint8_t mLogAlignment;
uint8_t mTypeCheckKind;
};
struct CVLABoundData {
struct CSourceLocation mLocation;
struct CTypeDescriptor *mType;
};
struct CFloatCastOverflowData {
struct CSourceLocation
mLocation; /* This field exists in this struct since 2015 August 11th */
struct CTypeDescriptor *mFromType;
struct CTypeDescriptor *mToType;
};
struct CImplicitConversionData {
struct CSourceLocation mLocation;
struct CTypeDescriptor *mFromType;
struct CTypeDescriptor *mToType;
uint8_t mKind;
};
struct CAlignmentAssumptionData {
struct CSourceLocation mLocation;
struct CSourceLocation mAssumptionLocation;
struct CTypeDescriptor *mType;
};
/* Public symbols used in the instrumentation of the code generation part */
void __ubsan_handle_add_overflow(struct COverflowData *pData,
unsigned long ulLHS, unsigned long ulRHS);
void __ubsan_handle_add_overflow_abort(struct COverflowData *pData,
unsigned long ulLHS,
unsigned long ulRHS);
void __ubsan_handle_alignment_assumption(struct CAlignmentAssumptionData *pData,
unsigned long ulPointer,
unsigned long ulAlignment,
unsigned long ulOffset);
void __ubsan_handle_alignment_assumption_abort(
struct CAlignmentAssumptionData *pData, unsigned long ulPointer,
unsigned long ulAlignment, unsigned long ulOffset);
void __ubsan_handle_builtin_unreachable(struct CUnreachableData *pData);
void __ubsan_handle_divrem_overflow(struct COverflowData *pData,
unsigned long ulLHS, unsigned long ulRHS);
void __ubsan_handle_divrem_overflow_abort(struct COverflowData *pData,
unsigned long ulLHS,
unsigned long ulRHS);
void __ubsan_handle_function_type_mismatch(
struct CFunctionTypeMismatchData *pData, unsigned long ulFunction);
void __ubsan_handle_function_type_mismatch_abort(
struct CFunctionTypeMismatchData *pData, unsigned long ulFunction);
void __ubsan_handle_function_type_mismatch_v1(
struct CFunctionTypeMismatchData *pData, unsigned long ulFunction,
unsigned long ulCalleeRTTI, unsigned long ulFnRTTI);
void __ubsan_handle_function_type_mismatch_v1_abort(
struct CFunctionTypeMismatchData *pData, unsigned long ulFunction,
unsigned long ulCalleeRTTI, unsigned long ulFnRTTI);
void __ubsan_handle_invalid_builtin(struct CInvalidBuiltinData *pData);
void __ubsan_handle_invalid_builtin_abort(struct CInvalidBuiltinData *pData);
void __ubsan_handle_load_invalid_value(struct CInvalidValueData *pData,
unsigned long ulVal);
void __ubsan_handle_load_invalid_value_abort(struct CInvalidValueData *pData,
unsigned long ulVal);
void __ubsan_handle_missing_return(struct CUnreachableData *pData);
void __ubsan_handle_mul_overflow(struct COverflowData *pData,
unsigned long ulLHS, unsigned long ulRHS);
void __ubsan_handle_mul_overflow_abort(struct COverflowData *pData,
unsigned long ulLHS,
unsigned long ulRHS);
void __ubsan_handle_negate_overflow(struct COverflowData *pData,
unsigned long ulOldVal);
void __ubsan_handle_negate_overflow_abort(struct COverflowData *pData,
unsigned long ulOldVal);
void __ubsan_handle_nullability_arg(struct CNonNullArgData *pData);
void __ubsan_handle_nullability_arg_abort(struct CNonNullArgData *pData);
void __ubsan_handle_nullability_return_v1(
struct CNonNullReturnData *pData,
struct CSourceLocation *pLocationPointer);
void __ubsan_handle_nullability_return_v1_abort(
struct CNonNullReturnData *pData,
struct CSourceLocation *pLocationPointer);
void __ubsan_handle_out_of_bounds(struct COutOfBoundsData *pData,
unsigned long ulIndex);
void __ubsan_handle_out_of_bounds_abort(struct COutOfBoundsData *pData,
unsigned long ulIndex);
void __ubsan_handle_pointer_overflow(struct CPointerOverflowData *pData,
unsigned long ulBase,
unsigned long ulResult);
void __ubsan_handle_pointer_overflow_abort(struct CPointerOverflowData *pData,
unsigned long ulBase,
unsigned long ulResult);
void __ubsan_handle_shift_out_of_bounds(struct CShiftOutOfBoundsData *pData,
unsigned long ulLHS,
unsigned long ulRHS);
void __ubsan_handle_shift_out_of_bounds_abort(
struct CShiftOutOfBoundsData *pData, unsigned long ulLHS,
unsigned long ulRHS);
void __ubsan_handle_sub_overflow(struct COverflowData *pData,
unsigned long ulLHS, unsigned long ulRHS);
void __ubsan_handle_sub_overflow_abort(struct COverflowData *pData,
unsigned long ulLHS,
unsigned long ulRHS);
void __ubsan_handle_type_mismatch(struct CTypeMismatchData *pData,
unsigned long ulPointer);
void __ubsan_handle_type_mismatch_abort(struct CTypeMismatchData *pData,
unsigned long ulPointer);
void __ubsan_handle_type_mismatch_v1(struct CTypeMismatchData_v1 *pData,
unsigned long ulPointer);
void __ubsan_handle_type_mismatch_v1_abort(struct CTypeMismatchData_v1 *pData,
unsigned long ulPointer);
void __ubsan_handle_vla_bound_not_positive(struct CVLABoundData *pData,
unsigned long ulBound);
void __ubsan_handle_vla_bound_not_positive_abort(struct CVLABoundData *pData,
unsigned long ulBound);
void __ubsan_get_current_report_data(const char **ppOutIssueKind,
const char **ppOutMessage,
const char **ppOutFilename,
uint32_t *pOutLine, uint32_t *pOutCol,
char **ppOutMemoryAddr);
static void HandleOverflow(bool isFatal, struct COverflowData *pData,
unsigned long ulLHS, unsigned long ulRHS,
const char *szOperation);
static void HandleNegateOverflow(bool isFatal, struct COverflowData *pData,
unsigned long ulOldValue);
static void HandleBuiltinUnreachable(bool isFatal,
struct CUnreachableData *pData);
static void HandleTypeMismatch(bool isFatal, struct CSourceLocation *mLocation,
struct CTypeDescriptor *mType,
unsigned long mLogAlignment,
uint8_t mTypeCheckKind, unsigned long ulPointer);
static void HandleVlaBoundNotPositive(bool isFatal, struct CVLABoundData *pData,
unsigned long ulBound);
static void HandleOutOfBounds(bool isFatal, struct COutOfBoundsData *pData,
unsigned long ulIndex);
static void HandleShiftOutOfBounds(bool isFatal,
struct CShiftOutOfBoundsData *pData,
unsigned long ulLHS, unsigned long ulRHS);
static void HandleLoadInvalidValue(bool isFatal,
struct CInvalidValueData *pData,
unsigned long ulValue);
static void HandleInvalidBuiltin(bool isFatal,
struct CInvalidBuiltinData *pData);
static void HandleFunctionTypeMismatch(bool isFatal,
struct CFunctionTypeMismatchData *pData,
unsigned long ulFunction);
static void HandleMissingReturn(bool isFatal, struct CUnreachableData *pData);
static void HandlePointerOverflow(bool isFatal,
struct CPointerOverflowData *pData,
unsigned long ulBase, unsigned long ulResult);
static void HandleAlignmentAssumption(bool isFatal,
struct CAlignmentAssumptionData *pData,
unsigned long ulPointer,
unsigned long ulAlignment,
unsigned long ulOffset);
#define NUMBER_SIGNED_BIT 1
#define NUMBER_MAXLEN 128
#define __arraycount(__a) (sizeof(__a) / sizeof(__a[0]))
#define __BIT(__n) (1UL << (__n))
#define SEPARATOR sbi_printf("===========================================\n")
#define ACK_REPORTED (1U << 31)
static bool isAlreadyReported(struct CSourceLocation *pLocation)
{
uint32_t siOldValue;
volatile uint32_t *pLine;
if (!pLocation)
return false;
pLine = &pLocation->mLine;
do {
siOldValue = *pLine;
} while (__sync_val_compare_and_swap(pLine, siOldValue,
siOldValue | ACK_REPORTED) !=
siOldValue);
if (!(siOldValue & ACK_REPORTED)) {
sbi_ubsan_report_count++;
return false;
}
return true;
}
static void HandleOverflow(bool isFatal, struct COverflowData *pData,
unsigned long ulLHS, unsigned long ulRHS,
const char *szOperation)
{
if (!pData) {
return;
}
if (isAlreadyReported(&pData->mLocation)) {
return;
}
SEPARATOR;
sbi_printf("UBSan: Undefined Behavior in %s:%u:%u\n",
pData->mLocation.mFilename,
pData->mLocation.mLine & ~ACK_REPORTED,
pData->mLocation.mColumn);
bool is_signed = pData->mType->mTypeInfo & NUMBER_SIGNED_BIT;
sbi_printf("UBSan: %s integer overflow: ",
is_signed ? "signed" : "unsigned");
if (is_signed) {
sbi_printf("%ld %s %ld ", (long)ulLHS, szOperation,
(long)ulRHS);
} else {
sbi_printf("%lu %s %lu ", ulLHS, szOperation, ulRHS);
}
sbi_printf("cannot be represented in type %s\n",
pData->mType->mTypeName);
SEPARATOR;
}
static void HandleNegateOverflow(bool isFatal, struct COverflowData *pData,
unsigned long ulOldValue)
{
if (!pData) {
return;
}
if (isAlreadyReported(&pData->mLocation)) {
return;
}
SEPARATOR;
sbi_printf("UBSan: Undefined Behavior in %s:%u:%u\n",
pData->mLocation.mFilename,
pData->mLocation.mLine & ~ACK_REPORTED,
pData->mLocation.mColumn);
bool is_signed = pData->mType->mTypeInfo & NUMBER_SIGNED_BIT;
sbi_printf("UBSan: Negation of ");
if (is_signed) {
sbi_printf("%ld", (long)ulOldValue);
} else {
sbi_printf("%lu", ulOldValue);
}
sbi_printf("cannot be represented in type %s\n",
pData->mType->mTypeName);
SEPARATOR;
}
static void HandleBuiltinUnreachable(bool isFatal,
struct CUnreachableData *pData)
{
if (!pData) {
return;
}
if (isAlreadyReported(&pData->mLocation)) {
return;
}
SEPARATOR;
sbi_printf("UBSan: Undefined Behavior in %s:%u:%u\n",
pData->mLocation.mFilename,
pData->mLocation.mLine & ~ACK_REPORTED,
pData->mLocation.mColumn);
SEPARATOR;
}
const char *rgczTypeCheckKinds[] = { "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",
"_Nonnull binding to",
"dynamic operation on" };
static void HandleTypeMismatch(bool isFatal, struct CSourceLocation *mLocation,
struct CTypeDescriptor *mType,
unsigned long mLogAlignment,
uint8_t mTypeCheckKind, unsigned long ulPointer)
{
if ((!mLocation) || (!mType)) {
return;
}
if (isAlreadyReported(mLocation)) {
return;
}
SEPARATOR;
sbi_printf("UBSan: Undefined Behavior in %s:%u:%u\n",
mLocation->mFilename, mLocation->mLine & ~ACK_REPORTED,
mLocation->mColumn);
const char *kind = (mTypeCheckKind < __arraycount(rgczTypeCheckKinds))
? rgczTypeCheckKinds[mTypeCheckKind]
: "access to";
if (ulPointer == 0) {
sbi_printf("%s null pointer of type %s\n", kind,
mType->mTypeName);
} else if (
(mLogAlignment - 1) &
ulPointer) { //mLogAlignment is converted on the wrapper function call
sbi_printf(
"%s misaligned address %p for type %s which requires %ld byte alignment\n",
kind, (void *)ulPointer, mType->mTypeName,
mLogAlignment);
} else {
sbi_printf(
"%s address %p with insufficient space for an object of type %s\n",
kind, (void *)ulPointer, mType->mTypeName);
}
SEPARATOR;
}
static void HandleVlaBoundNotPositive(bool isFatal, struct CVLABoundData *pData,
unsigned long ulBound)
{
if (!pData) {
return;
}
if (isAlreadyReported(&pData->mLocation)) {
return;
}
SEPARATOR;
sbi_printf("UBSan: Undefined Behavior in %s:%u:%u\n",
pData->mLocation.mFilename,
pData->mLocation.mLine & ~ACK_REPORTED,
pData->mLocation.mColumn);
sbi_printf("variable length array bound value ");
bool is_signed = pData->mType->mTypeInfo & NUMBER_SIGNED_BIT;
if (is_signed) {
sbi_printf("%ld", (long)ulBound);
} else {
sbi_printf("%lu", ulBound);
}
sbi_printf(" <= 0\n");
SEPARATOR;
}
static void HandleOutOfBounds(bool isFatal, struct COutOfBoundsData *pData,
unsigned long ulIndex)
{
if (!pData) {
return;
}
if (isAlreadyReported(&pData->mLocation)) {
return;
}
SEPARATOR;
sbi_printf("UBSan: Undefined Behavior in %s:%u:%u\n",
pData->mLocation.mFilename,
pData->mLocation.mLine & ~ACK_REPORTED,
pData->mLocation.mColumn);
bool is_signed = pData->mIndexType->mTypeInfo & NUMBER_SIGNED_BIT;
if (is_signed) {
sbi_printf("index %ld", (long)ulIndex);
} else {
sbi_printf("index %lu", ulIndex);
}
sbi_printf(" is out of range for type %s\n",
pData->mArrayType->mTypeName);
SEPARATOR;
}
static bool isNegativeNumber(struct CTypeDescriptor *pType, unsigned long ulVal)
{
if (!(pType->mTypeInfo & NUMBER_SIGNED_BIT)) {
return false;
}
return (long)ulVal < 0;
}
static size_t type_width(struct CTypeDescriptor *pType)
{
return 1UL << (pType->mTypeInfo >> 1);
}
static void HandleShiftOutOfBounds(bool isFatal,
struct CShiftOutOfBoundsData *pData,
unsigned long ulLHS, unsigned long ulRHS)
{
if (!pData) {
return;
}
if (isAlreadyReported(&pData->mLocation)) {
return;
}
SEPARATOR;
sbi_printf("UBSan: Undefined Behavior in %s:%u:%u\n",
pData->mLocation.mFilename,
pData->mLocation.mLine & ~ACK_REPORTED,
pData->mLocation.mColumn);
if (isNegativeNumber(pData->mRHSType, ulRHS)) {
sbi_printf("shift exponent %ld is negative\n", (long)ulRHS);
} else if (ulRHS >= type_width(pData->mLHSType)) {
sbi_printf(
"shift exponent %lu is too large for %lu-bit type %s\n",
ulRHS, (unsigned long)type_width(pData->mLHSType),
pData->mLHSType->mTypeName);
} else if (isNegativeNumber(pData->mLHSType, ulLHS)) {
sbi_printf("left shift of negative value %ld\n", (long)ulLHS);
} else {
sbi_printf(
"left shift of %lu by %lu places cannot be represented in type %s\n",
ulLHS, ulRHS, pData->mLHSType->mTypeName);
}
SEPARATOR;
}
static void HandleLoadInvalidValue(bool isFatal,
struct CInvalidValueData *pData,
unsigned long ulValue)
{
if (!pData) {
return;
}
if (isAlreadyReported(&pData->mLocation)) {
return;
}
SEPARATOR;
sbi_printf("UBSan: Undefined Behavior in %s:%u:%u\n",
pData->mLocation.mFilename,
pData->mLocation.mLine & ~ACK_REPORTED,
pData->mLocation.mColumn);
bool is_signed = pData->mType->mTypeInfo & NUMBER_SIGNED_BIT;
sbi_printf("load of value ");
if (is_signed) {
sbi_printf("%ld ", (long)ulValue);
} else {
sbi_printf("%lu ", ulValue);
}
sbi_printf("is not a valid value for type %s\n",
pData->mType->mTypeName);
SEPARATOR;
}
const char *rgczBuiltinCheckKinds[] = { "ctz()", "clz()" };
static void HandleInvalidBuiltin(bool isFatal,
struct CInvalidBuiltinData *pData)
{
if (!pData) {
return;
}
if (isAlreadyReported(&pData->mLocation)) {
return;
}
SEPARATOR;
sbi_printf("UBSan: Undefined Behavior in %s:%u:%u\n",
pData->mLocation.mFilename,
pData->mLocation.mLine & ~ACK_REPORTED,
pData->mLocation.mColumn);
const char *builtin =
(pData->mKind < __arraycount(rgczBuiltinCheckKinds))
? rgczBuiltinCheckKinds[pData->mKind]
: "unknown builtin";
sbi_printf("passing zero to %s, which is not a valid argument\n",
builtin);
SEPARATOR;
}
static void HandleFunctionTypeMismatch(bool isFatal,
struct CFunctionTypeMismatchData *pData,
unsigned long ulFunction)
{
if (!pData) {
return;
}
if (isAlreadyReported(&pData->mLocation)) {
return;
}
SEPARATOR;
sbi_printf("UBSan: Undefined Behavior in %s:%u:%u\n",
pData->mLocation.mFilename,
pData->mLocation.mLine & ~ACK_REPORTED,
pData->mLocation.mColumn);
sbi_printf(
"call to function %#lx through pointer to incorrect function type %s\n",
ulFunction, pData->mType->mTypeName);
SEPARATOR;
}
static void HandleMissingReturn(bool isFatal, struct CUnreachableData *pData)
{
if (!pData) {
return;
}
if (isAlreadyReported(&pData->mLocation)) {
return;
}
SEPARATOR;
sbi_printf("UBSan: Undefined Behavior in %s:%u:%u\n",
pData->mLocation.mFilename,
pData->mLocation.mLine & ~ACK_REPORTED,
pData->mLocation.mColumn);
sbi_printf(
"execution reached the end of a value-returning function without returning a value\n");
SEPARATOR;
}
static void HandlePointerOverflow(bool isFatal,
struct CPointerOverflowData *pData,
unsigned long ulBase, unsigned long ulResult)
{
if (!pData) {
return;
}
if (isAlreadyReported(&pData->mLocation)) {
return;
}
SEPARATOR;
sbi_printf("UBSan: Undefined Behavior in %s:%u:%u\n",
pData->mLocation.mFilename,
pData->mLocation.mLine & ~ACK_REPORTED,
pData->mLocation.mColumn);
sbi_printf("pointer expression with base %#lx overflowed to %#lx\n",
ulBase, ulResult);
SEPARATOR;
}
static void HandleAlignmentAssumption(bool isFatal,
struct CAlignmentAssumptionData *pData,
unsigned long ulPointer,
unsigned long ulAlignment,
unsigned long ulOffset)
{
if (!pData) {
return;
}
if (isAlreadyReported(&pData->mLocation)) {
return;
}
SEPARATOR;
sbi_printf("UBSan: Undefined Behavior in %s:%u:%u\n",
pData->mLocation.mFilename,
pData->mLocation.mLine & ~ACK_REPORTED,
pData->mLocation.mColumn);
unsigned long ulRealPointer = ulPointer - ulOffset;
sbi_printf("alignment assumption of %lu for pointer %p (offset %p)",
ulAlignment, (void *)ulRealPointer, (void *)ulOffset);
if (pData->mAssumptionLocation.mFilename != NULL) {
sbi_printf(", assumption made in %s:%u:%u",
pData->mAssumptionLocation.mFilename,
pData->mAssumptionLocation.mLine,
pData->mAssumptionLocation.mColumn);
}
sbi_printf("\n");
SEPARATOR;
}
/* Definions of public symbols emitted by the instrumentation code */
void __ubsan_handle_add_overflow(struct COverflowData *pData,
unsigned long ulLHS, unsigned long ulRHS)
{
HandleOverflow(false, pData, ulLHS, ulRHS, "+");
}
void __ubsan_handle_add_overflow_abort(struct COverflowData *pData,
unsigned long ulLHS, unsigned long ulRHS)
{
HandleOverflow(true, pData, ulLHS, ulRHS, "+");
}
void __ubsan_handle_sub_overflow(struct COverflowData *pData,
unsigned long ulLHS, unsigned long ulRHS)
{
HandleOverflow(false, pData, ulLHS, ulRHS, "-");
}
void __ubsan_handle_sub_overflow_abort(struct COverflowData *pData,
unsigned long ulLHS, unsigned long ulRHS)
{
HandleOverflow(true, pData, ulLHS, ulRHS, "-");
}
void __ubsan_handle_mul_overflow(struct COverflowData *pData,
unsigned long ulLHS, unsigned long ulRHS)
{
HandleOverflow(false, pData, ulLHS, ulRHS, "*");
}
void __ubsan_handle_mul_overflow_abort(struct COverflowData *pData,
unsigned long ulLHS, unsigned long ulRHS)
{
HandleOverflow(true, pData, ulLHS, ulRHS, "*");
}
void __ubsan_handle_negate_overflow(struct COverflowData *pData,
unsigned long ulOldValue)
{
HandleNegateOverflow(false, pData, ulOldValue);
}
void __ubsan_handle_negate_overflow_abort(struct COverflowData *pData,
unsigned long ulOldValue)
{
HandleNegateOverflow(true, pData, ulOldValue);
}
void __ubsan_handle_divrem_overflow(struct COverflowData *pData,
unsigned long ulLHS, unsigned long ulRHS)
{
HandleOverflow(false, pData, ulLHS, ulRHS, "divrem");
}
void __ubsan_handle_divrem_overflow_abort(struct COverflowData *pData,
unsigned long ulLHS,
unsigned long ulRHS)
{
HandleOverflow(true, pData, ulLHS, ulRHS, "divrem");
}
void __ubsan_handle_type_mismatch_v1(struct CTypeMismatchData_v1 *pData,
unsigned long ulPointer)
{
HandleTypeMismatch(false, &pData->mLocation, pData->mType,
__BIT(pData->mLogAlignment), pData->mTypeCheckKind,
ulPointer);
}
void __ubsan_handle_type_mismatch_v1_abort(struct CTypeMismatchData_v1 *pData,
unsigned long ulPointer)
{
HandleTypeMismatch(true, &pData->mLocation, pData->mType,
__BIT(pData->mLogAlignment), pData->mTypeCheckKind,
ulPointer);
}
void __ubsan_handle_out_of_bounds(struct COutOfBoundsData *pData,
unsigned long ulIndex)
{
HandleOutOfBounds(false, pData, ulIndex);
}
void __ubsan_handle_out_of_bounds_abort(struct COutOfBoundsData *pData,
unsigned long ulIndex)
{
HandleOutOfBounds(true, pData, ulIndex);
}
void __ubsan_handle_shift_out_of_bounds(struct CShiftOutOfBoundsData *pData,
unsigned long ulLHS,
unsigned long ulRHS)
{
HandleShiftOutOfBounds(false, pData, ulLHS, ulRHS);
}
void __ubsan_handle_shift_out_of_bounds_abort(
struct CShiftOutOfBoundsData *pData, unsigned long ulLHS,
unsigned long ulRHS)
{
HandleShiftOutOfBounds(true, pData, ulLHS, ulRHS);
}
void __ubsan_handle_pointer_overflow(struct CPointerOverflowData *pData,
unsigned long ulBase,
unsigned long ulResult)
{
HandlePointerOverflow(false, pData, ulBase, ulResult);
}
void __ubsan_handle_pointer_overflow_abort(struct CPointerOverflowData *pData,
unsigned long ulBase,
unsigned long ulResult)
{
HandlePointerOverflow(true, pData, ulBase, ulResult);
}
void __ubsan_handle_alignment_assumption(struct CAlignmentAssumptionData *pData,
unsigned long ulPointer,
unsigned long ulAlignment,
unsigned long ulOffset)
{
HandleAlignmentAssumption(false, pData, ulPointer, ulAlignment,
ulOffset);
}
void __ubsan_handle_alignment_assumption_abort(
struct CAlignmentAssumptionData *pData, unsigned long ulPointer,
unsigned long ulAlignment, unsigned long ulOffset)
{
HandleAlignmentAssumption(true, pData, ulPointer, ulAlignment,
ulOffset);
}
void __ubsan_handle_builtin_unreachable(struct CUnreachableData *pData)
{
HandleBuiltinUnreachable(true, pData);
}
void __ubsan_handle_invalid_builtin(struct CInvalidBuiltinData *pData)
{
HandleInvalidBuiltin(true, pData);
}
void __ubsan_handle_invalid_builtin_abort(struct CInvalidBuiltinData *pData)
{
HandleInvalidBuiltin(true, pData);
}
void __ubsan_handle_load_invalid_value(struct CInvalidValueData *pData,
unsigned long ulValue)
{
HandleLoadInvalidValue(false, pData, ulValue);
}
void __ubsan_handle_load_invalid_value_abort(struct CInvalidValueData *pData,
unsigned long ulValue)
{
HandleLoadInvalidValue(true, pData, ulValue);
}
void __ubsan_handle_missing_return(struct CUnreachableData *pData)
{
HandleMissingReturn(true, pData);
}
void __ubsan_handle_vla_bound_not_positive(struct CVLABoundData *pData,
unsigned long ulBound)
{
HandleVlaBoundNotPositive(false, pData, ulBound);
}
void __ubsan_handle_vla_bound_not_positive_abort(struct CVLABoundData *pData,
unsigned long ulBound)
{
HandleVlaBoundNotPositive(true, pData, ulBound);
}
void __ubsan_handle_function_type_mismatch(
struct CFunctionTypeMismatchData *pData, unsigned long ulFunction)
{
HandleFunctionTypeMismatch(false, pData, ulFunction);
}
void __ubsan_handle_function_type_mismatch_abort(
struct CFunctionTypeMismatchData *pData, unsigned long ulFunction)
{
HandleFunctionTypeMismatch(true, pData, ulFunction);
}
#endif
|