Add support for UBSan's invalid builtin check
This is needed to check calls to __builtin_clz for a zero argument.
All our existing checks were optimized out because the compiler could
statically determine the operand was nonzero.
Bug: 136576760
Change-Id: I4f47e04e58a38c62b0bd87f6051776e5a588cff8
diff --git a/lib/ubsan/ubsan.c b/lib/ubsan/ubsan.c
index ba248ce..a678a6d 100644
--- a/lib/ubsan/ubsan.c
+++ b/lib/ubsan/ubsan.c
@@ -274,6 +274,23 @@
UBSAN_FINISH;
}
+UBSAN_HANDLER(invalid_builtin, struct invalid_builtin_data* data) {
+ UBSAN_START;
+ const char* details;
+ switch (data->check_kind) {
+ case BCK_CTZ_PASSED_ZERO:
+ details = "zero passed to ctz";
+ break;
+ case BCK_CLZ_PASSED_ZERO:
+ details = "zero passed to clz";
+ break;
+ default:
+ details = "unknown builtin misuse kind";
+ }
+ log(&data->loc, "invalid builtin usage", details);
+ UBSAN_FINISH;
+}
+
UBSAN_HANDLER(type_mismatch_v1,
struct type_mismatch_data* data,
value_handle_t ptr) {