From e9858f22defe9320548a2a00e03b4bada4e588ab Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Sun, 25 Oct 2026 20:35:12 +0100 Subject: [PATCH 1/4] perfetto: support big-endian in trace_processor_shell paths --- include/perfetto/base/endian.h | 12 +++++++ src/ipc/buffered_frame_deserializer.cc | 7 ++-- .../buffered_frame_deserializer_unittest.cc | 11 +++--- .../core/dataframe/arrow_deserializer.cc | 5 +++ .../core/dataframe/arrow_internal.h | 2 -- .../core/dataframe/arrow_serializer.cc | 5 +++ src/trace_processor/core/util/bit_vector.h | 8 ++++- .../art_method/art_method_tokenizer.cc | 13 +++---- .../art_method/art_method_v2_tokenizer.cc | 6 ++-- .../plugins/wattson/table_function.cc | 11 +++--- src/trace_processor/util/zip_reader.cc | 5 +-- src/traced/probes/ftrace/cpu_reader.cc | 36 +++++++++++-------- src/tracing/v2/shared_ring_buffer.cc | 15 ++++---- src/tracing/v2/shared_ring_buffer_abi.h | 14 ++++---- 14 files changed, 94 insertions(+), 56 deletions(-) diff --git a/include/perfetto/base/endian.h b/include/perfetto/base/endian.h index 269e9971dd0..a85b43c991e 100644 --- a/include/perfetto/base/endian.h +++ b/include/perfetto/base/endian.h @@ -205,6 +205,12 @@ inline double BEDoubleToHost(double x) { } #endif +inline uint16_t HostToLE(uint16_t x) { + return HostToLE16(x); +} +inline int16_t HostToLE(int16_t x) { + return static_cast(HostToLE16(static_cast(x))); +} inline uint32_t HostToLE(uint32_t x) { return HostToLE32(x); } @@ -224,6 +230,12 @@ inline double HostToLE(double x) { return HostToLEDouble(x); } +inline uint16_t LEToHost(uint16_t x) { + return LE16ToHost(x); +} +inline int16_t LEToHost(int16_t x) { + return static_cast(LE16ToHost(static_cast(x))); +} inline uint32_t LEToHost(uint32_t x) { return LE32ToHost(x); } diff --git a/src/ipc/buffered_frame_deserializer.cc b/src/ipc/buffered_frame_deserializer.cc index c5a0c02eec5..cdfdf7a9a78 100644 --- a/src/ipc/buffered_frame_deserializer.cc +++ b/src/ipc/buffered_frame_deserializer.cc @@ -21,6 +21,7 @@ #include #include +#include "perfetto/base/endian.h" #include "perfetto/base/logging.h" #include "perfetto/ext/base/utils.h" @@ -95,7 +96,8 @@ bool BufferedFrameDeserializer::EndReceive(size_t recv_size) { // Read the header into |payload_size|. uint32_t payload_size = 0; const char* rd_ptr = buf() + consumed_size; - memcpy(base::AssumeLittleEndian(&payload_size), rd_ptr, kHeaderSize); + memcpy(&payload_size, rd_ptr, kHeaderSize); + payload_size = base::LEToHost(payload_size); // Saturate the |payload_size| to prevent overflows. The > capacity_ check // below will abort the parsing. @@ -173,7 +175,8 @@ std::string BufferedFrameDeserializer::Serialize(const Frame& frame) { const uint32_t payload_size = static_cast(payload.size()); std::string buf; buf.resize(kHeaderSize + payload_size); - memcpy(&buf[0], base::AssumeLittleEndian(&payload_size), kHeaderSize); + const uint32_t le_payload_size = base::HostToLE(payload_size); + memcpy(&buf[0], &le_payload_size, kHeaderSize); memcpy(&buf[kHeaderSize], payload.data(), payload.size()); return buf; } diff --git a/src/ipc/buffered_frame_deserializer_unittest.cc b/src/ipc/buffered_frame_deserializer_unittest.cc index 4adec92c626..c24a76e0322 100644 --- a/src/ipc/buffered_frame_deserializer_unittest.cc +++ b/src/ipc/buffered_frame_deserializer_unittest.cc @@ -19,6 +19,7 @@ #include #include +#include "perfetto/base/endian.h" #include "perfetto/base/logging.h" #include "perfetto/ext/base/utils.h" #include "test/gtest_and_gmock.h" @@ -68,7 +69,8 @@ std::vector GetSimpleFrame(size_t size) { char* enc_buf = encoded_frame.data(); std::string payload = frame.SerializeAsString(); - memcpy(enc_buf, base::AssumeLittleEndian(&payload_size), kHeaderSize); + const uint32_t le_payload_size = base::HostToLE(payload_size); + memcpy(enc_buf, &le_payload_size, kHeaderSize); memcpy(enc_buf + kHeaderSize, payload.data(), payload.size()); PERFETTO_CHECK(encoded_frame.size() == size); return encoded_frame; @@ -137,8 +139,8 @@ TEST(BufferedFrameDeserializerTest, FragmentedFrameIsCorrectlyDeserialized) { uint32_t payload_size = static_cast(payload.size()); serialized_frame.resize(kHeaderSize + payload_size); memcpy(serialized_frame.data() + kHeaderSize, payload.data(), payload_size); - memcpy(serialized_frame.data(), base::AssumeLittleEndian(&payload_size), - kHeaderSize); + const uint32_t le_payload_size = base::HostToLE(payload_size); + memcpy(serialized_frame.data(), &le_payload_size, kHeaderSize); std::vector simple_frame = GetSimpleFrame(32); std::vector frame_chunk1(serialized_frame.begin(), @@ -249,7 +251,8 @@ TEST(BufferedFrameDeserializerTest, RejectVeryLargeFrames) { BufferedFrameDeserializer bfd; BufferedFrameDeserializer::ReceiveBuffer rbuf = bfd.BeginReceive(); const uint32_t kBigSize = std::numeric_limits::max() - 2; - memcpy(rbuf.data, base::AssumeLittleEndian(&kBigSize), kHeaderSize); + const uint32_t kLEBigSize = base::HostToLE(kBigSize); + memcpy(rbuf.data, &kLEBigSize, kHeaderSize); memcpy(rbuf.data + kHeaderSize, "some initial payload", 20); ASSERT_FALSE(bfd.EndReceive(kHeaderSize + 20)); } diff --git a/src/trace_processor/core/dataframe/arrow_deserializer.cc b/src/trace_processor/core/dataframe/arrow_deserializer.cc index 160c43143ea..2f816c69fd5 100644 --- a/src/trace_processor/core/dataframe/arrow_deserializer.cc +++ b/src/trace_processor/core/dataframe/arrow_deserializer.cc @@ -520,6 +520,7 @@ base::StatusOr DeserializeFromArrow( const util::TraceBlobViewReader& data, StringPool* pool, const DataframeSpec& spec) { +#if PERFETTO_IS_LITTLE_ENDIAN() if (spec.column_names.size() != spec.column_specs.size() || spec.column_names.size() > std::numeric_limits::max()) { return base::ErrStatus("Invalid dataframe spec"); @@ -600,6 +601,10 @@ base::StatusOr DeserializeFromArrow( ++dataframe.non_column_mutations_; dataframe.Finalize(); return dataframe; +#else // PERFETTO_IS_LITTLE_ENDIAN() + return base::ErrStatus( + "Arrow deserialization is not supported on big-endian hosts"); +#endif // PERFETTO_IS_LITTLE_ENDIAN() } } // namespace perfetto::trace_processor::core::dataframe diff --git a/src/trace_processor/core/dataframe/arrow_internal.h b/src/trace_processor/core/dataframe/arrow_internal.h index 2bad1571ad4..65529a4ecf0 100644 --- a/src/trace_processor/core/dataframe/arrow_internal.h +++ b/src/trace_processor/core/dataframe/arrow_internal.h @@ -29,8 +29,6 @@ namespace perfetto::trace_processor::core::dataframe::arrow_internal { -static_assert(PERFETTO_IS_LITTLE_ENDIAN()); - // Constants and packed structs below mirror the official Arrow flatbuffer // schema. Keeping them together makes the serializer and deserializer use the // same wire definitions without depending on the full Arrow library. diff --git a/src/trace_processor/core/dataframe/arrow_serializer.cc b/src/trace_processor/core/dataframe/arrow_serializer.cc index 610524804d0..4485e7bd880 100644 --- a/src/trace_processor/core/dataframe/arrow_serializer.cc +++ b/src/trace_processor/core/dataframe/arrow_serializer.cc @@ -623,6 +623,7 @@ base::Status ArrowSerializer::BuildFileFraming(const BodyPlan& plan) { base::StatusOr ArrowSerializer::Prepare(const Dataframe& dataframe, const StringPool& pool) { Reset(); +#if PERFETTO_IS_LITTLE_ENDIAN() if (!dataframe.finalized()) { return base::ErrStatus( "Arrow serialization requires a finalized dataframe"); @@ -654,6 +655,10 @@ base::StatusOr ArrowSerializer::Prepare(const Dataframe& dataframe, prepared_string_pool_ = &pool; prepared_mutations_ = dataframe.mutations(); return static_cast(total); +#else // PERFETTO_IS_LITTLE_ENDIAN() + return base::ErrStatus( + "Arrow serialization is not supported on big-endian hosts"); +#endif // PERFETTO_IS_LITTLE_ENDIAN() } base::Status ArrowSerializer::WriteDictionary(const DictionaryPlan& dictionary, diff --git a/src/trace_processor/core/util/bit_vector.h b/src/trace_processor/core/util/bit_vector.h index d69fd89bdf6..965f2e0b179 100644 --- a/src/trace_processor/core/util/bit_vector.h +++ b/src/trace_processor/core/util/bit_vector.h @@ -87,16 +87,22 @@ struct BitVector { // ceil(size / 8) bytes holding bit i at byte i / 8, bit i % 8. This is the // layout of the words below, so the bitmap is taken verbatim. static BitVector CreateFromBitmap(const uint8_t* bitmap, uint64_t size) { - static_assert(PERFETTO_IS_LITTLE_ENDIAN()); if (size == 0) { return {}; } auto words = FlexVector::CreateWithSize((size + 63u) / 64u); size_t bitmap_bytes = static_cast((size + 7u) / 8u); size_t word_bytes = words.size() * sizeof(uint64_t); +#if PERFETTO_IS_LITTLE_ENDIAN() memcpy(words.data(), bitmap, bitmap_bytes); memset(reinterpret_cast(words.data()) + bitmap_bytes, 0, word_bytes - bitmap_bytes); +#else + memset(words.data(), 0, word_bytes); + for (size_t i = 0; i < bitmap_bytes; ++i) { + words[i / 8u] |= static_cast(bitmap[i]) << (8u * (i % 8u)); + } +#endif // Bits past `size` are unspecified in the source bitmap. if (size % 64u != 0u) { words.back() &= (1ull << (size % 64u)) - 1ull; diff --git a/src/trace_processor/importers/art_method/art_method_tokenizer.cc b/src/trace_processor/importers/art_method/art_method_tokenizer.cc index 41a2f62d74f..587e7d4c915 100644 --- a/src/trace_processor/importers/art_method/art_method_tokenizer.cc +++ b/src/trace_processor/importers/art_method/art_method_tokenizer.cc @@ -27,6 +27,7 @@ #include #include +#include "perfetto/base/endian.h" #include "perfetto/base/logging.h" #include "perfetto/base/status.h" #include "perfetto/ext/base/status_macros.h" @@ -80,20 +81,20 @@ std::string ConstructPathname(const std::string& class_name, uint64_t ToLong(const TraceBlobView& tbv) { uint64_t x = 0; - memcpy(base::AssumeLittleEndian(&x), tbv.data(), tbv.size()); - return x; + memcpy(&x, tbv.data(), tbv.size()); + return base::LEToHost(x); } uint32_t ToInt(const TraceBlobView& tbv) { uint32_t x = 0; - memcpy(base::AssumeLittleEndian(&x), tbv.data(), tbv.size()); - return x; + memcpy(&x, tbv.data(), tbv.size()); + return base::LEToHost(x); } uint16_t ToShort(const TraceBlobView& tbv) { uint16_t x = 0; - memcpy(base::AssumeLittleEndian(&x), tbv.data(), tbv.size()); - return x; + memcpy(&x, tbv.data(), tbv.size()); + return base::LEToHost(x); } } // namespace diff --git a/src/trace_processor/importers/art_method/art_method_v2_tokenizer.cc b/src/trace_processor/importers/art_method/art_method_v2_tokenizer.cc index 2e74df6b10b..f191bf1a2b5 100644 --- a/src/trace_processor/importers/art_method/art_method_v2_tokenizer.cc +++ b/src/trace_processor/importers/art_method/art_method_v2_tokenizer.cc @@ -25,6 +25,7 @@ #include #include +#include "perfetto/base/endian.h" #include "perfetto/base/logging.h" #include "perfetto/base/status.h" #include "perfetto/ext/base/status_macros.h" @@ -91,13 +92,10 @@ const uint8_t* DecodeSignedLeb128(const uint8_t* data, // Reads a simple little-endian integer from the byte stream. uint64_t ReadNumber(const uint8_t* ptr, size_t num_bytes) { - static_assert(PERFETTO_IS_LITTLE_ENDIAN(), - "ART Method traces are little-endian. Perfetto requires a " - "little-endian host to parse these effectively."); PERFETTO_DCHECK(num_bytes <= sizeof(uint64_t)); uint64_t number = 0; memcpy(&number, ptr, num_bytes); - return number; + return base::LEToHost(number); } std::string ConstructPathname(base::StringView class_name, diff --git a/src/trace_processor/plugins/wattson/table_function.cc b/src/trace_processor/plugins/wattson/table_function.cc index 3b8d3836150..cb1fa22daab 100644 --- a/src/trace_processor/plugins/wattson/table_function.cc +++ b/src/trace_processor/plugins/wattson/table_function.cc @@ -25,7 +25,7 @@ #include #include -#include "perfetto/base/compiler.h" +#include "perfetto/base/endian.h" #include "perfetto/base/logging.h" #include "perfetto/base/status.h" #include "perfetto/ext/base/status_or.h" @@ -46,21 +46,18 @@ using core::dataframe::ColumnSpec; using core::dataframe::Dataframe; using core::dataframe::DataframeSpec; -static_assert(PERFETTO_IS_LITTLE_ENDIAN(), - "wattson curve blobs are little-endian and require a " - "little-endian host"); - class WattsonCurvesBlobReader { public: explicit WattsonCurvesBlobReader(const uint8_t* data, size_t size) : data_(data), size_(size) {} + // Curve blobs are little-endian. template T Read() { - static_assert(std::is_trivially_copyable_v); + static_assert(std::is_arithmetic_v); T value; ReadInto(&value, sizeof(T)); - return value; + return base::LEToHost(value); } base::StringView ReadStringView(uint16_t size) { diff --git a/src/trace_processor/util/zip_reader.cc b/src/trace_processor/util/zip_reader.cc index 325edf31c13..3a80ecb2063 100644 --- a/src/trace_processor/util/zip_reader.cc +++ b/src/trace_processor/util/zip_reader.cc @@ -27,6 +27,7 @@ #include #include "perfetto/base/build_config.h" +#include "perfetto/base/endian.h" #include "perfetto/base/logging.h" #include "perfetto/base/status.h" #include "perfetto/base/time.h" @@ -86,9 +87,9 @@ const uint16_t kDeflate = 8; template T ReadAndAdvance(const uint8_t** ptr) { T res{}; - memcpy(base::AssumeLittleEndian(&res), *ptr, sizeof(T)); + memcpy(&res, *ptr, sizeof(T)); *ptr += sizeof(T); - return res; + return base::LEToHost(res); } } // namespace diff --git a/src/traced/probes/ftrace/cpu_reader.cc b/src/traced/probes/ftrace/cpu_reader.cc index 5ed43d07485..a2a567e6177 100644 --- a/src/traced/probes/ftrace/cpu_reader.cc +++ b/src/traced/probes/ftrace/cpu_reader.cc @@ -23,6 +23,7 @@ #include #include +#include "perfetto/base/compiler.h" #include "perfetto/base/logging.h" #include "perfetto/ext/base/metatrace.h" #include "perfetto/ext/base/utils.h" @@ -594,24 +595,25 @@ std::optional CpuReader::ParsePageHeader( &page_header.timestamp)) return std::nullopt; - uint32_t size_and_flags; - - // On little endian, we can just read a uint32_t and reject the rest of the - // number later. - if (!CpuReader::ReadAndAdvance( - ptr, end_of_page, base::AssumeLittleEndian(&size_and_flags))) - return std::nullopt; + // |commit| is a host-endian long: 4 bytes on 32-bit kernels, 8 on 64-bit. + uint64_t size_and_flags; + if (page_header_size_len == sizeof(uint64_t)) { + if (!CpuReader::ReadAndAdvance(ptr, end_of_page, + &size_and_flags)) + return std::nullopt; + } else { + uint32_t commit; + if (!CpuReader::ReadAndAdvance(ptr, end_of_page, &commit)) + return std::nullopt; + size_and_flags = commit; + PERFETTO_DCHECK(page_header_size_len >= 4); + *ptr += page_header_size_len - 4; + } page_header.size = size_and_flags & kDataSizeMask; page_header.lost_events = bool(size_and_flags & kMissedEventsFlag); PERFETTO_DCHECK(page_header.size <= base::GetSysPageSize()); - // Reject rest of the number, if applicable. On 32-bit, size_bytes - 4 will - // evaluate to 0 and this will be a no-op. On 64-bit, this will advance by 4 - // bytes. - PERFETTO_DCHECK(page_header_size_len >= 4); - *ptr += page_header_size_len - 4; - return std::make_optional(page_header); } @@ -938,8 +940,12 @@ bool CpuReader::ParseField(const Field& field, // should avoid making things worse by corrupting the stack but we // don't need to handle it correctly. size_t size = std::min(field.ftrace_size, sizeof(n)); - memcpy(base::AssumeLittleEndian(&n), - reinterpret_cast(field_start), size); + memcpy(&n, reinterpret_cast(field_start), size); +#if !PERFETTO_IS_LITTLE_ENDIAN() + // The host-endian field landed in the most significant bytes of n. + if (size > 0 && size < sizeof(n)) + n >>= 8 * (sizeof(n) - size); +#endif // Look up the address in the printk format map and write it into the // proto. base::StringView name = table->LookupTraceString(n); diff --git a/src/tracing/v2/shared_ring_buffer.cc b/src/tracing/v2/shared_ring_buffer.cc index 9d000e74197..565b22e1374 100644 --- a/src/tracing/v2/shared_ring_buffer.cc +++ b/src/tracing/v2/shared_ring_buffer.cc @@ -36,17 +36,20 @@ static_assert(kMaxFragmentSizeVarIntBytes == "Ring buffer fragment sizes must use the same varint byte limit " "as Protozero message lengths"); -// The buffer stores read_pos in the low 32 bits of rw_positions. Pass its -// address to the futex API, but keep all C++ accesses on the containing +// The buffer stores read_pos in the low 32 bits of rw_positions: its first +// four bytes on little-endian hosts and its last four on big-endian ones. Pass +// its address to the futex API, but keep all C++ accesses on the containing // atomic. // // The supported CPU/ABI set provides atomic observation of that 32-bit half // during the 64-bit CAS. const uint32_t* ReadPosFutexAddress(const std::atomic* rw_positions) { - static_assert(PERFETTO_IS_LITTLE_ENDIAN(), - "The low-word futex requires read_pos to be the first four " - "bytes of rw_positions"); - return reinterpret_cast(rw_positions); + const uint32_t* halves = reinterpret_cast(rw_positions); +#if PERFETTO_IS_LITTLE_ENDIAN() + return halves; +#else + return halves + 1; +#endif } // Callers validate untrusted layouts with NumChunksForRingBufferLayout() diff --git a/src/tracing/v2/shared_ring_buffer_abi.h b/src/tracing/v2/shared_ring_buffer_abi.h index c349527c197..262b626cc7a 100644 --- a/src/tracing/v2/shared_ring_buffer_abi.h +++ b/src/tracing/v2/shared_ring_buffer_abi.h @@ -49,7 +49,7 @@ namespace perfetto::tracing_v2 { // - Each chunk starts with an atomic state word. It records ownership and the // number of published fragments while the chunk belongs to a writer. // -// The ABI assumes little-endian producer and service processes. +// Producer and service processes share a host, so the ABI uses its byte order. // Sharing over IPC // ---------------- @@ -118,15 +118,15 @@ constexpr size_t kDefaultChunkBudgetBytes = 128 * 1024; // +--------------+--------------+--------------+--------------+ // |<---------- three atomic -------->| // -// Writers check capacity by loading rw_positions once. In memory: -// - The first four bytes hold read_pos. -// - The next four bytes hold write_pos. +// Writers check capacity by loading rw_positions once. Its low 32 bits hold +// read_pos and its high 32 bits write_pos; the byte offsets above are for +// little-endian hosts and swap on big-endian ones. // Keeping both in one atomic avoids combining counters read at different times. // // The reader is the only one that moves read_pos. It publishes a new value once // per drain pass and then wakes any writer parked on a full ring buffer. -// read_pos is also the first four bytes of rw_positions, which is the address -// the futex waits on. +// The four bytes of rw_positions holding read_pos are also the address the +// futex waits on. // // num_writers_waiting lets the reader skip a futex wake when nobody is waiting // for space. It is only an optimization and never decides whether the ring @@ -475,7 +475,7 @@ enum class ChunkState : uint32_t { }; enum class ChunkFormat : uint32_t { - // Bytes 4..5 contain a little-endian target BufferID. Payload starts at 6. + // Bytes 4..5 contain a host-endian target BufferID. Payload starts at 6. kTargetBuffer = 0, // Reserved for a format carrying per-packet routing information. From 0734ecd2cb8af061a0417b8096a27518c82d6b50 Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Mon, 26 Oct 2026 09:55:36 +0100 Subject: [PATCH 2/4] fixup! perfetto: support big-endian in trace_processor_shell paths --- include/perfetto/base/endian.h | 9 ----- src/ipc/buffered_frame_deserializer.cc | 7 ++-- .../buffered_frame_deserializer_unittest.cc | 11 +++--- .../plugins/wattson/table_function.cc | 2 +- src/traced/probes/ftrace/cpu_reader.cc | 36 ++++++++----------- src/tracing/v2/shared_ring_buffer.cc | 15 ++++---- src/tracing/v2/shared_ring_buffer_abi.h | 14 ++++---- 7 files changed, 35 insertions(+), 59 deletions(-) diff --git a/include/perfetto/base/endian.h b/include/perfetto/base/endian.h index a85b43c991e..43db833c12e 100644 --- a/include/perfetto/base/endian.h +++ b/include/perfetto/base/endian.h @@ -205,12 +205,6 @@ inline double BEDoubleToHost(double x) { } #endif -inline uint16_t HostToLE(uint16_t x) { - return HostToLE16(x); -} -inline int16_t HostToLE(int16_t x) { - return static_cast(HostToLE16(static_cast(x))); -} inline uint32_t HostToLE(uint32_t x) { return HostToLE32(x); } @@ -233,9 +227,6 @@ inline double HostToLE(double x) { inline uint16_t LEToHost(uint16_t x) { return LE16ToHost(x); } -inline int16_t LEToHost(int16_t x) { - return static_cast(LE16ToHost(static_cast(x))); -} inline uint32_t LEToHost(uint32_t x) { return LE32ToHost(x); } diff --git a/src/ipc/buffered_frame_deserializer.cc b/src/ipc/buffered_frame_deserializer.cc index cdfdf7a9a78..c5a0c02eec5 100644 --- a/src/ipc/buffered_frame_deserializer.cc +++ b/src/ipc/buffered_frame_deserializer.cc @@ -21,7 +21,6 @@ #include #include -#include "perfetto/base/endian.h" #include "perfetto/base/logging.h" #include "perfetto/ext/base/utils.h" @@ -96,8 +95,7 @@ bool BufferedFrameDeserializer::EndReceive(size_t recv_size) { // Read the header into |payload_size|. uint32_t payload_size = 0; const char* rd_ptr = buf() + consumed_size; - memcpy(&payload_size, rd_ptr, kHeaderSize); - payload_size = base::LEToHost(payload_size); + memcpy(base::AssumeLittleEndian(&payload_size), rd_ptr, kHeaderSize); // Saturate the |payload_size| to prevent overflows. The > capacity_ check // below will abort the parsing. @@ -175,8 +173,7 @@ std::string BufferedFrameDeserializer::Serialize(const Frame& frame) { const uint32_t payload_size = static_cast(payload.size()); std::string buf; buf.resize(kHeaderSize + payload_size); - const uint32_t le_payload_size = base::HostToLE(payload_size); - memcpy(&buf[0], &le_payload_size, kHeaderSize); + memcpy(&buf[0], base::AssumeLittleEndian(&payload_size), kHeaderSize); memcpy(&buf[kHeaderSize], payload.data(), payload.size()); return buf; } diff --git a/src/ipc/buffered_frame_deserializer_unittest.cc b/src/ipc/buffered_frame_deserializer_unittest.cc index c24a76e0322..4adec92c626 100644 --- a/src/ipc/buffered_frame_deserializer_unittest.cc +++ b/src/ipc/buffered_frame_deserializer_unittest.cc @@ -19,7 +19,6 @@ #include #include -#include "perfetto/base/endian.h" #include "perfetto/base/logging.h" #include "perfetto/ext/base/utils.h" #include "test/gtest_and_gmock.h" @@ -69,8 +68,7 @@ std::vector GetSimpleFrame(size_t size) { char* enc_buf = encoded_frame.data(); std::string payload = frame.SerializeAsString(); - const uint32_t le_payload_size = base::HostToLE(payload_size); - memcpy(enc_buf, &le_payload_size, kHeaderSize); + memcpy(enc_buf, base::AssumeLittleEndian(&payload_size), kHeaderSize); memcpy(enc_buf + kHeaderSize, payload.data(), payload.size()); PERFETTO_CHECK(encoded_frame.size() == size); return encoded_frame; @@ -139,8 +137,8 @@ TEST(BufferedFrameDeserializerTest, FragmentedFrameIsCorrectlyDeserialized) { uint32_t payload_size = static_cast(payload.size()); serialized_frame.resize(kHeaderSize + payload_size); memcpy(serialized_frame.data() + kHeaderSize, payload.data(), payload_size); - const uint32_t le_payload_size = base::HostToLE(payload_size); - memcpy(serialized_frame.data(), &le_payload_size, kHeaderSize); + memcpy(serialized_frame.data(), base::AssumeLittleEndian(&payload_size), + kHeaderSize); std::vector simple_frame = GetSimpleFrame(32); std::vector frame_chunk1(serialized_frame.begin(), @@ -251,8 +249,7 @@ TEST(BufferedFrameDeserializerTest, RejectVeryLargeFrames) { BufferedFrameDeserializer bfd; BufferedFrameDeserializer::ReceiveBuffer rbuf = bfd.BeginReceive(); const uint32_t kBigSize = std::numeric_limits::max() - 2; - const uint32_t kLEBigSize = base::HostToLE(kBigSize); - memcpy(rbuf.data, &kLEBigSize, kHeaderSize); + memcpy(rbuf.data, base::AssumeLittleEndian(&kBigSize), kHeaderSize); memcpy(rbuf.data + kHeaderSize, "some initial payload", 20); ASSERT_FALSE(bfd.EndReceive(kHeaderSize + 20)); } diff --git a/src/trace_processor/plugins/wattson/table_function.cc b/src/trace_processor/plugins/wattson/table_function.cc index cb1fa22daab..1e3ac3a13e5 100644 --- a/src/trace_processor/plugins/wattson/table_function.cc +++ b/src/trace_processor/plugins/wattson/table_function.cc @@ -54,7 +54,7 @@ class WattsonCurvesBlobReader { // Curve blobs are little-endian. template T Read() { - static_assert(std::is_arithmetic_v); + static_assert(std::is_trivially_copyable_v); T value; ReadInto(&value, sizeof(T)); return base::LEToHost(value); diff --git a/src/traced/probes/ftrace/cpu_reader.cc b/src/traced/probes/ftrace/cpu_reader.cc index a2a567e6177..5ed43d07485 100644 --- a/src/traced/probes/ftrace/cpu_reader.cc +++ b/src/traced/probes/ftrace/cpu_reader.cc @@ -23,7 +23,6 @@ #include #include -#include "perfetto/base/compiler.h" #include "perfetto/base/logging.h" #include "perfetto/ext/base/metatrace.h" #include "perfetto/ext/base/utils.h" @@ -595,25 +594,24 @@ std::optional CpuReader::ParsePageHeader( &page_header.timestamp)) return std::nullopt; - // |commit| is a host-endian long: 4 bytes on 32-bit kernels, 8 on 64-bit. - uint64_t size_and_flags; - if (page_header_size_len == sizeof(uint64_t)) { - if (!CpuReader::ReadAndAdvance(ptr, end_of_page, - &size_and_flags)) - return std::nullopt; - } else { - uint32_t commit; - if (!CpuReader::ReadAndAdvance(ptr, end_of_page, &commit)) - return std::nullopt; - size_and_flags = commit; - PERFETTO_DCHECK(page_header_size_len >= 4); - *ptr += page_header_size_len - 4; - } + uint32_t size_and_flags; + + // On little endian, we can just read a uint32_t and reject the rest of the + // number later. + if (!CpuReader::ReadAndAdvance( + ptr, end_of_page, base::AssumeLittleEndian(&size_and_flags))) + return std::nullopt; page_header.size = size_and_flags & kDataSizeMask; page_header.lost_events = bool(size_and_flags & kMissedEventsFlag); PERFETTO_DCHECK(page_header.size <= base::GetSysPageSize()); + // Reject rest of the number, if applicable. On 32-bit, size_bytes - 4 will + // evaluate to 0 and this will be a no-op. On 64-bit, this will advance by 4 + // bytes. + PERFETTO_DCHECK(page_header_size_len >= 4); + *ptr += page_header_size_len - 4; + return std::make_optional(page_header); } @@ -940,12 +938,8 @@ bool CpuReader::ParseField(const Field& field, // should avoid making things worse by corrupting the stack but we // don't need to handle it correctly. size_t size = std::min(field.ftrace_size, sizeof(n)); - memcpy(&n, reinterpret_cast(field_start), size); -#if !PERFETTO_IS_LITTLE_ENDIAN() - // The host-endian field landed in the most significant bytes of n. - if (size > 0 && size < sizeof(n)) - n >>= 8 * (sizeof(n) - size); -#endif + memcpy(base::AssumeLittleEndian(&n), + reinterpret_cast(field_start), size); // Look up the address in the printk format map and write it into the // proto. base::StringView name = table->LookupTraceString(n); diff --git a/src/tracing/v2/shared_ring_buffer.cc b/src/tracing/v2/shared_ring_buffer.cc index 565b22e1374..9d000e74197 100644 --- a/src/tracing/v2/shared_ring_buffer.cc +++ b/src/tracing/v2/shared_ring_buffer.cc @@ -36,20 +36,17 @@ static_assert(kMaxFragmentSizeVarIntBytes == "Ring buffer fragment sizes must use the same varint byte limit " "as Protozero message lengths"); -// The buffer stores read_pos in the low 32 bits of rw_positions: its first -// four bytes on little-endian hosts and its last four on big-endian ones. Pass -// its address to the futex API, but keep all C++ accesses on the containing +// The buffer stores read_pos in the low 32 bits of rw_positions. Pass its +// address to the futex API, but keep all C++ accesses on the containing // atomic. // // The supported CPU/ABI set provides atomic observation of that 32-bit half // during the 64-bit CAS. const uint32_t* ReadPosFutexAddress(const std::atomic* rw_positions) { - const uint32_t* halves = reinterpret_cast(rw_positions); -#if PERFETTO_IS_LITTLE_ENDIAN() - return halves; -#else - return halves + 1; -#endif + static_assert(PERFETTO_IS_LITTLE_ENDIAN(), + "The low-word futex requires read_pos to be the first four " + "bytes of rw_positions"); + return reinterpret_cast(rw_positions); } // Callers validate untrusted layouts with NumChunksForRingBufferLayout() diff --git a/src/tracing/v2/shared_ring_buffer_abi.h b/src/tracing/v2/shared_ring_buffer_abi.h index 262b626cc7a..c349527c197 100644 --- a/src/tracing/v2/shared_ring_buffer_abi.h +++ b/src/tracing/v2/shared_ring_buffer_abi.h @@ -49,7 +49,7 @@ namespace perfetto::tracing_v2 { // - Each chunk starts with an atomic state word. It records ownership and the // number of published fragments while the chunk belongs to a writer. // -// Producer and service processes share a host, so the ABI uses its byte order. +// The ABI assumes little-endian producer and service processes. // Sharing over IPC // ---------------- @@ -118,15 +118,15 @@ constexpr size_t kDefaultChunkBudgetBytes = 128 * 1024; // +--------------+--------------+--------------+--------------+ // |<---------- three atomic -------->| // -// Writers check capacity by loading rw_positions once. Its low 32 bits hold -// read_pos and its high 32 bits write_pos; the byte offsets above are for -// little-endian hosts and swap on big-endian ones. +// Writers check capacity by loading rw_positions once. In memory: +// - The first four bytes hold read_pos. +// - The next four bytes hold write_pos. // Keeping both in one atomic avoids combining counters read at different times. // // The reader is the only one that moves read_pos. It publishes a new value once // per drain pass and then wakes any writer parked on a full ring buffer. -// The four bytes of rw_positions holding read_pos are also the address the -// futex waits on. +// read_pos is also the first four bytes of rw_positions, which is the address +// the futex waits on. // // num_writers_waiting lets the reader skip a futex wake when nobody is waiting // for space. It is only an optimization and never decides whether the ring @@ -475,7 +475,7 @@ enum class ChunkState : uint32_t { }; enum class ChunkFormat : uint32_t { - // Bytes 4..5 contain a host-endian target BufferID. Payload starts at 6. + // Bytes 4..5 contain a little-endian target BufferID. Payload starts at 6. kTargetBuffer = 0, // Reserved for a format carrying per-packet routing information. From 45a8e70cacdf87bdc3319bfa8c66e82e6794eab3 Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Mon, 26 Oct 2026 17:41:33 +0100 Subject: [PATCH 3/4] fixup! perfetto: support big-endian in trace_processor_shell paths --- src/trace_processor/util/sql_bundle.h | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/trace_processor/util/sql_bundle.h b/src/trace_processor/util/sql_bundle.h index 91b938bde3f..85bd5aecd4f 100644 --- a/src/trace_processor/util/sql_bundle.h +++ b/src/trace_processor/util/sql_bundle.h @@ -23,6 +23,7 @@ #include #include +#include "perfetto/base/endian.h" #include "perfetto/base/logging.h" namespace perfetto::trace_processor { @@ -77,12 +78,14 @@ class SqlBundle { return; uint32_t path_size; std::memcpy(&path_size, next_, sizeof(path_size)); + path_size = base::LE32ToHost(path_size); next_ += sizeof(path_size); entry_.path = reinterpret_cast(next_); next_ += path_size + 1; // +1 for the NUL terminator. uint32_t sql_size; std::memcpy(&sql_size, next_, sizeof(sql_size)); + sql_size = base::LE32ToHost(sql_size); next_ += sizeof(sql_size); entry_.sql = std::string_view(reinterpret_cast(next_), sql_size); From f43cd7ff1b75e895eba758b92d46b8ecd1de1569 Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Mon, 26 Oct 2026 18:13:01 +0100 Subject: [PATCH 4/4] fixup! perfetto: support big-endian in trace_processor_shell paths --- src/trace_processor/core/dataframe/arrow_deserializer.cc | 4 ++-- src/trace_processor/core/dataframe/arrow_serializer.cc | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/trace_processor/core/dataframe/arrow_deserializer.cc b/src/trace_processor/core/dataframe/arrow_deserializer.cc index 2f816c69fd5..5c6c637e6be 100644 --- a/src/trace_processor/core/dataframe/arrow_deserializer.cc +++ b/src/trace_processor/core/dataframe/arrow_deserializer.cc @@ -601,10 +601,10 @@ base::StatusOr DeserializeFromArrow( ++dataframe.non_column_mutations_; dataframe.Finalize(); return dataframe; -#else // PERFETTO_IS_LITTLE_ENDIAN() +#else // PERFETTO_IS_LITTLE_ENDIAN() return base::ErrStatus( "Arrow deserialization is not supported on big-endian hosts"); -#endif // PERFETTO_IS_LITTLE_ENDIAN() +#endif // PERFETTO_IS_LITTLE_ENDIAN() } } // namespace perfetto::trace_processor::core::dataframe diff --git a/src/trace_processor/core/dataframe/arrow_serializer.cc b/src/trace_processor/core/dataframe/arrow_serializer.cc index 4485e7bd880..4d40d08bb9e 100644 --- a/src/trace_processor/core/dataframe/arrow_serializer.cc +++ b/src/trace_processor/core/dataframe/arrow_serializer.cc @@ -655,10 +655,10 @@ base::StatusOr ArrowSerializer::Prepare(const Dataframe& dataframe, prepared_string_pool_ = &pool; prepared_mutations_ = dataframe.mutations(); return static_cast(total); -#else // PERFETTO_IS_LITTLE_ENDIAN() +#else // PERFETTO_IS_LITTLE_ENDIAN() return base::ErrStatus( "Arrow serialization is not supported on big-endian hosts"); -#endif // PERFETTO_IS_LITTLE_ENDIAN() +#endif // PERFETTO_IS_LITTLE_ENDIAN() } base::Status ArrowSerializer::WriteDictionary(const DictionaryPlan& dictionary,