Skip to content

MOOSE adaptor ignores tensor dim — silent miscompute when dim != 3 #7

Open

Description

@petlenz

Summary

MooseMaterialTarget validates tensor rank (only 2 and 4 allowed) but never validates dim. A recipe with add_tensor_input("eps", 2, 2) produces:

  • C++ that compiles.
  • A tmech::adaptor<double const, 2, 2, tmech::full<2>> spanning 4 elements over a 9-double MOOSE buffer — silently wrong values.

With dim=5 the adaptor reads/writes past the buffer end → undefined behavior.

Where

  • Adaptors built with raw i.dim / o.dim: include/numsim_codegen/targets/moose_material.h:180-182, 191-193
  • Rank check (no dim check): include/numsim_codegen/targets/moose_material.h:256-263

Fix

Reject dim != 3 in MOOSE pre-flight inside emit(), alongside the existing History check:

for (auto const &s : model.symbols()) {
    if (s.kind == SymbolDecl::Kind::Tensor && s.dim != 3) {
        throw std::runtime_error(
            "MooseMaterialTarget: tensor '" + s.name + "' has dim=" +
            std::to_string(s.dim) + "; MOOSE RankNTensor storage is dim=3 only.");
    }
}
// Repeat for outputs.

Ideally this lives behind a generic Target::validate_recipe() hook (see the architecture follow-up issue), but the local check unblocks the bug today.

Refs

  • REVIEW.md → CORR-A2 (High) and CPP-2
  • Severity: High

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions