From 4cf1062dc49cc3512ff0b1f4bf58ffb77af01e6d Mon Sep 17 00:00:00 2001 From: Michael Nebel Date: Tue, 18 Jun 2024 09:42:48 +0200 Subject: [PATCH] Java: Base the model printing on the shared implementation. --- .../modelgenerator/internal/CaptureModels.qll | 12 +-- .../internal/CaptureModelsPrinting.qll | 75 ++----------------- .../internal/CaptureModelsSpecific.qll | 46 +++--------- .../internal/CaptureSummaryFlowQuery.qll | 2 +- .../CaptureTypeBasedSummaryModels.qll | 6 +- 5 files changed, 26 insertions(+), 115 deletions(-) diff --git a/java/ql/src/utils/modelgenerator/internal/CaptureModels.qll b/java/ql/src/utils/modelgenerator/internal/CaptureModels.qll index 0237e3fdc68b1..d28f380211fc0 100644 --- a/java/ql/src/utils/modelgenerator/internal/CaptureModels.qll +++ b/java/ql/src/utils/modelgenerator/internal/CaptureModels.qll @@ -33,13 +33,13 @@ class DataFlowTargetApi extends TargetApiSpecific { DataFlowTargetApi() { not isUninterestingForDataFlowModels(this) } } -private module Printing implements PrintingSig { +private module ModelPrintingInput implements ModelPrintingSig { class Api = DataFlowTargetApi; string getProvenance() { result = "df-generated" } } -module ModelPrinting = PrintingImpl; +module Printing = ModelPrinting; /** * Holds if `c` is a relevant content kind, where the underlying type is relevant. @@ -94,7 +94,7 @@ string captureQualifierFlow(TargetApiSpecific api) { api = returnNodeEnclosingCallable(ret) and isOwnInstanceAccessNode(ret) ) and - result = ModelPrinting::asValueModel(api, qualifierString(), "ReturnValue") + result = Printing::asValueModel(api, qualifierString(), "ReturnValue") } private int accessPathLimit0() { result = 2 } @@ -202,7 +202,7 @@ string captureThroughFlow(DataFlowTargetApi api) { input = parameterNodeAsInput(p) and output = returnNodeExt.getOutput() and input != output and - result = ModelPrinting::asTaintModel(api, input, output) + result = Printing::asTaintModel(api, input, output) ) } @@ -241,7 +241,7 @@ string captureSource(DataFlowTargetApi api) { ExternalFlow::sourceNode(source, kind) and api = sink.getEnclosingCallable() and isRelevantSourceKind(kind) and - result = ModelPrinting::asSourceModel(api, sink.getOutput(), kind) + result = Printing::asSourceModel(api, sink.getOutput(), kind) ) } @@ -277,6 +277,6 @@ string captureSink(DataFlowTargetApi api) { ExternalFlow::sinkNode(sink, kind) and api = src.getEnclosingCallable() and isRelevantSinkKind(kind) and - result = ModelPrinting::asSinkModel(api, asInputArgument(src), kind) + result = Printing::asSinkModel(api, asInputArgument(src), kind) ) } diff --git a/java/ql/src/utils/modelgenerator/internal/CaptureModelsPrinting.qll b/java/ql/src/utils/modelgenerator/internal/CaptureModelsPrinting.qll index c97a555c44475..33d176c3d893a 100644 --- a/java/ql/src/utils/modelgenerator/internal/CaptureModelsPrinting.qll +++ b/java/ql/src/utils/modelgenerator/internal/CaptureModelsPrinting.qll @@ -1,72 +1,11 @@ -private import CaptureModelsSpecific +private import java as J +private import codeql.mad.modelgenerator.ModelPrinting +private import CaptureModelsSpecific as Specific -signature module PrintingSig { - /** - * The class of APIs relevant for model generation. - */ - class Api extends TargetApiSpecific; +private module ModelPrintingLang implements ModelPrintingLangSig { + class Callable = J::Callable; - /** - * Gets the string representation of the provenance of the models. - */ - string getProvenance(); + predicate partialModel = Specific::partialModel/6; } -module PrintingImpl { - /** - * Gets the summary model for `api` with `input`, `output` and `kind`. - */ - bindingset[input, output, kind] - private string asSummaryModel(Printing::Api api, string input, string output, string kind) { - result = - asPartialModel(api) + input + ";" // - + output + ";" // - + kind + ";" // - + Printing::getProvenance() - } - - string asNeutralSummaryModel(Printing::Api api) { - result = - asPartialNeutralModel(api) // - + "summary" + ";" // - + Printing::getProvenance() - } - - /** - * Gets the value summary model for `api` with `input` and `output`. - */ - bindingset[input, output] - string asValueModel(Printing::Api api, string input, string output) { - result = asSummaryModel(api, input, output, "value") - } - - /** - * Gets the taint summary model for `api` with `input` and `output`. - */ - bindingset[input, output] - string asTaintModel(Printing::Api api, string input, string output) { - result = asSummaryModel(api, input, output, "taint") - } - - /** - * Gets the sink model for `api` with `input` and `kind`. - */ - bindingset[input, kind] - string asSinkModel(Printing::Api api, string input, string kind) { - result = - asPartialModel(api) + input + ";" // - + kind + ";" // - + Printing::getProvenance() - } - - /** - * Gets the source model for `api` with `output` and `kind`. - */ - bindingset[output, kind] - string asSourceModel(Printing::Api api, string output, string kind) { - result = - asPartialModel(api) + output + ";" // - + kind + ";" // - + Printing::getProvenance() - } -} +import ModelPrintingImpl diff --git a/java/ql/src/utils/modelgenerator/internal/CaptureModelsSpecific.qll b/java/ql/src/utils/modelgenerator/internal/CaptureModelsSpecific.qll index 1ba7ece8e8ec4..8d76f89912de5 100644 --- a/java/ql/src/utils/modelgenerator/internal/CaptureModelsSpecific.qll +++ b/java/ql/src/utils/modelgenerator/internal/CaptureModelsSpecific.qll @@ -110,53 +110,25 @@ private string isExtensible(Callable c) { } /** - * Returns the appropriate type name for the model. + * Holds if the callable `c` is in package `package` + * and is a member of `type`. */ -private string typeAsModel(Callable c) { - exists(RefType type | type = c.getDeclaringType() | - result = - type.getCompilationUnit().getPackage().getName() + ";" + - type.getErasure().(J::RefType).nestedName() +private predicate qualifiedName(Callable c, string package, string type) { + exists(RefType t | t = c.getDeclaringType() | + package = t.getCompilationUnit().getPackage().getName() and + type = t.getErasure().(J::RefType).nestedName() ) } -private predicate partialModel( - Callable api, string type, string extensible, string name, string parameters +predicate partialModel( + Callable api, string package, string type, string extensible, string name, string parameters ) { - type = typeAsModel(api) and + qualifiedName(api, package, type) and extensible = isExtensible(api) and name = api.getName() and parameters = ExternalFlow::paramsString(api) } -/** - * Computes the first 6 columns for MaD rows. - */ -string asPartialModel(TargetApiSpecific api) { - exists(string type, string extensible, string name, string parameters | - partialModel(api.lift(), type, extensible, name, parameters) and - result = - type + ";" // - + extensible + ";" // - + name + ";" // - + parameters + ";" // - + /* ext + */ ";" // - ) -} - -/** - * Computes the first 4 columns for neutral MaD rows. - */ -string asPartialNeutralModel(TargetApiSpecific api) { - exists(string type, string name, string parameters | - partialModel(api, type, _, name, parameters) and - result = - type + ";" // - + name + ";" // - + parameters + ";" // - ) -} - predicate isPrimitiveTypeUsedForBulkData(J::Type t) { t.hasName(["byte", "char", "Byte", "Character"]) } diff --git a/java/ql/src/utils/modelgenerator/internal/CaptureSummaryFlowQuery.qll b/java/ql/src/utils/modelgenerator/internal/CaptureSummaryFlowQuery.qll index 24e8cd92d51d9..e3d9f944b8d57 100644 --- a/java/ql/src/utils/modelgenerator/internal/CaptureSummaryFlowQuery.qll +++ b/java/ql/src/utils/modelgenerator/internal/CaptureSummaryFlowQuery.qll @@ -80,5 +80,5 @@ string captureFlow(DataFlowTargetApi api) { string captureNoFlow(DataFlowTargetApi api) { not exists(DataFlowTargetApi api0 | exists(captureFlow(api0)) and api0.lift() = api.lift()) and api.isRelevant() and - result = ModelPrinting::asNeutralSummaryModel(api) + result = Printing::asNeutralSummaryModel(api) } diff --git a/java/ql/src/utils/modelgenerator/internal/CaptureTypeBasedSummaryModels.qll b/java/ql/src/utils/modelgenerator/internal/CaptureTypeBasedSummaryModels.qll index fb2b8141c2ee6..2ab4b8425dd92 100644 --- a/java/ql/src/utils/modelgenerator/internal/CaptureTypeBasedSummaryModels.qll +++ b/java/ql/src/utils/modelgenerator/internal/CaptureTypeBasedSummaryModels.qll @@ -283,13 +283,13 @@ private predicate output(Callable callable, TypeVariable tv, string output) { functionalSink(callable, tv, output) } -module Printing implements PrintingSig { +module ModelPrintingInput implements ModelPrintingSig { class Api = TypeBasedFlowTargetApi; string getProvenance() { result = "tb-generated" } } -private module ModelPrinting = PrintingImpl; +private module Printing = ModelPrinting; /** * A class of callables that are relevant generating summaries for based @@ -327,7 +327,7 @@ class TypeBasedFlowTargetApi extends Specific::TargetApiSpecific { output(this, tv, output) and input != output | - result = ModelPrinting::asValueModel(this, input, output) + result = Printing::asValueModel(this, input, output) ) } }