From 135016a1feee5576014f377e8006057837ee73a2 Mon Sep 17 00:00:00 2001 From: Dmitry Petrochenko Date: Thu, 3 Apr 2014 14:35:54 +0700 Subject: [PATCH] art: Use SIRT::GetAlignedSirtSizeTarget in calling convention Calculate frame size based on SIRT::GetAlignedSirtSizeTarget existing method. Make offset functions pointer-size-dependent for cross-compiling. Add a test to check whether our computations are correct. Change-Id: Ic66daf6f9908890eda906bdcbbc4444c4fef614f Signed-off-by: Dmitry Petrochenko --- build/Android.gtest.mk | 1 + compiler/jni/quick/arm/calling_convention_arm.cc | 2 +- .../jni/quick/arm64/calling_convention_arm64.cc | 2 +- compiler/jni/quick/calling_convention.h | 6 +-- compiler/jni/quick/mips/calling_convention_mips.cc | 2 +- compiler/jni/quick/x86/calling_convention_x86.cc | 2 +- .../jni/quick/x86_64/calling_convention_x86_64.cc | 2 +- runtime/stack_indirect_reference_table.h | 18 ++++--- runtime/stack_indirect_reference_table_test.cc | 58 ++++++++++++++++++++++ 9 files changed, 78 insertions(+), 15 deletions(-) create mode 100644 runtime/stack_indirect_reference_table_test.cc diff --git a/build/Android.gtest.mk b/build/Android.gtest.mk index ef5819dd5..22e6df4be 100644 --- a/build/Android.gtest.mk +++ b/build/Android.gtest.mk @@ -59,6 +59,7 @@ RUNTIME_GTEST_COMMON_SRC_FILES := \ runtime/verifier/method_verifier_test.cc \ runtime/verifier/reg_type_test.cc \ runtime/zip_archive_test.cc \ + runtime/stack_indirect_reference_table_test.cc COMPILER_GTEST_COMMON_SRC_FILES := \ runtime/jni_internal_test.cc \ diff --git a/compiler/jni/quick/arm/calling_convention_arm.cc b/compiler/jni/quick/arm/calling_convention_arm.cc index ab39d6baa..ae18d2e94 100644 --- a/compiler/jni/quick/arm/calling_convention_arm.cc +++ b/compiler/jni/quick/arm/calling_convention_arm.cc @@ -145,7 +145,7 @@ size_t ArmJniCallingConvention::FrameSize() { // Method*, LR and callee save area size, local reference segment state size_t frame_data_size = (3 + CalleeSaveRegisters().size()) * kFramePointerSize; // References plus 2 words for SIRT header - size_t sirt_size = (ReferenceCount() + 2) * sirt_pointer_size_; + size_t sirt_size = StackIndirectReferenceTable::GetAlignedSirtSizeTarget(kFramePointerSize, ReferenceCount()); // Plus return value spill area size return RoundUp(frame_data_size + sirt_size + SizeOfReturnValue(), kStackAlignment); } diff --git a/compiler/jni/quick/arm64/calling_convention_arm64.cc b/compiler/jni/quick/arm64/calling_convention_arm64.cc index c408fa97c..2d1be9d75 100644 --- a/compiler/jni/quick/arm64/calling_convention_arm64.cc +++ b/compiler/jni/quick/arm64/calling_convention_arm64.cc @@ -175,7 +175,7 @@ size_t Arm64JniCallingConvention::FrameSize() { // Method*, LR and callee save area size, local reference segment state size_t frame_data_size = (3 + CalleeSaveRegisters().size()) * kFramePointerSize; // References plus 2 words for SIRT header - size_t sirt_size = (ReferenceCount() + 2) * sirt_pointer_size_; + size_t sirt_size = StackIndirectReferenceTable::GetAlignedSirtSizeTarget(kFramePointerSize, ReferenceCount()); // Plus return value spill area size return RoundUp(frame_data_size + sirt_size + SizeOfReturnValue(), kStackAlignment); } diff --git a/compiler/jni/quick/calling_convention.h b/compiler/jni/quick/calling_convention.h index 7e1cf630c..76d237e94 100644 --- a/compiler/jni/quick/calling_convention.h +++ b/compiler/jni/quick/calling_convention.h @@ -299,17 +299,17 @@ class JniCallingConvention : public CallingConvention { FrameOffset SirtLinkOffset() const { return FrameOffset(SirtOffset().Int32Value() + - StackIndirectReferenceTable::LinkOffset()); + StackIndirectReferenceTable::LinkOffset(frame_pointer_size_)); } FrameOffset SirtNumRefsOffset() const { return FrameOffset(SirtOffset().Int32Value() + - StackIndirectReferenceTable::NumberOfReferencesOffset()); + StackIndirectReferenceTable::NumberOfReferencesOffset(frame_pointer_size_)); } FrameOffset SirtReferencesOffset() const { return FrameOffset(SirtOffset().Int32Value() + - StackIndirectReferenceTable::ReferencesOffset()); + StackIndirectReferenceTable::ReferencesOffset(frame_pointer_size_)); } virtual ~JniCallingConvention() {} diff --git a/compiler/jni/quick/mips/calling_convention_mips.cc b/compiler/jni/quick/mips/calling_convention_mips.cc index 51a3f5488..8e1c0c7a7 100644 --- a/compiler/jni/quick/mips/calling_convention_mips.cc +++ b/compiler/jni/quick/mips/calling_convention_mips.cc @@ -149,7 +149,7 @@ size_t MipsJniCallingConvention::FrameSize() { // Method*, LR and callee save area size, local reference segment state size_t frame_data_size = (3 + CalleeSaveRegisters().size()) * kFramePointerSize; // References plus 2 words for SIRT header - size_t sirt_size = (ReferenceCount() + 2) * sirt_pointer_size_; + size_t sirt_size = StackIndirectReferenceTable::GetAlignedSirtSizeTarget(kFramePointerSize, ReferenceCount()); // Plus return value spill area size return RoundUp(frame_data_size + sirt_size + SizeOfReturnValue(), kStackAlignment); } diff --git a/compiler/jni/quick/x86/calling_convention_x86.cc b/compiler/jni/quick/x86/calling_convention_x86.cc index 8b440eda4..153f9538d 100644 --- a/compiler/jni/quick/x86/calling_convention_x86.cc +++ b/compiler/jni/quick/x86/calling_convention_x86.cc @@ -126,7 +126,7 @@ size_t X86JniCallingConvention::FrameSize() { // Method*, return address and callee save area size, local reference segment state size_t frame_data_size = (3 + CalleeSaveRegisters().size()) * kFramePointerSize; // References plus 2 words for SIRT header - size_t sirt_size = (ReferenceCount() + 2) * sirt_pointer_size_; + size_t sirt_size = StackIndirectReferenceTable::GetAlignedSirtSizeTarget(kFramePointerSize, ReferenceCount()); // Plus return value spill area size return RoundUp(frame_data_size + sirt_size + SizeOfReturnValue(), kStackAlignment); } diff --git a/compiler/jni/quick/x86_64/calling_convention_x86_64.cc b/compiler/jni/quick/x86_64/calling_convention_x86_64.cc index 21e0bd7f6..4dfa29a46 100644 --- a/compiler/jni/quick/x86_64/calling_convention_x86_64.cc +++ b/compiler/jni/quick/x86_64/calling_convention_x86_64.cc @@ -141,7 +141,7 @@ size_t X86_64JniCallingConvention::FrameSize() { // Method*, return address and callee save area size, local reference segment state size_t frame_data_size = (3 + CalleeSaveRegisters().size()) * kFramePointerSize; // References plus link_ (pointer) and number_of_references_ (uint32_t) for SIRT header - size_t sirt_size = kFramePointerSize + sizeof(uint32_t) + (ReferenceCount() * sirt_pointer_size_); + size_t sirt_size = StackIndirectReferenceTable::GetAlignedSirtSizeTarget(kFramePointerSize, ReferenceCount()); // Plus return value spill area size return RoundUp(frame_data_size + sirt_size + SizeOfReturnValue(), kStackAlignment); } diff --git a/runtime/stack_indirect_reference_table.h b/runtime/stack_indirect_reference_table.h index 6049e062a..b113129db 100644 --- a/runtime/stack_indirect_reference_table.h +++ b/runtime/stack_indirect_reference_table.h @@ -44,6 +44,10 @@ class StackIndirectReferenceTable { return number_of_references_; } + // We have versions with and without explicit pointer size of the following. The first two are + // used at runtime, so OFFSETOF_MEMBER computes the right offsets automatically. The last one + // takes the pointer size explicitly so that at compile time we can cross-compile correctly. + // Returns the size of a StackIndirectReferenceTable containing num_references sirts. static size_t SizeOf(uint32_t num_references) { size_t header_size = OFFSETOF_MEMBER(StackIndirectReferenceTable, references_); @@ -60,7 +64,7 @@ class StackIndirectReferenceTable { // Get the size of the SIRT for the number of entries, with padding added for potential alignment. static size_t GetAlignedSirtSizeTarget(size_t pointer_size, uint32_t num_references) { // Assume that the layout is packed. - size_t header_size = pointer_size + sizeof(uint32_t); + size_t header_size = pointer_size + sizeof(number_of_references_); // This assumes there is no layout change between 32 and 64b. size_t data_size = sizeof(StackReference) * num_references; size_t sirt_size = header_size + data_size; @@ -109,18 +113,18 @@ class StackIndirectReferenceTable { } // Offset of link within SIRT, used by generated code - static size_t LinkOffset() { - return OFFSETOF_MEMBER(StackIndirectReferenceTable, link_); + static size_t LinkOffset(size_t pointer_size) { + return 0; } // Offset of length within SIRT, used by generated code - static uint32_t NumberOfReferencesOffset() { - return OFFSETOF_MEMBER(StackIndirectReferenceTable, number_of_references_); + static size_t NumberOfReferencesOffset(size_t pointer_size) { + return pointer_size; } // Offset of link within SIRT, used by generated code - static size_t ReferencesOffset() { - return OFFSETOF_MEMBER(StackIndirectReferenceTable, references_); + static size_t ReferencesOffset(size_t pointer_size) { + return pointer_size + sizeof(number_of_references_); } private: diff --git a/runtime/stack_indirect_reference_table_test.cc b/runtime/stack_indirect_reference_table_test.cc new file mode 100644 index 000000000..72ef6b6a4 --- /dev/null +++ b/runtime/stack_indirect_reference_table_test.cc @@ -0,0 +1,58 @@ +/* + * Copyright (C) 2014 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 "stack_indirect_reference_table.h" +#include "gtest/gtest.h" + +namespace art { + +// Test the offsets computed for members of StackIndirectReferenceTable. Because of cross-compiling +// it is impossible the use OFFSETOF_MEMBER, so we do some reasonable computations ourselves. This +// test checks whether we do the right thing. +TEST(StackIndirectReferenceTableTest, Offsets) { + // As the members of StackIndirectReferenceTable are private, we cannot use OFFSETOF_MEMBER + // here. So do the inverse: set some data, and access it through pointers created from the offsets. + + StackIndirectReferenceTable test_table(reinterpret_cast(0x1234)); + test_table.SetLink(reinterpret_cast(0x5678)); + test_table.SetNumberOfReferences(0x9ABC); + + byte* table_base_ptr = reinterpret_cast(&test_table); + + { + uintptr_t* link_ptr = reinterpret_cast(table_base_ptr + + StackIndirectReferenceTable::LinkOffset(kPointerSize)); + EXPECT_EQ(*link_ptr, static_cast(0x5678)); + } + + { + uint32_t* num_ptr = reinterpret_cast(table_base_ptr + + StackIndirectReferenceTable::NumberOfReferencesOffset(kPointerSize)); + EXPECT_EQ(*num_ptr, static_cast(0x9ABC)); + } + + { + // Assume sizeof(StackReference) == sizeof(uint32_t) + // TODO: How can we make this assumption-less but still access directly and fully? + EXPECT_EQ(sizeof(StackReference), sizeof(uint32_t)); + + uint32_t* ref_ptr = reinterpret_cast(table_base_ptr + + StackIndirectReferenceTable::ReferencesOffset(kPointerSize)); + EXPECT_EQ(*ref_ptr, static_cast(0x1234)); + } +} + +} // namespace art -- 2.11.0