Skip to content

Optimize miri engine performance for sanity checks of arrays #53845

Closed
@oli-obk

Description

@oli-obk

The knowledge about all the elements in an array can be used to run a single check instead of running the checks on each element one at a time (suggested in #53671 (comment)).

This is currently only done for strings, but could just as well be done for arrays of various types with Scalar layout.

The E-easy part of this issue is to check for arrays/slices of the builtin integer types and simply verify that there are no relocations in the entire array and that the entire array has no undefined bytes. Should be possible by simply calling

pub fn read_bytes(&self, ptr: Scalar, size: Size) -> EvalResult<'tcx, &[u8]> {
and checking whether it returned Ok. No need to actually check the value.

The E-medium part is to refactor

fn validate_scalar(
to take a bunch of closures which do the final checks. Instead of doing the check for a single memory location, run it for the entire array at once. This means the function won't have
value: ScalarMaybeUndef,
as an argument anymore, but instead the closures pass around some generic value. E.g
let value = match value {
ScalarMaybeUndef::Scalar(scalar) => scalar,
ScalarMaybeUndef::Undef => return validation_failure!("undefined bytes", path),
};
would be replaced by a closure call, and its result would be fed into the next closure e.g. in
let in_range = |bound: RangeInclusive<u128>| bound.contains(&bits);
. Note that #53826 should be done before this, otherwise you'll go crazy while attempting to fiddle this refactoring into the current code

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-const-evalArea: Constant evaluation, covers all const contexts (static, const fn, ...)E-easyCall for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.E-mediumCall for participation: Medium difficulty. Experience needed to fix: Intermediate.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions