diff --git a/include/perfetto/base/endian.h b/include/perfetto/base/endian.h index 269e9971dd0..43db833c12e 100644 --- a/include/perfetto/base/endian.h +++ b/include/perfetto/base/endian.h @@ -224,6 +224,9 @@ inline double HostToLE(double x) { return HostToLEDouble(x); } +inline uint16_t LEToHost(uint16_t x) { + return LE16ToHost(x); +} inline uint32_t LEToHost(uint32_t x) { return LE32ToHost(x); } diff --git a/src/trace_processor/core/dataframe/arrow_deserializer.cc b/src/trace_processor/core/dataframe/arrow_deserializer.cc index 160c43143ea..5c6c637e6be 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..4d40d08bb9e 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..1e3ac3a13e5 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); 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/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); 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