v8/test/unittests/diagnostics/gdb-jit-unittest.cc
Ng Zhi An 5e8e2d04a3 [gdbjit] Fix overlapping AddressRegion check
Whenever we are adding a new AddressRegion to the CodeMap, we first
remove all overlapping regions. The logic to check for overlapping
region is incomplete. For example, if all existing regions are less than
the region to be added, we incorrectly remove all regions, effectively
deleting all JITCodeEntry we have constructed.

We extract this overlapping check into a helper function, so that we can
unittest this without worrying about JITCodeEvent functionality, and also
without dealing with V8 internals (like Isolate and SFI).

The overlapping logic is rather hard to understand, has many special
cases, it will probably be much easier to just loop through all the
entries, rather than using lower_bound. Ideally, we can refactor this to
use some sort of sweep-line algorithm. Hopefully the unittests catch the
most obvious cases.

Bug: v8:11908
Change-Id: Id96975599ac59974185c3dbf64cdfceb17e98d18
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/3105381
Commit-Queue: Zhi An Ng <zhin@chromium.org>
Reviewed-by: Jakob Kummerow <jkummerow@chromium.org>
Cr-Commit-Position: refs/heads/main@{#76397}
2021-08-19 17:29:57 +00:00

59 lines
1.6 KiB
C++

// Copyright 2021 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "src/diagnostics/gdb-jit.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace v8 {
namespace internal {
namespace GDBJITInterface {
#ifdef ENABLE_GDB_JIT_INTERFACE
TEST(GDBJITTest, OverlapEntries) {
ClearCodeMapForTesting();
base::AddressRegion ar{10, 10};
AddRegionForTesting(ar);
// Full containment.
ASSERT_EQ(1u, NumOverlapEntriesForTesting({11, 2}));
// Overlap start.
ASSERT_EQ(1u, NumOverlapEntriesForTesting({5, 10}));
// Overlap end.
ASSERT_EQ(1u, NumOverlapEntriesForTesting({15, 10}));
// No overlap.
// Completely smaller.
ASSERT_EQ(0u, NumOverlapEntriesForTesting({5, 5}));
// Completely bigger.
ASSERT_EQ(0u, NumOverlapEntriesForTesting({20, 10}));
AddRegionForTesting({20, 10});
// Now we have 2 code entries that don't overlap:
// [ entry 1 ][entry 2]
// ^ 10 ^ 20
// Overlap none.
ASSERT_EQ(0u, NumOverlapEntriesForTesting({0, 5}));
ASSERT_EQ(0u, NumOverlapEntriesForTesting({30, 5}));
// Overlap one.
ASSERT_EQ(1u, NumOverlapEntriesForTesting({15, 5}));
ASSERT_EQ(1u, NumOverlapEntriesForTesting({20, 5}));
// Overlap both.
ASSERT_EQ(2u, NumOverlapEntriesForTesting({15, 10}));
ASSERT_EQ(2u, NumOverlapEntriesForTesting({5, 20}));
ASSERT_EQ(2u, NumOverlapEntriesForTesting({15, 20}));
ASSERT_EQ(2u, NumOverlapEntriesForTesting({0, 40}));
ClearCodeMapForTesting();
}
#endif
} // namespace GDBJITInterface
} // namespace internal
} // namespace v8