[Lldb-commits] [lldb] r270793 - It has been brought to my attention that, given two variables

Enrico Granata via lldb-commits lldb-commits at lists.llvm.org
Wed May 25 16:19:01 PDT 2016


Author: enrico
Date: Wed May 25 18:19:01 2016
New Revision: 270793

URL: http://llvm.org/viewvc/llvm-project?rev=270793&view=rev
Log:
It has been brought to my attention that, given two variables

T x;
U y;

doing

x = *((T*)y)

is undefined behavior, even if sizeof(T) == sizeof(U), due to pointer aliasing rules

Fix up a couple of places in LLDB that were doing this, and transform them into a defined and safe memcpy() operation

Also, add a test case to ensure we didn't regress by doing this w.r.t. tagged pointer NSDate instances


Modified:
    lldb/trunk/packages/Python/lldbsuite/test/functionalities/data-formatter/data-formatter-objc/TestDataFormatterObjC.py
    lldb/trunk/packages/Python/lldbsuite/test/functionalities/data-formatter/data-formatter-objc/main.m
    lldb/trunk/source/Plugins/Language/ObjC/Cocoa.cpp

Modified: lldb/trunk/packages/Python/lldbsuite/test/functionalities/data-formatter/data-formatter-objc/TestDataFormatterObjC.py
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/packages/Python/lldbsuite/test/functionalities/data-formatter/data-formatter-objc/TestDataFormatterObjC.py?rev=270793&r1=270792&r2=270793&view=diff
==============================================================================
--- lldb/trunk/packages/Python/lldbsuite/test/functionalities/data-formatter/data-formatter-objc/TestDataFormatterObjC.py (original)
+++ lldb/trunk/packages/Python/lldbsuite/test/functionalities/data-formatter/data-formatter-objc/TestDataFormatterObjC.py Wed May 25 18:19:01 2016
@@ -271,16 +271,18 @@ class ObjCDataFormatterTestCase(TestBase
         # this test might fail if we hit the breakpoint late on December 31st of some given year
         # and midnight comes between hitting the breakpoint and running this line of code
         # hopefully the output will be revealing enough in that case :-)
-        now_year = str(datetime.datetime.now().year)
+        now_year = '%s-' % str(datetime.datetime.now().year)
 
-        self.expect('frame variable date3 date4',
-                    substrs = [now_year,'1970'])
+        self.expect('frame variable date3', substrs = [now_year])
+        self.expect('frame variable date4', substrs = ['1970'])
+        self.expect('frame variable date5', substrs = [now_year])
 
         self.expect('frame variable date1_abs date2_abs',
                     substrs = ['1985-04','2011-01'])
 
-        self.expect('frame variable date3_abs date4_abs',
-                    substrs = [now_year,'1970'])
+        self.expect('frame variable date3_abs', substrs = [now_year])
+        self.expect('frame variable date4_abs', substrs = ['1970'])
+        self.expect('frame variable date5_abs', substrs = [now_year])
 
         self.expect('frame variable cupertino home europe',
                     substrs = ['@"America/Los_Angeles"',
@@ -358,7 +360,6 @@ class ObjCDataFormatterTestCase(TestBase
             self.runCmd('type format clear', check=False)
             self.runCmd('type summary clear', check=False)
             self.runCmd('type synth clear', check=False)
-            self.runCmd('log timers disable', check=False)
 
 
         # Execute the cleanup function during test case tear down.

Modified: lldb/trunk/packages/Python/lldbsuite/test/functionalities/data-formatter/data-formatter-objc/main.m
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/packages/Python/lldbsuite/test/functionalities/data-formatter/data-formatter-objc/main.m?rev=270793&r1=270792&r2=270793&view=diff
==============================================================================
--- lldb/trunk/packages/Python/lldbsuite/test/functionalities/data-formatter/data-formatter-objc/main.m (original)
+++ lldb/trunk/packages/Python/lldbsuite/test/functionalities/data-formatter/data-formatter-objc/main.m Wed May 25 18:19:01 2016
@@ -489,11 +489,13 @@ int main (int argc, const char * argv[])
 		NSDate *date2 = [NSDate dateWithNaturalLanguageString:@"12am January 1, 2011"];
 		NSDate *date3 = [NSDate date];
 		NSDate *date4 = [NSDate dateWithTimeIntervalSince1970:24*60*60];
+    NSDate *date5 = [NSDate dateWithTimeIntervalSinceReferenceDate: floor([[NSDate date] timeIntervalSinceReferenceDate])];
 
 		CFAbsoluteTime date1_abs = CFDateGetAbsoluteTime(date1);
 		CFAbsoluteTime date2_abs = CFDateGetAbsoluteTime(date2);
 		CFAbsoluteTime date3_abs = CFDateGetAbsoluteTime(date3);
 		CFAbsoluteTime date4_abs = CFDateGetAbsoluteTime(date4);
+		CFAbsoluteTime date5_abs = CFDateGetAbsoluteTime(date5);
 
 	    NSIndexSet *iset1 = [[NSIndexSet alloc] initWithIndexesInRange:NSMakeRange(1, 4)];
 	    NSIndexSet *iset2 = [[NSIndexSet alloc] initWithIndexesInRange:NSMakeRange(1, 512)];

Modified: lldb/trunk/source/Plugins/Language/ObjC/Cocoa.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/Language/ObjC/Cocoa.cpp?rev=270793&r1=270792&r2=270793&view=diff
==============================================================================
--- lldb/trunk/source/Plugins/Language/ObjC/Cocoa.cpp (original)
+++ lldb/trunk/source/Plugins/Language/ObjC/Cocoa.cpp Wed May 25 18:19:01 2016
@@ -536,7 +536,8 @@ lldb_private::formatters::NSNumberSummar
                     uint32_t flt_as_int = process_sp->ReadUnsignedIntegerFromMemory(data_location, 4, 0, error);
                     if (error.Fail())
                         return false;
-                    float flt_value = *((float*)&flt_as_int);
+                    float flt_value = 0.0f;
+                    memcpy(&flt_value, &flt_as_int, sizeof(flt_as_int));
                     NSNumber_FormatFloat(valobj, stream, flt_value, options.GetLanguage());
                     break;
                 }
@@ -545,7 +546,8 @@ lldb_private::formatters::NSNumberSummar
                     uint64_t dbl_as_lng = process_sp->ReadUnsignedIntegerFromMemory(data_location, 8, 0, error);
                     if (error.Fail())
                         return false;
-                    double dbl_value = *((double*)&dbl_as_lng);
+                    double dbl_value = 0.0;
+                    memcpy(&dbl_value, &dbl_as_lng, sizeof(dbl_as_lng));
                     NSNumber_FormatDouble(valobj, stream, dbl_value, options.GetLanguage());
                     break;
                 }
@@ -666,7 +668,7 @@ lldb_private::formatters::NSDateSummaryP
         if (descriptor->GetTaggedPointerInfo(&info_bits,&value_bits))
         {
             date_value_bits = ((value_bits << 8) | (info_bits << 4));
-            date_value = *((double*)&date_value_bits);
+            memcpy(&date_value, &date_value_bits, sizeof(date_value_bits));
         }
         else
         {
@@ -674,7 +676,7 @@ lldb_private::formatters::NSDateSummaryP
             uint32_t delta = (triple.isWatchOS() && triple.isWatchABI()) ? 8 : ptr_size;
             Error error;
             date_value_bits = process_sp->ReadUnsignedIntegerFromMemory(valobj_addr+delta, 8, 0, error);
-            date_value = *((double*)&date_value_bits);
+            memcpy(&date_value, &date_value_bits, sizeof(date_value_bits));
             if (error.Fail())
                 return false;
         }
@@ -683,7 +685,7 @@ lldb_private::formatters::NSDateSummaryP
     {
         Error error;
         date_value_bits = process_sp->ReadUnsignedIntegerFromMemory(valobj_addr+2*ptr_size, 8, 0, error);
-        date_value = *((double*)&date_value_bits);
+        memcpy(&date_value, &date_value_bits, sizeof(date_value_bits));
         if (error.Fail())
             return false;
     }
@@ -940,7 +942,7 @@ lldb_private::formatters::GetOSXEpoch ()
         tm_epoch.tm_min = 0;
         tm_epoch.tm_mon = 0;
         tm_epoch.tm_mday = 1;
-        tm_epoch.tm_year = 2001-1900; // for some reason, we need to subtract 1900 from this field. not sure why.
+        tm_epoch.tm_year = 2001-1900;
         tm_epoch.tm_isdst = -1;
         tm_epoch.tm_gmtoff = 0;
         tm_epoch.tm_zone = nullptr;




More information about the lldb-commits mailing list