diff options
author | Ian Rogers <irogers@google.com> | 2014-10-21 15:05:36 -0700 |
---|---|---|
committer | Ian Rogers <irogers@google.com> | 2014-10-21 15:45:49 -0700 |
commit | b5cb18a116dce45fc077b3f5b94af9e521e79e8d (patch) | |
tree | d58188cb260beff312dfe4e6c32f0186cf30a591 /runtime/handle_scope.h | |
parent | 1428dce77b8b0e8ec3e3665d816678df1253fc10 (diff) | |
download | art-b5cb18a116dce45fc077b3f5b94af9e521e79e8d.zip art-b5cb18a116dce45fc077b3f5b94af9e521e79e8d.tar.gz art-b5cb18a116dce45fc077b3f5b94af9e521e79e8d.tar.bz2 |
Avoid strict-aliasing problems with Handles.
Replace use of reinterpret_cast with down_cast.
Bug: 18074773
Change-Id: Id42d462f2798f69a2210e5912f441c868b8b5812
Diffstat (limited to 'runtime/handle_scope.h')
-rw-r--r-- | runtime/handle_scope.h | 40 |
1 files changed, 16 insertions, 24 deletions
diff --git a/runtime/handle_scope.h b/runtime/handle_scope.h index f795e38..13c939f 100644 --- a/runtime/handle_scope.h +++ b/runtime/handle_scope.h @@ -159,7 +159,7 @@ class HandleWrapper : public MutableHandle<T> { } private: - T** obj_; + T** const obj_; }; // Scoped handle storage of a fixed size that is usually stack allocated. @@ -169,31 +169,10 @@ class PACKED(4) StackHandleScope FINAL : public HandleScope { explicit StackHandleScope(Thread* self); ~StackHandleScope(); - // Currently unused, using this GetReference instead of the one in HandleScope is preferred to - // avoid compiler optimizations incorrectly optimizing out of bound array accesses. - // TODO: Remove this when it is un-necessary. - ALWAYS_INLINE mirror::Object* GetReference(size_t i) const - SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { - DCHECK_LT(i, kNumReferences); - return GetReferences()[i].AsMirrorPtr(); - } - - ALWAYS_INLINE MutableHandle<mirror::Object> GetHandle(size_t i) - SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { - DCHECK_LT(i, kNumReferences); - return MutableHandle<mirror::Object>(&GetReferences()[i]); - } - - ALWAYS_INLINE void SetReference(size_t i, mirror::Object* object) - SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { - DCHECK_LT(i, kNumReferences); - GetReferences()[i].Assign(object); - } - template<class T> MutableHandle<T> NewHandle(T* object) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { SetReference(pos_, object); - MutableHandle<T> h(GetHandle(pos_)); + MutableHandle<T> h(GetHandle<T>(pos_)); pos_++; return h; } @@ -201,12 +180,25 @@ class PACKED(4) StackHandleScope FINAL : public HandleScope { template<class T> HandleWrapper<T> NewHandleWrapper(T** object) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { SetReference(pos_, *object); - MutableHandle<T> h(GetHandle(pos_)); + MutableHandle<T> h(GetHandle<T>(pos_)); pos_++; return HandleWrapper<T>(object, h); } private: + template<class T> + ALWAYS_INLINE MutableHandle<T> GetHandle(size_t i) + SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { + DCHECK_LT(i, kNumReferences); + return MutableHandle<T>(&GetReferences()[i]); + } + + ALWAYS_INLINE void SetReference(size_t i, mirror::Object* object) + SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { + DCHECK_LT(i, kNumReferences); + GetReferences()[i].Assign(object); + } + // Reference storage needs to be first as expected by the HandleScope layout. StackReference<mirror::Object> storage_[kNumReferences]; |