v8/test/unittests/codegen/source-position-table-unittest.cc
Clemens Backes 5c237388f0 [backend] Fix source position annotations
If two call instructions were generated right after each other, the
source position table could get populated with two entries for the same
PC (triggered by the follow-up CL: https://crrev.com/c/2697359).
This CL fixes that by slightly changing the carry-over of source
positions from nodes to instructions.

The call node which has a source position attached generates two
instructions:
      18: gap () ([rax|R|tp] = v16(-); [rbx|R|t] = v17(-);)
          [rax|R|t] = ArchCallWasmFunction [immediate:4] #-1 [rax|R|tp] [rbx|R|t] [immediate:5]
      19: gap () ()
          ArchJmp [immediate:6]

Those are then reversed, and the source position is attached to the first
one (the ArchJmp). After reversing it again later, the source position
will be set to the pc *after* the call instruction, which in the example
happened to be just another call instruction which already had a source
position, resulting in this code:

[...]
0x388ee467d426    66  e875feffff     call 0x388ee467d2a0     ;; wasm stub: WasmThrow
0x388ee467d42b    6b  e850feffff     call 0x388ee467d280     ;; wasm stub: WasmStackGuard
[...]
Source positions:
 pc offset  position
        6b         5
        6b         0

By attaching the source position to the *last* instruction (after
reversing), we ensure that it will be generated for an instruction
*before* the call, or the call itself if this is the first instruction
emitted for that node.

R=jgruber@chromium.org

Bug: v8:11490, v8:11496
Change-Id: Ie95c87d0d9daea56ca14a811abcd02ac07a4cf84
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/2697358
Commit-Queue: Clemens Backes <clemensb@chromium.org>
Reviewed-by: Jakob Gruber <jgruber@chromium.org>
Cr-Commit-Position: refs/heads/master@{#72951}
2021-02-23 12:49:11 +00:00

100 lines
3.1 KiB
C++

// Copyright 2016 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/init/v8.h"
#include "src/codegen/source-position-table.h"
#include "src/objects/objects.h"
#include "test/unittests/test-utils.h"
namespace v8 {
namespace internal {
namespace interpreter {
class SourcePositionTableTest : public TestWithIsolate {
public:
SourcePositionTableTest() : zone_(isolate()->allocator(), ZONE_NAME) {}
~SourcePositionTableTest() override = default;
SourcePosition toPos(int offset) {
return SourcePosition(offset, offset % 10 - 1);
}
SourcePositionTableBuilder* builder() { return &builder_; }
private:
Zone zone_;
SourcePositionTableBuilder builder_{&zone_};
};
// Some random offsets, mostly at 'suspicious' bit boundaries.
static int offsets[] = {0, 1, 2, 3, 4, 30, 31, 32,
33, 62, 63, 64, 65, 126, 127, 128,
129, 250, 1000, 9999, 12000, 31415926};
TEST_F(SourcePositionTableTest, EncodeStatement) {
for (size_t i = 0; i < arraysize(offsets); i++) {
builder()->AddPosition(offsets[i], toPos(offsets[i]), true);
}
// To test correctness, we rely on the assertions in ToSourcePositionTable().
// (Also below.)
CHECK(!builder()->ToSourcePositionTable(isolate()).is_null());
}
TEST_F(SourcePositionTableTest, EncodeStatementDuplicates) {
for (size_t i = 0; i < arraysize(offsets); i++) {
builder()->AddPosition(offsets[i], toPos(offsets[i]), true);
builder()->AddPosition(offsets[i], toPos(offsets[i] + 1), true);
}
// To test correctness, we rely on the assertions in ToSourcePositionTable().
// (Also below.)
CHECK(!builder()->ToSourcePositionTable(isolate()).is_null());
}
TEST_F(SourcePositionTableTest, EncodeExpression) {
for (size_t i = 0; i < arraysize(offsets); i++) {
builder()->AddPosition(offsets[i], toPos(offsets[i]), false);
}
CHECK(!builder()->ToSourcePositionTable(isolate()).is_null());
}
TEST_F(SourcePositionTableTest, EncodeAscendingPositive) {
int code_offset = 0;
int source_position = 0;
for (size_t i = 0; i < arraysize(offsets); i++) {
code_offset += offsets[i];
source_position += offsets[i];
if (i % 2) {
builder()->AddPosition(code_offset, toPos(source_position), true);
} else {
builder()->AddPosition(code_offset, toPos(source_position), false);
}
}
CHECK(!builder()->ToSourcePositionTable(isolate()).is_null());
}
TEST_F(SourcePositionTableTest, EncodeAscendingNegative) {
int code_offset = 0;
// Start with a big source position, then decrement it.
int source_position = 1 << 26;
for (size_t i = 0; i < arraysize(offsets); i++) {
code_offset += offsets[i];
source_position -= offsets[i];
if (i % 2) {
builder()->AddPosition(code_offset, toPos(source_position), true);
} else {
builder()->AddPosition(code_offset, toPos(source_position), false);
}
}
CHECK(!builder()->ToSourcePositionTable(isolate()).is_null());
}
} // namespace interpreter
} // namespace internal
} // namespace v8