Skip to content

[TG-2365] Parse BootstrapMethods attribute #1804

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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added regression/cbmc-java/lambda1/B.class
Binary file not shown.
9 changes: 9 additions & 0 deletions regression/cbmc-java/lambda1/B.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
public class B {
public int y;

public static java.util.function.Function<Double, Double> dmul = x -> x * 3.1415;

public void set(int x) {
y = x;
}
}
Binary file added regression/cbmc-java/lambda1/C.class
Binary file not shown.
Binary file added regression/cbmc-java/lambda1/CustomLambda.class
Binary file not shown.
4 changes: 4 additions & 0 deletions regression/cbmc-java/lambda1/CustomLambda.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
@FunctionalInterface
interface CustomLambda<T> {
Copy link
Contributor

Choose a reason for hiding this comment

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

Would it be better to annotate this with @FunctionalInterface or is it worth leaving this out of our java in order to avoid accidentally introducing any dependency on this optional annotation?

Copy link
Contributor

Choose a reason for hiding this comment

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

Why not both? 😛

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I added the annotation, this won't introduce a dependency as the annotation isn't used in any way in JBMC

Copy link
Contributor

Choose a reason for hiding this comment

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

Looks good.

boolean is_ok(T t);
}
Binary file added regression/cbmc-java/lambda1/Lambdatest$A.class
Binary file not shown.
Binary file added regression/cbmc-java/lambda1/Lambdatest.class
Binary file not shown.
84 changes: 84 additions & 0 deletions regression/cbmc-java/lambda1/Lambdatest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import java.util.function.*;

public class Lambdatest {

class A {
int x;
}

CustomLambda<Integer> custom = x -> true;
BiFunction<Float, Integer, Integer> add = (x0, y0) -> x0.intValue() + y0;
int z = 10;

A a;
B b = new B();

public Integer g(Float x, Integer y, BiFunction<Float, Integer, Integer> fun) {
return fun.apply(x, y);
}

public int f(Float x, Integer y, Integer z) {
Integer tmp = add.apply(x, y);
Function<Integer, Integer> mul = (a) -> a * tmp;
return mul.apply(z);
}

public int i(int x) {
int z = 5;
Function<Integer, Integer> foo = (a) -> a * z;
return foo.apply(x);
}

public int j(int x) {
Function<Integer, Integer> foo = (a) -> a * z;
return foo.apply(x);
}

public int k(int x) {
a.x = 10;

Function<Integer, Integer> foo = (y) -> y * a.x;
return foo.apply(x);
}

public int l(int x) {
b.y = 10;
Function<Integer, Integer> foo = (y) -> {
int r = y * b.y;
b.set(r);
return r;
};
b = new B();
b.y = 14;
return foo.apply(x);
}

public int m(int x) {
b.y = 10;
Function<Integer, Integer> foo = (y) -> {
int r = y * b.y;
b.y = r;
return r;
};
return foo.apply(x);
}

// test static field of different class
public double d(Double x) {
return B.dmul.apply(x);
}

public int capture2(Float a) {
return add.apply(a, 1);
}

public boolean custom(Integer i) {
return custom.is_ok(i);
}
}

class C implements CustomLambda<Integer> {
Copy link
Contributor

Choose a reason for hiding this comment

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

Why is this class C included? It doesn't appear to be referenced in the java or checked in the output.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

this is true, I'll add some check for it. This was done to see different instances of lambda functions in .class files.

public boolean is_ok(Integer i) {
return true;
}
}
12 changes: 12 additions & 0 deletions regression/cbmc-java/lambda1/test.desc
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
CORE
Lambdatest.class
--verbosity 10 --show-goto-functions
lambda function reference lambda\$new\$0 in class \"Lambdatest\"
lambda function reference lambda\$new\$1 in class \"Lambdatest\"
lambda function reference lambda\$f\$2 in class \"Lambdatest\"
lambda function reference lambda\$i\$3 in class \"Lambdatest\"
lambda function reference lambda\$j\$4 in class \"Lambdatest\"
lambda function reference lambda\$k\$5 in class \"Lambdatest\"
lambda function reference lambda\$l\$6 in class \"Lambdatest\"
lambda function reference lambda\$m\$7 in class \"Lambdatest\"
lambda function reference lambda\$static\$0 in class \"B\"
3 changes: 3 additions & 0 deletions src/java_bytecode/java_bytecode_parse_tree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ void java_bytecode_parse_treet::classt::swap(
other.fields.swap(fields);
other.methods.swap(methods);
other.annotations.swap(annotations);
std::swap(
other.attribute_bootstrapmethods_read, attribute_bootstrapmethods_read);
std::swap(other.lambda_method_handle_map, lambda_method_handle_map);
}

void java_bytecode_parse_treet::output(std::ostream &out) const
Expand Down
25 changes: 25 additions & 0 deletions src/java_bytecode/java_bytecode_parse_tree.h
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,33 @@ class java_bytecode_parse_treet
bool is_abstract=false;
bool is_enum=false;
bool is_public=false, is_protected=false, is_private=false;
bool attribute_bootstrapmethods_read = false;
size_t enum_elements=0;

enum class method_handle_typet
{
LAMBDA_METHOD_HANDLE,
UNKNOWN_HANDLE
};

typedef std::vector<u2> u2_valuest;
class lambda_method_handlet
{
public:
method_handle_typet handle_type;
irep_idt lambda_method_name;
irep_idt interface_type;
irep_idt method_type;
u2_valuest u2_values;
lambda_method_handlet() : handle_type(method_handle_typet::UNKNOWN_HANDLE)
{
}
};

typedef std::map<std::pair<irep_idt, size_t>, lambda_method_handlet>
lambda_method_handle_mapt;
lambda_method_handle_mapt lambda_method_handle_map;

typedef std::list<irep_idt> implementst;
implementst implements;
optionalt<std::string> signature;
Expand Down
Loading