lambdaclass / cairo-vm

cairo-vm is a Rust implementation of the Cairo VM. Cairo (CPU Algebraic Intermediate Representation) is a programming language for writing provable programs, where one party can prove to another that a certain computation was executed correctly without the need for this party to re-execute the same program.
https://lambdaclass.github.io/cairo-vm
Apache License 2.0
518 stars 148 forks source link

The instruction cache only supports execution from one segment #1529

Closed odesenfans closed 10 months ago

odesenfans commented 10 months ago

Describe the bug

In an effort to run the Cairo bootloader on top of the Cairo VM, I found out that the instruction cache used in vm_core.rs only considers execution from one segment. This hypothesis is not respected when running the bootloader as we can execute code from different segments.

In this situation, the bootloader and the program(s) it attempts to load pollute each other's instruction cache, which typically ends up with a crash of the VM.

Expected behavior The cache should either be multi-dimensional or just not get in the way. I see several solutions:

  1. Use a HashMap<Relocatable, Option<Instruction>> instead of the vector used right now.
  2. Use a Vec<Vec<Option<Instruction>>: if solution 1 ends up hitting performance too badly, using a 2-D vector could be a solution.
  3. Limit the use of the cache to segment 0: just add a condition to prevent cache conflicts.

What do you think? There's also the option to introduce a caching policy to keep the current caching solution for most cases and use a more complex one when running the bootloader.

What version/commit are you on? v0.9.1.

Oppen commented 10 months ago

The cache is already side-stepped in main for non-zero segments. We need to make a new release including support for running programs from other segments.

odesenfans commented 10 months ago

You're talking about #1493, right? That should work, I'll test it.