|
| 1 | +test_that("cpp_source local controls RTTI/vtable symbol visibility", { |
| 2 | + skip_on_os("windows") |
| 3 | + |
| 4 | + mk_dirs <- function() { |
| 5 | + d1 <- tempfile("cpp_source_local1") |
| 6 | + d2 <- tempfile("cpp_source_local2") |
| 7 | + dir.create(d1); dir.create(d2) |
| 8 | + list(provider = d1, consumer = d2) |
| 9 | + } |
| 10 | + |
| 11 | + unload_dirs <- function(dirs) { |
| 12 | + dlls <- getLoadedDLLs() |
| 13 | + for (nm in names(dlls)) { |
| 14 | + # Some R builds/platforms expose different DLL info; access $path |
| 15 | + # defensively to avoid errors when the structure differs (macOS). |
| 16 | + p <- tryCatch({ dlls[[nm]]$path }, error = function(e) NULL) |
| 17 | + if (!is.null(p)) { |
| 18 | + for (d in dirs) { |
| 19 | + if (grepl(d, p, fixed = TRUE)) { |
| 20 | + tryCatch(dyn.unload(p), error = function(e) NULL) |
| 21 | + } |
| 22 | + } |
| 23 | + } |
| 24 | + } |
| 25 | + } |
| 26 | + |
| 27 | + provider_so_path <- function(dir) { |
| 28 | + src <- file.path(dir, "src") |
| 29 | + files <- list.files(src, pattern = paste0("\\\\", .Platform$dynlib.ext, "$"), ignore.case = TRUE) |
| 30 | + if (length(files) == 0) return(character()) |
| 31 | + file.path(src, files[[1]]) |
| 32 | + } |
| 33 | + |
| 34 | + dirs <- mk_dirs() |
| 35 | + on.exit({ |
| 36 | + unload_dirs(unlist(dirs)) |
| 37 | + unlink(unlist(dirs), recursive = TRUE, force = TRUE) |
| 38 | + }, add = TRUE) |
| 39 | + |
| 40 | + # Provider: abstract Base + Impl factory (polymorphic triggers RTTI/vtable) |
| 41 | + provider_code <- ' |
| 42 | + #include <cpp11/R.hpp> |
| 43 | + struct Base { virtual ~Base(){}; virtual int foo() = 0; }; |
| 44 | + struct Impl : Base { int foo() override { return 77; } }; |
| 45 | +
|
| 46 | + extern "C" Base* make_impl() { return new Impl(); } |
| 47 | + extern "C" void destroy_impl(Base* p) { delete p; } |
| 48 | + ' |
| 49 | + |
| 50 | + # Consumer uses typeid(Base) (forces reference to typeinfo symbol) and |
| 51 | + # calls the factory produced by the provider. |
| 52 | + consumer_code <- '\n#include <cpp11/R.hpp>\n#include <typeinfo>\n#include <string>\nstruct Base { virtual ~Base(){}; virtual int foo() = 0; };\nextern "C" Base* make_impl();\nextern "C" SEXP call_typeinfo_and_run() {\n const std::type_info& t = typeid(Base);\n std::string n = t.name();\n Base* b = make_impl();\n int v = b->foo();\n delete b;\n SEXP out = PROTECT(Rf_allocVector(INTSXP, 2));\n INTEGER(out)[0] = (int)n.size();\n INTEGER(out)[1] = v;\n UNPROTECT(1);\n return out;\n}\n' |
| 53 | + |
| 54 | + # 1) provider loaded with local = TRUE -> consumer should fail to load |
| 55 | + expect_silent(cpp_source(code = provider_code, dir = dirs$provider, clean = FALSE, local = TRUE)) |
| 56 | + expect_error( |
| 57 | + cpp_source(code = consumer_code, dir = dirs$consumer, clean = FALSE), |
| 58 | + regexp = "undefined symbol|symbol .* not found|undefined reference|symbol not found in flat namespace", |
| 59 | + ignore.case = TRUE |
| 60 | + ) |
| 61 | + |
| 62 | + # Clean up partial loads |
| 63 | + unload_dirs(unlist(dirs)) |
| 64 | + |
| 65 | + # 2) provider loaded with local = FALSE -> consumer loads and runs |
| 66 | + expect_silent(cpp_source(code = provider_code, dir = dirs$provider, clean = FALSE, local = FALSE)) |
| 67 | + expect_silent(cpp_source(code = consumer_code, dir = dirs$consumer, clean = FALSE)) |
| 68 | + |
| 69 | + res <- .Call("call_typeinfo_and_run") |
| 70 | + expect_true(is.integer(res) && length(res) == 2) |
| 71 | + expect_equal(as.integer(res)[2], 77L) |
| 72 | + expect_true(as.integer(res)[1] > 0) |
| 73 | + |
| 74 | + # Explicit check that the manual dyn.load(...) workaround is unnecessary. |
| 75 | + # Emulate the snippet to locate the provider shared object and show that it |
| 76 | + # exists; we already demonstrated the consumer works without running this |
| 77 | + # manual snippet because cpp_source(local = FALSE) provided global symbols. |
| 78 | + # Try to determine the built provider shared object path. On some runners |
| 79 | + # the file may not be discoverable via listing (packaged R builds, macOS |
| 80 | + # variations). Fall back to inspecting getLoadedDLLs(); if still not |
| 81 | + # available, skip the explicit dyn.load check on this platform. |
| 82 | + so_path <- provider_so_path(dirs$provider) |
| 83 | + if (length(so_path) == 0) { |
| 84 | + dlls <- getLoadedDLLs() |
| 85 | + for (nm in names(dlls)) { |
| 86 | + p <- tryCatch({ dlls[[nm]]$path }, error = function(e) NULL) |
| 87 | + if (!is.null(p) && grepl(dirs$provider, p, fixed = TRUE)) { |
| 88 | + so_path <- p |
| 89 | + break |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + if (length(so_path) == 0 || !nzchar(so_path)) { |
| 95 | + skip("Could not locate provider shared object on this platform; skipping manual dyn.load check") |
| 96 | + } |
| 97 | + |
| 98 | + expect_true(file.exists(so_path)) |
| 99 | + # Loading it manually with local = FALSE would succeed, but wasn't required. |
| 100 | + expect_silent(dyn.load(so_path, local = FALSE, now = TRUE)) |
| 101 | +}) |
0 commit comments