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 Determinism Bug: Avoid Reducing Indices to Reserved Values #2850

Merged

Conversation

felixhandte
Copy link
Contributor

Previously, if an index was equal to reducerValue + 1, it would get remapped
during index reduction to 1 i.e. ZSTD_DUBT_UNSORTED_MARK. This can affect
the parsing of the input slightly, by causing tree nodes to be nullified when
they otherwise wouldn't be. This hardly matters from a correctness or
efficiency perspective, but it does impact determinism.

So this Pull Request changes index reduction to avoid mapping indices to
collide with ZSTD_DUBT_UNSORTED_MARK.

I am somewhat concerned that ZSTD_reduceTable_internal() will be slower
now. I'm not sure how important the speed of this is in practive, but it
looks like it's written to be auto-vectorized, and I'm not sure the new
version is similarly vectorizable.

Credit to OSS-Fuzz for discovery.

Previously, if an index was equal to `reducerValue + 1`, it would get remapped
during index reduction to 1 i.e. `ZSTD_DUBT_UNSORTED_MARK`. This can affect the
parsing of the input slightly, by causing tree nodes to be nullified when they
otherwise wouldn't be. This hardly matters from a correctness or efficiency
perspective, but it does impact determinism.

So this commit changes index reduction to avoid mapping indices to collide with
`ZSTD_DUBT_UNSORTED_MARK`.
@Cyan4973
Copy link
Contributor

Cyan4973 commented Nov 9, 2021

Indeed, if I do remember correctly, the previous version was designed to be auto-vectorized, the outcome of which was observed using godbolt.

That being said, the index reducer is hardly a performance bottleneck, so even if this part takes a little bit longer, it shouldn't be materially sensible in any scenario.

@felixhandte
Copy link
Contributor Author

I've put up a new version that does a superfluous write, but that helps the compiler vectorize the loop (https://godbolt.org/z/Kq4oc139n).

@felixhandte felixhandte changed the title Avoid Reducing Indices to Reserved Values Fix Determinism Bug: Avoid Reducing Indices to Reserved Values Nov 9, 2021
@felixhandte felixhandte merged commit a071e00 into facebook:dev Nov 10, 2021
felixhandte added a commit to felixhandte/zstd that referenced this pull request Nov 17, 2021
PR facebook#2850 attempted to fix a determinism bug that was uncovered by OSS-Fuzz. It
succeeded in addressing that source of non-determinism, but introduced a new
one: it was possible, when index reduction occurred, to map indices in the
window to the reserved value, which would cause them to be zeroed, potentially
altering parsing of the input.

This PR addresses this issue. It makes sure that the bottom of the window is
always `>= ZSTD_WINDOW_START_INDEX`.

I'm not sure if this makes facebook#2850 redundant. I think it's probably still
valuable to have that protection as well.

Credit to OSS-Fuzz for discovering this issue.
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.

3 participants