tcdi/plrust

plrustc panics during compilation when using the `rand` crate

anth0nyleung opened this issue · 3 comments

When trying to create a dummy function which uses a few crates as dependencies (without even actually using the crates), plrustc panics during compilation.

Function definition

CREATE OR REPLACE FUNCTION foo() RETURNS INT AS $$
[dependencies]
aes = "0.8.2"
ctr = "0.9.2"
rand = "0.8.5"
[code]
Ok(Some(0))
$$ LANGUAGE plrust;

Crates are allow-listed properly

[01:49:42] > $ cat plrust_allowed_dependencies.toml

aes = "=0.8.2"
ctr = "=0.9.2"
rand = "=0.8.5"

Error stack trace

   0: `cargo build` failed

Location:
   /rustc/90c541806f23a127002de5b4038be731ba1458ca/library/core/src/convert/mod.rs:727

`cargo build` stderr:
      Compiling regex v1.9.0
      Compiling serde v1.0.166
      Compiling thiserror v1.0.41
      Compiling pgrx-macros v0.9.5
      Compiling rand v0.8.5
      Compiling pest v2.7.0
      Compiling bindgen v0.65.1
   thread 'rustc' panicked at 'DefId(13:23 ~ rand_core[eac9]::block::BlockRngCore::Item) does not have a "type_of"', compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs:205:1
   stack backtrace:
      0:     0x7efd9696fcca - std::backtrace_rs::backtrace::libunwind::trace::h9a6b80bbf328ba5d
                                  at /rustc/90c541806f23a127002de5b4038be731ba1458ca/library/std/src/../../backtrace/src/backtrace/libunwind.rs:93:5
      1:     0x7efd9696fcca - std::backtrace_rs::backtrace::trace_unsynchronized::hd162ec543a11886b
                                  at /rustc/90c541806f23a127002de5b4038be731ba1458ca/library/std/src/../../backtrace/src/backtrace/mod.rs:66:5
      2:     0x7efd9696fcca - std::sys_common::backtrace::_print_fmt::h78a5099be12f51a6
                                  at /rustc/90c541806f23a127002de5b4038be731ba1458ca/library/std/src/sys_common/backtrace.rs:65:5
      3:     0x7efd9696fcca - <std::sys_common::backtrace::_print::DisplayBacktrace as core::fmt::Display>::fmt::ha1c5390454d74f71
                                  at /rustc/90c541806f23a127002de5b4038be731ba1458ca/library/std/src/sys_common/backtrace.rs:44:22
      4:     0x7efd969d40cf - core::fmt::write::h9ffde816c577717b
                                  at /rustc/90c541806f23a127002de5b4038be731ba1458ca/library/core/src/fmt/mod.rs:1254:17
      5:     0x7efd96962805 - std::io::Write::write_fmt::h88186074961638e4
                                  at /rustc/90c541806f23a127002de5b4038be731ba1458ca/library/std/src/io/mod.rs:1698:15
      6:     0x7efd9696fa95 - std::sys_common::backtrace::_print::h184198273ed08d59
                                  at /rustc/90c541806f23a127002de5b4038be731ba1458ca/library/std/src/sys_common/backtrace.rs:47:5
      7:     0x7efd9696fa95 - std::sys_common::backtrace::print::h1b4d8e7add699453
                                  at /rustc/90c541806f23a127002de5b4038be731ba1458ca/library/std/src/sys_common/backtrace.rs:34:9
      8:     0x7efd9697273e - std::panicking::default_hook::{{closure}}::h393bcea75423915a
                                  at /rustc/90c541806f23a127002de5b4038be731ba1458ca/library/std/src/panicking.rs:269:22
      9:     0x7efd969724e5 - std::panicking::default_hook::h48c64f31d8b3fd03
                                  at /rustc/90c541806f23a127002de5b4038be731ba1458ca/library/std/src/panicking.rs:288:9
     10:     0x564810f7e440 - plrustc::install_ice_hook::PANIC_HOOK::{{closure}}::{{closure}}::hf73581e9efce713e
     11:     0x7efd96972f34 - <alloc::boxed::Box<F,A> as core::ops::function::Fn<Args>>::call::hb9b860f5a1175bda
                                  at /rustc/90c541806f23a127002de5b4038be731ba1458ca/library/alloc/src/boxed.rs:1987:9
     12:     0x7efd96972f34 - std::panicking::rust_panic_with_hook::hafdc493a79370062
                                  at /rustc/90c541806f23a127002de5b4038be731ba1458ca/library/std/src/panicking.rs:695:13
     13:     0x7efd96972ca9 - std::panicking::begin_panic_handler::{{closure}}::h0a64bc82e36bedc7
                                  at /rustc/90c541806f23a127002de5b4038be731ba1458ca/library/std/src/panicking.rs:582:13
     14:     0x7efd96970136 - std::sys_common::backtrace::__rust_end_short_backtrace::hc203444fb7416a16
                                  at /rustc/90c541806f23a127002de5b4038be731ba1458ca/library/std/src/sys_common/backtrace.rs:150:18
     15:     0x7efd96972a02 - rust_begin_unwind
                                  at /rustc/90c541806f23a127002de5b4038be731ba1458ca/library/std/src/panicking.rs:578:5
     16:     0x7efd969d0443 - core::panicking::panic_fmt::h0f6ef0178afce4f2
                                  at /rustc/90c541806f23a127002de5b4038be731ba1458ca/library/core/src/panicking.rs:67:14
     17:     0x7efd95ba0792 - rustc_metadata[af2d1f1b6c1f5533]::rmeta::decoder::cstore_impl::provide_extern::type_of::{closure#2}
     18:     0x7efd93e939c0 - rustc_query_system[5e34f8384464b8a5]::query::plumbing::try_execute_query::<rustc_query_impl[3e42b93bfeb445bd]::queries::type_of, rustc_query_impl[3e42b93bfeb445bd]::plumbing::QueryCtxt>
     19:     0x7efd93e919aa - <rustc_query_impl[3e42b93bfeb445bd]::Queries as rustc_middle[a487dff4633a86d0]::ty::query::QueryEngine>::type_of
     20:     0x564810f7a7db - <plrustc::lints::sus_trait_object::PlrustSuspiciousTraitObject as rustc_lint::passes::LateLintPass>::check_expr::h7945f9fc3ba8e2a6
     21:     0x7efd95acff26 - <rustc_lint[e3a36e86999859a1]::early::RuntimeCombinedEarlyLintPass as rustc_lint[e3a36e86999859a1]::passes::EarlyLintPass>::check_generic_arg
     22:     0x7efd95aaab74 - <rustc_lint[e3a36e86999859a1]::late::LateContextAndPass<rustc_lint[e3a36e86999859a1]::late::RuntimeCombinedLateLintPass> as rustc_hir[d7f0338a4e902a5b]::intravisit::Visitor>::visit_expr
     23:     0x7efd95a872fc - rustc_hir[d7f0338a4e902a5b]::intravisit::walk_expr::<rustc_lint[e3a36e86999859a1]::late::LateContextAndPass<rustc_lint[e3a36e86999859a1]::late::RuntimeCombinedLateLintPass>>
     24:     0x7efd95aaab7f - <rustc_lint[e3a36e86999859a1]::late::LateContextAndPass<rustc_lint[e3a36e86999859a1]::late::RuntimeCombinedLateLintPass> as rustc_hir[d7f0338a4e902a5b]::intravisit::Visitor>::visit_expr
     25:     0x7efd95aaab7f - <rustc_lint[e3a36e86999859a1]::late::LateContextAndPass<rustc_lint[e3a36e86999859a1]::late::RuntimeCombinedLateLintPass> as rustc_hir[d7f0338a4e902a5b]::intravisit::Visitor>::visit_expr
     26:     0x7efd95a85d4b - rustc_hir[d7f0338a4e902a5b]::intravisit::walk_local::<rustc_lint[e3a36e86999859a1]::late::LateContextAndPass<rustc_lint[e3a36e86999859a1]::late::RuntimeCombinedLateLintPass>>
     27:     0x7efd95aaad3c - <rustc_lint[e3a36e86999859a1]::late::LateContextAndPass<rustc_lint[e3a36e86999859a1]::late::RuntimeCombinedLateLintPass> as rustc_hir[d7f0338a4e902a5b]::intravisit::Visitor>::visit_local
     28:     0x7efd95a85cfa - rustc_hir[d7f0338a4e902a5b]::intravisit::walk_block::<rustc_lint[e3a36e86999859a1]::late::LateContextAndPass<rustc_lint[e3a36e86999859a1]::late::RuntimeCombinedLateLintPass>>
     29:     0x7efd95aaacb6 - <rustc_lint[e3a36e86999859a1]::late::LateContextAndPass<rustc_lint[e3a36e86999859a1]::late::RuntimeCombinedLateLintPass> as rustc_hir[d7f0338a4e902a5b]::intravisit::Visitor>::visit_block
     30:     0x7efd95aaab7f - <rustc_lint[e3a36e86999859a1]::late::LateContextAndPass<rustc_lint[e3a36e86999859a1]::late::RuntimeCombinedLateLintPass> as rustc_hir[d7f0338a4e902a5b]::intravisit::Visitor>::visit_expr
     31:     0x7efd95aab014 - <rustc_lint[e3a36e86999859a1]::late::LateContextAndPass<rustc_lint[e3a36e86999859a1]::late::RuntimeCombinedLateLintPass> as rustc_hir[d7f0338a4e902a5b]::intravisit::Visitor>::visit_nested_body
     32:     0x7efd95aab1fe - <rustc_lint[e3a36e86999859a1]::late::LateContextAndPass<rustc_lint[e3a36e86999859a1]::late::RuntimeCombinedLateLintPass> as rustc_hir[d7f0338a4e902a5b]::intravisit::Visitor>::visit_fn
     33:     0x7efd95a86209 - rustc_hir[d7f0338a4e902a5b]::intravisit::walk_impl_item::<rustc_lint[e3a36e86999859a1]::late::LateContextAndPass<rustc_lint[e3a36e86999859a1]::late::RuntimeCombinedLateLintPass>>
     34:     0x7efd95aab809 - <rustc_lint[e3a36e86999859a1]::late::LateContextAndPass<rustc_lint[e3a36e86999859a1]::late::RuntimeCombinedLateLintPass> as rustc_hir[d7f0338a4e902a5b]::intravisit::Visitor>::visit_nested_impl_item
     35:     0x7efd95a87793 - rustc_hir[d7f0338a4e902a5b]::intravisit::walk_item::<rustc_lint[e3a36e86999859a1]::late::LateContextAndPass<rustc_lint[e3a36e86999859a1]::late::RuntimeCombinedLateLintPass>>
     36:     0x7efd95aab615 - <rustc_lint[e3a36e86999859a1]::late::LateContextAndPass<rustc_lint[e3a36e86999859a1]::late::RuntimeCombinedLateLintPass> as rustc_hir[d7f0338a4e902a5b]::intravisit::Visitor>::visit_nested_item
     37:     0x7efd95a86e99 - rustc_hir[d7f0338a4e902a5b]::intravisit::walk_mod::<rustc_lint[e3a36e86999859a1]::late::LateContextAndPass<rustc_lint[e3a36e86999859a1]::late::RuntimeCombinedLateLintPass>>
     38:     0x7efd95aab615 - <rustc_lint[e3a36e86999859a1]::late::LateContextAndPass<rustc_lint[e3a36e86999859a1]::late::RuntimeCombinedLateLintPass> as rustc_hir[d7f0338a4e902a5b]::intravisit::Visitor>::visit_nested_item
     39:     0x7efd95a86e99 - rustc_hir[d7f0338a4e902a5b]::intravisit::walk_mod::<rustc_lint[e3a36e86999859a1]::late::LateContextAndPass<rustc_lint[e3a36e86999859a1]::late::RuntimeCombinedLateLintPass>>
     40:     0x7efd95aab615 - <rustc_lint[e3a36e86999859a1]::late::LateContextAndPass<rustc_lint[e3a36e86999859a1]::late::RuntimeCombinedLateLintPass> as rustc_hir[d7f0338a4e902a5b]::intravisit::Visitor>::visit_nested_item
     41:     0x7efd95a86e99 - rustc_hir[d7f0338a4e902a5b]::intravisit::walk_mod::<rustc_lint[e3a36e86999859a1]::late::LateContextAndPass<rustc_lint[e3a36e86999859a1]::late::RuntimeCombinedLateLintPass>>
     42:     0x7efd95aab615 - <rustc_lint[e3a36e86999859a1]::late::LateContextAndPass<rustc_lint[e3a36e86999859a1]::late::RuntimeCombinedLateLintPass> as rustc_hir[d7f0338a4e902a5b]::intravisit::Visitor>::visit_nested_item
     43:     0x7efd95a86e99 - rustc_hir[d7f0338a4e902a5b]::intravisit::walk_mod::<rustc_lint[e3a36e86999859a1]::late::LateContextAndPass<rustc_lint[e3a36e86999859a1]::late::RuntimeCombinedLateLintPass>>
     44:     0x7efd94d92db9 - rustc_lint[e3a36e86999859a1]::late::late_lint_crate::<rustc_lint[e3a36e86999859a1]::BuiltinCombinedLateLintPass>
     45:     0x7efd94d92316 - <rustc_session[99ba0ed9c533f203]::session::Session>::time::<(), rustc_lint[e3a36e86999859a1]::late::check_crate<rustc_lint[e3a36e86999859a1]::BuiltinCombinedLateLintPass, rustc_interface[7aa24cde61830128]::passes::analysis::{closure#6}::{closure#1}::{closure#2}::{closure#0}::{closure#0}>::{closure#0}::{closure#0}>
     46:     0x7efd94c8dd53 - <rustc_session[99ba0ed9c533f203]::session::Session>::time::<(), rustc_interface[7aa24cde61830128]::passes::analysis::{closure#6}::{closure#1}::{closure#2}::{closure#0}>
     47:     0x7efd94cbb27c - <core[da82827a87f140f9]::panic::unwind_safe::AssertUnwindSafe<rustc_interface[7aa24cde61830128]::passes::analysis::{closure#6}::{closure#1}> as core[da82827a87f140f9]::ops::function::FnOnce<()>>::call_once
     48:     0x7efd94b9bc5e - <rustc_session[99ba0ed9c533f203]::session::Session>::time::<(), rustc_interface[7aa24cde61830128]::passes::analysis::{closure#6}>
     49:     0x7efd94b9a177 - rustc_interface[7aa24cde61830128]::passes::analysis
     50:     0x7efd94e15fc2 - rustc_query_system[5e34f8384464b8a5]::query::plumbing::try_execute_query::<rustc_query_impl[3e42b93bfeb445bd]::queries::analysis, rustc_query_impl[3e42b93bfeb445bd]::plumbing::QueryCtxt>
     51:     0x7efd94e15cd0 - <rustc_query_impl[3e42b93bfeb445bd]::Queries as rustc_middle[a487dff4633a86d0]::ty::query::QueryEngine>::analysis
     52:     0x7efd94d4c726 - <std[b70fedfd8b77e9]::thread::local::LocalKey<core[da82827a87f140f9]::cell::Cell<*const ()>>>::with::<rustc_middle[a487dff4633a86d0]::ty::context::tls::enter_context<<rustc_middle[a487dff4633a86d0]::ty::context::GlobalCtxt>::enter<rustc_driver_impl[fe6df70259db788d]::run_compiler::{closure#1}::{closure#2}::{closure#4}, core[da82827a87f140f9]::result::Result<(), rustc_span[2f805118d893a80f]::ErrorGuaranteed>>::{closure#0}, core[da82827a87f140f9]::result::Result<(), rustc_span[2f805118d893a80f]::ErrorGuaranteed>>::{closure#0}, core[da82827a87f140f9]::result::Result<(), rustc_span[2f805118d893a80f]::ErrorGuaranteed>>
     53:     0x7efd949b302e - <rustc_interface[7aa24cde61830128]::interface::Compiler>::enter::<rustc_driver_impl[fe6df70259db788d]::run_compiler::{closure#1}::{closure#2}, core[da82827a87f140f9]::result::Result<core[da82827a87f140f9]::option::Option<rustc_interface[7aa24cde61830128]::queries::Linker>, rustc_span[2f805118d893a80f]::ErrorGuaranteed>>
     54:     0x7efd949ae281 - rustc_span[2f805118d893a80f]::set_source_map::<core[da82827a87f140f9]::result::Result<(), rustc_span[2f805118d893a80f]::ErrorGuaranteed>, rustc_interface[7aa24cde61830128]::interface::run_compiler<core[da82827a87f140f9]::result::Result<(), rustc_span[2f805118d893a80f]::ErrorGuaranteed>, rustc_driver_impl[fe6df70259db788d]::run_compiler::{closure#1}>::{closure#0}::{closure#0}>
     55:     0x7efd949ad82f - std[b70fedfd8b77e9]::sys_common::backtrace::__rust_begin_short_backtrace::<rustc_interface[7aa24cde61830128]::util::run_in_thread_pool_with_globals<rustc_interface[7aa24cde61830128]::interface::run_compiler<core[da82827a87f140f9]::result::Result<(), rustc_span[2f805118d893a80f]::ErrorGuaranteed>, rustc_driver_impl[fe6df70259db788d]::run_compiler::{closure#1}>::{closure#0}, core[da82827a87f140f9]::result::Result<(), rustc_span[2f805118d893a80f]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[da82827a87f140f9]::result::Result<(), rustc_span[2f805118d893a80f]::ErrorGuaranteed>>
     56:     0x7efd94f91661 - <<std[b70fedfd8b77e9]::thread::Builder>::spawn_unchecked_<rustc_interface[7aa24cde61830128]::util::run_in_thread_pool_with_globals<rustc_interface[7aa24cde61830128]::interface::run_compiler<core[da82827a87f140f9]::result::Result<(), rustc_span[2f805118d893a80f]::ErrorGuaranteed>, rustc_driver_impl[fe6df70259db788d]::run_compiler::{closure#1}>::{closure#0}, core[da82827a87f140f9]::result::Result<(), rustc_span[2f805118d893a80f]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[da82827a87f140f9]::result::Result<(), rustc_span[2f805118d893a80f]::ErrorGuaranteed>>::{closure#1} as core[da82827a87f140f9]::ops::function::FnOnce<()>>::call_once::{shim:vtable#0}
     57:     0x7efd9697d3b5 - <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once::ha1f2224656a778fb
                                  at /rustc/90c541806f23a127002de5b4038be731ba1458ca/library/alloc/src/boxed.rs:1973:9
     58:     0x7efd9697d3b5 - <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once::haa29ed9703f354b7
                                  at /rustc/90c541806f23a127002de5b4038be731ba1458ca/library/alloc/src/boxed.rs:1973:9
     59:     0x7efd9697d3b5 - std::sys::unix::thread::Thread::new::thread_start::h33b6dae3e3692197
                                  at /rustc/90c541806f23a127002de5b4038be731ba1458ca/library/std/src/sys/unix/thread.rs:108:17
     60:     0x7efd9207840b - start_thread
     61:     0x7efd91db309f - __GI___clone
     62:                0x0 - <unknown>

   error: internal compiler error: unexpected panic

   note: `plrustc` unexpectedly panicked. This is probably a bug.

   note: Please file a bug report at <https://github.com/tcdi/plrust/issues/new>

   note: plrustc version: 1.2.0

   query stack during panic:
   #0 [type_of] computing type of `rand_core::block::BlockRngCore::Item`
   #1 [analysis] running analysis passes on this crate
   end of query stack
   error: could not compile `rand` (lib)
   warning: build failed, waiting for other jobs to finish...

Whee! This looks fun!

17: rustc_metadata[af2d1f1b6c1f5533]::rmeta::decoder::cstore_impl::provide_extern::type_of::{closure#2}
18: rustc_query_system[5e34f8384464b8a5]::query::plumbing::try_execute_query::<rustc_query_impl[3e42b93bfeb445bd]::queries::type_of, rustc_query_impl[3e42b93bfeb445bd]::plumbing::QueryCtxt>
19: <rustc_query_impl[3e42b93bfeb445bd]::Queries as rustc_middle[a487dff4633a86d0]::ty::query::QueryEngine>::type_of
20: <plrustc::lints::sus_trait_object::PlrustSuspiciousTraitObject as rustc_lint::passes::LateLintPass>::check_expr::h7945f9fc3ba8e2a6
21: <rustc_lint[e3a36e86999859a1]::early::RuntimeCombinedEarlyLintPass as rustc_lint[e3a36e86999859a1]::passes::EarlyLintPass>::check_generic_arg
22: <rustc_lint[e3a36e86999859a1]::late::LateContextAndPass<rustc_lint[e3a36e86999859a1]::late::RuntimeCombinedLateLintPass> as rustc_hir[d7f0338a4e902a5b]::intravisit::Visitor>::visit_expr

Here's our offending code:

let is_trait_obj = match &ty.kind {
hir::TyKind::TraitObject(..) => true,
hir::TyKind::Path(qpath) => {
let res = cx.qpath_res(qpath, ty.hir_id);
let did = match res {
Res::Def(DefKind::TyAlias | DefKind::AssocTy, def_id) => def_id,
Res::SelfTyAlias { alias_to, .. } => alias_to,
_ => continue,
};
let binder = cx.tcx.type_of(did);

This code at least seems to be using QueryEngine::type_of in a way consistent with how the fn is documented to work but something tells me there's a hidden catch we aren't accounting for in our usage. Or a genuine bug in rustc, that would be fun!

Relevant links for tomorrow:

thomcc commented

Apparently it's invalid to call cx.tcx.type_of(def_id) for an associated type. The general recommendation is to get cx.typeck_results() (or cx.maybe_typeck_results()) and then call those_results.expr_type(relevant_expression) rather than trying to compute it from the path.

Unfortunately, this is not possible in this case, as the turbofish-ee has no corresponding expression. However, allowing associated types isn't viable for us, since we need to check them to avoid this variant of the unsoundness the suspicious-trait-object is looking for in the first place: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=515daafc9d9e2011ed6c35e0062b6495.

There are two things we should do:

  1. If no plrust_ lints were otherwise enabled in the CLI (we want this for tests and to run lints in advance on deps), we should only lint the primary crate, not dependencies. This is easy-ish, but is a bandaid, altho worth doing for performance reasons.

  2. That said, to really fix this we need to change that code, probably to manually resolve the associated type on the trait. This can be done similar to:

    let assoc_ty_did: DefId;
    let trait_did = cx.tcx.parent(assoc_ty_did)
        .expect("dunno if need to handle");
    let assoc_item = cx.tcx
        .associated_items(trait_did)
        .in_definition_order()
        // this is goofy, perhaps there's a better way
        .find(|item| item.def_id == assoc_ty_did)
        .expect("dunno if need to handle");

    ...and then maybe we just do a lint on this assoc_item (which is an AssocItem), I guess. That is, the issue I thought of where we need to worry about assoc types could be blocked if we forbid an lifetime-generic associated type which is also ?Sized.... Which maybe should be linted on anyway?