stan-dev / stan-dev/rstan

Misaligned address sanitizer errors with `csr_matrix_times_vector()` - leading to CRAN package failing additional tests

Open
#1,111 25 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
R
Stars
1.1k
Forks
266
Avg merge
2h 56m
Merged PRs (30d)
1

Description

Summary:

Sparse matrix arithmetic using csr_matrix_times_vector() seems to trigger sanitizer "misaligned address" errors.

Description:

My package multinma that fits models using rstan has been flagged as failing additional tests in gcc-UBSAN. Closer inspection seems to point to csr_matrix_times_vector() as the cause.

I have also opened a thread on discourse.

Reproducible Steps:

This small model recreates the sanitizer errors:

library(rstan)

mod <- "
data {
  int n;
  matrix[n, n] M;
}
transformed data {
  int nz = num_elements(csr_extract_w(M));
  vector[nz] Mw = csr_extract_w(M);
  array[nz] int Mv = csr_extract_v(M);
  array[n + 1] int Mu = csr_extract_u(M);
}
parameters {
  vector[n] x;
}
transformed parameters {
  vector[n] y = csr_matrix_times_vector(n, n, Mw, Mv, Mu, x);
}
model {
  x ~ std_normal();
}
"

stan(model_code = mod,
     data = list(
       n = 2,
       M = matrix(c(1, 0.5, 0.5, 1), nrow = 2, ncol = 2)
     ),
     test_grad = TRUE)

Commenting out the transformed parameter removes the sanitizer warnings, so this seems to be a problem with csr_matrix_times_vector().

Current Output:
Details

TESTING GRADIENT FOR MODEL 'anon_model' NOW (CHAIN 1).
/usr/local/lib/R/site-library/RcppEigen/include/Eigen/src/Core/CoreEvaluators.h:852:18: runtime error: reference binding to misaligned address 0x5556fef3455c for type 'struct Scalar', which requires 8 byte alignment
0x5556fef3455c: note: pointer points here
  04 00 00 00 00 00 00 00  b0 1b a4 f9 56 55 00 00  20 4b 9e f9 56 55 00 00  b8 da 9f f9 56 55 00 00
              ^
/usr/local/lib/R/site-library/RcppEigen/include/Eigen/src/Core/AssignEvaluator.h:637:26: runtime error: reference binding to misaligned address 0x5556fef3455c for type 'struct var_value', which requires 8 byte alignment
0x5556fef3455c: note: pointer points here
  04 00 00 00 00 00 00 00  b0 1b a4 f9 56 55 00 00  20 4b 9e f9 56 55 00 00  b8 da 9f f9 56 55 00 00
              ^
/usr/local/lib/R/site-library/RcppEigen/include/Eigen/src/Core/functors/AssignmentFunctors.h:24:100: runtime error: store to misaligned address 0x5556fef3455c for type 'struct var_value', which requires 8 byte alignment
0x5556fef3455c: note: pointer points here
  04 00 00 00 00 00 00 00  b0 1b a4 f9 56 55 00 00  20 4b 9e f9 56 55 00 00  b8 da 9f f9 56 55 00 00
              ^
/usr/local/lib/R/site-library/RcppEigen/include/Eigen/src/Core/CoreEvaluators.h:840:18: runtime error: reference binding to misaligned address 0x5556fef3455c for type 'const struct var_value', which requires 8 byte alignment
0x5556fef3455c: note: pointer points here
  04 00 00 00 d0 40 f3 fe  56 55 00 00 e8 40 f3 fe  56 55 00 00 56 55 00 00  b8 da 9f f9 56 55 00 00
              ^
/usr/local/lib/R/site-library/RcppEigen/include/Eigen/src/Core/CoreEvaluators.h:549:21: runtime error: reference binding to misaligned address 0x5556fef3455c for type 'const struct var_value', which requires 8 byte alignment
0x5556fef3455c: note: pointer points here
  04 00 00 00 d0 40 f3 fe  56 55 00 00 e8 40 f3 fe  56 55 00 00 56 55 00 00  b8 da 9f f9 56 55 00 00
              ^
/usr/local/lib/R/site-library/StanHeaders/include/stan/math/rev/fun/value_of.hpp:25: runtime error: member access within misaligned address 0x5556fef3455c for type 'const struct var_value', which requires 8 byte alignment
0x5556fef3455c: note: pointer points here
  04 00 00 00 d0 40 f3 fe  56 55 00 00 e8 40 f3 fe  56 55 00 00 56 55 00 00  b8 da 9f f9 56 55 00 00
              ^
/usr/local/lib/R/site-library/StanHeaders/include/stan/math/rev/fun/value_of.hpp:25: runtime error: member access within misaligned address 0x5556fef3455c for type 'const struct var_value', which requires 8 byte alignment
0x5556fef3455c: note: pointer points here
  04 00 00 00 d0 40 f3 fe  56 55 00 00 e8 40 f3 fe  56 55 00 00 56 55 00 00  b8 da 9f f9 56 55 00 00
              ^
/usr/local/lib/R/site-library/RcppEigen/include/Eigen/src/Core/CoreEvaluators.h:852:18: runtime error: reference binding to misaligned address 0x5556fef3457c for type 'struct Scalar', which requires 8 byte alignment
0x5556fef3457c: note: pointer points here
  60 bd 9d f9 56 55 00 00  70 c0 9d f9 56 55 00 00  70 c0 9d f9 56 55 00 00  70 c0 9d f9 56 55 00 00
              ^
/usr/local/lib/R/site-library/RcppEigen/include/Eigen/src/Core/AssignEvaluator.h:637:26: runtime error: reference binding to misaligned address 0x5556fef3457c for type 'struct var_value', which requires 8 byte alignment
0x5556fef3457c: note: pointer points here
  60 bd 9d f9 56 55 00 00  70 c0 9d f9 56 55 00 00  70 c0 9d f9 56 55 00 00  70 c0 9d f9 56 55 00 00
              ^
/usr/local/lib/R/site-library/StanHeaders/include/stan/math/rev/core/var.hpp:82: runtime error: constructor call on misaligned address 0x5556fef3458c for type 'struct vari_type', which requires 8 byte alignment
0x5556fef3458c: note: pointer points here
  70 c0 9d f9 56 55 00 00  70 c0 9d f9 56 55 00 00  70 c0 9d f9 56 55 00 00  70 c0 9d f9 56 55 00 00
              ^
/usr/local/lib/R/site-library/StanHeaders/include/stan/math/rev/core/vari.hpp:126: runtime error: member access within misaligned address 0x5556fef3458c for type 'struct vari_value', which requires 8 byte alignment
0x5556fef3458c: note: pointer points here
  70 c0 9d f9 56 55 00 00  70 c0 9d f9 56 55 00 00  70 c0 9d f9 56 55 00 00  70 c0 9d f9 56 55 00 00
              ^
/usr/local/lib/R/site-library/StanHeaders/include/stan/math/rev/core/vari.hpp:126: runtime error: member access within misaligned address 0x5556fef3458c for type 'struct vari_value', which requires 8 byte alignment
0x5556fef3458c: note: pointer points here
  70 c0 9d f9 00 00 00 00  00 00 00 00 56 55 00 00  70 c0 9d f9 56 55 00 00  70 c0 9d f9 56 55 00 00
              ^
/usr/local/lib/R/site-library/StanHeaders/include/stan/math/rev/core/vari.hpp:126: runtime error: constructor call on misaligned address 0x5556fef3458c for type 'struct vari_base', which requires 8 byte alignment
0x5556fef3458c: note: pointer points here
  70 c0 9d f9 00 00 00 00  00 00 00 00 56 55 00 00  70 c0 9d f9 56 55 00 00  70 c0 9d f9 56 55 00 00
              ^
/usr/local/lib/R/site-library/StanHeaders/include/stan/math/rev/core/vari.hpp:28: runtime error: member access within misaligned address 0x5556fef3458c for type 'struct vari_base', which requires 8 byte alignment
0x5556fef3458c: note: pointer points here
  70 c0 9d f9 00 00 00 00  00 00 00 00 56 55 00 00  70 c0 9d f9 56 55 00 00  70 c0 9d f9 56 55 00 00
              ^
/usr/local/lib/R/site-library/StanHeaders/include/stan/math/rev/core/vari.hpp:126: runtime error: member access within misaligned address 0x5556fef3458c for type 'struct vari_value', which requires 8 byte alignment
0x5556fef3458c: note: pointer points here
  70 c0 9d f9 b0 3f 14 7c  5d 7f 00 00 56 55 00 00  70 c0 9d f9 56 55 00 00  70 c0 9d f9 56 55 00 00
              ^
/usr/local/lib/R/site-library/StanHeaders/include/stan/math/rev/core/vari.hpp:126: runtime error: member access within misaligned address 0x5556fef3458c for type 'struct vari_value', which requires 8 byte alignment
0x5556fef3458c: note: pointer points here
  70 c0 9d f9 d0 3f 14 7c  5d 7f 00 00 56 55 00 00  70 c0 9d f9 56 55 00 00  70 c0 9d f9 56 55 00 00
              ^
/usr/local/lib/R/site-library/StanHeaders/include/stan/math/rev/core/vari.hpp:126: runtime error: member access within misaligned address 0x5556fef3458c for type 'struct vari_value', which requires 8 byte alignment
0x5556fef3458c: note: pointer points here
  70 c0 9d f9 d0 3f 14 7c  5d 7f 00 00 27 1c f4 c8  b8 b8 ea 3f 56 55 00 00  70 c0 9d f9 56 55 00 00
              ^
/usr/local/lib/R/site-library/StanHeaders/include/stan/math/rev/core/vari.hpp:130: runtime error: member access within misaligned address 0x5556fef3458c for type 'struct vari_value', which requires 8 byte alignment
0x5556fef3458c: note: pointer points here
  70 c0 9d f9 d0 3f 14 7c  5d 7f 00 00 27 1c f4 c8  b8 b8 ea 3f 00 00 00 00  00 00 00 00 56 55 00 00
              ^
/usr/local/lib/R/site-library/RcppEigen/include/Eigen/src/Core/functors/AssignmentFunctors.h:24:100: runtime error: store to misaligned address 0x5556fef3457c for type 'struct var_value', which requires 8 byte alignment
0x5556fef3457c: note: pointer points here
  60 bd 9d f9 56 55 00 00  70 c0 9d f9 56 55 00 00  70 c0 9d f9 d0 3f 14 7c  5d 7f 00 00 27 1c f4 c8
              ^
/usr/local/lib/R/site-library/RcppEigen/include/Eigen/src/Core/CoreEvaluators.h:840:18: runtime error: reference binding to misaligned address 0x5556fef3457c for type 'const struct var_value', which requires 8 byte alignment
0x5556fef3457c: note: pointer points here
  60 bd 9d f9 8c 45 f3 fe  56 55 00 00 a4 45 f3 fe  56 55 00 00 d0 3f 14 7c  5d 7f 00 00 27 1c f4 c8
              ^
/usr/local/lib/R/site-library/RcppEigen/include/Eigen/src/Core/AssignEvaluator.h:637:26: runtime error: reference binding to misaligned address 0x5556fef3457c for type 'const struct var_value', which requires 8 byte alignment
0x5556fef3457c: note: pointer points here
  60 bd 9d f9 8c 45 f3 fe  56 55 00 00 a4 45 f3 fe  56 55 00 00 d0 3f 14 7c  5d 7f 00 00 27 1c f4 c8
              ^
/usr/local/lib/R/site-library/RcppEigen/include/Eigen/src/Core/functors/AssignmentFunctors.h:24:100: runtime error: load of misaligned address 0x5556fef3457c for type 'const struct var_value', which requires 8 byte alignment
0x5556fef3457c: note: pointer points here
  60 bd 9d f9 8c 45 f3 fe  56 55 00 00 a4 45 f3 fe  56 55 00 00 d0 3f 14 7c  5d 7f 00 00 27 1c f4 c8
              ^
/usr/local/lib/R/site-library/RcppEigen/include/Eigen/src/Core/CoreEvaluators.h:834:18: runtime error: reference binding to misaligned address 0x5556fef3457c for type 'const struct var_value', which requires 8 byte alignment
0x5556fef3457c: note: pointer points here
  60 bd 9d f9 8c 45 f3 fe  56 55 00 00 a4 45 f3 fe  56 55 00 00 d0 3f 14 7c  5d 7f 00 00 27 1c f4 c8
              ^
/usr/local/lib/R/site-library/RcppEigen/include/Eigen/src/Core/CoreEvaluators.h:774:21: runtime error: reference binding to misaligned address 0x5556fef3457c for type 'const struct var_value', which requires 8 byte alignment
0x5556fef3457c: note: pointer points here
  60 bd 9d f9 8c 45 f3 fe  56 55 00 00 a4 45 f3 fe  56 55 00 00 d0 3f 14 7c  5d 7f 00 00 27 1c f4 c8
              ^
/usr/local/lib/R/site-library/StanHeaders/include/stan/math/prim/eigen_plugins.h:160:41: runtime error: member access within misaligned address 0x5556fef3457c for type 'const struct var_value', which requires 8 byte alignment
0x5556fef3457c: note: pointer points here
  60 bd 9d f9 8c 45 f3 fe  56 55 00 00 a4 45 f3 fe  56 55 00 00 d0 3f 14 7c  5d 7f 00 00 27 1c f4 c8
              ^
/usr/local/lib/R/site-library/StanHeaders/include/stan/math/prim/eigen_plugins.h:160:46: runtime error: member access within misaligned address 0x5556fef3458c for type 'struct vari_type', which requires 8 byte alignment
0x5556fef3458c: note: pointer points here
  56 55 00 00 d0 3f 14 7c  5d 7f 00 00 27 1c f4 c8  b8 b8 ea 3f 00 00 00 00  00 00 00 00 d0 3f 14 7c
              ^
/usr/local/lib/R/site-library/StanHeaders/include/stan/math/prim/eigen_plugins.h:160:46: runtime error: reference binding to misaligned address 0x5556fef3459c for type 'const double', which requires 8 byte alignment
0x5556fef3459c: note: pointer points here
  b8 b8 ea 3f 00 00 00 00  00 00 00 00 d0 3f 14 7c  5d 7f 00 00 40 bf c4 63  46 6c 9d bf 00 00 00 00
              ^
/usr/local/lib/R/site-library/RcppEigen/include/Eigen/src/Core/CoreEvaluators.h:774:47: runtime error: reference binding to misaligned address 0x5556fef3459c for type 'const double', which requires 8 byte alignment
0x5556fef3459c: note: pointer points here
  b8 b8 ea 3f 00 00 00 00  00 00 00 00 d0 3f 14 7c  5d 7f 00 00 40 bf c4 63  46 6c 9d bf 00 00 00 00
              ^
/usr/local/lib/R/site-library/RcppEigen/include/Eigen/src/Core/DenseCoeffsBase.h:100:67: runtime error: reference binding to misaligned address 0x5556fef3459c for type 'const double', which requires 8 byte alignment
0x5556fef3459c: note: pointer points here
              ^
/usr/local/lib/R/site-library/RcppEigen/include/Eigen/src/SparseCore/SparseDenseProduct.h:100:96: runtime error: load of misaligned address 0x5556fef3459c for type 'const double', which requires 8 byte alignment
0x5556fef3459c: note: pointer points here
  b8 b8 ea 3f 00 00 00 00  00 00 00 00 d0 3f 14 7c  5d 7f 00 00 40 bf c4 63  46 6c 9d bf 00 00 00 00
              ^
/usr/local/lib/R/site-library/RcppEigen/include/Eigen/src/Core/CoreEvaluators.h:852:18: runtime error: reference binding to misaligned address 0x5556fef3455c for type 'struct Scalar', which requires 8 byte alignment
0x5556fef3455c: note: pointer points here
  04 00 00 00 d0 40 f3 fe  56 55 00 00 e8 40 f3 fe  56 55 00 00 56 55 00 00  b8 da 9f f9 56 55 00 00
              ^
/usr/local/lib/R/site-library/RcppEigen/include/Eigen/src/Core/CoreEvaluators.h:792:21: runtime error: reference binding to misaligned address 0x5556fef3455c for type 'struct var_value', which requires 8 byte alignment
0x5556fef3455c: note: pointer points here
  04 00 00 00 d0 40 f3 fe  56 55 00 00 e8 40 f3 fe  56 55 00 00 56 55 00 00  b8 da 9f f9 56 55 00 00
              ^
/usr/local/lib/R/site-library/StanHeaders/include/stan/math/prim/eigen_plugins.h:160:41: runtime error: member access within misaligned address 0x5556fef3455c for type 'struct var_value', which requires 8 byte alignment
0x5556fef3455c: note: pointer points here
  04 00 00 00 d0 40 f3 fe  56 55 00 00 e8 40 f3 fe  56 55 00 00 56 55 00 00  b8 da 9f f9 56 55 00 00
              ^

TESTING GRADIENT FOR MODEL 'anon_model' NOW (CHAIN 2).

TESTING GRADIENT FOR MODEL 'anon_model' NOW (CHAIN 3).

TESTING GRADIENT FOR MODEL 'anon_model' NOW (CHAIN 4).

RStan Version:
> packageVersion("rstan")
[1] ‘2.32.5’
R Version:

Issue recreated using the rocker/r-devel-san docker container, which builds r-devel with compiler sanitizers.

> R.version.string
[1] "R Under development (unstable) (2024-01-22 r85820)"
Operating System:

Linux run in rocker/r-devel-san docker container. Issue first appeared on CRAN gcc-UBSAN test environment (also a flavour of linux).

Contributor guide

No contributing guide indexed for this repository

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 the supplied Stan model and the csr_matrix_times_vector() entry point, then reproduce the issue under gcc-UBSAN and inspect the reported Eigen and StanHeaders locations. Done means the model no longer produces misaligned-address sanitizer warnings while retaining the existing sparse matrix behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
r
Domain
backend, testing-qa
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.