blob: 8b5635d2ec8bede1c885de37f6322816e71f6b6b [file] [log] [blame]
Mathieu Chartier78f7b4c2014-05-06 10:57:27 -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 Gampe277ccbd2014-11-03 21:36:10 -080017#include "java_lang_ref_Reference.h"
18
Andreas Gampea14100c2017-04-24 15:09:56 -070019#include "nativehelper/jni_macros.h"
20
Mathieu Chartier78f7b4c2014-05-06 10:57:27 -070021#include "gc/heap.h"
22#include "gc/reference_processor.h"
Vladimir Markoa3ad0cd2018-05-04 10:06:38 +010023#include "jni/jni_internal.h"
Mathieu Chartier78f7b4c2014-05-06 10:57:27 -070024#include "mirror/object-inl.h"
25#include "mirror/reference-inl.h"
Andreas Gampe87583b32017-05-25 11:22:18 -070026#include "native_util.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070027#include "scoped_fast_native_object_access-inl.h"
Mathieu Chartier78f7b4c2014-05-06 10:57:27 -070028
29namespace art {
30
Mathieu Chartiercd48f2d2014-09-09 13:51:09 -070031static jobject Reference_getReferent(JNIEnv* env, jobject javaThis) {
Mathieu Chartier78f7b4c2014-05-06 10:57:27 -070032 ScopedFastNativeObjectAccess soa(env);
Vladimir Marko0984e482019-03-27 16:41:41 +000033 const ObjPtr<mirror::Reference> ref = soa.Decode<mirror::Reference>(javaThis);
34 const ObjPtr<mirror::Object> referent =
Mathieu Chartier5d3f73a2016-10-14 14:28:47 -070035 Runtime::Current()->GetHeap()->GetReferenceProcessor()->GetReferent(soa.Self(), ref);
Mathieu Chartier78f7b4c2014-05-06 10:57:27 -070036 return soa.AddLocalReference<jobject>(referent);
37}
38
Hans Boehma5c1a822020-11-10 14:58:06 -080039static jboolean Reference_refersTo0(JNIEnv* env, jobject javaThis, jobject o) {
Lokesh Gidraca5ed9f2022-04-20 01:39:28 +000040 if (gUseReadBarrier && !kUseBakerReadBarrier) {
Hans Boehma5c1a822020-11-10 14:58:06 -080041 // Fall back to naive implementation that may block and needlessly preserve javaThis.
42 return env->IsSameObject(Reference_getReferent(env, javaThis), o);
43 }
44 ScopedFastNativeObjectAccess soa(env);
45 const ObjPtr<mirror::Reference> ref = soa.Decode<mirror::Reference>(javaThis);
46 const ObjPtr<mirror::Object> other = soa.Decode<mirror::Reference>(o);
47 const ObjPtr<mirror::Object> referent = ref->template GetReferent<kWithoutReadBarrier>();
48 if (referent == other) {
49 return JNI_TRUE;
50 }
Lokesh Gidraca5ed9f2022-04-20 01:39:28 +000051 if (!gUseReadBarrier || referent.IsNull() || other.IsNull()) {
Hans Boehma5c1a822020-11-10 14:58:06 -080052 return JNI_FALSE;
53 }
54 // Explicitly handle the case in which referent is a from-space pointer. Don't use a
55 // read-barrier, since that could easily mark an object we no longer need and, since it
56 // creates new gray objects, may not be safe without blocking.
57 //
58 // ConcurrentCopying::Copy ensure that whenever a pointer to a to_space object is published,
59 // the forwarding pointer is also visible. We need that guarantee to ensure that if referent
60 // == other and referent is in from-space, then referent has a forwarding pointer. In order to
61 // use that guarantee, we need to ensure that the forwarding pointer is loaded after we
62 // retrieved other. Hence this fence:
63 atomic_thread_fence(std::memory_order_acquire);
64 // Note: On ARM, the above could be replaced by an asm fake-dependency hack to make
65 // referent appear to depend on other. That would be faster and uglier.
66 return gc::collector::ConcurrentCopying::GetFwdPtrUnchecked(referent.Ptr()) == other.Ptr() ?
67 JNI_TRUE : JNI_FALSE;
68}
69
Mathieu Chartier6f43e3a2016-12-14 14:12:17 -080070static void Reference_clearReferent(JNIEnv* env, jobject javaThis) {
Mathieu Chartierc9a70282016-12-13 14:44:33 -080071 ScopedFastNativeObjectAccess soa(env);
Vladimir Marko0984e482019-03-27 16:41:41 +000072 const ObjPtr<mirror::Reference> ref = soa.Decode<mirror::Reference>(javaThis);
Mathieu Chartierc9a70282016-12-13 14:44:33 -080073 Runtime::Current()->GetHeap()->GetReferenceProcessor()->ClearReferent(ref);
74}
75
Mathieu Chartier78f7b4c2014-05-06 10:57:27 -070076static JNINativeMethod gMethods[] = {
Igor Murashkin3b6f4402017-02-16 16:13:17 -080077 FAST_NATIVE_METHOD(Reference, getReferent, "()Ljava/lang/Object;"),
78 FAST_NATIVE_METHOD(Reference, clearReferent, "()V"),
Hans Boehma5c1a822020-11-10 14:58:06 -080079 FAST_NATIVE_METHOD(Reference, refersTo0, "(Ljava/lang/Object;)Z"),
Mathieu Chartier78f7b4c2014-05-06 10:57:27 -070080};
81
82void register_java_lang_ref_Reference(JNIEnv* env) {
83 REGISTER_NATIVE_METHODS("java/lang/ref/Reference");
84}
85
86} // namespace art