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

fix: improve error message for local memory index bounds check #1640

Open
wants to merge 6 commits into
base: next
Choose a base branch
from

Conversation

crStiv
Copy link

@crStiv crStiv commented Jan 22, 2025

Description:

  • Enhanced error message for out-of-bounds local memory access
  • Added specific details in error message:
    • Invalid index value
    • Total number of available locals
    • Valid index range
  • Replaced generic validation with explicit bounds check

Example of new error message:
"invalid local memory index: 5 is out of bounds for procedure with 3 locals (valid range: 0..2)"

Fixes #1619

Comment on lines 129 to 137
return Err(AssemblyError::Other(
Report::msg(
format!(
"invalid local memory index: {} is out of bounds for procedure with {} locals (valid range: 0..{})",
index, num_proc_locals, max
)
)
.into(),
));
Copy link
Contributor

Choose a reason for hiding this comment

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

We actually already have the machinery to output very nice error messages (built on top of miette). The desired solution would output an error variant that is similar to InvalidSyscallTarget (in that it takes a SourceSpan and a SourceFile).

@crStiv
Copy link
Author

crStiv commented Jan 23, 2025

@plafer hi Philippe! made some changes, what do you think abt them?

Copy link
Contributor

@plafer plafer left a comment

Choose a reason for hiding this comment

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

Left some suggestions on how to fix the problems.

Could you also fix the merge conflicts by merging in the next branch?

Also please make sure that the code compiles, as well as removing any compiler warnings (i.e. cargo check should not print any warning).

Thank you!

Comment on lines +130 to +131
span: block_builder.get_current_span(),
source_file: block_builder.get_source_file(),
Copy link
Contributor

Choose a reason for hiding this comment

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

This currently doesn't compile, right? These methods don't exist on BasicBlockBuilder.

Let rewrite the method to:

pub fn local_to_absolute_addr(
    block_builder: &mut BasicBlockBuilder,
    index: u16,
    proc_ctx: &ProcedureContext,
) -> Result<(), AssemblyError> {
    let num_proc_locals = proc_ctx.num_locals();
    if num_proc_locals == 0 {
        return Err(AssemblyError::Other(
            Report::msg(
                "number of procedure locals was not set (or set to 0), but local values were used"
                    .to_string(),
            )
            .into(),
        ));
    }

    let max_index = num_proc_locals - 1;
    if index > max_index {
        let span = proc_ctx.span();
        return Err(AssemblyError::InvalidLocalMemoryIndex {
            span,
            source_file: proc_ctx.source_manager().get(span.source_id()).ok(),
            index,
            num_locals: num_proc_locals,
            max_index,
        });
    }

    push_felt(block_builder, -Felt::from(max_index - index));
    block_builder.push_op(FmpAdd);

    Ok(())
}

and adjust every caller to pass in the ProcedureContext instead of num_proc_locals

Comment on lines 93 to 101
#[error("invalid local word index: {local_addr}")]
#[diagnostic()]
InvalidLocalWordIndex {
#[label]
span: SourceSpan,
#[source_code]
source_file: Option<Arc<SourceFile>>,
local_addr: u16,
},
Copy link
Contributor

Choose a reason for hiding this comment

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

let's remove this as it's unused

source_file: Option<Arc<SourceFile>>,
index: u16,
num_locals: u16,
max: u16,
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
max: u16,
max_index: u16,

Comment on lines 82 to 83
#[error("invalid local memory index: {index} is out of bounds (valid range: 0..{max})")]
#[diagnostic(help("procedure has {num_locals} locals available"))]
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
#[error("invalid local memory index: {index} is out of bounds (valid range: 0..{max})")]
#[diagnostic(help("procedure has {num_locals} locals available"))]
#[error("invalid local memory index: {index} is out of bounds")]
#[diagnostic(help("procedure has {num_locals} locals available, so the valid range for the index is 0..{max_index}"))]

@crStiv crStiv changed the base branch from main to next January 30, 2025 18:13
@crStiv
Copy link
Author

crStiv commented Feb 1, 2025

@plafer made an update

@plafer
Copy link
Contributor

plafer commented Feb 3, 2025

@crStiv this branch still doesn't compile (i.e. make sure cargo build succeeds)

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.

Improve error message for locals index being out of bound
2 participants