bridge.utils.safe_pickle#
Module Contents#
Classes#
Unpickler that only allows safe built-in types to prevent arbitrary code execution. |
|
Unpickler that allows safe builtins and the narrow set of numpy types needed for object array reconstruction. |
|
Unpickler for Energon dataloader state files ( |
Functions#
Load an Energon dataloader state |
|
Deserialize from a file using a restricted unpickler that only allows safe types. |
|
Deserialize pickle data using a restricted unpickler that only allows safe types. |
|
Load a |
Data#
API#
- bridge.utils.safe_pickle._BUILTIN_SAFE_TYPES#
‘frozenset(…)’
- class bridge.utils.safe_pickle._RestrictedUnpickler#
Bases:
pickle.UnpicklerUnpickler that only allows safe built-in types to prevent arbitrary code execution.
Initialization
Initialize self. See help(type(self)) for accurate signature.
- _SAFE_MODULES#
‘MappingProxyType(…)’
- find_class(module: str, name: str) type#
- class bridge.utils.safe_pickle._NumpyRestrictedUnpickler#
Bases:
pickle.UnpicklerUnpickler that allows safe builtins and the narrow set of numpy types needed for object array reconstruction.
NumPy object arrays (dtype=’O’) are serialized via pickle inside
.npyfiles. The pickle stream referencesnumpy.core.multiarray._reconstruct,numpy.ndarray, andnumpy.dtypeto rebuild the array container, while the elements (dicts, lists, ints, …) use only standard builtins.This unpickler permits exactly those types and nothing else — in particular,
os,subprocess,builtins.eval, etc. are blocked, preventing arbitrary-code-execution attacks via crafted.npyfiles.Initialization
Initialize self. See help(type(self)) for accurate signature.
- _SAFE_MODULES#
‘MappingProxyType(…)’
- find_class(module: str, name: str) type#
- class bridge.utils.safe_pickle._EnergonUnpickler#
Bases:
bridge.utils.safe_pickle._NumpyRestrictedUnpicklerUnpickler for Energon dataloader state files (
.pt).Extends the NumPy-safe unpickler with the exact Energon dataclass types that Energon serialises into dataloader checkpoint files. All other globals — including
os,subprocess, and any__reduce__payload callable outside this allowlist — are blocked, preventing arbitrary code execution from attacker-controlled checkpoint files.Use via :func:
energon_torch_loadrather than instantiating directly.Initialization
Initialize self. See help(type(self)) for accurate signature.
- _SAFE_MODULES: types.MappingProxyType#
‘MappingProxyType(…)’
- find_class(module: str, name: str) type#
- bridge.utils.safe_pickle.energon_torch_load(path: str, *, map_location: str = 'cpu') object#
Load an Energon dataloader state
.ptfile through a restricted unpickler.Parses the torch zip format directly without calling
torch.load. Security is enforced by :class:_EnergonUnpickler: any GLOBAL opcode whose(module, name)is not in the explicit allowlist raisespickle.UnpicklingError, blocking__reduce__-based code execution from attacker-controlled checkpoint files.torch.load(weights_only=True)is not used because PyTorch ≥ 2.13 restricts SETITEM/SETITEMS to exactdict,OrderedDict, andCountertypes, rejecting dict subclasses such as Energon’sFlexState— which is always present in real Energon checkpoints (SavableDatasetState.dataset_stateis typedFlexState, not Optional).torch.savewrites a zip archive whose directory prefix is the file stem. This function opens the zip, runs :class:_EnergonUnpickleron the pickle stream, and reconstructs tensor storages from the raw blobs viapersistent_load. Storages are cached by key so that tensors sharing a storage (views, slices) remain aliased after restore.- Parameters:
path –
Path to the
.ptfile written by- func:
~megatron.bridge.training.checkpointing.maybe_save_dataloader_state.
map_location – Device to map tensor storages to; defaults to
"cpu"to avoid GPU allocation during restore.
- Returns:
The deserialized object (a
dictcontaining"dataloader_state_dict").
- bridge.utils.safe_pickle.safe_pickle_load(fp) object#
Deserialize from a file using a restricted unpickler that only allows safe types.
- bridge.utils.safe_pickle.safe_pickle_loads(data: bytes) object#
Deserialize pickle data using a restricted unpickler that only allows safe types.
- bridge.utils.safe_pickle.safe_load_npy(data: bytes)#
Load a
.npyfile from raw bytes without enabling unrestricted pickle.For numeric arrays the fast
allow_pickle=Falsepath is used. For object arrays (packed datasets storing dicts of variable-length lists) the pickle payload is deserialized through :class:_NumpyRestrictedUnpickler, which blocks dangerous modules likeosandsubprocess.- Parameters:
data – Raw bytes of a
.npyfile.- Returns:
numpy.ndarray loaded from the file.