[Git][ghc/ghc][wip/add-ddump-specialisations] 3 commits: rts/eventlog: Fix off-by-one in assertion

Finley McIlwaine (@FinleyMcIlwaine) gitlab at gitlab.haskell.org
Wed Nov 22 14:24:50 UTC 2023



Finley McIlwaine pushed to branch wip/add-ddump-specialisations at Glasgow Haskell Compiler / GHC


Commits:
96c1c4f8 by Ben Gamari at 2023-11-22T06:24:12-08: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.

- - - - -
2f1ba11d by Ben Gamari at 2023-11-22T06:24:23-08:00
rts/eventlog: Honor result of ensureRoomForVariableEvent is

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

- - - - -
bc361d2f by Ben Gamari at 2023-11-22T06:24:32-08: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;
     }
@@ -1218,7 +1218,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);
@@ -1270,7 +1270,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);
@@ -1291,7 +1291,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);
@@ -1314,7 +1314,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);
@@ -1340,7 +1340,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);
@@ -1370,7 +1370,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);
 
@@ -1437,7 +1437,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));
@@ -1494,6 +1494,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;
@@ -1505,9 +1506,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) {
@@ -1522,16 +1524,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
=====================================
@@ -221,6 +221,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/4bec35fb31bd4858a3a5fa3c0aff1a1bc59ef330...bc361d2f675e5e2c1da07f66a91666914311e4c3

-- 
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/compare/4bec35fb31bd4858a3a5fa3c0aff1a1bc59ef330...bc361d2f675e5e2c1da07f66a91666914311e4c3
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/20231122/d2c8066e/attachment-0001.html>


More information about the ghc-commits mailing list