Skip to content

offset intrinsic doesn't check that pointers stay within bounds #1233

Closed

Description

@adpaco

The documentation for the offset method mentions in the safety conditions that:

Both the starting and resulting pointer must be either in bounds or one byte past the end of the same allocated object.

At present, the standard library prevents us from adding this check because otherwise we run into spurious failures (i.e., offsets that go out-of-bounds, are frequently used).

Activity

  1. self-assigned this
    on May 27, 2022
  2. removed
    [F] Soundness
    on Jul 14, 2022
  3. adpaco commented on Jul 14, 2022

    @adpaco
    ContributorAuthor

    Removing the soundness label since this is not strictly a soundness issue.

    Adding this check would introduce many failures that aren't part of the user code, because offset operations often go out-of-bounds (in the standard library and other projects), but that's not an issue unless the pointer gets used. Let me illustrate with an example in C:

    int *p = malloc(10);
    p = p + 50; // pointer has been offset 50 units (out of bounds) - not an error since the pointer is not used
    int x = *p // this is an error, the index points to somewhere out of the bounds for the object

    Rust allows manipulations as in line (2), so there's no soundness concerns about that. On the other hand, de-referencing the pointer in line (3) should raise an error, which Kani does.

  4. giltho commented on Sep 12, 2022

    @giltho
    Contributor

    i.e., offsets that go out-of-bounds, are frequently used

    Do you have an example of that? The stdlib is tested with MIRI I believe, and MIRI detects creating OOB pointers as UB.

    de-referencing the pointer in line (3) should raise an error, which Kani does

    Not always, see #1665

  5. added
    [C] Bug
    [F] Spurious Failure
    on Nov 14, 2022
  6. added
    [F] Soundness
    and removed
    [F] Spurious Failure
    on Nov 5, 2024
  7. celinval commented on Nov 6, 2024

    @celinval
    Contributor

    I think we need to revisit these cases. This is a potential soundness issue since Kani would not be able to detect a pointer going out of bounds. From what I can recall, the spurious CEX would only happen for cases where the offset started from a dangling pointer and the count was 0, which is what should handle. Note that the offset documentation has been updated:

    If the computed offset is non-zero, then self must be derived from a pointer to some allocated object (...)

  8. added a commit that references this issue on Dec 11, 2024
    2334f2c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

[C] Bug[F] Soundness

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions