Skip to content

Undefined behavior in combination of apply_scalar_binary/make_holder/to_ref_if #3208

Open
@WardBrian

Description

@WardBrian

This issue is to collect information from #3203 in one place.

Basic summary:

On GCC, since c71dd3e (#2642), both ./test/prob/poisson/poisson_ccdf_log_00000_generated_v_test and ./test/prob/loglogistic/loglogistic_cdf_00001_generated_ffv_test fail due to taking a reference to something on the stack. The first fails under normal optimization settings consistently, the second is sporadic but can be made consistent by compiling with ASAN.

ASAN specifically blames

const auto& log_Pi = to_ref_if<!is_constant_all<T_rate>::value>(
log(gamma_p(n_val + 1.0, lambda_val)));
T_partials_return P = sum(log_Pi);

and

partials<2>(ops_partials) = beta_deriv * cdf_div_elt;

as the problematic lines.

The discussion in #2414 (comment) seems relevant, as @bgoodri was running into segfaults with a similar line in poisson_lcdf.

There are shockingly few places where we use to_ref_if on an expression which is the result of an apply_scalar_binary call, I believe it is only in:

stan/math/prim/prob/loglogistic_cdf.hpp
stan/math/prim/prob/loglogistic_lpdf.hpp
stan/math/prim/prob/pareto_type_2_lcdf.hpp
stan/math/prim/prob/poisson_cdf.hpp
stan/math/prim/prob/poisson_lccdf.hpp
stan/math/prim/prob/poisson_lcdf.hpp
stan/math/prim/prob/weibull_lpdf.hpp

@SteveBronder believes the correct fix is probably to make the apply_scalar_ functions and all callsites thereof into perfect forwarding templates. The other solution involves materializing a signficant number of copies which would have a performance impact

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugdistributionsIssues that deal with distribution functions: pdf, pmf, cdf

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions