-
Notifications
You must be signed in to change notification settings - Fork 277
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
Include annotations in Java class loading [TG-3831] #3227
Conversation
There was a problem hiding this 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); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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( |
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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)) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 todo
s 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
.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
.
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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)"
e835fd7
to
01925c4
Compare
There was a problem hiding this 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.
01925c4
to
6fce632
Compare
There was a problem hiding this 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.
There was a problem hiding this 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
There was a problem hiding this 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)) |
There was a problem hiding this comment.
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?
jbmc/regression/jbmc/class-loading-annotations/test-class-value-annotation-on-method.desc
Show resolved
Hide resolved
jbmc/regression/jbmc/class-loading-annotations/test-class-value-annotation-on-method.desc
Outdated
Show resolved
Hide resolved
There was a problem hiding this 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)) |
There was a problem hiding this comment.
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
.
d3561d3
to
080f05e
Compare
There was a problem hiding this 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.
There was a problem hiding this 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) {} |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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'
.
There was a problem hiding this comment.
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
.
080f05e
to
2c4b05b
Compare
There was a problem hiding this 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.
2c4b05b
to
c51be4d
Compare
There was a problem hiding this 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.
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.
c51be4d
to
48d9e44
Compare
There was a problem hiding this 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
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.