Skip to content
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

Always use a normal segment for first SegmentArena segment #1845

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

notlesh
Copy link

@notlesh notlesh commented Sep 25, 2024

(Note that this is basically a clone of Moonsong-Labs#45. We'd love to get this accepted upstream if possible.)

Problem: Starknet OS expects SegmentArena segments to all be temporary except the first segment, which must be normal. This is so that relocation rules for all segments can chain back into the first.

Solution: Use temporary segments only if it's not the first.

Description

The Starknet OS verifies the continuity of the segments in SegmentArenas here, which requires relocation rules to be properly set up.

The requirements for this to work are that each src_ptr is a temporary segment and each dest_ptr is a non-temporary segment (or a temporary segment with an existing rule mapping it to a non-temporary segment).

cairo-vm uses a number of hints to accomplish this, and these are injected by the sierra-to-casm compiler. However, it relies on one additional hint which is not injected anywhere in the case of SNOS.

This hint seems to ensure the same condition needed in SNOS: that the initial segment is a normal segment and that all other segments are temporary segments with relocation rules pointing back to the initial one. However, it creates the normal segment inside this last hint rather than when the SegmentArena is initialized.

SNOS handles this by simply allocating the initial segment as a normal one as explained here. This seems to alleviate the need to later recreate the segment in relocate_all_segments(), allowing the implementation to work in SNOS without the hint mentioned above.

Checklist

  • Linked to Github Issue
  • Unit tests added
  • Integration tests added.
  • This change requires new documentation.
    • Documentation has been added/updated.
    • CHANGELOG has been updated.

@pefontana
Copy link
Member

pefontana commented Sep 27, 2024

Hi @notlesh
Thanks for the contribution!
There is just a minor error in the CI in the make check-fmt check
Also, would it be possible to add a small program or a little test for this fix?

@notlesh
Copy link
Author

notlesh commented Sep 29, 2024

Hi @notlesh Thanks for the contribution! There is just a minor error in the CI in the make check-fmt check Also, would it be possible to add a small program or a little test for this fix?

I've wanted to produce a better way to test this, but it's complicated because the failure occurs in a different repo.

One idea for a test case is to show that the dictionaries do not have a temporary segment as their first segment prior to the RelocateAllDictionaries cheatcode being called -- this is fundamentally what was broken and what is fixed here.

This should act as a regression test (reproducing the problem and demonstrating that it's fixed) but it doesn't reproduce the exact error. Let me know what you think and if you want more details or want to know how you can reproduce the exact error yourself.

@pefontana
Copy link
Member

This should act as a regression test (reproducing the problem and demonstrating that it's fixed) but it doesn't reproduce the exact error. Let me know what you think and if you want more details or want to know how you can reproduce the exact error yourself.

What do you mean by it doesn't reproduce the exact error? Can we add some Cairo program that plays with dictionaries triggering the error? O maybe just add some unit tests

Also, there are merge conflicts that need to let me approve the CI run, can you solve them?

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.

2 participants