[libubsan] Handling of floating-point types

Marek Polacek polacek at redhat.com
Thu May 22 10:39:36 PDT 2014


Hi!

I'm trying to implement -fsanitize=float-cast-overflow support to GCC.
One of the issues is that libubsan doesn't handle 96-bit floating-point
types, so e.g. on

int
main ()
{
  volatile long double ld = 300.0;
  char c = ld;
  return 0;
}

with -m32 we hit
==27463==Sanitizer CHECK failed: /builddir/build/BUILD/llvm-3.4/projects/compiler-rt/lib/ubsan/ubsan_value.cc:100 ((0 && "unexpected floating point bit width")) != (0) (0, 0)

The same for __float80 types in -m32 mode (but since clang doesn't
support __float{80,128} types, this is not so relevant for you I guess).
Fix is simple:

diff --git a/libsanitizer/ubsan/ubsan_value.cc b/libsanitizer/ubsan/ubsan_value.cc
index 141e8b5..e2f664d 100644
--- a/libsanitizer/ubsan/ubsan_value.cc
+++ b/libsanitizer/ubsan/ubsan_value.cc
@@ -92,6 +92,7 @@ FloatMax Value::getFloatValue() const {
     switch (getType().getFloatBitWidth()) {
     case 64: return *reinterpret_cast<double*>(Val);
     case 80: return *reinterpret_cast<long double*>(Val);
+    case 96: return *reinterpret_cast<long double*>(Val);
     case 128: return *reinterpret_cast<long double*>(Val);
     }
   }

Could someone please review & commit this one?

Another issue is printing of __float128 and _Decimal{32,64,128} types;
libubsan doesn't handle such types, so currently there's no way how
to print them.  Unfortunately, I don't think the solution is as simple
as adding e.g. TK_Decimal/TK_Float_Extended constants, because
libstdc++ I think doesn't have the routines to convert such values to
strings, and adding extra library dependencies to e.g. libdecnumber
doesn't sound viable.  But for these rather obscure types we can use
TK_Unknown - and just print "<unknown>".  I'd guess that DFP types
are rare anyway and I'm fine with keeping it this way.

Thanks.

	Marek



More information about the llvm-commits mailing list