Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 5a313b70 authored by Treehugger Robot's avatar Treehugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Move platform lint tooling to lint_checks repo" into main

parents aaeccbe7 d1a9277c
Loading
Loading
Loading
Loading

tools/lint/common/Android.bp

deleted100644 → 0
+0 −56
Original line number Diff line number Diff line
// Copyright (C) 2022 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package {
    // See: http://go/android-license-faq
    // A large-scale-change added 'default_applicable_licenses' to import
    // all of the 'license_kinds' from "frameworks_base_license"
    // to get the below license kinds:
    //   SPDX-license-identifier-Apache-2.0
    default_applicable_licenses: ["frameworks_base_license"],
}

java_library_host {
    name: "AndroidCommonLint",
    srcs: ["src/main/java/**/*.kt"],
    libs: ["lint_api"],
    kotlincflags: ["-Xjvm-default=all"],
}

java_defaults {
    name: "AndroidLintCheckerTestDefaults",
    srcs: ["checks/src/test/java/**/*.kt"],
    static_libs: [
        "junit",
        "lint",
        "lint_tests",
    ],
    test_options: {
        unit_test: true,
        tradefed_options: [
            {
                // lint bundles in some classes that were built with older versions
                // of libraries, and no longer load. Since tradefed tries to load
                // all classes in the jar to look for tests, it crashes loading them.
                // Exclude these classes from tradefed's search.
                name: "exclude-paths",
                value: "org/apache",
            },
            {
                name: "exclude-paths",
                value: "META-INF",
            },
        ],
    },
}
+0 −42
Original line number Diff line number Diff line
/*
 * Copyright (C) 2022 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.android.lint

import com.google.android.lint.model.Method

const val CLASS_STUB = "Stub"
const val CLASS_CONTEXT = "android.content.Context"
const val CLASS_ACTIVITY_MANAGER_SERVICE = "com.android.server.am.ActivityManagerService"
const val CLASS_ACTIVITY_MANAGER_INTERNAL = "android.app.ActivityManagerInternal"
const val CLASS_PERMISSION_CHECKER = "android.content.PermissionChecker"
const val CLASS_PERMISSION_MANAGER = "android.permission.PermissionManager"

// Enforce permission APIs
val ENFORCE_PERMISSION_METHODS = listOf(
        Method(CLASS_CONTEXT, "checkPermission"),
        Method(CLASS_CONTEXT, "checkCallingPermission"),
        Method(CLASS_CONTEXT, "checkCallingOrSelfPermission"),
        Method(CLASS_CONTEXT, "enforcePermission"),
        Method(CLASS_CONTEXT, "enforceCallingPermission"),
        Method(CLASS_CONTEXT, "enforceCallingOrSelfPermission"),
        Method(CLASS_ACTIVITY_MANAGER_SERVICE, "checkPermission"),
        Method(CLASS_ACTIVITY_MANAGER_INTERNAL, "enforceCallingPermission")
)

const val ANNOTATION_PERMISSION_METHOD = "android.annotation.PermissionMethod"
const val ANNOTATION_PERMISSION_NAME = "android.annotation.PermissionName"
const val ANNOTATION_PERMISSION_RESULT = "android.content.pm.PackageManager.PermissionResult"
+0 −52
Original line number Diff line number Diff line
/*
 * Copyright (C) 2022 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.android.lint

import com.android.tools.lint.detector.api.getUMethod
import org.jetbrains.uast.UAnnotation
import org.jetbrains.uast.UCallExpression
import org.jetbrains.uast.UElement
import org.jetbrains.uast.UMethod
import org.jetbrains.uast.UParameter
import org.jetbrains.uast.UQualifiedReferenceExpression

fun isPermissionMethodCall(callExpression: UCallExpression): Boolean {
    val method = callExpression.resolve()?.getUMethod() ?: return false
    return hasPermissionMethodAnnotation(method)
}

fun hasPermissionMethodAnnotation(method: UMethod): Boolean =
        getPermissionMethodAnnotation(method) != null

fun getPermissionMethodAnnotation(method: UMethod?): UAnnotation? = method?.uAnnotations
        ?.firstOrNull { it.qualifiedName == ANNOTATION_PERMISSION_METHOD }

fun hasPermissionNameAnnotation(parameter: UParameter) = parameter.annotations.any {
    it.hasQualifiedName(ANNOTATION_PERMISSION_NAME)
}

/**
 * Attempts to return a CallExpression from a QualifiedReferenceExpression (or returns it directly if passed directly)
 * @param callOrReferenceCall expected to be UCallExpression or UQualifiedReferenceExpression
 * @return UCallExpression, if available
 */
fun findCallExpression(callOrReferenceCall: UElement?): UCallExpression? =
        when (callOrReferenceCall) {
            is UCallExpression -> callOrReferenceCall
            is UQualifiedReferenceExpression -> callOrReferenceCall.selector as? UCallExpression
            else -> null
        }
+0 −52
Original line number Diff line number Diff line
/*
 * Copyright (C) 2022 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.android.lint.aidl

import com.android.tools.lint.client.api.UElementHandler
import com.android.tools.lint.detector.api.Detector
import com.android.tools.lint.detector.api.JavaContext
import com.android.tools.lint.detector.api.SourceCodeScanner
import org.jetbrains.uast.UBlockExpression
import org.jetbrains.uast.UElement
import org.jetbrains.uast.UMethod

/**
 * Abstract class for detectors that look for methods implementing
 * generated AIDL interface stubs
 */
abstract class AidlImplementationDetector : Detector(), SourceCodeScanner {
    override fun getApplicableUastTypes(): List<Class<out UElement?>> =
            listOf(UMethod::class.java)

    override fun createUastHandler(context: JavaContext): UElementHandler = AidlStubHandler(context)

    private inner class AidlStubHandler(val context: JavaContext) : UElementHandler() {
        override fun visitMethod(node: UMethod) {
            val interfaceName = getContainingAidlInterface(context, node)
                    .takeUnless(EXCLUDED_CPP_INTERFACES::contains) ?: return
            val body = (node.uastBody as? UBlockExpression) ?: return
            visitAidlMethod(context, node, interfaceName, body)
        }
    }

    abstract fun visitAidlMethod(
            context: JavaContext,
            node: UMethod,
            interfaceName: String,
            body: UBlockExpression,
    )
}
+0 −152
Original line number Diff line number Diff line
/*
 * Copyright (C) 2022 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.android.lint.aidl

const val ANNOTATION_ENFORCE_PERMISSION = "android.annotation.EnforcePermission"
const val ANNOTATION_REQUIRES_PERMISSION = "android.annotation.RequiresPermission"
const val ANNOTATION_REQUIRES_NO_PERMISSION = "android.annotation.RequiresNoPermission"
const val ANNOTATION_PERMISSION_MANUALLY_ENFORCED = "android.annotation.PermissionManuallyEnforced"

val AIDL_PERMISSION_ANNOTATIONS = listOf(
        ANNOTATION_ENFORCE_PERMISSION,
        ANNOTATION_REQUIRES_NO_PERMISSION,
        ANNOTATION_PERMISSION_MANUALLY_ENFORCED
)

const val BINDER_CLASS = "android.os.Binder"
const val IINTERFACE_INTERFACE = "android.os.IInterface"

const val PERMISSION_PREFIX_LITERAL = "android.permission."

const val AIDL_PERMISSION_HELPER_SUFFIX = "_enforcePermission"

/**
 * If a non java (e.g. c++) backend is enabled, the @EnforcePermission
 * annotation cannot be used.  At time of writing, the mechanism
 * is not implemented for non java backends.
 * TODO: b/242564874 (have lint know which interfaces have the c++ backend enabled)
 * rather than hard coding this list?
 */
val EXCLUDED_CPP_INTERFACES = listOf(
        "AdbTransportType",
        "FingerprintAndPairDevice",
        "IAdbCallback",
        "IAdbManager",
        "PairDevice",
        "IStatsBootstrapAtomService",
        "StatsBootstrapAtom",
        "StatsBootstrapAtomValue",
        "FixedSizeArrayExample",
        "PlaybackTrackMetadata",
        "RecordTrackMetadata",
        "SinkMetadata",
        "SourceMetadata",
        "IUpdateEngineStable",
        "IUpdateEngineStableCallback",
        "AudioCapabilities",
        "ConfidenceLevel",
        "ModelParameter",
        "ModelParameterRange",
        "Phrase",
        "PhraseRecognitionEvent",
        "PhraseRecognitionExtra",
        "PhraseSoundModel",
        "Properties",
        "RecognitionConfig",
        "RecognitionEvent",
        "RecognitionMode",
        "RecognitionStatus",
        "SoundModel",
        "SoundModelType",
        "Status",
        "IThermalService",
        "IPowerManager",
        "ITunerResourceManager",
        // b/278147400
        "IActivityManager",
        "IUidObserver",
        "IDrm",
        "IVsyncCallback",
        "IVsyncService",
        "ICallback",
        "IIPCTest",
        "ISafeInterfaceTest",
        "IGpuService",
        "IConsumerListener",
        "IGraphicBufferConsumer",
        "ITransactionComposerListener",
        "SensorEventConnection",
        "SensorServer",
        "ICamera",
        "ICameraClient",
        "ICameraRecordingProxy",
        "ICameraRecordingProxyListener",
        "ICrypto",
        "IOMXObserver",
        "IStreamListener",
        "IStreamSource",
        "IAudioService",
        "IDataSource",
        "IDrmClient",
        "IMediaCodecList",
        "IMediaDrmService",
        "IMediaExtractor",
        "IMediaExtractorService",
        "IMediaHTTPConnection",
        "IMediaHTTPService",
        "IMediaLogService",
        "IMediaMetadataRetriever",
        "IMediaMetricsService",
        "IMediaPlayer",
        "IMediaPlayerClient",
        "IMediaPlayerService",
        "IMediaRecorder",
        "IMediaRecorderClient",
        "IMediaResourceMonitor",
        "IMediaSource",
        "IRemoteDisplay",
        "IRemoteDisplayClient",
        "IResourceManagerClient",
        "IResourceManagerService",
        "IComplexTypeInterface",
        "IPermissionController",
        "IPingResponder",
        "IProcessInfoService",
        "ISchedulingPolicyService",
        "IStringConstants",
        "IObbActionListener",
        "IStorageEventListener",
        "IStorageManager",
        "IStorageShutdownObserver",
        "IPersistentVrStateCallbacks",
        "IVrManager",
        "IVrStateCallbacks",
        "ISurfaceComposer",
        "IMemory",
        "IMemoryHeap",
        "IProcfsInspector",
        "IAppOpsCallback",
        "IAppOpsService",
        "IBatteryStats",
        "IResultReceiver",
        "IShellCallback",
        "IDrmManagerService",
        "IDrmServiceListener",
        "IAAudioClient",
        "IAAudioService",
        "VtsFuzzer",
)
Loading