Skip to content

Include annotations in Java class loading [TG-3831] #3227

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 31, 2018

Conversation

antlechner
Copy link
Contributor

  • Each commit message has a non-empty body, explaining why the change was made. [Except for tests and comment changes.]
  • Methods or procedures I have added are documented, following the guidelines provided in CODING_STANDARD.md.
  • Regression or unit tests are included, or existing tests cover the modified code (in this case I have detailed which ones those are in the commit message).
  • My commit message includes data points confirming performance improvements (if claimed). [Not claimed.]
  • My PR is restricted to a single feature or bugfix.
  • White-space or formatting changes outside the feature-related changed lines are in commits of their own.

For every class, field and method annotation that we see in our initial dependency analysis, we load the class that the annotation is defined in as well as all types of the values it stores.

This can be useful for when we use annotations to analyze properties related to certain classes, or to force-load certain classes.

Currently no such annotations are defined in the cbmc repository, but we are using them in Diffblue code.

@antlechner antlechner changed the title Include annotations in Java class loading Include annotations in Java class loading [TG-3831] Oct 25, 2018
Copy link
Contributor

@NathanJPhillips NathanJPhillips left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems like a good PR, fixing the situation where annotations use pointers to types that are not available in the symbol table.

@@ -570,6 +576,7 @@ void java_bytecode_parsert::get_class_refs()

for(const auto &method : parse_tree.parsed_class.methods)
{
get_annotation_refs(method.annotations);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You may also like to iterate over the method parameters and grab the annotations from those too.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. I also added tests for that.

@@ -603,6 +610,43 @@ void java_bytecode_parsert::get_class_refs()
}
}

/// For each of the given annotations, get a reference to its class and
/// recursively get class references of the values it stores.
void java_bytecode_parsert::get_annotation_refs(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe get_annotation_class_refs as it's not references to the annotation that you're getting.

get_class_refs_rec(annotation.type);
for(const auto &element_value_pair : annotation.element_value_pairs)
{
const exprt &value = element_value_pair.value;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need this abbreviation? Perhaps inline it.

const typet value_type = java_type_from_string(id2string(value_id));
get_class_refs_rec(value_type);
}
if(const auto &array_expr = expr_try_dynamic_cast<array_exprt>(value))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An else here would shortcut the check.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But that could lead to unexpected behaviour if the todos in get_relement_value are addressed but the ones in this function are not? Since an enum or annotation (in the nested annotation case) could be passed as an argument to this function, so just because the argument is not a symbol_exprt, we can't just assume that it's an array_exprt.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about else if then?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I meant an else if, not just an else.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed if to else if.
Also moved the new functions below get_class_refs_rec so get_class_refs and get_class_refs_rec are together.

/// See \ref java_bytecode_parsert::get_annotation_refs.
/// For the different cases of `exprt`, see \ref
/// java_bytecode_parsert::get_relement_value.
void java_bytecode_parsert::get_annotation_refs_rec(const exprt &value)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps get_annotation_value_class_refs?

for(const exprt &operand : array_expr->operands())
get_annotation_refs_rec(operand);
}
// TODO: enum and annotation cases
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

annotation -> nested annotation
Add suffix " (once these are correctly parsed by get_relement_value)"

@antlechner antlechner force-pushed the load-annotation-classes branch from e835fd7 to 01925c4 Compare October 25, 2018 16:51
Copy link
Contributor

@allredj allredj left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR failed Diffblue compatibility checks (cbmc commit: e835fd7).
Build URL: https://travis-ci.com/diffblue/test-gen/builds/89192406
Status will be re-evaluated on next push.
Please contact @peterschrammel, @thk123, or @allredj for support.

Common spurious failures:

  • the cbmc commit has disappeared in the mean time (e.g. in a force-push)
  • the author is not in the list of contributors (e.g. first-time contributors).

The incompatibility may have been introduced by an earlier PR. In that case merging this
PR should be avoided unless it fixes the current incompatibility.

Copy link
Contributor

@allredj allredj left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR failed Diffblue compatibility checks (cbmc commit: 01925c4).
Build URL: https://travis-ci.com/diffblue/test-gen/builds/89196546
Status will be re-evaluated on next push.
Please contact @peterschrammel, @thk123, or @allredj for support.

Common spurious failures:

  • the cbmc commit has disappeared in the mean time (e.g. in a force-push)
  • the author is not in the list of contributors (e.g. first-time contributors).

The incompatibility may have been introduced by an earlier PR. In that case merging this
PR should be avoided unless it fixes the current incompatibility.

Copy link
Contributor

@allredj allredj left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Passed Diffblue compatibility checks (cbmc commit: 6fce632).
Build URL: https://travis-ci.com/diffblue/test-gen/builds/89199407

Copy link

@majakusber majakusber left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, just minor comments. And I learned about annotations on the way 😉

const typet value_type = java_type_from_string(id2string(value_id));
get_class_refs_rec(value_type);
}
if(const auto &array_expr = expr_try_dynamic_cast<array_exprt>(value))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about else if then?

Copy link
Contributor

@NathanJPhillips NathanJPhillips left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clarified my earlier suggestion but Approve anyway whether or not you implement this.

const typet value_type = java_type_from_string(id2string(value_id));
get_class_refs_rec(value_type);
}
if(const auto &array_expr = expr_try_dynamic_cast<array_exprt>(value))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I meant an else if, not just an else.

@antlechner antlechner force-pushed the load-annotation-classes branch 2 times, most recently from d3561d3 to 080f05e Compare October 26, 2018 17:29
Copy link
Contributor

@allredj allredj left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR failed Diffblue compatibility checks (cbmc commit: d3561d3).
Build URL: https://travis-ci.com/diffblue/test-gen/builds/89340401
Status will be re-evaluated on next push.
Please contact @peterschrammel, @thk123, or @allredj for support.

Common spurious failures:

  • the cbmc commit has disappeared in the mean time (e.g. in a force-push)
  • the author is not in the list of contributors (e.g. first-time contributors).

The incompatibility may have been introduced by an earlier PR. In that case merging this
PR should be avoided unless it fixes the current incompatibility.

Copy link
Contributor

@allredj allredj left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Passed Diffblue compatibility checks (cbmc commit: 080f05e).
Build URL: https://travis-ci.com/diffblue/test-gen/builds/89340641

@@ -0,0 +1,4 @@
public class ClassValueAnnotationOnParameter {
public void classValueAnnotatedParameter(
@AnnotationWithClassValue(MyClassA.class) int param) {}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⛏️ indent

--verbosity 10
Getting class `MyClassA' from file \.[\\/]MyClassA\.class
--
Getting class `MyClassB' from file \.[\\/]MyClassB\.class
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⛏️ 💡 In general I try to be more cautious about the regexes in the non-matching part (because a single type could make the test useless). Consider replacing with Getting class 'MyClassB'.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. Replaced the non-matching regexes with just MyClassB.

@antlechner antlechner force-pushed the load-annotation-classes branch from 080f05e to 2c4b05b Compare October 29, 2018 20:18
Copy link
Contributor

@allredj allredj left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚫
This PR failed Diffblue compatibility checks (cbmc commit: 2c4b05b).
Build URL: https://travis-ci.com/diffblue/test-gen/builds/89575021
Status will be re-evaluated on next push.
Please contact @peterschrammel, @thk123, or @allredj for support.

Common spurious failures:

  • the cbmc commit has disappeared in the mean time (e.g. in a force-push)
  • the author is not in the list of contributors (e.g. first-time contributors).

The incompatibility may have been introduced by an earlier PR. In that case merging this
PR should be avoided unless it fixes the current incompatibility.

Copy link
Contributor

@allredj allredj left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚫
This PR failed Diffblue compatibility checks (cbmc commit: c51be4d).
Build URL: https://travis-ci.com/diffblue/test-gen/builds/89640828
Status will be re-evaluated on next push.
Please contact @peterschrammel, @thk123, or @allredj for support.

Common spurious failures:

  • the cbmc commit has disappeared in the mean time (e.g. in a force-push)
  • the author is not in the list of contributors (e.g. first-time contributors).

The incompatibility may have been introduced by an earlier PR. In that case merging this
PR should be avoided unless it fixes the current incompatibility.

@antlechner
Copy link
Contributor Author

cbmc/develop is incompatible with Diffblue products, so this PR will have to wait for a fix.

For every class, field and method annotation that we see in our initial
dependency analysis, we load the class that the annotation is defined in
as well as all types of the values it stores.
This can be useful for when we use annotations to analyze properties
related to certain classes, or to force-load certain classes.
Copy link
Contributor

@allredj allredj left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✔️
Passed Diffblue compatibility checks (cbmc commit: 48d9e44).
Build URL: https://travis-ci.com/diffblue/test-gen/builds/89707435

@antlechner antlechner merged commit 26df513 into diffblue:develop Oct 31, 2018
@antlechner antlechner deleted the load-annotation-classes branch October 31, 2018 10:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants