Skip to content

Commit 2c0e7dd

Browse files
Merge pull request #94 from DataDog/rgs/fix-overflow
buffer overflow fix
2 parents f1a9e47 + c81d6a5 commit 2c0e7dd

File tree

1 file changed

+19
-11
lines changed

1 file changed

+19
-11
lines changed

src/flightRecorder.cpp

+19-11
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ const int RECORDING_BUFFER_LIMIT = RECORDING_BUFFER_SIZE - 4096;
4949
const int MAX_STRING_LENGTH = 8191;
5050
const u64 MAX_JLONG = 0x7fffffffffffffffULL;
5151
const u64 MIN_JLONG = 0x8000000000000000ULL;
52+
const int MAX_JFR_EVENT_SIZE = 256;
53+
const int JFR_EVENT_FLUSH_THRESHOLD = RECORDING_BUFFER_LIMIT - MAX_JFR_EVENT_SIZE;
5254

5355

5456
static SpinLock _rec_lock(1);
@@ -718,7 +720,7 @@ class Recording {
718720
buf->reset();
719721
}
720722

721-
void flushIfNeeded(Buffer* buf, int limit = RECORDING_BUFFER_LIMIT) {
723+
void flushIfNeeded(Buffer* buf, int limit = JFR_EVENT_FLUSH_THRESHOLD) {
722724
if (buf->offset() >= limit) {
723725
flush(buf);
724726
}
@@ -1197,6 +1199,12 @@ class Recording {
11971199
}
11981200
}
11991201

1202+
void writeEventSizePrefix(Buffer* buf, int start) {
1203+
int size = buf->offset() - start;
1204+
assert(size < MAX_JFR_EVENT_SIZE);
1205+
buf->put8(start, size);
1206+
}
1207+
12001208
void recordExecutionSample(Buffer* buf, int tid, u32 call_trace_id, ExecutionEvent* event) {
12011209
int start = buf->skip(1);
12021210
buf->putVar64(T_EXECUTION_SAMPLE);
@@ -1206,7 +1214,7 @@ class Recording {
12061214
buf->putVar64(event->_thread_state);
12071215
buf->putVar64(event->_weight);
12081216
writeContext(buf, Contexts::get(tid));
1209-
buf->put8(start, buf->offset() - start);
1217+
writeEventSizePrefix(buf, start);
12101218
flushIfNeeded(buf);
12111219
}
12121220

@@ -1219,7 +1227,7 @@ class Recording {
12191227
buf->putVar64(event->_thread_state);
12201228
buf->putVar64(event->_weight);
12211229
writeContext(buf, Contexts::get(tid));
1222-
buf->put8(start, buf->offset() - start);
1230+
writeEventSizePrefix(buf, start);
12231231
flushIfNeeded(buf);
12241232
}
12251233

@@ -1233,8 +1241,8 @@ class Recording {
12331241
buf->putVar64(event->_num_failed_samples);
12341242
buf->putVar64(event->_num_exited_threads);
12351243
buf->putVar64(event->_num_permission_denied);
1236-
buf->put8(start, buf->offset() - start);
1237-
flushIfNeeded(buf);
1244+
writeEventSizePrefix(buf, start);
1245+
flushIfNeeded(buf);
12381246
}
12391247

12401248
void recordTraceRoot(Buffer* buf, int tid, TraceRootEvent* event) {
@@ -1246,7 +1254,7 @@ class Recording {
12461254
buf->put8(0);
12471255
buf->putVar32(event->_label);
12481256
buf->putVar64(event->_local_root_span_id);
1249-
buf->put8(start, buf->offset() - start);
1257+
writeEventSizePrefix(buf, start);
12501258
flushIfNeeded(buf);
12511259
}
12521260

@@ -1260,7 +1268,7 @@ class Recording {
12601268
buf->putVar64(event->_size);
12611269
buf->putFloat(event->_weight);
12621270
writeContext(buf, Contexts::get(tid));
1263-
buf->put8(start, buf->offset() - start);
1271+
writeEventSizePrefix(buf, start);
12641272
flushIfNeeded(buf);
12651273
}
12661274

@@ -1275,7 +1283,7 @@ class Recording {
12751283
buf->putVar64(event->_alloc._size);
12761284
buf->putFloat(event->_alloc._weight);
12771285
writeContext(buf, event->_ctx);
1278-
buf->put8(start, buf->offset() - start);
1286+
writeEventSizePrefix(buf, start);
12791287
flushIfNeeded(buf);
12801288
}
12811289

@@ -1290,7 +1298,7 @@ class Recording {
12901298
buf->put8(0);
12911299
buf->putVar64(event->_address);
12921300
writeContext(buf, Contexts::get(tid));
1293-
buf->put8(start, buf->offset() - start);
1301+
writeEventSizePrefix(buf, start);
12941302
flushIfNeeded(buf);
12951303
}
12961304

@@ -1305,7 +1313,7 @@ class Recording {
13051313
buf->putVar64(event->_timeout);
13061314
buf->putVar64(MIN_JLONG);
13071315
buf->putVar64(event->_address);
1308-
buf->put8(start, buf->offset() - start);
1316+
writeEventSizePrefix(buf, start);
13091317
flushIfNeeded(buf);
13101318
}
13111319

@@ -1316,7 +1324,7 @@ class Recording {
13161324
buf->putFloat(proc_user);
13171325
buf->putFloat(proc_system);
13181326
buf->putFloat(machine_total);
1319-
buf->put8(start, buf->offset() - start);
1327+
writeEventSizePrefix(buf, start);
13201328
flushIfNeeded(buf);
13211329
}
13221330

0 commit comments

Comments
 (0)