Skip to content

Introduce temporary stack variables for getfield/-static and ?load #931

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

Closed

Conversation

mgudemann
Copy link
Contributor

When a value is pushed on the stack, it must be ensured that modification of the
origin will not modify the values on the stack.

@mgudemann mgudemann mentioned this pull request May 16, 2017
@mgudemann
Copy link
Contributor Author

this fixes the obvious cases, regression tests and thinking about possibly other cases is needed

@mgudemann
Copy link
Contributor Author

strong candidates for additional cases:

  • ?aload - returns a reference to an element in an array
  • dup? - duplicates entries on the stack, only values should be duplicated

Copy link
Member

@peterschrammel peterschrammel left a comment

Choose a reason for hiding this comment

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

Do you have tests that expose the bug?

@mgudemann
Copy link
Contributor Author

one is the code in #926 where the assert currently still succeeds (which is shouldn't) but the accessed index is the correct one now

@mgudemann
Copy link
Contributor Author

instead of

        n = n + (int)1;
        // 63 file incr.java line 7 function java::incr.f:(I)V bytecode_index 8
        ASSERT n >= 0 // Array index < 0
        // 64 file incr.java line 7 function java::incr.f:(I)V
        ASSERT !(this == null) // reference is null in *((struct incr *)(void *)this)
        // 65 file incr.java line 7 function java::incr.f:(I)V
        ASSERT !(this->nums == null) // reference is null in *((struct java::array[int] *)((struct incr *)(void *)this)->nums)
        // 66 file incr.java line 7 function java::incr.f:(I)V bytecode_index 8
        ASSERT n < ((struct java::array[int] *)((struct incr *)(void *)this)->nums)->length // Array index >= length
        // 67 file incr.java line 7 function java::incr.f:(I)V
        ASSERT !(this->nums->data + n == null) // reference is null in this->nums->data[n]
        // 68 file incr.java line 7 function java::incr.f:(I)V bytecode_index 10
        IF this->nums->data[n] == 2 THEN GOTO 1

it's now

        stack_field_tmp2 = ((struct incr *)stack_var_tmp1)->nums;
        // 78 file incr.java line 7 function java::incr.f:(I)V bytecode_index 6
        stack_var_tmp3 = n;
        // 79 file incr.java line 7 function java::incr.f:(I)V bytecode_index 7
        n = n + (int)1;
        // 80 file incr.java line 7 function java::incr.f:(I)V bytecode_index 8
        ASSERT stack_var_tmp3 >= 0 // Array index < 0
        // 81 file incr.java line 7 function java::incr.f:(I)V
        ASSERT !(stack_field_tmp2 == null) // reference is null in *((struct java::array[int] *)stack_field_tmp2)
        // 82 file incr.java line 7 function java::incr.f:(I)V bytecode_index 8
        ASSERT stack_var_tmp3 < ((struct java::array[int] *)stack_field_tmp2)->length // Array index >= length
        // 83 file incr.java line 7 function java::incr.f:(I)V
        ASSERT !(stack_field_tmp2->data + stack_var_tmp3 == null) // reference is null in stack_field_tmp2->data[stack_var_tmp3]
        // 84 file incr.java line 7 function java::incr.f:(I)V bytecode_index 10
        IF !(stack_field_tmp2->data[stack_var_tmp3] == 2) THEN GOTO 2

Copy link
Contributor

@smowton smowton left a comment

Choose a reason for hiding this comment

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

A possibility for the future to obtain correctness without dramatic uglification of the code: create the temp variables only when we come across an instruction that writes while there are reads on the stack (e.g. before an astore, for each operand it would not remove from the stack and which is non-constant, now synthesise temporary names to store them; in the common case that there are no such non-constants held over the write op (i.e. the write op drains the stack) we could still produce brief code as we do now.

However this is trickier, so this fix is probably the right way to go for now.

@tautschnig
Copy link
Collaborator

Can this please not be merged without regression tests being added? (Also all three checks are failing at the moment.)

@mgudemann
Copy link
Contributor Author

@tautschnig the regression tests are fixed, I will add specific ones for this, too
currently I am adapting the variants of dup

@mgudemann mgudemann force-pushed the fix/bytecode_args_push_on_stack branch 3 times, most recently from 88d5080 to 9f41808 Compare May 18, 2017 09:25
@mgudemann mgudemann force-pushed the fix/bytecode_args_push_on_stack branch 2 times, most recently from 78ffc65 to 6303a86 Compare May 19, 2017 06:50
@mgudemann mgudemann force-pushed the fix/bytecode_args_push_on_stack branch from 6303a86 to 8cacd7d Compare May 19, 2017 06:58
@mgudemann
Copy link
Contributor Author

@tautschnig I added regression tests for the bytecode instructions in question

@smowton you are right about polluting the GOTOs, effectively this partially recreates the stack state now, which is sub-optimal.
If I understand correctly, you propose changing the "dual" instructions (?store instead of ?load, ?putfield instead of ?getfield etc.) in a way that saves the variables / references that remain on the stack after the write into temporary variables. Now that regression tests exist to validate the implementation, I will have a look at that.

@@ -0,0 +1,22 @@
.class public stack_var1
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why is this file (and the other .j files) included?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

these are the source files I used to create the classes. This is very bytecode specific, in particular the dup2_x? are very rare, so I used the jasmin assembler to create the regression tests.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Ah, cool. Would you mind adding information in either the commit message or some extra readme file so that these steps become reproducible? I had naively thought that the .class files had been produced from the accompanying .java.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ok, will do, the stack_test.java is always the entry point and creates an object instance of the class created via jasmin.

@@ -0,0 +1,2 @@
This regression test is created from the .j file with the jasmin assembler.
https://github.com/Sable/jasmin
Copy link
Collaborator

Choose a reason for hiding this comment

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

Would it be possible to add as many details as possible, such as the precise command line? This is information that you've now got right at hand, but it will be much more work to reproduce later on. Memory is weak, git is strong!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

:-) it is actually jasmin $FILE.j but I'll add this to the READMEs

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 some comments about jasmin, how to get it and how to use it on .j files

Copy link
Collaborator

Choose a reason for hiding this comment

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

Thank you!

@mgudemann mgudemann force-pushed the fix/bytecode_args_push_on_stack branch from 67d361d to 52f522d Compare May 19, 2017 12:30
@mgudemann
Copy link
Contributor Author

see #951 for an alternative fix working on the write instructions

@mgudemann
Copy link
Contributor Author

superseded by #951 which takes a different approach that creates less temporary variables

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