<commit_before>// Copyright (c) The Diem Core Contributors
// SPDX-License-Identifier: Apache-2.0

use crate::{
    binary_views::BinaryIndexedView,
    errors::{
        bounds_error, offset_out_of_bounds as offset_out_of_bounds_error, verification_error,
        PartialVMError, PartialVMResult,
    },
    file_format::{
        AbilitySet, Bytecode, CodeOffset, CodeUnit, CompiledModule, CompiledScript, Constant,
        FieldHandle, FieldInstantiation, FunctionDefinition, FunctionDefinitionIndex,
        FunctionHandle, FunctionInstantiation, ModuleHandle, Signature, SignatureToken,
        StructDefInstantiation, StructDefinition, StructFieldInformation, StructHandle, TableIndex,
    },
    internals::ModuleIndex,
    IndexKind,
};
use move_core_types::vm_status::StatusCode;
use std::u8;

enum BoundsCheckingContext {
    Module,
    ModuleFunction(FunctionDefinitionIndex),
    Script,
}
pub struct BoundsChecker<'a> {
    view: BinaryIndexedView<'a>,
    context: BoundsCheckingContext,
}

impl<'a> BoundsChecker<'a> {
    pub fn verify_script(script: &'a CompiledScript) -> PartialVMResult<()> {
        let mut bounds_check = Self {
            view: BinaryIndexedView::Script(script),
            context: BoundsCheckingContext::Script,
        };
        bounds_check.verify_impl()?;

        let signatures = &script.signatures;
        let parameters = &script.parameters;
        match signatures.get(parameters.into_index()) {
            // The bounds checker has already checked each function definition's code, but a script's
            // code exists outside of any function definition. It gets checked here.
            Some(signature) => {
                bounds_check.check_code(&script.code, &script.type_parameters, signature)
            }
            None => Err(bounds_error(
                StatusCode::INDEX_OUT_OF_BOUNDS,
                IndexKind::Signature,
                parameters.into_index() as u16,
                signatures.len(),
            )),
        }
    }

    pub fn verify_module(module: &'a CompiledModule) -> PartialVMResult<()> {
        let mut bounds_check = Self {
            view: BinaryIndexedView::Module(module),
            context: BoundsCheckingContext::Module,
        };
        if bounds_check.view.module_handles().is_empty() {
            let status =
                verification_error(StatusCode::NO_MODULE_HANDLES, IndexKind::ModuleHandle, 0);
            return Err(status);
        }
        bounds_check.verify_impl()
    }

    fn verify_impl(&mut self) -> PartialVMResult<()> {
        for signature in self.view.signatures() {
            self.check_signature(signature)?
        }
        for constant in self.view.constant_pool() {
            self.check_constant(constant)?
        }
        for script_handle in self.view.module_handles() {
            self.check_module_handle(script_handle)?
        }
        for struct_handle in self.view.struct_handles() {
            self.check_struct_handle(struct_handle)?
        }
        for function_handle in self.view.function_handles() {
            self.check_function_handle(function_handle)?
        }
        for field_handle in self.view.field_handles().into_iter().flatten() {
            self.check_field_handle(field_handle)?
        }
        for friend_decl in self.view.friend_decls().into_iter().flatten() {
            self.check_module_handle(friend_decl)?
        }
        for struct_instantiation in self.view.struct_instantiations().into_iter().flatten() {
            self.check_struct_instantiation(struct_instantiation)?
        }
        for function_instantiation in self.view.function_instantiations() {
            self.check_function_instantiation(function_instantiation)?
        }
        for field_instantiation in self.view.field_instantiations().into_iter().flatten() {
            self.check_field_instantiation(field_instantiation)?
        }
        for struct_def in self.view.struct_defs().into_iter().flatten() {
            self.check_struct_def(struct_def)?
        }
        let view = self.view;
        for (function_def_idx, function_def) in
            view.function_defs().into_iter().flatten().enumerate()
        {
            self.check_function_def(function_def_idx, function_def)?
        }
        self.check_self_module_handle()
    }

    fn check_module_handle(&self, module_handle: &ModuleHandle) -> PartialVMResult<()> {
        check_bounds_impl(self.view.address_identifiers(), module_handle.address)?;
        check_bounds_impl(self.view.identifiers(), module_handle.name)
    }

    fn check_self_module_handle(&self) -> PartialVMResult<()> {
        match self.view.self_handle_idx() {
            Some(idx) => check_bounds_impl(self.view.module_handles(), idx),
            None => Ok(()),
        }
    }

    fn check_struct_handle(&self, struct_handle: &StructHandle) -> PartialVMResult<()> {
        check_bounds_impl(self.view.module_handles(), struct_handle.module)?;
        check_bounds_impl(self.view.identifiers(), struct_handle.name)
    }<commit_msg>[language] Refactor verify_impl for MIRAI analysis<commit_after>// Copyright (c) The Diem Core Contributors
// SPDX-License-Identifier: Apache-2.0

use crate::{
    binary_views::BinaryIndexedView,
    errors::{
        bounds_error, offset_out_of_bounds as offset_out_of_bounds_error, verification_error,
        PartialVMError, PartialVMResult,
    },
    file_format::{
        AbilitySet, Bytecode, CodeOffset, CodeUnit, CompiledModule, CompiledScript, Constant,
        FieldHandle, FieldInstantiation, FunctionDefinition, FunctionDefinitionIndex,
        FunctionHandle, FunctionInstantiation, ModuleHandle, Signature, SignatureToken,
        StructDefInstantiation, StructDefinition, StructFieldInformation, StructHandle, TableIndex,
    },
    internals::ModuleIndex,
    IndexKind,
};
use move_core_types::vm_status::StatusCode;
use std::u8;

enum BoundsCheckingContext {
    Module,
    ModuleFunction(FunctionDefinitionIndex),
    Script,
}
pub struct BoundsChecker<'a> {
    view: BinaryIndexedView<'a>,
    context: BoundsCheckingContext,
}

impl<'a> BoundsChecker<'a> {
    pub fn verify_script(script: &'a CompiledScript) -> PartialVMResult<()> {
        let mut bounds_check = Self {
            view: BinaryIndexedView::Script(script),
            context: BoundsCheckingContext::Script,
        };
        bounds_check.verify_impl()?;

        let signatures = &script.signatures;
        let parameters = &script.parameters;
        match signatures.get(parameters.into_index()) {
            // The bounds checker has already checked each function definition's code, but a script's
            // code exists outside of any function definition. It gets checked here.
            Some(signature) => {
                bounds_check.check_code(&script.code, &script.type_parameters, signature)
            }
            None => Err(bounds_error(
                StatusCode::INDEX_OUT_OF_BOUNDS,
                IndexKind::Signature,
                parameters.into_index() as u16,
                signatures.len(),
            )),
        }
    }

    pub fn verify_module(module: &'a CompiledModule) -> PartialVMResult<()> {
        let mut bounds_check = Self {
            view: BinaryIndexedView::Module(module),
            context: BoundsCheckingContext::Module,
        };
        if bounds_check.view.module_handles().is_empty() {
            let status =
                verification_error(StatusCode::NO_MODULE_HANDLES, IndexKind::ModuleHandle, 0);
            return Err(status);
        }
        bounds_check.verify_impl()
    }

    fn verify_impl(&mut self) -> PartialVMResult<()> {
        self.check_signatures()?;
        self.check_constants()?;
        self.check_module_handles()?;
        self.check_self_module_handle()?;
        self.check_struct_handles()?;
        self.check_function_handles()?;
        self.check_field_handles()?;
        self.check_friend_decls()?;
        self.check_struct_instantiations()?;
        self.check_function_instantiations()?;
        self.check_field_instantiations()?;
        self.check_struct_defs()?;
        self.check_function_defs()
    }

    fn check_signatures(&self) -> PartialVMResult<()> {
        for signature in self.view.signatures() {
            self.check_signature(signature)?
        }
        Ok(())
    }

    fn check_constants(&self) -> PartialVMResult<()> {
        for constant in self.view.constant_pool() {
            self.check_constant(constant)?
        }
        Ok(())
    }

    fn check_module_handles(&self) -> PartialVMResult<()> {
        for script_handle in self.view.module_handles() {
            self.check_module_handle(script_handle)?
        }
        Ok(())
    }

    fn check_struct_handles(&self) -> PartialVMResult<()> {
        for struct_handle in self.view.struct_handles() {
            self.check_struct_handle(struct_handle)?
        }
        Ok(())
    }

    fn check_function_handles(&self) -> PartialVMResult<()> {
        for function_handle in self.view.function_handles() {
            self.check_function_handle(function_handle)?
        }
        Ok(())
    }

    fn check_field_handles(&self) -> PartialVMResult<()> {
        for field_handle in self.view.field_handles().into_iter().flatten() {
            self.check_field_handle(field_handle)?
        }
        Ok(())
    }

    fn check_friend_decls(&self) -> PartialVMResult<()> {
        for friend_decl in self.view.friend_decls().into_iter().flatten() {
            self.check_module_handle(friend_decl)?
        }
        Ok(())
    }

    fn check_struct_instantiations(&self) -> PartialVMResult<()> {
        for struct_instantiation in self.view.struct_instantiations().into_iter().flatten() {
            self.check_struct_instantiation(struct_instantiation)?
        }
        Ok(())
    }

    fn check_function_instantiations(&self) -> PartialVMResult<()> {
        for function_instantiation in self.view.function_instantiations() {
            self.check_function_instantiation(function_instantiation)?
        }
        Ok(())
    }

    fn check_field_instantiations(&self) -> PartialVMResult<()> {
        for field_instantiation in self.view.field_instantiations().into_iter().flatten() {
            self.check_field_instantiation(field_instantiation)?
        }
        Ok(())
    }

    fn check_struct_defs(&self) -> PartialVMResult<()> {
        for struct_def in self.view.struct_defs().into_iter().flatten() {
            self.check_struct_def(struct_def)?
        }
        Ok(())
    }

    fn check_function_defs(&mut self) -> PartialVMResult<()> {
        let view = self.view;
        for (function_def_idx, function_def) in
            view.function_defs().into_iter().flatten().enumerate()
        {
            self.check_function_def(function_def_idx, function_def)?
        }
        Ok(())
    }

    fn check_module_handle(&self, module_handle: &ModuleHandle) -> PartialVMResult<()> {
        check_bounds_impl(self.view.address_identifiers(), module_handle.address)?;
        check_bounds_impl(self.view.identifiers(), module_handle.name)
    }

    fn check_self_module_handle(&self) -> PartialVMResult<()> {
        match self.view.self_handle_idx() {
            Some(idx) => check_bounds_impl(self.view.module_handles(), idx),
            None => Ok(()),
        }
    }

    fn check_struct_handle(&self, struct_handle: &StructHandle) -> PartialVMResult<()> {
        check_bounds_impl(self.view.module_handles(), struct_handle.module)?;
        check_bounds_impl(self.view.identifiers(), struct_handle.name)
    }
{'fraction_non_alphanumeric': 0.1102606003268255, 'fraction_numerical': 0.0010320805022791778, 'mean_word_length': 2.716203259827421, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 67, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1683446', 'n_tokens_mistral': 3354, 'n_tokens_neox': 3307, 'n_words': 552}