stan-dev / stan-dev/stan

Potential issues with slicing

Open
#2,904 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C++
Stars
2.8k
Forks
388
Avg merge
2d 17h
Merged PRs (30d)
15

Description

Summary:

CRAN identified with their clang sanitiser tests (see here: https://cran.csiro.au/doc/manuals/r-patched/R-exts.html#Using-Address-Sanitizer) issues in one of my Stan models on CRAN.

When tracing down the log file it appears that statements in the Stan language which use slicing of bigger data-structures are guilty of provoking use of uninitialised memory (and NULL pointers being used where they should not).

Description:

Spoiler alert: I was never myself able to reproduce the problems! Anyway, here are the respective files:

After guessing a bit, I did replace these lines (line 838 of the Stan model); the lines which change is what you should pay attention to as changing these fixed the problem:

      vector[num_mix_comp] mix_lpmf =
          blrm_mix_lpmf_comp(// subset data
              r[obs_gidx],   // <-- this line changes
              n[obs_gidx],   // <-- this line changes
              X_comp[:,obs_gidx],   // <-- this line changes
              finite_cov[:,obs_gidx],  // <-- this line changes
              X_inter[obs_gidx],
              // select EX+NEX of this group
              beta[{g,g+num_groups}], mix_idx_beta, // <-- this line changes
              eta[{g,g+num_groups}], mix_idx_eta).    // <-- this line changes
          // prior weight for each component
          + mix_log_weight[g];

with this, which CRAN now has accepted (line 852):

      vector[num_mix_comp] mix_lpmf =
          blrm_mix_lpmf_comp(// subset data
              g, num_groups,
              obs_gidx,
              r,
              n,
              X_comp,
              finite_cov,
              X_inter,
              // select EX+NEX of this group
              beta, mix_idx_beta,
              eta, mix_idx_eta)
          // prior weight for each component
          + mix_log_weight[g];

So I removed all of the fancier subsetting things from the entire model.

Reproducible Steps:

I was not able to reproduce the findings from CRAN using a CmdStan 2.19 installation. Another route I tried to reproduce this was to use the dedicated Docker images (https://www.rocker-project.org/images/ - the image r-devel-ubsan-clang) which did not help either since it kept crashing way before I was able to run the code in question.

Current Output:

What is interesting to note is that I relatively confident that the 0.6-0 model did compute the right thing as I have a very large SBC battery of tests working on this.

Expected Output:

I would love to change back the Stan model to use the much easier to read subsetting things.

Additional Information:

NA

Current Version:

This affects my R package OncoBayes2 which is build against RStan 2.19.3 / StanHeaders 2.19.2 (this is cmdstan 2.19.1)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with testthat_Rout_fail.txt, the blrm_exnex-0-6-0_stan.txt model, and combo3_data_R.txt, then compare its slicing expressions with blrm_exnex-0-6-3_stan.txt. Run the model under the cited Clang sanitizer setup and investigate whether the reported uninitialized-memory or NULL-pointer behavior can be reproduced; done means the slicing version runs without those sanitizer findings.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
compilers
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.