[Git][ghc/ghc][master] 3 commits: rts/eventlog: Fix off-by-one in assertion

Marge Bot (@marge-bot) gitlab at gitlab.haskell.org
Wed Dec 13 11:34:13 UTC 2023



Marge Bot pushed to branch master at Glasgow Haskell Compiler / GHC


Commits:
d0b17576 by Ben Gamari at 2023-12-13T06:33:37-05:00
rts/eventlog: Fix off-by-one in assertion

Previously we failed to account for the NULL terminator `postString`
asserted that there is enough room in the buffer for the string.

- - - - -
a10f9b9b by Ben Gamari at 2023-12-13T06:33:37-05:00
rts/eventlog: Honor result of ensureRoomForVariableEvent is

Previously we would keep plugging along, even if isn't enough room for
the event.

- - - - -
0e0f41c0 by Ben Gamari at 2023-12-13T06:33:37-05:00
rts/eventlog: Avoid truncating event sizes

Previously ensureRoomForVariableEvent would truncate the desired size to
16-bits, resulting in #24197.

Fixes #24197.

- - - - -


2 changed files:

- rts/eventlog/EventLog.c
- rts/include/Stg.h


Changes:

=====================================
rts/eventlog/EventLog.c
=====================================
@@ -136,12 +136,12 @@ static void postBlockMarker(EventsBuf *eb);
 static void closeBlockMarker(EventsBuf *ebuf);
 
 static StgBool hasRoomForEvent(EventsBuf *eb, EventTypeNum eNum);
-static StgBool hasRoomForVariableEvent(EventsBuf *eb, uint32_t payload_bytes);
+static StgBool hasRoomForVariableEvent(EventsBuf *eb, StgWord payload_bytes);
 
 static void freeEventLoggingBuffer(void);
 
 static void ensureRoomForEvent(EventsBuf *eb, EventTypeNum tag);
-static int ensureRoomForVariableEvent(EventsBuf *eb, StgWord16 size);
+static int ensureRoomForVariableEvent(EventsBuf *eb, StgWord size);
 
 static inline void postWord8(EventsBuf *eb, StgWord8 i)
 {
@@ -180,7 +180,7 @@ static inline void postString(EventsBuf *eb, const char *buf)
 {
     if (buf) {
         const int len = strlen(buf);
-        ASSERT(eb->begin + eb->size > eb->pos + len);
+        ASSERT(eb->begin + eb->size > eb->pos + len + 1);
         memcpy(eb->pos, buf, len);
         eb->pos += len;
     }
@@ -1220,7 +1220,7 @@ void postHeapProfBegin(StgWord8 profile_id)
         1+8+4 + modSelector_len + descrSelector_len +
         typeSelector_len + ccSelector_len + ccsSelector_len +
         retainerSelector_len + bioSelector_len + 7;
-    ensureRoomForVariableEvent(&eventBuf, len);
+    CHECK(!ensureRoomForVariableEvent(&eventBuf, len));
     postEventHeader(&eventBuf, EVENT_HEAP_PROF_BEGIN);
     postPayloadSize(&eventBuf, len);
     postWord8(&eventBuf, profile_id);
@@ -1272,7 +1272,7 @@ void postHeapProfSampleString(StgWord8 profile_id,
     ACQUIRE_LOCK(&eventBufMutex);
     StgWord label_len = strlen(label);
     StgWord len = 1+8+label_len+1;
-    ensureRoomForVariableEvent(&eventBuf, len);
+    CHECK(!ensureRoomForVariableEvent(&eventBuf, len));
     postEventHeader(&eventBuf, EVENT_HEAP_PROF_SAMPLE_STRING);
     postPayloadSize(&eventBuf, len);
     postWord8(&eventBuf, profile_id);
@@ -1293,7 +1293,7 @@ void postHeapProfCostCentre(StgWord32 ccID,
     StgWord module_len = strlen(module);
     StgWord srcloc_len = strlen(srcloc);
     StgWord len = 4+label_len+module_len+srcloc_len+3+1;
-    ensureRoomForVariableEvent(&eventBuf, len);
+    CHECK(!ensureRoomForVariableEvent(&eventBuf, len));
     postEventHeader(&eventBuf, EVENT_HEAP_PROF_COST_CENTRE);
     postPayloadSize(&eventBuf, len);
     postWord32(&eventBuf, ccID);
@@ -1316,7 +1316,7 @@ void postHeapProfSampleCostCentre(StgWord8 profile_id,
     if (depth > 0xff) depth = 0xff;
 
     StgWord len = 1+8+1+depth*4;
-    ensureRoomForVariableEvent(&eventBuf, len);
+    CHECK(!ensureRoomForVariableEvent(&eventBuf, len));
     postEventHeader(&eventBuf, EVENT_HEAP_PROF_SAMPLE_COST_CENTRE);
     postPayloadSize(&eventBuf, len);
     postWord8(&eventBuf, profile_id);
@@ -1342,7 +1342,7 @@ void postProfSampleCostCentre(Capability *cap,
     if (depth > 0xff) depth = 0xff;
 
     StgWord len = 4+8+1+depth*4;
-    ensureRoomForVariableEvent(&eventBuf, len);
+    CHECK(!ensureRoomForVariableEvent(&eventBuf, len));
     postEventHeader(&eventBuf, EVENT_PROF_SAMPLE_COST_CENTRE);
     postPayloadSize(&eventBuf, len);
     postWord32(&eventBuf, cap->no);
@@ -1372,7 +1372,7 @@ void postProfBegin(void)
 static void postTickyCounterDef(EventsBuf *eb, StgEntCounter *p)
 {
     StgWord len = 8 + 2 + strlen(p->arg_kinds)+1 + strlen(p->str)+1 + 8 + strlen(p->ticky_json)+1;
-    ensureRoomForVariableEvent(eb, len);
+    CHECK(!ensureRoomForVariableEvent(eb, len));
     postEventHeader(eb, EVENT_TICKY_COUNTER_DEF);
     postPayloadSize(eb, len);
 
@@ -1439,7 +1439,7 @@ void postIPE(const InfoProvEnt *ipe)
     // 1 null after each string
     // 1 colon between src_file and src_span
     StgWord len = 8+table_name_len+1+closure_desc_len+1+ty_desc_len+1+label_len+1+module_len+1+src_file_len+1+src_span_len+1;
-    ensureRoomForVariableEvent(&eventBuf, len);
+    CHECK(!ensureRoomForVariableEvent(&eventBuf, len));
     postEventHeader(&eventBuf, EVENT_IPE);
     postPayloadSize(&eventBuf, len);
     postWord64(&eventBuf, (StgWord) INFO_PTR_TO_STRUCT(ipe->info));
@@ -1496,6 +1496,7 @@ void resetEventsBuf(EventsBuf* eb)
     eb->marker = NULL;
 }
 
+STG_WARN_UNUSED_RESULT
 StgBool hasRoomForEvent(EventsBuf *eb, EventTypeNum eNum)
 {
   uint32_t size = sizeof(EventTypeNum) + sizeof(EventTimestamp) + eventTypes[eNum].size;
@@ -1507,9 +1508,10 @@ StgBool hasRoomForEvent(EventsBuf *eb, EventTypeNum eNum)
   }
 }
 
-StgBool hasRoomForVariableEvent(EventsBuf *eb, uint32_t payload_bytes)
+STG_WARN_UNUSED_RESULT
+StgBool hasRoomForVariableEvent(EventsBuf *eb, StgWord payload_bytes)
 {
-  uint32_t size = sizeof(EventTypeNum) + sizeof(EventTimestamp) +
+  StgWord size = sizeof(EventTypeNum) + sizeof(EventTimestamp) +
       sizeof(EventPayloadSize) + payload_bytes;
 
   if (eb->pos + size > eb->begin + eb->size) {
@@ -1524,16 +1526,19 @@ void ensureRoomForEvent(EventsBuf *eb, EventTypeNum tag)
     if (!hasRoomForEvent(eb, tag)) {
         // Flush event buffer to make room for new event.
         printAndClearEventBuf(eb);
+        ASSERT(hasRoomForEvent(eb, tag));
     }
 }
 
-int ensureRoomForVariableEvent(EventsBuf *eb, StgWord16 size)
+STG_WARN_UNUSED_RESULT
+int ensureRoomForVariableEvent(EventsBuf *eb, StgWord size)
 {
     if (!hasRoomForVariableEvent(eb, size)) {
         // Flush event buffer to make room for new event.
         printAndClearEventBuf(eb);
-        if (!hasRoomForVariableEvent(eb, size))
+        if (!hasRoomForVariableEvent(eb, size)) {
             return 1; // Not enough space
+        }
     }
     return 0;
 }


=====================================
rts/include/Stg.h
=====================================
@@ -200,6 +200,7 @@
 
 #define STG_UNUSED    GNUC3_ATTRIBUTE(__unused__)
 #define STG_USED      GNUC3_ATTRIBUTE(__used__)
+#define STG_WARN_UNUSED_RESULT GNUC3_ATTRIBUTE(warn_unused_result)
 
 /* Prevent functions from being optimized.
    See Note [Windows Stack allocations] */



View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/compare/1c9496e0bbb41f494c66e430689841968e872be3...0e0f41c0e3d9c67fc669e975060e88bccdc7d823

-- 
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/compare/1c9496e0bbb41f494c66e430689841968e872be3...0e0f41c0e3d9c67fc669e975060e88bccdc7d823
You're receiving this email because of your account on gitlab.haskell.org.


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/ghc-commits/attachments/20231213/f8584168/attachment-0001.html>


More information about the ghc-commits mailing list