From d00618f4f4776180fc43d2b0fc80330990a5d8b8 Mon Sep 17 00:00:00 2001 From: Anders Schack-Mulligen Date: Fri, 21 May 2021 14:56:47 +0200 Subject: [PATCH 1/2] Java: Improve performance of virtual dispatch calculation. --- .../dataflow/internal/DataFlowDispatch.qll | 2 +- .../code/java/dispatch/VirtualDispatch.qll | 19 +++++++++++-------- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/java/ql/src/semmle/code/java/dataflow/internal/DataFlowDispatch.qll b/java/ql/src/semmle/code/java/dataflow/internal/DataFlowDispatch.qll index 3a98ed62847..2b6179bfc96 100644 --- a/java/ql/src/semmle/code/java/dataflow/internal/DataFlowDispatch.qll +++ b/java/ql/src/semmle/code/java/dataflow/internal/DataFlowDispatch.qll @@ -106,7 +106,7 @@ private module DispatchImpl { mayBenefitFromCallContext(ma, c, i) and c = viableCallable(ctx) and contextArgHasType(ctx, i, t, exact) and - ma.getMethod() = def + ma.getMethod().getSourceDeclaration() = def | exact = true and result = VirtualDispatch::exactMethodImpl(def, t.getSourceDeclaration()) or diff --git a/java/ql/src/semmle/code/java/dispatch/VirtualDispatch.qll b/java/ql/src/semmle/code/java/dispatch/VirtualDispatch.qll index bb6884ced06..46c29c386b2 100644 --- a/java/ql/src/semmle/code/java/dispatch/VirtualDispatch.qll +++ b/java/ql/src/semmle/code/java/dispatch/VirtualDispatch.qll @@ -39,9 +39,12 @@ Callable viableCallable(Call c) { c instanceof ConstructorCall and result = c.getCallee().getSourceDeclaration() } -/** A method that is the target of a call. */ -class CalledMethod extends Method { - CalledMethod() { exists(MethodAccess ma | ma.getMethod() = this) } +/** The source declaration of a method that is the target of a virtual call. */ +class VirtCalledSrcMethod extends SrcMethod { + pragma[nomagic] + VirtCalledSrcMethod() { + exists(VirtualMethodAccess ma | ma.getMethod().getSourceDeclaration() = this) + } } cached @@ -185,8 +188,8 @@ private module Dispatch { not result.isAbstract() and if source instanceof VirtualMethodAccess then - exists(CalledMethod def, RefType t, boolean exact | - source.getMethod() = def and + exists(VirtCalledSrcMethod def, RefType t, boolean exact | + source.getMethod().getSourceDeclaration() = def and hasQualifierType(source, t, exact) | exact = true and result = exactMethodImpl(def, t.getSourceDeclaration()) @@ -301,14 +304,14 @@ private module Dispatch { /** Gets the implementation of `top` present on a value of precisely type `t`. */ cached - Method exactMethodImpl(CalledMethod top, SrcRefType t) { + Method exactMethodImpl(VirtCalledSrcMethod top, SrcRefType t) { hasSrcMethod(t, result) and - top.getAPossibleImplementation() = result + top.getAPossibleImplementationOfSrcMethod() = result } /** Gets the implementations of `top` present on viable subtypes of `t`. */ cached - Method viableMethodImpl(CalledMethod top, SrcRefType tsrc, RefType t) { + Method viableMethodImpl(VirtCalledSrcMethod top, SrcRefType tsrc, RefType t) { exists(SrcRefType sub | result = exactMethodImpl(top, sub) and tsrc = t.getSourceDeclaration() and From 4884da363f41dd73521f62b8de0ce918955afcfe Mon Sep 17 00:00:00 2001 From: Anders Schack-Mulligen Date: Tue, 25 May 2021 11:48:35 +0200 Subject: [PATCH 2/2] Java: Bugfix. --- java/ql/src/semmle/code/java/dispatch/VirtualDispatch.qll | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/java/ql/src/semmle/code/java/dispatch/VirtualDispatch.qll b/java/ql/src/semmle/code/java/dispatch/VirtualDispatch.qll index 46c29c386b2..a26ff92b704 100644 --- a/java/ql/src/semmle/code/java/dispatch/VirtualDispatch.qll +++ b/java/ql/src/semmle/code/java/dispatch/VirtualDispatch.qll @@ -76,7 +76,7 @@ private module Dispatch { ( exists(Method def, RefType t, boolean exact | qualType(ma, t, exact) and - def = ma.getMethod() + def = ma.getMethod().getSourceDeclaration() | exact = true and result = exactMethodImpl(def, t.getSourceDeclaration()) or