https://gcc.gnu.org/bugzilla/show_bug.cgi?id=129233

            Bug ID: 129233
           Summary: [C++26][modules] GGC use-after-free and corrupted
                    mangling of handle_contract_violation when importing
                    module with contracts
           Product: gcc
           Version: 17.0
            Status: UNCONFIRMED
          Keywords: c++-contracts, c++26, GC, ice-on-valid-code,
                    wrong-code
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: adrian.herrmann.xyz at gmail dot com
  Target Milestone: ---

Instantiating an exported function template with a contract from a module whose
GMF declares std::contracts::contract_violation (or std::source_location)
triggers a GGC use-after-free when the importing TU does not include
<contracts>:

--- fwd_contracts.hpp ---
#pragma once

namespace std::contracts {
    class contract_violation;
}

--- mod.cppm ---
module;

#include "fwd_contracts.hpp"

export module m;

export template <typename T>
T checked_inc(T x) {
    contract_assert(x >= 0);
    return x + 1;
}

--- main.cpp ---
import m;

int use(int x) {
    return checked_inc(x);
}

$ g++ -std=c++26 -fmodules -fcontract-evaluation-semantic=enforce -c mod.cppm
-o mod.o
$ g++ -std=c++26 -fmodules -fcontract-evaluation-semantic=enforce -S main.cpp
-o main.s

With --enable-checking=yes, cc1plus ICEs (tree check / poisoned GGC memory).
With --enable-checking=release, it emits a corrupted mangled symbol containing
heap pointers, e.g.:
  call _Z25handle_contract_violationRK2430355968<binary bytes>
instead of:
  call _Z25handle_contract_violationRKNSt9contracts18contract_violationE

In gcc/cp/contracts.cc, lookup_std_type, lookup_std_contracts_type, and
declare_handle_contract_violation call lookup_qualified_name without
LOOK_want::ANY_REACHABLE. When the decl is reachable-but-not-visible via an
imported module GMF, lookup fails, GCC synthesizes a fallback decl, and calls
pushdecl_namespace_level (decl, true) while discarding the return value.
pushdecl_namespace_level merges via duplicate_decls and ggc_frees the newly
allocated decl/type, leaving the caller to use and mangle freed memory.

Also, lookup_std_type doesn't set TYPE_CONTEXT or clear module_kind flags when
synthesizing the fallback, and get_contracts_source_location_impl_type calls
get_source_location_impl_type() which re-looks up std::source_location without
LOOK_want::ANY_REACHABLE.

I have a patch for this and plan to send it to gcc-patches.

Tested on master 48688cfeadf6ec3b2dd22783f5048db6fc444d04 (also reproduces on
5a61875b0cd983e57dd44b7d6e12b9eb11985bae):
Using built-in specs.
COLLECT_GCC=xg++
Target: x86_64-elf
Configured with: src/configure --target=x86_64-elf --enable-languages=c++
--disable-nls --disable-lto --disable-libssp --disable-multilib
--without-headers --without-isl --without-zstd --enable-checking=yes
Thread model: single
gcc version 17.0.0 20261126 (experimental) (GCC)

Reply via email to