summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author Santiago Aboy Solanes <solanes@google.com> 2024-03-19 13:58:44 +0000
committer Treehugger Robot <android-test-infra-autosubmit@system.gserviceaccount.com> 2024-03-21 10:57:16 +0000
commit1e78696010ab61314b29802e1772c3bba8ad9b00 (patch)
tree13655f55565d6ebc9d5680ab87d1beb703232f28
parentc8d6e3b523f31baa548934f822c6e36adc2a52e8 (diff)
Improve PointerSize in ImageHeader
We were saving it as a raw uint32 which meant that we were having unnecessary conversion and checks. Bug: 329379384 Bug: 329378408 Test: art/test/testrunner/testrunner.py --host --64 --optimizing -b Test: m test-art-host-gtest Change-Id: Ib6d9a9abfec7588dad4ff345cc6b569df7144782
-rw-r--r--dex2oat/linker/image_test.cc6
-rw-r--r--dex2oat/linker/image_writer.cc2
-rw-r--r--libartbase/base/enums.h3
-rw-r--r--libartbase/base/utils.h14
-rw-r--r--runtime/class_linker.cc11
-rw-r--r--runtime/gc/collector/immune_spaces_test.cc3
-rw-r--r--runtime/mirror/class.h2
-rw-r--r--runtime/oat/image.cc7
-rw-r--r--runtime/oat/image.h8
-rw-r--r--runtime/runtime_image.cc2
10 files changed, 23 insertions, 35 deletions
diff --git a/dex2oat/linker/image_test.cc b/dex2oat/linker/image_test.cc
index f64462e614..959264d634 100644
--- a/dex2oat/linker/image_test.cc
+++ b/dex2oat/linker/image_test.cc
@@ -19,6 +19,7 @@
#include "image_test.h"
+#include "base/enums.h"
#include "oat/image.h"
#include "scoped_thread_state_change-inl.h"
#include "thread.h"
@@ -89,8 +90,7 @@ TEST_F(ImageTest, ImageHeaderIsValid) {
/*boot_image_size=*/ 0u,
/*boot_image_component_count=*/ 0u,
/*boot_image_checksum=*/ 0u,
- sizeof(void*));
-
+ kRuntimePointerSize);
ASSERT_TRUE(image_header.IsValid());
// Please note that for the following condition to be true, the above values should be chosen in
@@ -239,7 +239,7 @@ TEST_F(ImageTest, ImageChecksum) {
/*boot_image_size=*/ 0u,
/*boot_image_component_count=*/ 0u,
/*boot_image_checksum=*/ 0u,
- sizeof(void*));
+ kRuntimePointerSize);
ASSERT_TRUE(image_header.IsValid());
std::string error_msg;
diff --git a/dex2oat/linker/image_writer.cc b/dex2oat/linker/image_writer.cc
index 78d5eca68f..998b48b87a 100644
--- a/dex2oat/linker/image_writer.cc
+++ b/dex2oat/linker/image_writer.cc
@@ -2808,7 +2808,7 @@ void ImageWriter::CreateHeader(size_t oat_index, size_t component_count) {
boot_image_size_,
boot_image_components,
boot_image_checksums,
- static_cast<uint32_t>(target_ptr_size_));
+ target_ptr_size_);
}
ArtMethod* ImageWriter::GetImageMethodAddress(ArtMethod* method) {
diff --git a/libartbase/base/enums.h b/libartbase/base/enums.h
index c5fb880ba5..6f4405d7b6 100644
--- a/libartbase/base/enums.h
+++ b/libartbase/base/enums.h
@@ -18,11 +18,12 @@
#define ART_LIBARTBASE_BASE_ENUMS_H_
#include <cstddef>
+#include <cstdint>
#include <iosfwd>
namespace art {
-enum class PointerSize : size_t {
+enum class PointerSize : uint32_t {
k32 = 4,
k64 = 8
};
diff --git a/libartbase/base/utils.h b/libartbase/base/utils.h
index 5e04cb0074..106c65a2ca 100644
--- a/libartbase/base/utils.h
+++ b/libartbase/base/utils.h
@@ -88,10 +88,6 @@ inline bool TestBitmap(size_t idx, const uint8_t* bitmap) {
return ((bitmap[idx / kBitsPerByte] >> (idx % kBitsPerByte)) & 0x01) != 0;
}
-static inline constexpr bool ValidPointerSize(size_t pointer_size) {
- return pointer_size == 4 || pointer_size == 8;
-}
-
static inline const void* EntryPointToCodePointer(const void* entry_point) {
uintptr_t code = reinterpret_cast<uintptr_t>(entry_point);
// TODO: Make this Thumb2 specific. It is benign on other architectures as code is always at
@@ -133,16 +129,6 @@ bool IsKernelVersionAtLeast(int reqd_major, int reqd_minor);
// On some old kernels, a cache operation may segfault.
WARN_UNUSED bool CacheOperationsMaySegFault();
-template <typename T>
-constexpr PointerSize ConvertToPointerSize(T any) {
- if (any == 4 || any == 8) {
- return static_cast<PointerSize>(any);
- } else {
- LOG(FATAL);
- UNREACHABLE();
- }
-}
-
// Return -1 if <, 0 if ==, 1 if >.
template <typename T>
inline static int32_t Compare(T lhs, T rhs) {
diff --git a/runtime/class_linker.cc b/runtime/class_linker.cc
index ff5803e69f..ecd15ba8f0 100644
--- a/runtime/class_linker.cc
+++ b/runtime/class_linker.cc
@@ -32,6 +32,7 @@
#include <utility>
#include <vector>
+#include "android-base/macros.h"
#include "android-base/stringprintf.h"
#include "android-base/strings.h"
#include "art_field-inl.h"
@@ -40,6 +41,7 @@
#include "base/arena_allocator.h"
#include "base/arena_bit_vector.h"
#include "base/casts.h"
+#include "base/enums.h"
#include "base/file_utils.h"
#include "base/hash_map.h"
#include "base/hash_set.h"
@@ -1301,12 +1303,13 @@ bool ClassLinker::InitFromBootImage(std::string* error_msg) {
std::vector<gc::space::ImageSpace*> spaces = heap->GetBootImageSpaces();
CHECK(!spaces.empty());
const ImageHeader& image_header = spaces[0]->GetImageHeader();
- uint32_t pointer_size_unchecked = image_header.GetPointerSizeUnchecked();
- if (!ValidPointerSize(pointer_size_unchecked)) {
- *error_msg = StringPrintf("Invalid image pointer size: %u", pointer_size_unchecked);
+ image_pointer_size_ = image_header.GetPointerSize();
+ if (UNLIKELY(image_pointer_size_ != PointerSize::k32 &&
+ image_pointer_size_ != PointerSize::k64)) {
+ *error_msg =
+ StringPrintf("Invalid image pointer size: %u", static_cast<uint32_t>(image_pointer_size_));
return false;
}
- image_pointer_size_ = image_header.GetPointerSize();
if (!runtime->IsAotCompiler()) {
// Only the Aot compiler supports having an image with a different pointer size than the
// runtime. This happens on the host for compiling 32 bit tests since we use a 64 bit libart
diff --git a/runtime/gc/collector/immune_spaces_test.cc b/runtime/gc/collector/immune_spaces_test.cc
index 6cc7e307af..5bc3ac83b3 100644
--- a/runtime/gc/collector/immune_spaces_test.cc
+++ b/runtime/gc/collector/immune_spaces_test.cc
@@ -17,6 +17,7 @@
#include <sys/mman.h>
#include "base/common_art_test.h"
+#include "base/enums.h"
#include "base/utils.h"
#include "gc/collector/immune_spaces.h"
#include "gc/space/image_space.h"
@@ -151,7 +152,7 @@ class ImmuneSpacesTest : public CommonArtTest {
/*boot_image_size=*/ 0u,
/*boot_image_component_count=*/ 0u,
/*boot_image_checksum=*/ 0u,
- /*pointer_size=*/ sizeof(void*));
+ /*pointer_size=*/ kRuntimePointerSize);
return new FakeImageSpace(std::move(image_map),
std::move(live_bitmap),
std::move(oat_file),
diff --git a/runtime/mirror/class.h b/runtime/mirror/class.h
index b69086c971..13e328da63 100644
--- a/runtime/mirror/class.h
+++ b/runtime/mirror/class.h
@@ -59,7 +59,7 @@ class ImTable;
enum InvokeType : uint32_t;
template <typename Iter> class IterationRange;
template<typename T> class LengthPrefixedArray;
-enum class PointerSize : size_t;
+enum class PointerSize : uint32_t;
class Signature;
template<typename T> class StrideIterator;
template<size_t kNumReferences> class PACKED(4) StackHandleScope;
diff --git a/runtime/oat/image.cc b/runtime/oat/image.cc
index a7ac8e0ebd..e801aff1e6 100644
--- a/runtime/oat/image.cc
+++ b/runtime/oat/image.cc
@@ -52,7 +52,7 @@ ImageHeader::ImageHeader(uint32_t image_reservation_size,
uint32_t boot_image_size,
uint32_t boot_image_component_count,
uint32_t boot_image_checksum,
- uint32_t pointer_size)
+ PointerSize pointer_size)
: image_reservation_size_(image_reservation_size),
component_count_(component_count),
image_begin_(image_begin),
@@ -78,7 +78,8 @@ ImageHeader::ImageHeader(uint32_t image_reservation_size,
CHECK_LT(oat_data_begin, oat_data_end);
CHECK_LE(oat_data_end, oat_file_end);
}
- CHECK(ValidPointerSize(pointer_size_)) << pointer_size_;
+ static_assert(sizeof(PointerSize) == sizeof(uint32_t),
+ "PointerSize class is expected to be a uint32_t for the header");
memcpy(magic_, kImageMagic, sizeof(kImageMagic));
memcpy(version_, kImageVersion, sizeof(kImageVersion));
std::copy_n(sections, kSectionCount, sections_);
@@ -196,7 +197,7 @@ void ImageHeader::VisitObjects(ObjectVisitor* visitor,
}
PointerSize ImageHeader::GetPointerSize() const {
- return ConvertToPointerSize(pointer_size_);
+ return pointer_size_;
}
bool LZ4_decompress_safe_checked(const char* source,
diff --git a/runtime/oat/image.h b/runtime/oat/image.h
index 23c92a1aa8..b605ecd403 100644
--- a/runtime/oat/image.h
+++ b/runtime/oat/image.h
@@ -144,7 +144,7 @@ class PACKED(8) ImageHeader {
uint32_t boot_image_size,
uint32_t boot_image_component_count,
uint32_t boot_image_checksum,
- uint32_t pointer_size);
+ PointerSize pointer_size);
EXPORT bool IsValid() const;
EXPORT const char* GetMagic() const;
@@ -201,10 +201,6 @@ class PACKED(8) ImageHeader {
EXPORT PointerSize GetPointerSize() const;
- uint32_t GetPointerSizeUnchecked() const {
- return pointer_size_;
- }
-
static std::string GetOatLocationFromImageLocation(const std::string& image) {
return GetLocationFromImageLocation(image, "oat");
}
@@ -502,7 +498,7 @@ class PACKED(8) ImageHeader {
uint32_t image_roots_ = 0u;
// Pointer size, this affects the size of the ArtMethods.
- uint32_t pointer_size_ = 0u;
+ PointerSize pointer_size_;
// Image section sizes/offsets correspond to the uncompressed form.
ImageSection sections_[kSectionCount];
diff --git a/runtime/runtime_image.cc b/runtime/runtime_image.cc
index 5d304698bf..276c901fc4 100644
--- a/runtime/runtime_image.cc
+++ b/runtime/runtime_image.cc
@@ -164,7 +164,7 @@ class RuntimeImageHelper {
heap->GetBootImagesSize(),
boot_image_components,
boot_image_checksums,
- static_cast<uint32_t>(kRuntimePointerSize));
+ kRuntimePointerSize);
// Data size includes everything except the bitmap and the header.
header_.data_size_ = sections_end - sizeof(ImageHeader);