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
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 16 additions & 20 deletions assembly/src/assembler/instruction/mem_ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ pub fn mem_read(
// if the address was provided as an immediate value, put it onto the stack
if let Some(addr) = addr {
if is_local {
let num_locals = proc_ctx.num_locals();
local_to_absolute_addr(block_builder, addr as u16, num_locals, is_single)?;
local_to_absolute_addr(block_builder, addr as u16, proc_ctx)?;
} else {
push_u32_value(block_builder, addr);
}
Expand Down Expand Up @@ -81,7 +80,7 @@ pub fn mem_write_imm(
is_single: bool,
) -> Result<(), AssemblyError> {
if is_local {
local_to_absolute_addr(block_builder, addr as u16, proc_ctx.num_locals(), is_single)?;
local_to_absolute_addr(block_builder, addr as u16, proc_ctx)?;
} else {
push_u32_value(block_builder, addr);
}
Expand Down Expand Up @@ -111,10 +110,10 @@ pub fn mem_write_imm(
/// Returns an error if index is greater than the number of procedure locals.
pub fn local_to_absolute_addr(
block_builder: &mut BasicBlockBuilder,
index_of_local: u16,
num_proc_locals: u16,
is_single: bool,
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(
Expand All @@ -125,21 +124,18 @@ pub fn local_to_absolute_addr(
));
}

// If a single local value is being accessed, then the index can take the full range
// [0, num_proc_locals - 1]. Otherwise, the index can take the range [0, num_proc_locals - 4]
// to account for the fact that a full word is being accessed.
let max = if is_single {
num_proc_locals - 1
} else {
num_proc_locals - 4
};
validate_param(index_of_local, 0..=max)?;
let max_index = num_proc_locals - 1;
if index > max_index {
return Err(AssemblyError::InvalidLocalMemoryIndex {
span: block_builder.get_current_span(),
source_file: block_builder.get_source_file(),
Comment on lines +130 to +131
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

index,
num_locals: num_proc_locals,
max_index,
});
}

// Local values are placed under the frame pointer, so we need to calculate the offset of the
// local value from the frame pointer.
// The offset is in the range [1, num_proc_locals], which is then subtracted from `fmp`.
let fmp_offset_of_local = num_proc_locals - index_of_local;
push_felt(block_builder, -Felt::from(fmp_offset_of_local));
push_felt(block_builder, -Felt::from(max_index - index));
block_builder.push_op(FmpAdd);

Ok(())
Expand Down
11 changes: 11 additions & 0 deletions assembly/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,17 @@ pub enum AssemblyError {
#[source_code]
source_file: Option<Arc<SourceFile>>,
},
#[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}"))]
InvalidLocalMemoryIndex {
#[label("invalid index used here")]
span: SourceSpan,
#[source_code]
source_file: Option<Arc<SourceFile>>,
index: u16,
num_locals: u16,
max_index: u16,
},
#[error(transparent)]
#[diagnostic(transparent)]
Other(RelatedError),
Expand Down