Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Functional algorithms allow to mismatch collection types #274

Closed
1 task done
tmadlener opened this issue Jan 17, 2025 · 0 comments · Fixed by #275
Closed
1 task done

Functional algorithms allow to mismatch collection types #274

tmadlener opened this issue Jan 17, 2025 · 0 comments · Fixed by #275
Labels
bug Something isn't working

Comments

@tmadlener
Copy link
Contributor

tmadlener commented Jan 17, 2025

Check duplicate issues.

  • Checked for duplicates

Issue

It is possible to run Functional algorithms with mismatched input types without any check catching that at runtime. I.e. if users put in a wrong collection name by accident, things will ideally explode without an apparent reason why or, even worse, run but produce garbage.

Operating System and Version

all

compiler

all

The version of the key4hep stack

nightlies

Package Version

main

Reproducer

As a minimal reproducer consider the following transformer

struct TypeMisMatchDemo final : k4FWCore::Transformer<edm4hep::MCParticleCollection(const edm4hep::TrackCollection&)> {
  TypeMisMatchDemo(const std::string& name, ISvcLocator* svcLoc)
      : Transformer(name, svcLoc, {KeyValues("InputCollection", {"MCParticles"})},
                    {KeyValues("OutputCollection", {"MCParticles2"})}) {}

  edm4hep::MCParticleCollection operator()(const edm4hep::TrackCollection& inputs) const final {
    debug() << inputs.size() << " type = " << inputs.getTypeName() << endmsg;
    auto track = inputs[0];
    // The next line goes boom if the input is not really a TrackerHitCollection
    debug() << track.getTrackerHits().size() << endmsg;

    return edm4hep::MCParticleCollection{};
  }
};

if we pair this with the following python config:

from Gaudi.Configuration import INFO, DEBUG
from Configurables import ExampleFunctionalProducer, TypeMisMatchDemo, EventDataSvc

from k4FWCore import ApplicationMgr

producer = ExampleFunctionalProducer("Producer", OutputCollection=["MCParticles"])

mismatch = TypeMisMatchDemo(InputCollection=["MCParticles"], OutputLevel=DEBUG)

ApplicationMgr(
    TopAlg=[producer, mismatch],
    EvtSel="NONE",
    EvtMax=1,
    ExtSvc=[EventDataSvc("EventDataSvc")],
    OutputLevel=INFO,
)

We get a segmentation fault, because inputs is not actually a edm4hep::TrackerHitCollection, but rather a edm4hep::MCParticleCollection. Depending on which functions we call on the input collection and its members, we either get a segmentation fault (yay) or simply apparently run without problems (but most likely not with what we intended to to).

I have added the simple reproducer as a test case on https://github.com/tmadlener/K4FWCore/tree/mismatch-repro

Additional context

The underlying issue is the static_cast here:

std::get<Index>(inputTuple) = static_cast<std::tuple_element_t<Index, std::tuple<In...>>*>(in);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant