[R-SIG-Mac] Some exceptions not thrown properly with clang4

Kevin Ushey kevinushey at gmail.com
Thu Sep 21 18:46:18 CEST 2017


>From what I understand, the issue ultimately stems from the mixing of
different C++ standard library implementations: the system-provided
libc++, and the LLVM libc++ bundled with R.

A similar issue is described and investigated with the V8 package:

    https://github.com/jeroen/V8/issues/37

This becomes even more complicated with R packages that themselves
attempt to link to external libraries, which themselves are typically
compiled to use the system's libc++ rather than the R-provided LLVM
libc++.

To investigate, try launching R with DYLD_SHARED_REGION=avoid to see
what system libraries are getting loaded (otherwise, macOS loads them
all together as part of a single 'cache' library), e.g.

    R_HOME=/Library/Frameworks/R.framework/Resources \
        DYLD_SHARED_REGION=avoid \
        /Library/Frameworks/R.framework/Resources/bin/exec/R --vanilla

(note that we run the executable directly to ensure the environment
variables are inherited by the process)

Then, load Rcpp and compile your example, and look at the libraries
loaded by that R process. For example, I see:

    $ lsof -p 40556 | grep libc++
    R       40556 kevin  txt    REG    1,4   436256 119314065
/usr/lib/libc++abi.dylib
    R       40556 kevin  txt    REG    1,4  1436752 108804146
/usr/lib/libc++.1.dylib
    R       40556 kevin  txt    REG    1,4  1205156 118545962
/Library/Frameworks/R.framework/Versions/3.4/Resources/lib/libc++.1.dylib
    R       40556 kevin  txt    REG    1,4   358888 118545963
/Library/Frameworks/R.framework/Versions/3.4/Resources/lib/libc++abi.1.dylib
    R       40556 kevin  txt    REG    1,4   862316 124608853
/usr/local/clang4/lib/libc++.1.0.dylib
    R       40556 kevin  txt    REG    1,4   297040 124608857
/usr/local/clang4/lib/libc++abi.1.0.dylib

Note that the system libc++ gets loaded when R itself is launched; I
suspect that R depends on some system libraries that themselves depend
on the system libc++ implementation. My guess is that the mixture of
multiple copies of libc++ leads to the issue you're seeing here.

I'm not sure if there's a clean way to resolve this, but please
correct me if I'm wrong or I've misunderstood / misrepresented
anything.

Best,
Kevin

On Thu, Sep 21, 2017 at 12:02 AM, Brian Ripley <ripley at stats.ox.ac.uk> wrote:
>
> The issue is not clang 4 per se.  It can be seen in the CRAN logs for quite a few packages built with the clang 4 build from r.research.att.com, all of which worked correctly with the LLVM distribution of clang 4.0.0 (and 4.0.1): the latter required macOS Sierra (without saying so).  And, as you say, with at least one version of the Command Line Tools (via Xcode or directly), that for 10.12 (but that lacks OpenMP support).
>
>
> > On 20 Sep 2017, at 23:53, Jonah S Gabry <jsg2201 at columbia.edu> wrote:
> >
> > Sorry if line breaks are missing from the test.cpp example file in my
> > original post.
> >
> >> On Wed, Sep 20, 2017 at 2:00 PM, Jonah S Gabry <jsg2201 at columbia.edu> wrote:
> >>
> >> With clang4 installed and Makevars updated as described at
> >> https://cran.r-project.org/bin/macosx/ we are finding unexpected behavior
> >> related to exceptions. The issue was brought to our attention at
> >>
> >> http://discourse.mc-stan.org/t/error-in-rstan-2-16-2-after-
> >> upgrading-to-r-3-4-1-with-clang-4-0-0/1893
> >>
> >> but here is a minimal example:
> >>
> >> Put the following in "test.cpp":
> >>
> >> #include <Rcpp.h> using namespace Rcpp; // [[Rcpp::export]] int
> >> throw_exception() { std::stringstream errmsg; errmsg << "this is an
> >> exception"; throw std::domain_error(errmsg.str()); return 0; }
> >>
> >> Then compile and test it with:
> >>
> >> Rcpp::sourceCpp("test.cpp")
> >> throw_exception()
> >>
> >> The output is:
> >>
> >> Error in throw_exception() : c++ exception (unknown reason)
> >>
> >> But the expected output (and the output I get when using e.g. the clang
> >> installation from Xcode) is:
> >>
> >> Error in throw_exception() : this is an exception
> >>
> >> Is this a known issue?
> >>
> >> Thanks,
> >>
> >> Jonah
> >>
> >> -----
> >>
> >> I'm running the following:
> >>
> >> R version 3.4.1 (2017-06-30)
> >> Platform: x86_64-apple-darwin15.6.0 (64-bit)
> >> Running under: macOS Sierra 10.12.6
> >>
> >
> >    [[alternative HTML version deleted]]
> >
> > _______________________________________________
> > R-SIG-Mac mailing list
> > R-SIG-Mac at r-project.org
> > https://stat.ethz.ch/mailman/listinfo/r-sig-mac
>
> _______________________________________________
> R-SIG-Mac mailing list
> R-SIG-Mac at r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-mac



More information about the R-SIG-Mac mailing list