-
Notifications
You must be signed in to change notification settings - Fork 139
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Generate features from parameter values in annotations.
- Loading branch information
Pascal Kesseli
committed
Sep 29, 2023
1 parent
1ec4924
commit 362e527
Showing
32 changed files
with
3,085 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
#include <mariana-trench/AnnotationFeature.h> | ||
#include <mariana-trench/Context.h> | ||
#include <mariana-trench/FeatureFactory.h> | ||
#include <mariana-trench/JsonValidation.h> | ||
|
||
namespace marianatrench { | ||
|
||
AnnotationFeature::AnnotationFeature(Root port, const DexType* dex_type, std::optional<std::string> label) : port_(std::move(port)), dex_type_(dex_type), label_(label) { | ||
} | ||
|
||
const AnnotationFeature* AnnotationFeature::from_json(const Json::Value& value, Context& context) { | ||
JsonValidation::validate_object(value); | ||
Root port = Root::from_json(value["port"]); | ||
const DexType* dex_type = JsonValidation::dex_type(value, "type"); | ||
Json::Value configured_label = value["label"]; | ||
std::optional<std::string> label; | ||
if (!configured_label.isNull()) { | ||
label = JsonValidation::string(configured_label); | ||
} | ||
return context.feature_factory->get_unique_annotation_feature(AnnotationFeature{std::move(port), dex_type, std::move(label)}); | ||
} | ||
|
||
std::size_t AnnotationFeature::hash() const { | ||
std::size_t seed = 0; | ||
boost::hash_combine(seed, std::hash<Root>{}(port_)); | ||
boost::hash_combine(seed, dex_type_); | ||
boost::hash_combine(seed, label_); | ||
return seed; | ||
} | ||
|
||
bool AnnotationFeature::operator==(const AnnotationFeature& other) const { | ||
return port_ == other.port_ && dex_type_ == other.dex_type_ && label_ == other.label_; | ||
} | ||
|
||
const Root& AnnotationFeature::port() const { | ||
return port_; | ||
} | ||
|
||
const DexType* AnnotationFeature::dex_type() const { | ||
return dex_type_; | ||
} | ||
|
||
const std::optional<std::string>& AnnotationFeature::label() const { | ||
return label_; | ||
} | ||
|
||
std::ostream& operator<<(std::ostream& out, const AnnotationFeature& annotation_feature) { | ||
out << "AnnotationFeature(port=`" | ||
<< annotation_feature.port() | ||
<< "`, dex_type=`" | ||
<< annotation_feature.dex_type()->str(); | ||
if (annotation_feature.label()) { | ||
out << "`, label=`" << *annotation_feature.label(); | ||
} | ||
return out << "`)"; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include <functional> | ||
|
||
#include <mariana-trench/Access.h> | ||
|
||
namespace marianatrench { | ||
|
||
/** | ||
* Annotation features are mapped to regular user features at model template | ||
* instantiation time. They consist of an annotation type to find and a root | ||
* port to indicate whether it is a method annotation (Root::Return) or one of | ||
* its parameters. The usere label content is set to the content of the | ||
* annotation's `value()` parameter, if present. | ||
*/ | ||
class AnnotationFeature { | ||
public: | ||
/** Public for testing purposes only, prod only uses from_json. */ | ||
AnnotationFeature(Root port, const class DexType* dex_type, std::optional<std::string> label); | ||
|
||
/** | ||
* Parses the given JSON object, returning a feature created using a | ||
* UniquePointerFactory. | ||
*/ | ||
static const AnnotationFeature* from_json(const Json::Value& value, class Context& context); | ||
|
||
/** Used to store in a UniquePointerFactory. */ | ||
std::size_t hash() const; | ||
bool operator==(const AnnotationFeature& other) const; | ||
|
||
/** | ||
* Annotation location. Root::Return for method, Root::Argument for | ||
* parameters. | ||
*/ | ||
const Root& port() const; | ||
|
||
/** Type of the annotation. */ | ||
const DexType* dex_type() const; | ||
|
||
/** Label to use for annotation feature value. */ | ||
const std::optional<std::string>& label() const; | ||
|
||
friend std::ostream& operator<<( | ||
std::ostream& out, | ||
const AnnotationFeature& annotation_feature); | ||
|
||
private: | ||
Root port_; | ||
const DexType* dex_type_; | ||
std::optional<std::string> label_; | ||
}; | ||
|
||
} | ||
|
||
template <> | ||
struct std::hash<marianatrench::AnnotationFeature> { | ||
std::size_t operator()(const marianatrench::AnnotationFeature& annotation_feature) const { | ||
return annotation_feature.hash(); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
#include <Show.h> | ||
|
||
#include <mariana-trench/AnnotationFeatureSet.h> | ||
#include <mariana-trench/JsonValidation.h> | ||
#include <mariana-trench/Show.h> | ||
|
||
namespace marianatrench { | ||
|
||
AnnotationFeatureSet::AnnotationFeatureSet(Set set) : set_(std::move(set)) {} | ||
|
||
AnnotationFeatureSet AnnotationFeatureSet::from_json(const Json::Value& value, Context& context) { | ||
AnnotationFeatureSet features; | ||
for (const auto& feature_value : JsonValidation::null_or_array(value)) { | ||
features.add(AnnotationFeature::from_json(feature_value, context)); | ||
} | ||
return features; | ||
} | ||
|
||
std::ostream& operator<<(std::ostream& out, const AnnotationFeatureSet& annotation_features) { | ||
return show_set(out, annotation_features); | ||
} | ||
|
||
} // namespace marianatrench |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include <sparta/AbstractDomain.h> | ||
|
||
#include <mariana-trench/AnnotationFeature.h> | ||
#include <mariana-trench/IncludeMacros.h> | ||
#include <mariana-trench/PatriciaTreeSetAbstractDomain.h> | ||
|
||
namespace marianatrench { | ||
|
||
/** | ||
* Used to store annotation features in a TaintConfig. Annotation features are | ||
* incomplete user feature templates, and are thus not copied to a Frame. | ||
* Instead they are instantiated as user features during template model | ||
* instantiation. | ||
*/ | ||
class AnnotationFeatureSet final : public sparta::AbstractDomain<AnnotationFeatureSet> { | ||
private: | ||
using Set = PatriciaTreeSetAbstractDomain< | ||
const AnnotationFeature*, | ||
/* bottom_is_empty */ true, | ||
/* with_top */ false>; | ||
|
||
public: | ||
INCLUDE_SET_MEMBER_TYPES(Set, const AnnotationFeature*) | ||
|
||
private: | ||
explicit AnnotationFeatureSet(Set set); | ||
|
||
public: | ||
/* Create the bottom (i.e, empty) feature set. */ | ||
AnnotationFeatureSet() = default; | ||
|
||
INCLUDE_DEFAULT_COPY_CONSTRUCTORS_AND_ASSIGNMENTS(AnnotationFeatureSet) | ||
INCLUDE_ABSTRACT_DOMAIN_METHODS(AnnotationFeatureSet, Set, set_) | ||
INCLUDE_SET_METHODS(AnnotationFeatureSet, Set, set_, const AnnotationFeature*, iterator) | ||
|
||
static AnnotationFeatureSet from_json(const Json::Value& value, Context& context); | ||
|
||
friend std::ostream& operator<<( | ||
std::ostream& out, | ||
const AnnotationFeatureSet& annotation_features); | ||
|
||
public: | ||
Set set_; | ||
}; | ||
|
||
} // namespace marianatrench |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.