Skip to content

Possible bug with the generation of the entry code #2011

@FrancoGiachetta

Description

@FrancoGiachetta

There has already been some specific programs which return VirtualMachine(CantWriteReturnFp(...)) when they are executed. The issue may be coming from the generated entry_code (which is the first part of the casm that gets executed first). It seems to be creating a case in which the vm tries to store two different values in the same memory cell. I’ve gathered some information about one of these programs which is stated in this issue.

This is entry_code that gets generated:

[ap + 0] = [fp + -3], ap++
ap += 1
%{ memory[ap + -1] = segments.add() %}
ap += 1
call rel 4
[ap + 0] = [ap + -6], ap++
ret

These are the steps until the program crashes:

  1. Inserts the range-check builtin in address ap + 1 and updates the ap by 1.
  2. Updates the ap by 1. This is due to the use of the gas builtin, the gas used gets stored implicitly here and then the ap gets updated here. This is why we only see the update of the ap.
  3. Tries to write the memory in ap - 1. Since that’s where the gas had just been stored, it fails (the same memory cell can’t be writen twice with two different values).

Something I’ve tried is to update the ap by an arbitrary value num after the initial gas gets stored. Surprisingly, the resulting casm would try to compensate that increment by the same value, so it would try to access the same conflicting memory cell anyways:

ap += <num>
%{ memory[ap + -<num>] = segments.add() %}

I've also tried to run the same program with cairo-lang-runner, which run successfully, and got this entry_code:

[ap + 0] = [fp + -3], ap++
%{ raise NotImplementedError("memory[ap + 0].. = params[0])") %}
%{ memory[ap + 1] = segments.add() %}
ap += 2
call rel 3
ret

It handles the ap updating differently, and avoids this collision.

Other issues related:

To Reproduce
Execute the cairo code in this issue.

Expected behavior
Should execute without errors

What version/commit are you on?
2.0.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions