Dmitry Petrochenko | 135016a | 2014-04-03 14:35:54 +0700 | [diff] [blame] | 1 | /* |
| 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 Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 17 | #include "base/enums.h" |
Dmitry Petrochenko | 135016a | 2014-04-03 14:35:54 +0700 | [diff] [blame] | 18 | #include "gtest/gtest.h" |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 19 | #include "handle_scope-inl.h" |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 20 | #include "scoped_thread_state_change-inl.h" |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 21 | #include "thread.h" |
Dmitry Petrochenko | 135016a | 2014-04-03 14:35:54 +0700 | [diff] [blame] | 22 | |
| 23 | namespace art { |
| 24 | |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 25 | // Handle scope with a fixed size which is allocated on the stack. |
| 26 | template<size_t kNumReferences> |
| 27 | class NoThreadStackHandleScope : public HandleScope { |
| 28 | public: |
Ian Rogers | 59c0706 | 2014-10-10 13:03:39 -0700 | [diff] [blame] | 29 | explicit NoThreadStackHandleScope(HandleScope* link) : HandleScope(link, kNumReferences) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 30 | } |
| 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 Petrochenko | 135016a | 2014-04-03 14:35:54 +0700 | [diff] [blame] | 40 | // 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 Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 42 | TEST(HandleScopeTest, Offsets) NO_THREAD_SAFETY_ANALYSIS { |
| 43 | // As the members of HandleScope are private, we cannot use OFFSETOF_MEMBER |
Dmitry Petrochenko | 135016a | 2014-04-03 14:35:54 +0700 | [diff] [blame] | 44 | // here. So do the inverse: set some data, and access it through pointers created from the offsets. |
Ian Rogers | 59c0706 | 2014-10-10 13:03:39 -0700 | [diff] [blame] | 45 | NoThreadStackHandleScope<0x9ABC> test_table(reinterpret_cast<HandleScope*>(0x5678)); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 46 | test_table.SetReference(0, reinterpret_cast<mirror::Object*>(0x1234)); |
Dmitry Petrochenko | 135016a | 2014-04-03 14:35:54 +0700 | [diff] [blame] | 47 | |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 48 | uint8_t* table_base_ptr = reinterpret_cast<uint8_t*>(&test_table); |
Dmitry Petrochenko | 135016a | 2014-04-03 14:35:54 +0700 | [diff] [blame] | 49 | |
| 50 | { |
| 51 | uintptr_t* link_ptr = reinterpret_cast<uintptr_t*>(table_base_ptr + |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 52 | HandleScope::LinkOffset(kRuntimePointerSize)); |
Dmitry Petrochenko | 135016a | 2014-04-03 14:35:54 +0700 | [diff] [blame] | 53 | 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 Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 58 | HandleScope::NumberOfReferencesOffset(kRuntimePointerSize)); |
Dmitry Petrochenko | 135016a | 2014-04-03 14:35:54 +0700 | [diff] [blame] | 59 | 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 Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 68 | HandleScope::ReferencesOffset(kRuntimePointerSize)); |
Dmitry Petrochenko | 135016a | 2014-04-03 14:35:54 +0700 | [diff] [blame] | 69 | EXPECT_EQ(*ref_ptr, static_cast<uint32_t>(0x1234)); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | } // namespace art |