| ... | @@ -321,17 +321,12 @@ const TypeMismatchData = extern struct { | ... | @@ -321,17 +321,12 @@ const TypeMismatchData = extern struct { |
| 321 | }, | 321 | }, |
| 322 | }; | 322 | }; |
| 323 | | 323 | |
| 324 | fn simpleHandler( | 324 | fn SimpleHandler(comptime error_name: []const u8) type { |
| 325 | comptime sym_name: []const u8, | 325 | return struct { |
| 326 | comptime error_name: []const u8, | | |
| 327 | comptime abort: bool, | | |
| 328 | ) void { | | |
| 329 | const S = struct { | | |
| 330 | fn handler() callconv(.C) noreturn { | 326 | fn handler() callconv(.C) noreturn { |
| 331 | logMessage("{s}", .{error_name}); | 327 | logMessage("{s}", .{error_name}); |
| 332 | } | 328 | } |
| 333 | }; | 329 | }; |
| 334 | exportHandler(&S.handler, sym_name, abort); | | |
| 335 | } | 330 | } |
| 336 | | 331 | |
| 337 | inline fn logMessage(comptime fmt: []const u8, args: anytype) noreturn { | 332 | inline fn logMessage(comptime fmt: []const u8, args: anytype) noreturn { |
| ... | @@ -354,6 +349,31 @@ fn exportHandler( | ... | @@ -354,6 +349,31 @@ fn exportHandler( |
| 354 | } | 349 | } |
| 355 | } | 350 | } |
| 356 | | 351 | |
| | 352 | fn exportMinimal( |
| | 353 | handler: anytype, |
| | 354 | comptime sym_name: []const u8, |
| | 355 | comptime abort: bool, |
| | 356 | ) void { |
| | 357 | const linkage = if (builtin.is_test) .internal else .weak; |
| | 358 | { |
| | 359 | const N = "__ubsan_handle_" ++ sym_name ++ "_minimal"; |
| | 360 | @export(handler, .{ .name = N, .linkage = linkage }); |
| | 361 | } |
| | 362 | if (abort) { |
| | 363 | const N = "__ubsan_handle_" ++ sym_name ++ "_minimal_abort"; |
| | 364 | @export(handler, .{ .name = N, .linkage = linkage }); |
| | 365 | } |
| | 366 | } |
| | 367 | |
| | 368 | fn exportHelper( |
| | 369 | comptime err_name: []const u8, |
| | 370 | comptime sym_name: []const u8, |
| | 371 | comptime abort: bool, |
| | 372 | ) void { |
| | 373 | exportHandler(&SimpleHandler(err_name).handler, sym_name, abort); |
| | 374 | exportMinimal(&SimpleHandler(err_name).handler, sym_name, abort); |
| | 375 | } |
| | 376 | |
| 357 | comptime { | 377 | comptime { |
| 358 | overflowHandler("add_overflow", "+"); | 378 | overflowHandler("add_overflow", "+"); |
| 359 | overflowHandler("sub_overflow", "-"); | 379 | overflowHandler("sub_overflow", "-"); |
| ... | @@ -365,24 +385,36 @@ comptime { | ... | @@ -365,24 +385,36 @@ comptime { |
| 365 | exportHandler(&outOfBounds, "out_of_bounds", true); | 385 | exportHandler(&outOfBounds, "out_of_bounds", true); |
| 366 | exportHandler(&pointerOverflow, "pointer_overflow", true); | 386 | exportHandler(&pointerOverflow, "pointer_overflow", true); |
| 367 | | 387 | |
| 368 | simpleHandler("type_mismatch_v1", "type-mismatch-v1", true); | 388 | exportMinimal("add-overflow", "add_overflow", true); |
| 369 | simpleHandler("builtin_unreachable", "builtin-unreachable", false); | 389 | exportMinimal("sub-overflow", "sub_overflow", true); |
| 370 | simpleHandler("missing_return", "missing-return", false); | 390 | exportMinimal("mul-overflow", "mul_overflow", true); |
| 371 | simpleHandler("vla_bound_not_positive", "vla-bound-not-positive", true); | 391 | exportMinimal("negation-handler", "negate_overflow", true); |
| 372 | simpleHandler("float_cast_overflow", "float-cast-overflow", true); | 392 | exportMinimal("divrem-handler", "divrem_overflow", true); |
| 373 | simpleHandler("load_invalid_value", "load-invalid-value", true); | 393 | exportMinimal("alignment-assumption-handler", "alignment_assumption", true); |
| 374 | simpleHandler("invalid_builtin", "invalid-builtin", true); | 394 | exportMinimal("shift-oob", "shift_out_of_bounds", true); |
| 375 | simpleHandler("function_type_mismatch", "function-type-mismatch", true); | 395 | exportMinimal("out-of-bounds", "out_of_bounds", true); |
| 376 | simpleHandler("implicit_conversion", "implicit-conversion", true); | 396 | exportMinimal("pointer-overflow", "pointer_overflow", true); |
| 377 | simpleHandler("nonnull_arg", "nonnull-arg", true); | 397 | |
| 378 | simpleHandler("nonnull_return", "nonnull-return", true); | 398 | exportHandler(&SimpleHandler("type-mismatch-v1").handler, "type_mismatch_v1", true); |
| 379 | simpleHandler("nullability_arg", "nullability-arg", true); | 399 | exportMinimal(&SimpleHandler("type-mismatch").handler, "type_mismatch", true); |
| 380 | simpleHandler("nullability_return", "nullability-return", true); | 400 | |
| 381 | simpleHandler("cfi_check_fail", "cfi-check-fail", true); | 401 | exportHelper("builtin-unreachable", "builtin_unreachable", true); |
| 382 | simpleHandler("function_type_mismatch_v1", "function-type-mismatch-v1", true); | 402 | exportHelper("missing-return", "missing_return", false); |
| | 403 | exportHelper("vla-bound-not-positive", "vla_bound_not_positive", true); |
| | 404 | exportHelper("float-cast-overflow", "float_cast_overflow", true); |
| | 405 | exportHelper("load-invalid-value", "load_invalid_value", true); |
| | 406 | exportHelper("invalid-builtin", "invalid_builtin", true); |
| | 407 | exportHelper("function-type-mismatch", "function_type_mismatch", true); |
| | 408 | exportHelper("implicit-conversion", "implicit_conversion", true); |
| | 409 | exportHelper("nonnull-arg", "nonnull_arg", true); |
| | 410 | exportHelper("nonnull-return", "nonnull_return", true); |
| | 411 | exportHelper("nullability-arg", "nullability_arg", true); |
| | 412 | exportHelper("nullability-return", "nullability_return", true); |
| | 413 | exportHelper("cfi-check-fail", "cfi_check_fail", true); |
| | 414 | exportHelper("function-type-mismatch-v1", "function_type_mismatch_v1", true); |
| 383 | | 415 | |
| 384 | // these checks are nearly impossible to duplicate in zig, as they rely on nuances | 416 | // these checks are nearly impossible to duplicate in zig, as they rely on nuances |
| 385 | // in the Itanium C++ ABI. | 417 | // in the Itanium C++ ABI. |
| 386 | simpleHandler("dynamic_type_cache_miss", "dynamic-type-cache-miss", true); | 418 | // exportHelper("dynamic_type_cache_miss", "dynamic-type-cache-miss", true); |
| 387 | simpleHandler("vptr_type_cache", "vptr-type-cache", true); | 419 | // exportHelper("vptr_type_cache", "vptr-type-cache", true); |
| 388 | } | 420 | } |