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 unwind tests on win-builder #801

Merged
merged 3 commits into from
Jan 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 0 additions & 30 deletions inst/unitTests/cpp/misc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@
// You should have received a copy of the GNU General Public License
// along with Rcpp. If not, see <http://www.gnu.org/licenses/>.

#define RCPP_PROTECTED_EVAL

#include <Rcpp.h>
using namespace Rcpp;
using namespace std;
Expand Down Expand Up @@ -226,31 +224,3 @@ String testNullableString(Rcpp::Nullable<Rcpp::String> param = R_NilValue) {
else
return String("");
}

// Class that indicates to R caller whether C++ stack was unwound
struct unwindIndicator {
unwindIndicator(LogicalVector indicator_) {
// Reset the indicator to FALSE
indicator = indicator_;
*LOGICAL(indicator) = 0;
}

// Set indicator to TRUE when stack unwinds
~unwindIndicator() {
*LOGICAL(indicator) = 1;
}

LogicalVector indicator;
};

// [[Rcpp::export]]
SEXP testEvalUnwindImpl(RObject expr, Environment env, LogicalVector indicator) {
unwindIndicator my_data(indicator);
return Rcpp::Rcpp_fast_eval(expr, env);
}

// [[Rcpp::export]]
SEXP testSendInterrupt() {
Rf_onintr();
return R_NilValue;
}
53 changes: 53 additions & 0 deletions inst/unitTests/cpp/stack.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; indent-tabs-mode: nil; -*-
//
// misc.cpp: Rcpp R/C++ interface class library -- misc unit tests
//
// Copyright (C) 2013 - 2015 Dirk Eddelbuettel and Romain Francois
//
// This file is part of Rcpp.
//
// Rcpp is free software: you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 2 of the License, or
// (at your option) any later version.
//
// Rcpp is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Rcpp. If not, see <http://www.gnu.org/licenses/>.

#define RCPP_PROTECTED_EVAL

#include <Rcpp.h>
using namespace Rcpp;

// Class that indicates to R caller whether C++ stack was unwound
struct unwindIndicator {
unwindIndicator(LogicalVector indicator_) {
// Reset the indicator to FALSE
indicator = indicator_;
*LOGICAL(indicator) = 0;
}

// Set indicator to TRUE when stack unwinds
~unwindIndicator() {
*LOGICAL(indicator) = 1;
}

LogicalVector indicator;
};

// [[Rcpp::export]]
SEXP testFastEval(RObject expr, Environment env, LogicalVector indicator) {
unwindIndicator my_data(indicator);
return Rcpp::Rcpp_fast_eval(expr, env);
}

// [[Rcpp::export]]
SEXP testSendInterrupt() {
Rf_onintr();
return R_NilValue;
}
51 changes: 0 additions & 51 deletions inst/unitTests/runit.misc.R
Original file line number Diff line number Diff line change
Expand Up @@ -214,55 +214,4 @@ if (.runThisTest) {
checkTrue(nchar(Rcpp:::bib()) > 0, msg="bib file")
}

test.stackUnwinds <- function() {
# On old versions of R, Rcpp_fast_eval() falls back to Rcpp_eval() and
# leaks on longjumps
hasUnwind <- getRversion() >= "3.5.0"
checkUnwound <- if (hasUnwind) checkTrue else function(x) checkTrue(!x)
testEvalUnwind <- function(expr, indicator) {
testEvalUnwindImpl(expr, parent.frame(), indicator)
}

# On errors - Always unwound
unwound <- FALSE
out <- tryCatch(testEvalUnwind(quote(stop("err")), unwound), error = identity)
checkTrue(unwound)
msg <- if (hasUnwind) "err" else "Evaluation error: err."
checkIdentical(out$message, msg)

# On interrupts - Always unwound
unwound <- FALSE
expr <- quote({
repeat testSendInterrupt()
"returned"
})
out <- tryCatch(testEvalUnwind(expr, unwound), interrupt = function(c) "onintr")
checkTrue(unwound)
checkIdentical(out, "onintr")

# On caught conditions
unwound <- FALSE
expr <- quote(signalCondition(simpleCondition("cnd")))
cnd <- tryCatch(testEvalUnwind(expr, unwound), condition = identity)
checkTrue(inherits(cnd, "simpleCondition"))
checkUnwound(unwound)

# On restart jumps
unwound <- FALSE
expr <- quote(invokeRestart("rst"))
out <- withRestarts(testEvalUnwind(expr, unwound), rst = function(...) "restarted")
checkIdentical(out, "restarted")
checkUnwound(unwound)

# On returns
unwound <- FALSE
expr <- quote(signalCondition(simpleCondition(NULL)))
out <- callCC(function(k)
withCallingHandlers(testEvalUnwind(expr, unwound),
simpleCondition = function(e) k("jumped")
)
)
checkIdentical(out, "jumped")
checkUnwound(unwound)
}
}
103 changes: 103 additions & 0 deletions inst/unitTests/runit.stack.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
#!/usr/bin/env r
#
# Copyright (C) 2010 - 2017 Dirk Eddelbuettel and Romain Francois
#
# This file is part of Rcpp.
#
# Rcpp is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# Rcpp is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Rcpp. If not, see <http://www.gnu.org/licenses/>.

.runThisTest <- Sys.getenv("RunAllRcppTests") == "yes"


if (FALSE && .runThisTest) {

.setUp <- Rcpp:::unitTestSetup("stack.cpp")

# On old versions of R, Rcpp_fast_eval() falls back to Rcpp_eval() and
# leaks on longjumps
hasUnwind <- getRversion() >= "3.5.0"
checkUnwound <- if (hasUnwind) checkTrue else function(x) checkTrue(!x)
EvalUnwind <- function(expr, indicator) {
testFastEval(expr, parent.frame(), indicator)
}

# Stack is always unwound on errors and interrupts
test.stackUnwindsOnErrors <- function() {
unwound <- FALSE
out <- tryCatch(EvalUnwind(quote(stop("err")), unwound), error = identity)
checkTrue(unwound)
msg <- if (hasUnwind) "err" else "Evaluation error: err."
checkIdentical(out$message, msg)
}

test.stackUnwindsOnInterrupts <- function() {
unwound <- FALSE
expr <- quote({
repeat testSendInterrupt()
"returned"
})
out <- tryCatch(EvalUnwind(expr, unwound), interrupt = function(c) "onintr")
checkTrue(unwound)
checkIdentical(out, "onintr")

}

test.stackUnwindsOnCaughtConditions <- function() {
unwound <- FALSE
expr <- quote(signalCondition(simpleCondition("cnd")))
cnd <- tryCatch(EvalUnwind(expr, unwound), condition = identity)
checkTrue(inherits(cnd, "simpleCondition"))
checkUnwound(unwound)

}

test.stackUnwindsOnRestartJumps <- function() {
unwound <- FALSE
expr <- quote(invokeRestart("rst"))
out <- withRestarts(EvalUnwind(expr, unwound), rst = function(...) "restarted")
checkIdentical(out, "restarted")
checkUnwound(unwound)

}

test.stackUnwindsOnReturns <- function() {
unwound <- FALSE
expr <- quote(signalCondition(simpleCondition(NULL)))
out <- callCC(function(k) {
withCallingHandlers(EvalUnwind(expr, unwound),
simpleCondition = function(e) k("jumped")
)
})
checkIdentical(out, "jumped")
checkUnwound(unwound)

}

test.stackUnwindsOnReturnedConditions <- function() {
unwound <- FALSE
cnd <- simpleError("foo")
out <- tryCatch(EvalUnwind(quote(cnd), unwound),
error = function(c) "abort"
)
checkTrue(unwound)

# The old mechanism cannot differentiate between a returned error and a
# thrown error
if (hasUnwind) {
checkIdentical(out, cnd)
} else {
checkIdentical(out, "abort")
}
}
}