Skip to content

Commit

Permalink
Merge changes I81e73bdd,Ic429735f into main
Browse files Browse the repository at this point in the history
* changes:
  Avoid builtin overflow arithmetic
  implicit_segment_forest: Fix MSVC warning C4146
  • Loading branch information
ddiproietto authored and Gerrit Code Review committed Jan 21, 2025
2 parents c78fc82 + e81d243 commit cc4b7aa
Show file tree
Hide file tree
Showing 6 changed files with 10 additions and 21 deletions.
2 changes: 1 addition & 1 deletion src/trace_processor/containers/implicit_segment_forest.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ class ImplicitSegmentForest {
uint32_t size() const { return static_cast<uint32_t>(values_.size() / 2); }

private:
static uint32_t Lsp(uint32_t x) { return x & -x; }
static uint32_t Lsp(uint32_t x) { return x & ~(x - 1); }
static uint32_t Msp(uint32_t x) {
return (1u << (sizeof(x) * 8 - 1)) >> __builtin_clz(x);
}
Expand Down
4 changes: 3 additions & 1 deletion src/trace_processor/importers/etm/etm_v4_stream.cc
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,9 @@ base::Status EtmV4Stream::ParseFramedData(uint64_t offset, TraceBlobView data) {
data.data(), &num_bytes_processed));
PERFETTO_CHECK(keep_going);
PERFETTO_CHECK(num_bytes_processed == data_block_size);
PERFETTO_CHECK(perf_importer::SafeAdd(index_, data_block_size, &index_));
PERFETTO_CHECK(index_ <= std::numeric_limits<decltype(index_)>::max() -
data_block_size);
index_ += data_block_size;

ASSIGN_OR_RETURN(keep_going, frame_decoder_->TraceDataIn(
OCSD_OP_EOT, index_, 0, nullptr, nullptr));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,15 +130,16 @@ ReadCpuConfig(perf_importer::Reader& reader) {
}

uint32_t cpu;
if (!perf_importer::SafeAdd(cpu_header.cpu, 0, &cpu)) {
if (!perf_importer::SafeCast(cpu_header.cpu, &cpu)) {
return base::ErrStatus("Integer overflow in ETM info header");
}

uint32_t size;
if (!perf_importer::SafeMultiply(cpu_header.trace_parameter_count, 8,
&size)) {
if (cpu_header.trace_parameter_count >
std::numeric_limits<uint32_t>::max() / 8) {
return base::ErrStatus("Integer overflow in ETM info header");
}
size = static_cast<uint32_t>(cpu_header.trace_parameter_count * 8);

TraceBlobView blob;
if (!reader.ReadBlob(blob, size)) {
Expand Down
3 changes: 1 addition & 2 deletions src/trace_processor/importers/perf/aux_record.cc
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ base::Status AuxRecord::Parse(const Record& record) {
return base::ErrStatus("Failed to parse AUX record");
}

uint64_t unused;
if (!SafeAdd(offset, size, &unused)) {
if (offset > std::numeric_limits<decltype(offset)>::max() - size) {
return base::ErrStatus("AUX record overflows");
}

Expand Down
4 changes: 1 addition & 3 deletions src/trace_processor/importers/perf/auxtrace_record.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
#include "perfetto/base/status.h"
#include "src/trace_processor/importers/perf/reader.h"
#include "src/trace_processor/importers/perf/record.h"
#include "src/trace_processor/importers/perf/util.h"

namespace perfetto::trace_processor::perf_importer {

Expand All @@ -29,8 +28,7 @@ base::Status AuxtraceRecord::Parse(const Record& record) {
return base::ErrStatus("Failed to parse PERF_RECORD_AUXTRACE");
}

uint64_t unused;
if (!SafeAdd(offset, size, &unused)) {
if (offset > std::numeric_limits<decltype(offset)>::max() - size) {
return base::ErrStatus("AUXTRACE record overflows");
}

Expand Down
11 changes: 0 additions & 11 deletions src/trace_processor/importers/perf/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,10 @@
#define SRC_TRACE_PROCESSOR_IMPORTERS_PERF_UTIL_H_

#include <cstddef>
#include <cstdint>
#include <type_traits>

namespace perfetto::trace_processor::perf_importer {

template <typename A, typename B, typename Res>
inline bool SafeAdd(A a, B b, Res* result) {
return !__builtin_add_overflow(a, b, result);
}

template <typename A, typename B, typename Res>
inline bool SafeMultiply(A a, B b, Res* result) {
return !__builtin_mul_overflow(a, b, result);
}

template <typename A,
typename Res,
typename = std::enable_if_t<std::is_integral<A>::value &&
Expand Down

0 comments on commit cc4b7aa

Please sign in to comment.