Skip to content

Commit 9dca1ff

Browse files
authored
gh-149300: _remote_debugging: clean up magic and duplicate consts in the binary format helper (#149301)
1 parent 9a268e3 commit 9dca1ff

3 files changed

Lines changed: 47 additions & 29 deletions

File tree

Modules/_remote_debugging/binary_io.h

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,36 @@ extern "C" {
6161
#define HDR_SIZE_COMPRESSION 4
6262
#define FILE_HEADER_SIZE (HDR_OFF_COMPRESSION + HDR_SIZE_COMPRESSION)
6363
#define FILE_HEADER_PLACEHOLDER_SIZE 64
64-
#define SAMPLE_HEADER_FIXED_SIZE (sizeof(uint64_t) + sizeof(uint32_t) + 1)
6564

6665
static_assert(FILE_HEADER_SIZE <= FILE_HEADER_PLACEHOLDER_SIZE,
6766
"FILE_HEADER_SIZE exceeds FILE_HEADER_PLACEHOLDER_SIZE");
6867

68+
/* Sample header field offsets and sizes */
69+
#define SMP_OFF_THREAD_ID 0
70+
#define SMP_SIZE_THREAD_ID sizeof(uint64_t)
71+
#define SMP_OFF_INTERPRETER_ID (SMP_OFF_THREAD_ID + SMP_SIZE_THREAD_ID)
72+
#define SMP_SIZE_INTERPRETER_ID sizeof(uint32_t)
73+
#define SMP_OFF_ENCODING (SMP_OFF_INTERPRETER_ID + SMP_SIZE_INTERPRETER_ID)
74+
#define SMP_SIZE_ENCODING sizeof(uint8_t)
75+
#define SAMPLE_HEADER_FIXED_SIZE (SMP_OFF_ENCODING + SMP_SIZE_ENCODING)
76+
77+
static_assert(SAMPLE_HEADER_FIXED_SIZE == 13,
78+
"SAMPLE_HEADER_FIXED_SIZE must remain 13");
79+
80+
/* Footer field offsets and sizes */
81+
#define FTR_OFF_STRINGS 0
82+
#define FTR_SIZE_STRINGS sizeof(uint32_t)
83+
#define FTR_OFF_FRAMES (FTR_OFF_STRINGS + FTR_SIZE_STRINGS)
84+
#define FTR_SIZE_FRAMES sizeof(uint32_t)
85+
#define FTR_OFF_FILE_SIZE (FTR_OFF_FRAMES + FTR_SIZE_FRAMES)
86+
#define FTR_SIZE_FILE_SIZE sizeof(uint64_t)
87+
#define FTR_OFF_CHECKSUM (FTR_OFF_FILE_SIZE + FTR_SIZE_FILE_SIZE)
88+
#define FTR_SIZE_CHECKSUM (2 * sizeof(uint64_t))
89+
#define FILE_FOOTER_SIZE (FTR_OFF_CHECKSUM + FTR_SIZE_CHECKSUM)
90+
91+
static_assert(FILE_FOOTER_SIZE == 32,
92+
"FILE_FOOTER_SIZE must remain 32");
93+
6994
/* Buffer sizes: 512KB balances syscall amortization against memory use,
7095
* and aligns well with filesystem block sizes and zstd dictionary windows */
7196
#define WRITE_BUFFER_SIZE (512 * 1024)

Modules/_remote_debugging/binary_io_reader.c

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,11 @@
2323
* ============================================================================ */
2424

2525
/* File structure sizes */
26-
#define FILE_FOOTER_SIZE 32
2726
#define MIN_DECOMPRESS_BUFFER_SIZE (64 * 1024) /* Minimum decompression buffer */
2827

2928
/* Progress callback frequency */
3029
#define PROGRESS_CALLBACK_INTERVAL 1000
3130

32-
/* Maximum decompression size limit (1GB) */
33-
#define MAX_DECOMPRESS_SIZE (1ULL << 30)
34-
3531
/* ============================================================================
3632
* BINARY READER IMPLEMENTATION
3733
* ============================================================================ */
@@ -47,8 +43,8 @@ reader_parse_header(BinaryReader *reader, const uint8_t *data, size_t file_size)
4743
/* Use memcpy to avoid strict aliasing violations and unaligned access */
4844
uint32_t magic;
4945
uint32_t version;
50-
memcpy(&magic, &data[0], sizeof(magic));
51-
memcpy(&version, &data[4], sizeof(version));
46+
memcpy(&magic, &data[HDR_OFF_MAGIC], HDR_SIZE_MAGIC);
47+
memcpy(&version, &data[HDR_OFF_VERSION], HDR_SIZE_VERSION);
5248

5349
/* Detect endianness from magic number */
5450
if (magic == BINARY_FORMAT_MAGIC) {
@@ -119,8 +115,8 @@ reader_parse_footer(BinaryReader *reader, const uint8_t *data, size_t file_size)
119115
const uint8_t *footer = data + file_size - FILE_FOOTER_SIZE;
120116
/* Use memcpy to avoid strict aliasing violations */
121117
uint32_t strings_count, frames_count;
122-
memcpy(&strings_count, &footer[0], sizeof(strings_count));
123-
memcpy(&frames_count, &footer[4], sizeof(frames_count));
118+
memcpy(&strings_count, &footer[FTR_OFF_STRINGS], FTR_SIZE_STRINGS);
119+
memcpy(&frames_count, &footer[FTR_OFF_FRAMES], FTR_SIZE_FRAMES);
124120

125121
reader->strings_count = SWAP32_IF(reader->needs_swap, strings_count);
126122
reader->frames_count = SWAP32_IF(reader->needs_swap, frames_count);
@@ -984,11 +980,11 @@ binary_reader_replay(BinaryReader *reader, PyObject *collector, PyObject *progre
984980
/* Use memcpy to avoid strict aliasing violations, then byte-swap if needed */
985981
uint64_t thread_id_raw;
986982
uint32_t interpreter_id_raw;
987-
memcpy(&thread_id_raw, &reader->sample_data[offset], sizeof(thread_id_raw));
988-
offset += 8;
983+
memcpy(&thread_id_raw, &reader->sample_data[offset], SMP_SIZE_THREAD_ID);
984+
offset += SMP_SIZE_THREAD_ID;
989985

990-
memcpy(&interpreter_id_raw, &reader->sample_data[offset], sizeof(interpreter_id_raw));
991-
offset += 4;
986+
memcpy(&interpreter_id_raw, &reader->sample_data[offset], SMP_SIZE_INTERPRETER_ID);
987+
offset += SMP_SIZE_INTERPRETER_ID;
992988

993989
uint64_t thread_id = SWAP64_IF(reader->needs_swap, thread_id_raw);
994990
uint32_t interpreter_id = SWAP32_IF(reader->needs_swap, interpreter_id_raw);

Modules/_remote_debugging/binary_io_writer.c

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,6 @@
2929
/* Frame buffer: depth varint (max 2 bytes for 256) + 256 frames * 5 bytes/varint + margin */
3030
#define MAX_FRAME_BUFFER_SIZE ((MAX_STACK_DEPTH * MAX_VARINT_SIZE_U32) + MAX_VARINT_SIZE_U32 + 16)
3131

32-
/* File structure sizes */
33-
#define FILE_FOOTER_SIZE 32
34-
3532
/* Helper macro: convert PyLong to int32, using default_val if conversion fails */
3633
#define PYLONG_TO_INT32_OR_DEFAULT(obj, var, default_val) \
3734
do { \
@@ -588,9 +585,9 @@ static inline int
588585
write_sample_header(BinaryWriter *writer, ThreadEntry *entry, uint8_t encoding)
589586
{
590587
uint8_t header[SAMPLE_HEADER_FIXED_SIZE];
591-
memcpy(header, &entry->thread_id, 8);
592-
memcpy(header + 8, &entry->interpreter_id, 4);
593-
header[12] = encoding;
588+
memcpy(header + SMP_OFF_THREAD_ID, &entry->thread_id, SMP_SIZE_THREAD_ID);
589+
memcpy(header + SMP_OFF_INTERPRETER_ID, &entry->interpreter_id, SMP_SIZE_INTERPRETER_ID);
590+
header[SMP_OFF_ENCODING] = encoding;
594591
return writer_write_bytes(writer, header, SAMPLE_HEADER_FIXED_SIZE);
595592
}
596593

@@ -649,9 +646,9 @@ write_sample_with_encoding(BinaryWriter *writer, ThreadEntry *entry,
649646
{
650647
/* Header: thread_id(8) + interpreter_id(4) + encoding(1) + delta(varint) + status(1) */
651648
uint8_t header_buf[SAMPLE_HEADER_MAX_SIZE];
652-
memcpy(header_buf, &entry->thread_id, 8);
653-
memcpy(header_buf + 8, &entry->interpreter_id, 4);
654-
header_buf[12] = (uint8_t)encoding_type;
649+
memcpy(header_buf + SMP_OFF_THREAD_ID, &entry->thread_id, SMP_SIZE_THREAD_ID);
650+
memcpy(header_buf + SMP_OFF_INTERPRETER_ID, &entry->interpreter_id, SMP_SIZE_INTERPRETER_ID);
651+
header_buf[SMP_OFF_ENCODING] = (uint8_t)encoding_type;
655652
size_t varint_len = encode_varint_u64(
656653
header_buf + SAMPLE_HEADER_FIXED_SIZE,
657654
timestamp_delta);
@@ -1145,17 +1142,17 @@ binary_writer_finalize(BinaryWriter *writer)
11451142
PyErr_SetFromErrno(PyExc_IOError);
11461143
return -1;
11471144
}
1148-
uint64_t file_size = (uint64_t)footer_offset + 32;
1149-
uint8_t footer[32] = {0};
1145+
uint64_t file_size = (uint64_t)footer_offset + FILE_FOOTER_SIZE;
1146+
uint8_t footer[FILE_FOOTER_SIZE] = {0};
11501147
/* Cast size_t to uint32_t before memcpy to ensure correct bytes are copied
11511148
* on both little-endian and big-endian systems (size_t is 8 bytes on 64-bit) */
11521149
uint32_t string_count_u32 = (uint32_t)writer->string_count;
11531150
uint32_t frame_count_u32 = (uint32_t)writer->frame_count;
1154-
memcpy(footer + 0, &string_count_u32, 4);
1155-
memcpy(footer + 4, &frame_count_u32, 4);
1156-
memcpy(footer + 8, &file_size, 8);
1157-
/* bytes 16-31: checksum placeholder (zeros) */
1158-
if (fwrite_checked_allow_threads(footer, 32, writer->fp) < 0) {
1151+
memcpy(footer + FTR_OFF_STRINGS, &string_count_u32, FTR_SIZE_STRINGS);
1152+
memcpy(footer + FTR_OFF_FRAMES, &frame_count_u32, FTR_SIZE_FRAMES);
1153+
memcpy(footer + FTR_OFF_FILE_SIZE, &file_size, FTR_SIZE_FILE_SIZE);
1154+
/* checksum (FTR_OFF_CHECKSUM..FILE_FOOTER_SIZE-1): placeholder zeros */
1155+
if (fwrite_checked_allow_threads(footer, FILE_FOOTER_SIZE, writer->fp) < 0) {
11591156
return -1;
11601157
}
11611158

0 commit comments

Comments
 (0)