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

            Bug ID: 129229
           Summary: [modules] Off-by-one in module_state::write_location
                    causes ICE / Bad file data
           Product: gcc
           Version: 17.0
            Status: UNCONFIRMED
          Keywords: ice-checking, ice-on-valid-code, rejects-valid
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: adrian.herrmann.xyz at gmail dot com
  Target Milestone: ---

In module_state::write_location (gcc/cp/module.cc), both upper-bound checks on
remapped local/partition location spans use '>' instead of '>=' against the
half-open span size:
  if (offset > info->src->n_tokens)
  if (offset > info->span)

When an imported location borders a local or partition map (offset == n_tokens
or offset == span), write_location fails to reject the span.

--- dep.cppm ---
module;
#define DEP_MAC(x) ((x) + 1)
export module dep;
export template <typename T> inline T dep_fn(T a) {
    return DEP_MAC(a);
}

--- part.cppm ---
module;
#define PART_MAC(x) ((x) + 2)
export module m:p;
export inline int part_fn(int a) {
    return PART_MAC(a);
}

--- m.cppm ---
export module m;
import dep;
export import :p;
export inline int call_both(int a) {
    return dep_fn(a) + part_fn(a);
}

--- main.cpp ---
import m;
int main() { return call_both(1); }

$ g++ -std=c++20 -fmodules -c dep.cppm -o dep.o
$ g++ -std=c++20 -fmodules -c part.cppm -o part.o
$ g++ -std=c++20 -fmodules -c m.cppm -o m.o
$ g++ -std=c++20 -fmodules -c main.cpp -o main.o

With --enable-checking=yes:
m.cppm:1:8: internal compiler error: in write_location, at cp/module.cc:18852
    1 | export module m;
      |        ^~~~~~
0x52c378d internal_error(char const*, ...)
        gcc/diagnostic-global-context.cc:787
0x52d48d2 fancy_abort(char const*, int, char const*)
        gcc/diagnostics/context.cc:1813
0x222c17c write_location
        gcc/cp/module.cc:18852

With --enable-checking=release, m.cppm writes an out-of-range offset and
importing it fails:
In module imported at main.cpp:1:1:
m: In function 'int main()':
m: error: failed to read compiled module cluster 2: Bad file data
m: note: compiled module file is 'gcm.cache/m.gcm'
m: error: failed to read compiled module cluster 3: Bad file data
main.cpp:2:21: fatal error: failed to load binding '::call_both@m'

Changing both checks from '>' to '>=' matches loc_spans::macro,
loc_spans::ordinary, and module_state::read_location. I have a patch ready 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