Fix invalid bounds check in ScatteredStreamWriter::ReserveBytes

This wasn't the original instance of the problem, but was another one
caught by -fsanitize=pointer-overflow. In generate, ptr + size is only
valid if the result would say within the array that ptr points to, so
this kind of ptr + size > end bounds check is never valid. It must
always be written as size > end - ptr, though then you need to deal with
end - ptr being a signed value.

[ RUN      ] MessageTest.MessageHandle
../../third_party/perfetto/src/protozero/scattered_stream_writer.cc:69:18: runtime error: applying non-zero offset 4 to null pointer
    #0 0x6039411e8baf in protozero::ScatteredStreamWriter::ReserveBytes(unsigned long) third_party/perfetto/src/protozero/scattered_stream_writer.cc:69:18
    #1 0x603940bd8473 in protozero::(anonymous namespace)::MessageTest::NewMessageWithSizeField() third_party/perfetto/src/protozero/message_unittest.cc:102:25
    #2 0x603940bd6bc9 in protozero::(anonymous namespace)::MessageTest_MessageHandle_Test::TestBody() third_party/perfetto/src/protozero/message_unittest.cc:353:27
    #3 0x60394164c82e in testing::Test::Run() third_party/googletest/src/googletest/src/gtest.cc:2728:5
    #4 0x60394164e270 in testing::TestInfo::Run() third_party/googletest/src/googletest/src/gtest.cc:2874:11
    #5 0x60394165082b in testing::TestSuite::Run() third_party/googletest/src/googletest/src/gtest.cc:3052:30
    #6 0x603941667ce8 in testing::internal::UnitTestImpl::RunAllTests() third_party/googletest/src/googletest/src/gtest.cc:5982:44
    #7 0x603941666be7 in testing::UnitTest::Run() third_party/googletest/src/googletest/src/gtest.cc:5561:10
    #8 0x6039423d36aa in base::TestSuite::Run() base/test/test_suite.cc:420:16
    #9 0x6039423ea1f3 in base::OnceCallback<int ()>::Run() && base/functional/callback.h:156:12
    #10 0x6039423e8a9f in RunTestSuite base/test/launcher/unit_test_launcher.cc:187:38
    #11 0x6039423e8a9f in base::(anonymous namespace)::LaunchUnitTestsInternal(base::OnceCallback<int ()>, unsigned long, int, unsigned long, bool, base::RepeatingCallback<void ()>, base::OnceCallback<void ()>) base/test/launcher/unit_test_launcher.cc:267:10
    #12 0x6039423e86af in base::LaunchUnitTests(int, char**, base::OnceCallback<int ()>, unsigned long) base/test/launcher/unit_test_launcher.cc:332:10
    #13 0x6039423d330c in main base/test/run_all_unittests.cc:79:10
    #14 0x7c3b8de29d8f in __libc_start_call_main csu/../sysdeps/nptl/libc_start_call_main.h:58:16
    #15 0x7c3b8de29e3f in __libc_start_main csu/../csu/libc-start.c:392:3
    #16 0x603940994109 in _start (/b/s/w/ir/out/6034-linux_chromium_/perfetto_unittests+0x23c4109) (BuildId: e58a1993916b9841)

SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior ../../third_party/perfetto/src/protozero/scattered_stream_writer.cc:69:18

Bug: 384391188
Change-Id: I5a3d8a1e74885571f281bb2fb1b9cc9b5374da66
1 file changed
tree: eccd9d174639356071d05948032fbbdec0e8ed02
  1. .github/
  2. bazel/
  3. build_overrides/
  4. buildtools/
  5. debian/
  6. docs/
  7. examples/
  8. gn/
  9. include/
  10. infra/
  11. protos/
  12. python/
  13. src/
  14. test/
  15. third_party/
  16. tools/
  17. ui/
  18. .bazelignore
  19. .bazelrc
  20. .clang-format
  21. .clang-tidy
  22. .git-blame-ignore-revs
  23. .gitattributes
  24. .gitignore
  25. .gn
  26. .style.yapf
  27. Android.bp
  28. Android.bp.extras
  29. BUILD
  30. BUILD.extras
  31. BUILD.gn
  32. CHANGELOG
  33. codereview.settings
  34. DIR_METADATA
  35. heapprofd.rc
  36. LICENSE
  37. meson.build
  38. METADATA
  39. MODULE_LICENSE_APACHE2
  40. OWNERS
  41. perfetto.rc
  42. PerfettoIntegrationTests.xml
  43. persistent_cfg.pbtxt
  44. PRESUBMIT.py
  45. README.chromium
  46. README.md
  47. TEST_MAPPING
  48. traced_perf.rc
  49. WATCHLISTS
  50. WORKSPACE
README.md

Perfetto - System profiling, app tracing and trace analysis

Perfetto is a production-grade open-source stack for performance instrumentation and trace analysis. It offers services and libraries and for recording system-level and app-level traces, native + java heap profiling, a library for analyzing traces using SQL and a web-based UI to visualize and explore multi-GB traces.

See https://perfetto.dev/docs or the /docs/ directory for documentation.