1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/// Resource limits applied while opening and reading a dataset.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct ReadLimits {
max_source_bytes: usize,
max_working_bytes: usize,
max_region_bytes: usize,
max_materialized_bytes: usize,
max_metadata_bytes: usize,
max_component_lanes: usize,
max_transform_coefficients: usize,
max_modulation_period: usize,
max_transform_work: usize,
retained_working_bytes: usize,
}
impl ReadLimits {
/// Returns the default limits.
pub fn new() -> Self {
Self::default()
}
/// Sets the maximum combined size of all numeric sample files.
pub fn max_source_bytes(mut self, value: usize) -> Self {
self.max_source_bytes = value;
self
}
/// Sets a bound on simultaneous temporary decoding and mapping buffers.
/// Includes owned numeric input bytes, intermediate samples and data indexes.
/// Lazy Reader accesses include the final region/materialized sample buffer,
/// adapter decoding buffers and any cropped trace that coexist. Trace,
/// region, layout and observation container payloads are also included.
/// Separate region/materialized limits also apply. Other complete-read entry points
/// retain their documented decoding estimates. Bruker raw additionally
/// checks combined parameter-table expansion before parsing, including
/// owned path text, and retains the parsed-table charge during decoding
/// and Reader access. Lazy Bruker NUS also charges its schedule and row map.
/// Lazy Bruker source records include their owned roles and locator paths.
/// Its boxed adapter is included as well.
/// Lazy Bruker descriptors reserve axes, text, evidence and component transforms
/// before construction, conservatively retaining their construction bound.
/// Borrowed caller input,
/// other retained metadata structures, allocator overhead and fixed-size stack
/// state are excluded; this is not yet the shared full-memory contract and
/// is not a bound on process RSS or on concurrent calls combined.
pub fn max_working_bytes(mut self, value: usize) -> Self {
self.max_working_bytes = value;
self
}
/// Sets the maximum allocation produced by one region read.
pub fn max_region_bytes(mut self, value: usize) -> Self {
self.max_region_bytes = value;
self
}
/// Sets the maximum allocation produced by materializing a complete dataset.
pub fn max_materialized_bytes(mut self, value: usize) -> Self {
self.max_materialized_bytes = value;
self
}
/// Sets the maximum combined metadata input size.
pub fn max_metadata_bytes(mut self, value: usize) -> Self {
self.max_metadata_bytes = value;
self
}
/// Sets the maximum number of raw lanes on any one axis.
pub fn max_component_lanes(mut self, value: usize) -> Self {
self.max_component_lanes = value;
self
}
/// Sets the maximum coefficient count of any resolved component transform.
pub fn max_transform_coefficients(mut self, value: usize) -> Self {
self.max_transform_coefficients = value;
self
}
/// Sets the maximum periodic modulation length in phases.
pub fn max_modulation_period(mut self, value: usize) -> Self {
self.max_modulation_period = value;
self
}
/// Sets the maximum charged component-transform multiply-accumulate work.
pub fn max_transform_work(mut self, value: usize) -> Self {
self.max_transform_work = value;
self
}
/// Returns the combined numeric-sample input limit.
pub fn source_bytes(&self) -> usize {
self.max_source_bytes
}
/// Returns the working-memory limit.
pub fn working_bytes(&self) -> usize {
self.max_working_bytes
}
/// Returns the per-region output limit.
pub fn region_bytes(&self) -> usize {
self.max_region_bytes
}
/// Returns the complete-dataset output limit.
pub fn materialized_bytes(&self) -> usize {
self.max_materialized_bytes
}
/// Returns the metadata limit.
pub fn metadata_bytes(&self) -> usize {
self.max_metadata_bytes
}
/// Returns the per-axis raw lane limit.
pub fn component_lanes(&self) -> usize {
self.max_component_lanes
}
/// Returns the component-transform coefficient limit.
pub fn transform_coefficients(&self) -> usize {
self.max_transform_coefficients
}
/// Returns the periodic modulation phase limit.
pub fn modulation_period(&self) -> usize {
self.max_modulation_period
}
/// Returns the component-transform work limit.
pub fn transform_work(&self) -> usize {
self.max_transform_work
}
pub(crate) fn with_retained_working(mut self, bytes: usize) -> Result<Self, crate::ReadError> {
self.retained_working_bytes = self
.retained_working_bytes
.checked_add(bytes)
.ok_or(crate::ReadError::SizeOverflow)?;
self.check_working(crate::raw::ReadResource::WorkingBytes, 0)?;
Ok(self)
}
pub(crate) fn check_working(
&self,
resource: crate::raw::ReadResource,
temporary_bytes: usize,
) -> Result<(), crate::ReadError> {
let required = self
.retained_working_bytes
.checked_add(temporary_bytes)
.ok_or(crate::ReadError::SizeOverflow)?;
if required > self.max_working_bytes {
return Err(crate::ReadError::limit(
resource,
self.max_working_bytes,
required,
));
}
Ok(())
}
pub(crate) fn validate_descriptor(
&self,
descriptor: &crate::raw::RawDescriptor,
) -> Result<(), crate::ReadError> {
use crate::raw::{IndirectComponents, ReadResource};
let logical_points = descriptor
.layout()
.logical_shape()
.iter()
.try_fold(1usize, |total, &points| total.checked_mul(points))
.ok_or(crate::ReadError::SizeOverflow)?;
for axis in descriptor.axes() {
let lanes = axis.component_lanes();
if lanes > self.max_component_lanes {
return Err(crate::ReadError::limit(
ReadResource::ComponentLanes,
self.max_component_lanes,
lanes,
));
}
if let crate::raw::RawAxisKind::Indirect(IndirectComponents::Encoded(resolved)) =
axis.kind()
{
let transform = resolved.transform();
if transform.coefficients().len() > self.max_transform_coefficients {
return Err(crate::ReadError::limit(
ReadResource::TransformCoefficients,
self.max_transform_coefficients,
transform.coefficients().len(),
));
}
if transform.modulation().period() > self.max_modulation_period {
return Err(crate::ReadError::limit(
ReadResource::ModulationPeriod,
self.max_modulation_period,
transform.modulation().period(),
));
}
let work = logical_points
.checked_mul(transform.input_lanes())
.and_then(|value| value.checked_mul(2))
.ok_or(crate::ReadError::SizeOverflow)?;
if work > self.max_transform_work {
return Err(crate::ReadError::limit(
ReadResource::TransformWork,
self.max_transform_work,
work,
));
}
}
}
Ok(())
}
}
impl Default for ReadLimits {
fn default() -> Self {
Self {
max_source_bytes: 1024 * 1024 * 1024,
max_working_bytes: 512 * 1024 * 1024,
max_region_bytes: 512 * 1024 * 1024,
max_materialized_bytes: 512 * 1024 * 1024,
max_metadata_bytes: 64 * 1024 * 1024,
max_component_lanes: 64,
max_transform_coefficients: 128,
max_modulation_period: 1024,
max_transform_work: 1_000_000_000,
retained_working_bytes: 0,
}
}
}