xref: /aosp_15_r20/frameworks/base/core/jni/android_app_PropertyInvalidatedCache.cpp (revision d57664e9bc4670b3ecf6748a746a57c557b6bc9e)
1 /*
2  * Copyright (C) 2024 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 
17 #define LOG_TAG "CacheNonce"
18 
19 #include <string.h>
20 #include <memory.h>
21 
22 #include <atomic>
23 
24 #include <nativehelper/JNIHelp.h>
25 #include <nativehelper/scoped_primitive_array.h>
26 #include <android-base/logging.h>
27 
28 #include "core_jni_helpers.h"
29 #include "android_app_PropertyInvalidatedCache.h"
30 
31 namespace android::app::PropertyInvalidatedCache {
32 
33 // These provide run-time access to the sizing parameters.
getMaxNonce() const34 int NonceStore::getMaxNonce() const {
35     return kMaxNonce;
36 }
37 
getMaxByte() const38 size_t NonceStore::getMaxByte() const {
39     return kMaxByte;
40 }
41 
42 // Fetch a nonce, returning UNSET if the index is out of range.  This method specifically
43 // does not throw or generate an error if the index is out of range; this allows the method
44 // to be called in a CriticalNative JNI API.
getNonce(int index) const45 int64_t NonceStore::getNonce(int index) const {
46     if (index < 0 || index >= kMaxNonce) {
47         return UNSET;
48     } else {
49         return nonce()[index];
50     }
51 }
52 
53 // Set a nonce and return true. Return false if the index is out of range.  This method
54 // specifically does not throw or generate an error if the index is out of range; this
55 // allows the method to be called in a CriticalNative JNI API.
setNonce(int index,int64_t value)56 bool NonceStore::setNonce(int index, int64_t value) {
57     if (index < 0 || index >= kMaxNonce) {
58         return false;
59     } else {
60         nonce()[index] = value;
61         return true;
62     }
63 }
64 
65 // Fetch just the byte-block hash
getHash() const66 int32_t NonceStore::getHash() const {
67     return mByteHash;
68 }
69 
70 // Copy the byte block to the target and return the current hash.
getByteBlock(block_t * block,size_t len) const71 int32_t NonceStore::getByteBlock(block_t* block, size_t len) const {
72     memcpy(block, (void*) byteBlock(), std::min(kMaxByte, len));
73     return mByteHash;
74 }
75 
76 // Set the byte block and the hash.
setByteBlock(int hash,const block_t * block,size_t len)77 void NonceStore::setByteBlock(int hash, const block_t* block, size_t len) {
78     memcpy((void*) byteBlock(), block, len = std::min(kMaxByte, len));
79     mByteHash = hash;
80 }
81 
82 } // namespace android::app::PropertyInvalidatedCache;
83 
84 namespace {
85 
86 using namespace android::app::PropertyInvalidatedCache;
87 
88 // Convert a jlong to a nonce block.  This is a convenience function that should be inlined by
89 // the compiler.
nonceCache(jlong ptr)90 inline NonceStore* nonceCache(jlong ptr) {
91     return reinterpret_cast<NonceStore*>(ptr);
92 }
93 
94 // Return the number of nonces in the nonce block.
getMaxNonce(JNIEnv *,jclass,jlong ptr)95 jint getMaxNonce(JNIEnv*, jclass, jlong ptr) {
96     return nonceCache(ptr)->getMaxNonce();
97 }
98 
99 // Return the number of string bytes in the nonce block.
getMaxByte(JNIEnv *,jclass,jlong ptr)100 jint getMaxByte(JNIEnv*, jclass, jlong ptr) {
101     return nonceCache(ptr)->getMaxByte();
102 }
103 
104 // Set the byte block.  The first int is the hash to set and the second is the array to copy.
105 // This should be synchronized in the Java layer.
setByteBlock(JNIEnv * env,jclass,jlong ptr,jint hash,jbyteArray val)106 void setByteBlock(JNIEnv* env, jclass, jlong ptr, jint hash, jbyteArray val) {
107     ScopedByteArrayRO value(env, val);
108     if (value.get() == nullptr) {
109         jniThrowExceptionFmt(env, "java/lang/IllegalArgumentException", "null byte block");
110         return;
111     }
112     nonceCache(ptr)->setByteBlock(hash, value.get(), value.size());
113 }
114 
115 // Fetch the byte block.  If the incoming hash is the same as the local hash, the Java layer is
116 // presumed to have an up-to-date copy of the byte block; do not copy byte array.  The local
117 // hash is returned.
getByteBlock(JNIEnv * env,jclass,jlong ptr,jint hash,jbyteArray val)118 jint getByteBlock(JNIEnv* env, jclass, jlong ptr, jint hash, jbyteArray val) {
119     if (nonceCache(ptr)->getHash() == hash) {
120         return hash;
121     }
122     ScopedByteArrayRW value(env, val);
123     return nonceCache(ptr)->getByteBlock(value.get(), value.size());
124 }
125 
126 // Fetch the byte block hash.
127 //
128 // This is a CriticalNative method and therefore does not get the JNIEnv or jclass parameters.
getByteBlockHash(jlong ptr)129 jint getByteBlockHash(jlong ptr) {
130     return nonceCache(ptr)->getHash();
131 }
132 
133 // Get a nonce value. So that this method can be CriticalNative, it returns 0 if the value is
134 // out of range, rather than throwing an exception.  This is a CriticalNative method and
135 // therefore does not get the JNIEnv or jclass parameters.
136 //
137 // This method is @CriticalNative and does not take a JNIEnv* or jclass argument.
getNonce(jlong ptr,jint index)138 jlong getNonce(jlong ptr, jint index) {
139     return nonceCache(ptr)->getNonce(index);
140 }
141 
142 // Set a nonce value. So that this method can be CriticalNative, it returns a boolean: false if
143 // the index is out of range and true otherwise.  Callers may test the returned boolean and
144 // generate an exception.
145 //
146 // This method is @CriticalNative and does not take a JNIEnv* or jclass argument.
setNonce(jlong ptr,jint index,jlong value)147 jboolean setNonce(jlong ptr, jint index, jlong value) {
148     return nonceCache(ptr)->setNonce(index, value);
149 }
150 
151 static const JNINativeMethod gMethods[] = {
152     {"nativeGetMaxNonce",      "(J)I",    (void*) getMaxNonce },
153     {"nativeGetMaxByte",       "(J)I",    (void*) getMaxByte },
154     {"nativeSetByteBlock",     "(JI[B)V", (void*) setByteBlock },
155     {"nativeGetByteBlock",     "(JI[B)I", (void*) getByteBlock },
156     {"nativeGetByteBlockHash", "(J)I",    (void*) getByteBlockHash },
157     {"nativeGetNonce",         "(JI)J",   (void*) getNonce },
158     {"nativeSetNonce",         "(JIJ)Z",  (void*) setNonce },
159 };
160 
161 static const char* kClassName = "android/app/PropertyInvalidatedCache";
162 
163 } // anonymous namespace
164 
165 namespace android {
166 
register_android_app_PropertyInvalidatedCache(JNIEnv * env)167 int register_android_app_PropertyInvalidatedCache(JNIEnv* env) {
168     RegisterMethodsOrDie(env, kClassName, gMethods, NELEM(gMethods));
169     return JNI_OK;
170 }
171 
172 } // namespace android
173