What is Parcel.enforceInterface?

Can someone tell me what the ensceInterface method does in the Android Parcel class? The developer's website does not explain its purpose, and Google does not return any useful hits.

Thanks.

+5
source share
1 answer

This method checks if the interface is the same between the client and server.

Detailed process:

  1. calling the IPC method on the client side, before calling mRemote.transact this method is _data.writeInterfaceToken(DESCRIPTOR); I will call first.
  2. will be called onTransact server side, and then call data.enforceInterface(descriptor); to check if the interface matches, if they do not match, the error message "Binder invocation to an incorrect interface" .

Package Source Code:

 static void android_os_Parcel_enforceInterface(JNIEnv* env, jclass clazz, jlong nativePtr, jstring name) { Parcel* parcel = reinterpret_cast<Parcel*>(nativePtr); if (parcel != NULL) { const jchar* str = env->GetStringCritical(name, 0); if (str) { IPCThreadState* threadState = IPCThreadState::self(); const int32_t oldPolicy = threadState->getStrictModePolicy(); const bool isValid = parcel->enforceInterface( String16(reinterpret_cast<const char16_t*>(str), env->GetStringLength(name)), threadState); env->ReleaseStringCritical(name, str); if (isValid) { const int32_t newPolicy = threadState->getStrictModePolicy(); if (oldPolicy != newPolicy) { // Need to keep the Java-level thread-local strict // mode policy in sync for the libcore // enforcements, which involves an upcall back // into Java. (We can't modify the // Parcel.enforceInterface signature, as it's // pseudo-public, and used via AIDL // auto-generation...) set_dalvik_blockguard_policy(env, newPolicy); } return; // everything was correct -> return silently } } } // all error conditions wind up here jniThrowException(env, "java/lang/SecurityException", "Binder invocation to an incorrect interface"); } bool Parcel::enforceInterface(const String16& interface, IPCThreadState* threadState) const { int32_t strictPolicy = readInt32(); if (threadState == NULL) { threadState = IPCThreadState::self(); } if ((threadState->getLastTransactionBinderFlags() & IBinder::FLAG_ONEWAY) != 0) { // For one-way calls, the callee is running entirely // disconnected from the caller, so disable StrictMode entirely. // Not only does disk/network usage not impact the caller, but // there no way to commuicate back any violations anyway. threadState->setStrictModePolicy(0); } else { threadState->setStrictModePolicy(strictPolicy); } const String16 str(readString16()); if (str == interface) { return true; } else { ALOGW("**** enforceInterface() expected '%s' but read '%s'\n", String8(interface).string(), String8(str).string()); return false; } } 
0
source

Source: https://habr.com/ru/post/1247397/


All Articles