/*
* Copyright ( C ) 2015 The Android Open Source Project
*
* Licensed under the Apache License , Version 2 . 0 ( the " License " ) ;
* you may not use this file except in compliance with the License .
* You may obtain a copy of the License at
*
* http : //www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing , software
* distributed under the License is distributed on an " AS IS " BASIS ,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND , either express or implied .
* See the License for the specific language governing permissions and
* limitations under the License .
*/
#include "linker/x86/relative_patcher_x86.h"
#include "linker/relative_patcher_test.h"
namespace art {
namespace linker {
class X86RelativePatcherTest : public RelativePatcherTest {
public :
X86RelativePatcherTest() : RelativePatcherTest(InstructionSet::kX86, "default" ) { }
protected :
static const uint8_t kCallRawCode[];
static const ArrayRef<const uint8_t> kCallCode;
uint32_t GetMethodOffset(uint32_t method_idx) {
auto result = method_offset_map_.FindMethodOffset(MethodRef(method_idx));
CHECK(result.first);
return result.second;
}
};
const uint8_t X86RelativePatcherTest::kCallRawCode[] = {
0 xe8, 0 x00, 0 x01, 0 x00, 0 x00
};
const ArrayRef<const uint8_t> X86RelativePatcherTest::kCallCode(kCallRawCode);
TEST_F(X86RelativePatcherTest, CallSelf) {
LinkerPatch patches[] = {
LinkerPatch::RelativeCodePatch(kCallCode.size() - 4 u, nullptr, 1 u),
};
AddCompiledMethod(MethodRef(1 u), kCallCode, ArrayRef<const LinkerPatch>(patches));
Link();
static const uint8_t expected_code[] = {
0 xe8, 0 xfb, 0 xff, 0 xff, 0 xff
};
EXPECT_TRUE(CheckLinkedMethod(MethodRef(1 u), ArrayRef<const uint8_t>(expected_code)));
}
TEST_F(X86RelativePatcherTest, CallOther) {
LinkerPatch method1_patches[] = {
LinkerPatch::RelativeCodePatch(kCallCode.size() - 4 u, nullptr, 2 u),
};
AddCompiledMethod(MethodRef(1 u), kCallCode, ArrayRef<const LinkerPatch>(method1_patches));
LinkerPatch method2_patches[] = {
LinkerPatch::RelativeCodePatch(kCallCode.size() - 4 u, nullptr, 1 u),
};
AddCompiledMethod(MethodRef(2 u), kCallCode, ArrayRef<const LinkerPatch>(method2_patches));
Link();
uint32_t method1_offset = GetMethodOffset(1 u);
uint32_t method2_offset = GetMethodOffset(2 u);
uint32_t diff_after = method2_offset - (method1_offset + kCallCode.size() /* PC adjustment */);
static const uint8_t method1_expected_code[] = {
0 xe8,
static_cast <uint8_t>(diff_after),
static_cast <uint8_t>(diff_after >> 8 ),
static_cast <uint8_t>(diff_after >> 16 ),
static_cast <uint8_t>(diff_after >> 24 )
};
EXPECT_TRUE(CheckLinkedMethod(MethodRef(1 u), ArrayRef<const uint8_t>(method1_expected_code)));
uint32_t diff_before = method1_offset - (method2_offset + kCallCode.size() /* PC adjustment */);
static const uint8_t method2_expected_code[] = {
0 xe8,
static_cast <uint8_t>(diff_before),
static_cast <uint8_t>(diff_before >> 8 ),
static_cast <uint8_t>(diff_before >> 16 ),
static_cast <uint8_t>(diff_before >> 24 )
};
EXPECT_TRUE(CheckLinkedMethod(MethodRef(2 u), ArrayRef<const uint8_t>(method2_expected_code)));
}
TEST_F(X86RelativePatcherTest, CallTrampoline) {
LinkerPatch patches[] = {
LinkerPatch::RelativeCodePatch(kCallCode.size() - 4 u, nullptr, 2 u),
};
AddCompiledMethod(MethodRef(1 u), kCallCode, ArrayRef<const LinkerPatch>(patches));
Link();
auto result = method_offset_map_.FindMethodOffset(MethodRef(1 ));
ASSERT_TRUE(result.first);
uint32_t diff = kTrampolineOffset - (result.second + kCallCode.size());
static const uint8_t expected_code[] = {
0 xe8,
static_cast <uint8_t>(diff),
static_cast <uint8_t>(diff >> 8 ),
static_cast <uint8_t>(diff >> 16 ),
static_cast <uint8_t>(diff >> 24 )
};
EXPECT_TRUE(CheckLinkedMethod(MethodRef(1 u), ArrayRef<const uint8_t>(expected_code)));
}
TEST_F(X86RelativePatcherTest, StringBssEntry) {
bss_begin_ = 0 x12345678;
constexpr size_t kStringEntryOffset = 0 x1234;
constexpr uint32_t kStringIndex = 1 u;
string_index_to_offset_map_.Put(kStringIndex, kStringEntryOffset);
static const uint8_t raw_code[] = {
0 xe8, 0 x00, 0 x00, 0 x00, 0 x00, // call +0
0 x5b, // pop ebx
0 x8b, 0 x83, 0 x00, 0 x01, 0 x00, 0 x00, // mov eax, [ebx + 256 (kPlaceholder32BitOffset)]
};
constexpr uint32_t anchor_offset = 5 u; // After call +0.
ArrayRef<const uint8_t> code(raw_code);
LinkerPatch patches[] = {
LinkerPatch::StringBssEntryPatch(code.size() - 4 u, nullptr, anchor_offset, kStringIndex),
};
AddCompiledMethod(MethodRef(1 u), code, ArrayRef<const LinkerPatch>(patches));
Link();
auto result = method_offset_map_.FindMethodOffset(MethodRef(1 u));
ASSERT_TRUE(result.first);
uint32_t diff = bss_begin_ + kStringEntryOffset - (result.second + anchor_offset);
static const uint8_t expected_code[] = {
0 xe8, 0 x00, 0 x00, 0 x00, 0 x00, // call +0
0 x5b, // pop ebx
0 x8b, 0 x83, // mov eax, [ebx + diff]
static_cast <uint8_t>(diff),
static_cast <uint8_t>(diff >> 8 ),
static_cast <uint8_t>(diff >> 16 ),
static_cast <uint8_t>(diff >> 24 )
};
EXPECT_TRUE(CheckLinkedMethod(MethodRef(1 u), ArrayRef<const uint8_t>(expected_code)));
}
TEST_F(X86RelativePatcherTest, StringReference) {
constexpr uint32_t kStringIndex = 1 u;
constexpr uint32_t kStringOffset = 0 x12345678;
string_index_to_offset_map_.Put(kStringIndex, kStringOffset);
static const uint8_t raw_code[] = {
0 xe8, 0 x00, 0 x00, 0 x00, 0 x00, // call +0
0 x5b, // pop ebx
0 x8d, 0 x83, 0 x00, 0 x01, 0 x00, 0 x00, // lea eax, [ebx + 256 (kPlaceholder32BitOffset)]
};
constexpr uint32_t anchor_offset = 5 u; // After call +0.
ArrayRef<const uint8_t> code(raw_code);
LinkerPatch patches[] = {
LinkerPatch::RelativeStringPatch(code.size() - 4 u, nullptr, anchor_offset, kStringIndex),
};
AddCompiledMethod(MethodRef(1 u), code, ArrayRef<const LinkerPatch>(patches));
Link();
auto result = method_offset_map_.FindMethodOffset(MethodRef(1 u));
ASSERT_TRUE(result.first);
uint32_t diff = kStringOffset - (result.second + anchor_offset);
static const uint8_t expected_code[] = {
0 xe8, 0 x00, 0 x00, 0 x00, 0 x00, // call +0
0 x5b, // pop ebx
0 x8d, 0 x83, // lea eax, [ebx + diff]
static_cast <uint8_t>(diff),
static_cast <uint8_t>(diff >> 8 ),
static_cast <uint8_t>(diff >> 16 ),
static_cast <uint8_t>(diff >> 24 )
};
EXPECT_TRUE(CheckLinkedMethod(MethodRef(1 u), ArrayRef<const uint8_t>(expected_code)));
}
} // namespace linker
} // namespace art
Messung V0.5 in Prozent C=88 H=96 G=91
¤ Dauer der Verarbeitung: 0.9 Sekunden
(vorverarbeitet am 2026-06-29)
¤
*© Formatika GbR, Deutschland