NethermindEth / cairo-vm-go

A virtual machine for Cairo written in Go
MIT License
79 stars 49 forks source link

bug: memory and trace slight differ for one relocated value in execution mode #520

Closed TAdev0 closed 2 weeks ago

TAdev0 commented 1 month ago

It seems the error is always related to relocation at the very beginning of the execution segment.

for example with some cairo file, python VM produces the following execution segment in memory:

⋮
1:0   2:0
1:1   3:0
1:2   4:0
1:3   2:0

which, after relocation, corresponds to :

242
255
255
242

Because segments 3 and 4 both refer to 255 which is the end of the relocated memory, it means segment 3 and 4 are the empty ones used for the return values of fp an pc

with our VM, we produce the following memory:

 ⋮
1:0   2:0
1:1   2:0
1:2   4:0
1:3   2:0

and after relocation:

242
242
255
242

I checked like 20 Cairo files, its always the same problem, we have this only one relocatable that points to the wrong segment, maybe an issue in the way fp and pc are returned in execution mode?

MKVEERENDRA commented 1 month ago

It seems like you're encountering an issue where the relocation of memory segments in your Python VM is resulting in incorrect pointers or values pointing to the wrong segments. This type of issue often arises from how pointers or addresses are managed during the execution phase, particularly when dealing with relocatable memory segments.

Here are some insights and steps you can consider to debug and potentially resolve this issue:

Understanding the Problem From your description, it appears that after relocation, segments in your memory are not pointing to the expected locations. Specifically:

Original memory segments seem to be correctly indexed and referenced. After relocation, the indexing or referencing is incorrect, leading to segments pointing to unexpected locations (e.g., segment 3 and 4 pointing to 255, which is not correct). Potential Causes Pointer Calculation Issues: Verify how pointers or addresses are calculated during the relocation process. Ensure that pointers are correctly updated to reflect the new memory layout after relocation.

Segment Identification: Check how segments are identified or indexed before and after relocation. Ensure that the indexing mechanism accurately reflects the relocated memory layout.

Segment Boundaries: Verify that segment boundaries are correctly defined and respected during relocation. Incorrect boundary definitions can lead to overlapping or misaligned segments.

Debugging Steps To address this issue, you can take the following steps:

Logging and Debugging: Introduce logging statements or debug outputs in your Python VM code to track the values of pointers and segment indices before and after relocation. This can help pinpoint where the incorrect values or pointers are being generated.

Review Relocation Logic: Review the code responsible for relocating memory segments. Ensure that all segments are correctly updated according to their new addresses after relocation.

Memory Allocation Checks: Verify how memory allocation and segment assignment are handled in your VM. Ensure that each segment is allocated and assigned the correct address space.

Compare with Working Examples: If possible, compare the problematic Cairo files with similar ones that work correctly. Identify any differences in how memory relocation or segment referencing is handled.

Unit Tests: Develop or enhance unit tests specifically targeting memory relocation and segment referencing. Use these tests to validate that memory segments are correctly relocated and referenced.

Collaboration and Community Support If the issue persists despite these efforts, consider reaching out to the community or forums where similar VM implementations or Cairo files are discussed. Others may have encountered similar issues and can provide insights or solutions based on their experiences.

By systematically reviewing your relocation logic, ensuring correct pointer calculations, and validating segment boundaries, you should be able to resolve the issue of segments pointing to incorrect locations after relocation in your Python VM.

MaksymMalicki commented 2 weeks ago

Hey, could you provide more details on this? Which Cairo files did you run? What do you mean by execution mode?

TAdev0 commented 2 weeks ago

execution mode is non proof mode

All cairo files that make use of builtins overall

It seems we dont write correctly in memory the empty segment created for returning either FP or PC