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 access to unitialized memory in RNG ops #5394

Closed
wants to merge 1 commit into from

Conversation

klecki
Copy link
Contributor

@klecki klecki commented Mar 25, 2024

Category: Bug fix

Description:

Random operators used vector of reinterpreted bytes as a target of assignment when creating distribution objects.
The random choice uses non-trivial std::discrete_distribution carrying some state. Assigning to that memory caused a leak.
Switch to a proper vector<distribution>, so the copy/move assignments are not UB as the location that we assign to is now
properly constructed ahead of time.

Additional information:

Affected modules and functionalities:

RNG operators

Key points relevant for the review:

Tests:

  • Existing tests apply
    The relevant fn.random.choice tests no longer report leak under ASAN.
  • New tests added
    • Python tests
    • GTests
    • Benchmark
    • Other
  • N/A

Checklist

Documentation

  • Existing documentation applies
  • Documentation updated
    • Docstring
    • Doxygen
    • RST
    • Jupyter
    • Other
  • N/A

DALI team only

Requirements

  • Implements new requirements
  • Affects existing requirements
  • N/A

REQ IDs: N/A

JIRA TASK: N/A

Operator used vector of reintereted bytes with assignment of
std::distribution object carrying some state.
Switch to a vector so the copy and move are not UB.

Signed-off-by: Krzysztof Lecki <[email protected]>
@dali-automaton
Copy link
Collaborator

CI MESSAGE: [13784528]: BUILD STARTED

@dali-automaton
Copy link
Collaborator

CI MESSAGE: [13784528]: BUILD FAILED

@klecki klecki marked this pull request as ready for review March 26, 2024 10:21
@JanuszL JanuszL self-assigned this Mar 26, 2024
dists_cpu.resize(sizeof(Dist) * nsamples); // memory was already reserved in the constructor
Dist* dists = reinterpret_cast<Dist*>(dists_cpu.data());
bool use_default_dist = !This().template SetupDists<T>(dists, ws, nsamples);
std::vector<Dist> dists(nsamples);
Copy link
Contributor

@mzient mzient Mar 26, 2024

Choose a reason for hiding this comment

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

This is captured by value in task lambdas (see line 162 and 180).

Copy link
Contributor

@mzient mzient left a comment

Choose a reason for hiding this comment

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

Please capture the distribution vector by reference or return to the use of a pointer.

@klecki
Copy link
Contributor Author

klecki commented Mar 26, 2024

Same fix with less allocations reimplemented in #5395

@klecki klecki closed this Mar 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants