Skip to content

Commit

Permalink
Ignore signature polymorphic invocations in ImportDepsChecker
Browse files Browse the repository at this point in the history
Calls to MethodHandle.invoke` and `invokeExact` are signature polymorphic, it
isn't possible to check that the corresponding descriptors exist statically.

PiperOrigin-RevId: 445519747
  • Loading branch information
cushon authored and copybara-github committed Apr 29, 2022
1 parent 341d7f3 commit 793ae52
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -337,10 +337,29 @@ public void visitFieldInsn(int opcode, String owner, String name, String desc) {

@Override
public void visitMethodInsn(int opcode, String owner, String name, String desc, boolean itf) {
checkMember(owner, name, desc);
if (!isMethodHandle(opcode, owner, name)) {
checkMember(owner, name, desc);
}
super.visitMethodInsn(opcode, owner, name, desc, itf);
}

private boolean isMethodHandle(int opcode, String owner, String name) {
if (opcode != Opcodes.INVOKEVIRTUAL) {
return false;
}
if (!owner.equals("java/lang/invoke/MethodHandle")) {
return false;
}
switch (name) {
case "invoke":
case "invokeExact":
break;
default:
return false;
}
return true;
}

@Override
public void visitInvokeDynamicInsn(String name, String desc, Handle bsm, Object... bsmArgs) {
checkDescriptor(desc);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,32 @@ sh_test(
],
)

sh_test(
name = "invoke_polymorphic_test",
srcs = ["golden_test.sh"],
args = [
"$(location golden_invoke_polymorphic_test.txt)",
"$(location golden_invoke_polymorphic_test.stderr.txt)",
"199",
"$(location :DumpProto)",
"$(location //src/java_tools/import_deps_checker/java/com/google/devtools/build/importdeps:ImportDepsChecker)",
"--checking_mode=error",
"--bootclasspath_entry",
"$(location @local_jdk//:jre/lib/rt.jar)",
"--input",
"$(location //src/java_tools/import_deps_checker/javatests/com/google/devtools/build/importdeps/testdata:libInvokePolymorphic.jar)",
],
data = [
":DumpProto",
":libempty.jar",
"//src/java_tools/import_deps_checker/java/com/google/devtools/build/importdeps:ImportDepsChecker",
"//src/java_tools/import_deps_checker/javatests/com/google/devtools/build/importdeps:golden_invoke_polymorphic_test.stderr.txt",
"//src/java_tools/import_deps_checker/javatests/com/google/devtools/build/importdeps:golden_invoke_polymorphic_test.txt",
"//src/java_tools/import_deps_checker/javatests/com/google/devtools/build/importdeps/testdata:libInvokePolymorphic.jar",
"@local_jdk//:jre/lib/rt.jar",
],
)

java_binary(
name = "DumpProto",
srcs = ["DumpProto.java"],
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
rule_label: ""
success: true
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,8 @@ java_library(
name = "StringAnnotation",
srcs = ["StringAnnotation.java"],
)

java_library(
name = "InvokePolymorphic",
srcs = ["InvokePolymorphic.java"],
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright 2022 The Bazel Authors. All rights reserved.
//
// 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.devtools.build.importdeps.testdata;

import java.lang.invoke.MethodHandle;

public class InvokePolymorphic {
void f(MethodHandle mh) throws Throwable {
mh.invoke(true, "hello", 42);
}
}

0 comments on commit 793ae52

Please sign in to comment.