-
Notifications
You must be signed in to change notification settings - Fork 248
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
analyze: memcpy rewrite improvements (#1176)
This branch has several improvements to the rewriting of `memcpy` and `memset`: * Support `memcpy` on `Quantity::Single` references (`&T`/`&mut T`, as opposed to `&[T]`/`&mut [T]`). The reference is automatically converted to a 1-element slice. The `copy_from_slice` call will panic with an out-of-bounds error if the number of elements to copy is not 0 or 1. * Support `memcpy` on `Option` references. If the source or destination pointer is `None`, then this will panic if the element count is nonzero, and otherwise will do nothing. * Use `mem::size_of::<T>()` instead of the original size of `T` for converting `memcpy`/`memset` byte counts to element counts. The `size_of` change is a bit subtle. In C, if `sizeof(struct foo) == 16`, it's legal to `memcpy` an array of `struct foo` using `n * 16` as the byte length (or `n * SIZE_OF_FOO`, where `SIZE_OF_FOO` is `#define`'d to be 16) instead of `n * sizeof(struct foo)`. This presents a problem for us because `c2rust-analyze` may change the size of `foo` when rewriting its pointer fields. After rewriting, the `n * 16` version computes a byte length based on the original size for `struct foo`, while the `n * sizeof(struct foo)` computes it using the rewritten size. For converting the byte length to an element count, we previously used the original size, which works for the `n * 16` version but not for `n * sizeof(struct foo)`. This branch changes the element count calculation to divide by the rewritten size instead, which works for `n * sizeof(struct foo)` but not for `n * 16`. The hope is that `n * sizeof(struct foo)` is more common, since it's usually preferred in C for portability reasons.
- Loading branch information
Showing
6 changed files
with
209 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.