blob: 58f3800e8fb801fb773641e68c9d8c3141dc7788 [file] [log] [blame]
Dmitry Petrochenko135016a2014-04-03 14:35:54 +07001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Andreas Gampe542451c2016-07-26 09:02:02 -070017#include "base/enums.h"
Dmitry Petrochenko135016a2014-04-03 14:35:54 +070018#include "gtest/gtest.h"
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070019#include "handle_scope-inl.h"
20#include "scoped_thread_state_change.h"
21#include "thread.h"
Dmitry Petrochenko135016a2014-04-03 14:35:54 +070022
23namespace art {
24
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070025// Handle scope with a fixed size which is allocated on the stack.
26template<size_t kNumReferences>
27class NoThreadStackHandleScope : public HandleScope {
28 public:
Ian Rogers59c07062014-10-10 13:03:39 -070029 explicit NoThreadStackHandleScope(HandleScope* link) : HandleScope(link, kNumReferences) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070030 }
31 ~NoThreadStackHandleScope() {
32 }
33
34 private:
35 // references_storage_ needs to be first so that it matches the address of references_
36 StackReference<mirror::Object> references_storage_[kNumReferences];
37};
38
39// Test the offsets computed for members of HandleScope. Because of cross-compiling
Dmitry Petrochenko135016a2014-04-03 14:35:54 +070040// it is impossible the use OFFSETOF_MEMBER, so we do some reasonable computations ourselves. This
41// test checks whether we do the right thing.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070042TEST(HandleScopeTest, Offsets) NO_THREAD_SAFETY_ANALYSIS {
43 // As the members of HandleScope are private, we cannot use OFFSETOF_MEMBER
Dmitry Petrochenko135016a2014-04-03 14:35:54 +070044 // here. So do the inverse: set some data, and access it through pointers created from the offsets.
Ian Rogers59c07062014-10-10 13:03:39 -070045 NoThreadStackHandleScope<0x9ABC> test_table(reinterpret_cast<HandleScope*>(0x5678));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070046 test_table.SetReference(0, reinterpret_cast<mirror::Object*>(0x1234));
Dmitry Petrochenko135016a2014-04-03 14:35:54 +070047
Ian Rogers13735952014-10-08 12:43:28 -070048 uint8_t* table_base_ptr = reinterpret_cast<uint8_t*>(&test_table);
Dmitry Petrochenko135016a2014-04-03 14:35:54 +070049
50 {
51 uintptr_t* link_ptr = reinterpret_cast<uintptr_t*>(table_base_ptr +
Andreas Gampe542451c2016-07-26 09:02:02 -070052 HandleScope::LinkOffset(kRuntimePointerSize));
Dmitry Petrochenko135016a2014-04-03 14:35:54 +070053 EXPECT_EQ(*link_ptr, static_cast<size_t>(0x5678));
54 }
55
56 {
57 uint32_t* num_ptr = reinterpret_cast<uint32_t*>(table_base_ptr +
Andreas Gampe542451c2016-07-26 09:02:02 -070058 HandleScope::NumberOfReferencesOffset(kRuntimePointerSize));
Dmitry Petrochenko135016a2014-04-03 14:35:54 +070059 EXPECT_EQ(*num_ptr, static_cast<size_t>(0x9ABC));
60 }
61
62 {
63 // Assume sizeof(StackReference<mirror::Object>) == sizeof(uint32_t)
64 // TODO: How can we make this assumption-less but still access directly and fully?
65 EXPECT_EQ(sizeof(StackReference<mirror::Object>), sizeof(uint32_t));
66
67 uint32_t* ref_ptr = reinterpret_cast<uint32_t*>(table_base_ptr +
Andreas Gampe542451c2016-07-26 09:02:02 -070068 HandleScope::ReferencesOffset(kRuntimePointerSize));
Dmitry Petrochenko135016a2014-04-03 14:35:54 +070069 EXPECT_EQ(*ref_ptr, static_cast<uint32_t>(0x1234));
70 }
71}
72
73} // namespace art