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 22838 - BitArray.count reads beyond data #8401

Merged
merged 1 commit into from
Mar 3, 2022
Merged

Conversation

JohanEngelen
Copy link
Contributor

This fixes issue 22838
https://issues.dlang.org/show_bug.cgi?id=22838

All hail AddressSanitizer! ;-)

@JohanEngelen JohanEngelen requested a review from andralex as a code owner March 3, 2022 00:00
@dlang-bot
Copy link
Contributor

Thanks for your pull request and interest in making D better, @JohanEngelen! We are looking forward to reviewing it, and you should be hearing from a maintainer soon.
Please verify that your PR follows this checklist:

  • My PR is fully covered with tests (you can see the coverage diff by visiting the details link of the codecov check)
  • My PR is as minimal as possible (smaller, focused PRs are easier to review than big ones)
  • I have provided a detailed rationale explaining my changes
  • New or modified functions have Ddoc comments (with Params: and Returns:)

Please see CONTRIBUTING.md for more information.


If you have addressed all reviews or aren't sure how to proceed, don't hesitate to ping us with a simple comment.

Bugzilla references

Auto-close Bugzilla Severity Description
22838 critical std.bitmanip.BitArray.count() reads beyond data when data size is integer size_t multiple

Testing this PR locally

If you don't have a local development environment setup, you can use Digger to test this PR:

dub run digger -- build "stable + phobos#8401"

@thewilsonator
Copy link
Contributor

Please include the test case

@JohanEngelen
Copy link
Contributor Author

Please include the test case

The test case is already there :) The bug is triggered by a unittest (see bug report). The invalid memory access is of course only found when enabling ASan.

@CyberShadow
Copy link
Member

The test is already there. The failure is visible only when running with AddressSanitizer, which we don't do as part of our unit tests, so there is nothing to add.

Is it possible to write a unit test which purposefully clobbers respective memory so that the failure is visible outside AddressSanitizer, though?

Copy link
Member

@CyberShadow CyberShadow left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that was the only case where _ptr[fullWords] was accessed before checking that endBits is non-zero.

@CyberShadow
Copy link
Member

Reopening the PR with the same branch as another confused the CI I think.

@JohanEngelen
Copy link
Contributor Author

JohanEngelen commented Mar 3, 2022

Is it possible to write a unit test which purposefully clobbers respective memory so that the failure is visible outside AddressSanitizer, though?

No. It is an invalid read, but the calculation of bit count still works, because of the masking with endMask.

Edit:

{
    size_t[3] buffer;
    BitArray b = BitArray(buffer[], 2 * size_t.sizeof * 8);

    b[] = true;
    b[0 .. 1] = true;
    buffer[2] = ~size_t(0); // these bits will be read, but masked by endMask
    b.flip(1);
    assert(b.count() == 2 * size_t.sizeof * 8 - 1); // works
}

@maxhaton
Copy link
Member

maxhaton commented Mar 3, 2022

All hail AddressSanitizer!

I started implementing it in dmd during the Christmas holiday but then I realized it was supposed to be a holiday and stopped.

@JohanEngelen
Copy link
Contributor Author

CI errors are not due to this PR

@kinke kinke merged commit 90bd2d3 into stable Mar 3, 2022
@kinke kinke deleted the JohanEngelen-patch-1 branch March 3, 2022 15:45
@kinke
Copy link
Contributor

kinke commented Mar 3, 2022

[AFAIK, CI doesn't like direct branches on the upstream repo. That's mainly intended for bigger features targeting master, e.g., where druntime and compiler both need to be patched, and where one can use same-named branches for both original repos.]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants