Skip to content

Commit ff506fe

Browse files
prateek9623vcsajjansoumitra-chatterjee-technilapateamd-anchaudh
committed
Validate the plugin contract before rewriting anything
A plugin whose SUBSTITUTIONS is not a mapping passed through unnoticed: `key in substitutions` is a substring test on a string and a membership test on any container, so nothing matched and the build emitted the unmodified CUDA lists into a ROCm wheel, at exit 0. Injecting SUBSTITUTIONS = "not a dict" produced a requires.txt carrying cucim-cu12, cucim-cu13 and nvidia-ml-py alongside the rocm requirement. That is precisely the failure VendorPluginError exists to prevent. It only covered plugins that fail to import, not plugins that import cleanly and are then malformed. _load now checks the contract after importing -- SUBSTITUTIONS is a Mapping, extras_for and extra_requirements are callable -- and raises VendorPluginError otherwise. All four injected faults (SyntaxError, raise at import, raise while computing the requirement, malformed table) now fail the ROCm build while CPU and NVIDIA metadata stay byte-identical to dev. Signed-off-by: Prateek Chokse <prateek.chokse@amd.com> Co-authored-by: Vikas C Sajjan <Vikas.CSajjan@amd.com> Co-authored-by: Soumitra Chatterjee <Soumitra.Chatterjee@amd.com> Co-authored-by: Nilaykumar K Patel <NilaykumarKantibhai.Patel@amd.com> Co-authored-by: Anik Chaudhuri <Anik.Chaudhuri@amd.com>
1 parent 732a952 commit ff506fe

2 files changed

Lines changed: 49 additions & 2 deletions

File tree

‎monai/config/vendor_deps.py‎

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
import re
4747
import sys
4848
import warnings
49-
from collections.abc import Callable
49+
from collections.abc import Callable, Mapping
5050
from dataclasses import dataclass
5151
from typing import Any
5252

@@ -214,12 +214,28 @@ def apply_to_optional_dependencies(self, optional_dependencies: dict[str, list[s
214214
return apply_to_optional_dependencies(optional_dependencies, self.plugin)
215215

216216

217+
def _validate(name: str, plugin: Any) -> None:
218+
"""Check the plugin contract before anything is rewritten with it.
219+
220+
A malformed table does not raise where it is used: ``key in substitutions`` is a substring test
221+
on a string and a membership test on any container, so a plugin whose ``SUBSTITUTIONS`` is not a
222+
mapping would quietly match nothing and ship another vendor's packages.
223+
"""
224+
if not isinstance(getattr(plugin, "SUBSTITUTIONS", None), Mapping):
225+
raise VendorPluginError(f"{name} plugin SUBSTITUTIONS must be a mapping")
226+
for attribute in ("extras_for", "extra_requirements"):
227+
if not callable(getattr(plugin, attribute, None)):
228+
raise VendorPluginError(f"{name} plugin must define {attribute}()")
229+
230+
217231
def _load(name: str) -> ActiveVendor:
218232
"""Import the plugin for ``name``; a failure here is fatal, never a silent "no vendor"."""
219233
try:
220-
return ActiveVendor(name, _import_plugin(REGISTRY[name].module))
234+
plugin = _import_plugin(REGISTRY[name].module)
221235
except Exception as exc:
222236
raise VendorPluginError(f"{name} was detected but its plugin could not be loaded: {exc}") from exc
237+
_validate(name, plugin)
238+
return ActiveVendor(name, plugin)
223239

224240

225241
def active_vendor() -> ActiveVendor | None:

‎tests/config/test_vendor_deps.py‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,37 @@ def test_explicit_vendor_that_cannot_load_is_fatal(self):
140140
active_vendor()
141141

142142

143+
class TestPluginValidation(unittest.TestCase):
144+
"""A plugin that imports cleanly but is malformed must not reach the rewrites.
145+
146+
``key in substitutions`` is a substring test on a string, so a non-mapping table would match
147+
nothing and silently emit the unmodified lists into this vendor's wheel.
148+
"""
149+
150+
def _load_with(self, **attributes):
151+
plugin = types.SimpleNamespace(SUBSTITUTIONS={}, extras_for=lambda name: [], extra_requirements=list)
152+
for key, value in attributes.items():
153+
setattr(plugin, key, value)
154+
with (
155+
patch.dict("os.environ", {"MONAI_VENDOR": "rocm"}),
156+
patch.object(vendor_deps, "_import_plugin", return_value=plugin),
157+
):
158+
return active_vendor()
159+
160+
def test_well_formed_plugin_loads(self):
161+
self.assertEqual("rocm", self._load_with().name)
162+
163+
def test_substitutions_must_be_a_mapping(self):
164+
for table in ("not a dict", ["cucim-cu12"], None, 7):
165+
with self.subTest(table=table), self.assertRaises(vendor_deps.VendorPluginError):
166+
self._load_with(SUBSTITUTIONS=table)
167+
168+
def test_required_callables_are_checked(self):
169+
for attribute in ("extras_for", "extra_requirements"):
170+
with self.subTest(attribute=attribute), self.assertRaises(vendor_deps.VendorPluginError):
171+
self._load_with(**{attribute: "not callable"})
172+
173+
143174
class TestHardwareWithoutVendor(unittest.TestCase):
144175
"""``pip`` build isolation can hand the build a generic torch while sitting on vendor hardware."""
145176

0 commit comments

Comments
 (0)