[Lldb-commits] [lldb] 4b074b4 - [lldb] Fix UB in half2float and add some more tests.

Raphael “Teemperor” Isemann via lldb-commits lldb-commits at lists.llvm.org
Thu May 20 01:46:36 PDT 2021


I think what you're looking for is llvm::bit_cast (in ADT/bit.h) :) But that code was written before that, so that's why it's still using unions

> On 19 May 2021, at 23:27, Shafik Yaghmour <syaghmour at apple.com> wrote:
> 
> *sigh*I wish we had std::bit_cast instead of using union based type punning, we do have __builtin_bit_cast but it is different enough that replacing it later on w/ std::bit_cast wouldn’t just be search/replace.
> 
>> On May 19, 2021, at 12:37 PM, Raphael Isemann via lldb-commits <lldb-commits at lists.llvm.org> wrote:
>> 
>> 
>> Author: Raphael Isemann
>> Date: 2021-05-19T21:37:10+02:00
>> New Revision: 4b074b49be206306330076b9fa40632ef1960823
>> 
>> URL: https://github.com/llvm/llvm-project/commit/4b074b49be206306330076b9fa40632ef1960823
>> DIFF: https://github.com/llvm/llvm-project/commit/4b074b49be206306330076b9fa40632ef1960823.diff
>> 
>> LOG: [lldb] Fix UB in half2float and add some more tests.
>> 
>> The added DumpDataExtractorTest uncovered that this is lshifting a negative
>> integer which upsets ubsan and breaks the sanitizer bot. This patch just
>> changes the variable we shift to be unsigned and adds a bunch of tests to make
>> sure this function does what it promises.
>> 
>> Added: 
>> 
>> 
>> Modified: 
>>   lldb/source/Core/DumpDataExtractor.cpp
>>   lldb/unittests/Core/DumpDataExtractorTest.cpp
>> 
>> Removed: 
>> 
>> 
>> 
>> ################################################################################
>> diff  --git a/lldb/source/Core/DumpDataExtractor.cpp b/lldb/source/Core/DumpDataExtractor.cpp
>> index ec44e3481c1e5..34c9353c9feaa 100644
>> --- a/lldb/source/Core/DumpDataExtractor.cpp
>> +++ b/lldb/source/Core/DumpDataExtractor.cpp
>> @@ -52,7 +52,9 @@ static float half2float(uint16_t half) {
>>    float f;
>>    uint32_t u;
>>  } u;
>> -  int32_t v = (int16_t)half;
>> +  // Sign extend to 4 byte.
>> +  int32_t sign_extended = static_cast<int16_t>(half);
>> +  uint32_t v = static_cast<uint32_t>(sign_extended);
>> 
>>  if (0 == (v & 0x7c00)) {
>>    u.u = v & 0x80007FFFU;
>> 
>> diff  --git a/lldb/unittests/Core/DumpDataExtractorTest.cpp b/lldb/unittests/Core/DumpDataExtractorTest.cpp
>> index c4ec5f2e9a35b..05cd13add1e99 100644
>> --- a/lldb/unittests/Core/DumpDataExtractorTest.cpp
>> +++ b/lldb/unittests/Core/DumpDataExtractorTest.cpp
>> @@ -174,8 +174,30 @@ TEST(DumpDataExtractorTest, Formats) {
>>           "{0x0000000000000000 0xaaaabbbbccccdddd}");
>> 
>>  // See half2float for format details.
>> +  // Test zeroes.
>> +  TestDump(std::vector<uint16_t>{0x0000, 0x8000},
>> +           lldb::Format::eFormatVectorOfFloat16, "{0 -0}");
>> +  // Some subnormal numbers.
>> +  TestDump(std::vector<uint16_t>{0x0001, 0x8001},
>> +           lldb::Format::eFormatVectorOfFloat16, "{5.96046e-08 -5.96046e-08}");
>> +  // A full mantisse and empty expontent.
>> +  TestDump(std::vector<uint16_t>{0x83ff, 0x03ff},
>> +           lldb::Format::eFormatVectorOfFloat16, "{-6.09756e-05 6.09756e-05}");
>> +  // Some normal numbers.
>> +  TestDump(std::vector<uint16_t>{0b0100001001001000},
>> +           lldb::Format::eFormatVectorOfFloat16, "{3.14062}");
>>  TestDump(std::vector<uint16_t>{0xabcd, 0x1234},
>>           lldb::Format::eFormatVectorOfFloat16, "{-0.0609436 0.000757217}");
>> +  // Largest and smallest normal number.
>> +  TestDump(std::vector<uint16_t>{0x0400, 0x7bff},
>> +           lldb::Format::eFormatVectorOfFloat16, "{6.10352e-05 65504}");
>> +  // quiet/signaling NaNs.
>> +  TestDump(std::vector<uint16_t>{0xffff, 0xffc0, 0x7fff, 0x7fc0},
>> +           lldb::Format::eFormatVectorOfFloat16, "{nan nan nan nan}");
>> +  // +/-Inf.
>> +  TestDump(std::vector<uint16_t>{0xfc00, 0x7c00},
>> +           lldb::Format::eFormatVectorOfFloat16, "{-inf inf}");
>> +
>>  TestDump(std::vector<float>{std::numeric_limits<float>::min(),
>>                              std::numeric_limits<float>::max()},
>>           lldb::Format::eFormatVectorOfFloat32, "{1.17549e-38 3.40282e+38}");
>> 
>> 
>> 
>> _______________________________________________
>> lldb-commits mailing list
>> lldb-commits at lists.llvm.org
>> https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits
> 



More information about the lldb-commits mailing list