xml-sec 0.2.0

Pure Rust XML Security: XMLDSig, XMLEnc, C14N. Drop-in replacement for libxmlsec1.
Documentation
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
use std::collections::{HashMap, HashSet};
use std::hash::{BuildHasher, Hash};

use super::{Error, Result};

pub(crate) const ENTITY_EXPANSION_DEPTH_CEILING: usize = 256;
// This is an absolute parser safety ceiling, not a deployment policy default.
// Caller budgets may reject smaller documents at the compile/execution boundary.
pub(crate) const ENTITY_EXPANSION_BYTE_CEILING: usize = 16 * 1024 * 1024;
// Compilation still uses bounded native recursion while borrowing frontend nodes. This absolute
// process-safety ceiling only tightens caller policy until module and instruction traversal are
// represented entirely by explicit work stacks.
pub(crate) const COMPILE_RECURSION_DEPTH_CEILING: usize = 256;
// XInclude resolution currently retains one small native frame per nested acquired document.
// This absolute process-safety ceiling only tightens the caller's execution policy.
pub(crate) const XINCLUDE_RECURSION_DEPTH_CEILING: usize = 256;
// Global initialization, capture constructors, attribute-set expansion, and stylesheet-defined
// functions retain native Rust frames. This process-safety ceiling only tightens caller policy;
// iterative template depth is independent of the count of live native sequence frames.
pub(crate) const EXECUTION_RECURSION_DEPTH_CEILING: usize = 256;

/// Independently metered XSLT resource dimensions.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum BudgetKind {
    StylesheetBytes,
    SourceBytes,
    SourceNodes,
    EntityReferences,
    EntityExpansionBytes,
    NamespaceScopeBytes,
    ImportedModules,
    ExternalDocuments,
    RecursionDepth,
    XPathEvaluations,
    XPathOperations,
    ExtensionOperations,
    PatternEvaluations,
    TemplateApplications,
    SortComparisons,
    KeyEntries,
    ResultNodes,
    SerializedBytes,
    Messages,
    OwnedBytes,
    /// XML text scanned by parser passes, one charge per pass over a text.
    ParseWorkBytes,
    /// Every kind of execution work together, against the caller's work ceiling.
    AggregateWork,
}

/// Policy-neutral enforcement limits for parsing one caller-supplied XML document.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ParseBudget {
    /// Maximum decoded XML bytes accepted for one document.
    pub source_bytes: usize,
    /// Maximum semantic nodes, including the synthetic document root.
    pub source_nodes: usize,
    /// Maximum element nesting depth.
    pub recursion_depth: usize,
    /// Maximum declared general and parameter entity-reference occurrences expanded.
    pub entity_references: usize,
    /// Maximum aggregate bytes materialized while expanding entities.
    pub entity_expansion_bytes: usize,
    /// Maximum peak bytes used to materialize inherited namespace scopes and their indexes.
    pub namespace_scope_bytes: usize,
    /// Maximum live bytes of the tree and parser staging, including namespace scopes.
    pub owned_bytes: usize,
    /// Maximum XML text scanned, one charge per parser pass.
    pub parse_work_bytes: usize,
}

impl ParseBudget {
    /// Limits without a tree-size or parser-work ceiling; see [`Self::with_owned_bytes`]
    /// and [`Self::with_parse_work_bytes`].
    #[must_use]
    pub const fn new(
        source_bytes: usize,
        source_nodes: usize,
        recursion_depth: usize,
        entity_references: usize,
        entity_expansion_bytes: usize,
        namespace_scope_bytes: usize,
    ) -> Self {
        Self {
            source_bytes,
            source_nodes,
            recursion_depth,
            entity_references,
            entity_expansion_bytes,
            namespace_scope_bytes,
            owned_bytes: usize::MAX,
            parse_work_bytes: usize::MAX,
        }
    }

    /// Bound live tree and parser staging bytes.
    #[must_use]
    pub const fn with_owned_bytes(mut self, owned_bytes: usize) -> Self {
        self.owned_bytes = owned_bytes;
        self
    }

    /// Bound the XML text scanned by every parser pass together.
    #[must_use]
    pub const fn with_parse_work_bytes(mut self, parse_work_bytes: usize) -> Self {
        self.parse_work_bytes = parse_work_bytes;
        self
    }

    pub(crate) const UNBOUNDED: Self = Self::new(
        usize::MAX,
        usize::MAX,
        usize::MAX,
        usize::MAX,
        usize::MAX,
        usize::MAX,
    );
}

/// Policy-neutral immutable enforcement limits for compiling a stylesheet graph.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct CompileBudget {
    pub stylesheet_bytes: usize,
    pub imported_modules: usize,
    pub recursion_depth: usize,
    pub owned_bytes: usize,
    /// Maximum stylesheet text scanned, one charge per parser pass.
    pub parse_work_bytes: usize,
}

impl CompileBudget {
    /// Limits without a parser-work ceiling; see [`Self::with_parse_work_bytes`].
    #[must_use]
    pub const fn new(
        stylesheet_bytes: usize,
        imported_modules: usize,
        recursion_depth: usize,
        owned_bytes: usize,
    ) -> Self {
        Self {
            stylesheet_bytes,
            imported_modules,
            recursion_depth,
            owned_bytes,
            parse_work_bytes: usize::MAX,
        }
    }

    /// Bound the stylesheet text scanned by every parser pass together.
    #[must_use]
    pub const fn with_parse_work_bytes(mut self, parse_work_bytes: usize) -> Self {
        self.parse_work_bytes = parse_work_bytes;
        self
    }
}

/// The outcome of a budgeted operation and what it consumed, reported whether
/// it succeeded or failed, so a caller sharing one allowance across operations
/// can debit failed work too.
#[derive(Debug)]
pub struct Reported<T, U> {
    pub outcome: Result<T>,
    pub usage: U,
}

/// Parser work one compilation or parse admitted.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct ParseUsage {
    /// Text bytes charged, one charge per parser pass.
    pub parse_work_bytes: usize,
}

/// A parser-work meter: each pass is charged before it scans.
#[derive(Debug, Clone, Copy)]
pub(crate) struct ParseWork {
    limit: usize,
    used: usize,
}

impl ParseWork {
    pub(crate) const fn new(limit: usize) -> Self {
        Self { limit, used: 0 }
    }

    pub(crate) fn charge(&mut self, bytes: usize) -> Result<()> {
        let actual = self.used.checked_add(bytes).ok_or(Error::Budget {
            kind: BudgetKind::ParseWorkBytes,
            limit: self.limit,
            actual: usize::MAX,
        })?;
        ensure(BudgetKind::ParseWorkBytes, self.limit, actual)?;
        self.used = actual;
        Ok(())
    }

    pub(crate) const fn usage(&self) -> ParseUsage {
        ParseUsage {
            parse_work_bytes: self.used,
        }
    }
}

/// Policy-neutral immutable enforcement limits shared by one transformation execution.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ExecutionBudget {
    pub source_bytes: usize,
    pub external_documents: usize,
    pub recursion_depth: usize,
    pub xpath_evaluations: usize,
    /// Maximum primitive traversal, predicate, comparison, and node-set insertion operations.
    pub xpath_operations: usize,
    /// Internal work performed by extension functions after one XPath call is dispatched.
    pub extension_operations: usize,
    pub pattern_evaluations: usize,
    pub template_applications: usize,
    pub sort_comparisons: usize,
    pub key_entries: usize,
    pub result_nodes: usize,
    pub serialized_bytes: usize,
    pub messages: usize,
    pub owned_bytes: usize,
}

/// Work admitted by one execution, whether it succeeded or failed.
///
/// Each counter is the amount the corresponding [`ExecutionBudget`] limit admitted, never more
/// than that limit, so a caller sharing one allowance across executions can pass the remainder
/// as the next budget.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct ExecutionUsage {
    pub external_documents: usize,
    pub xpath_evaluations: usize,
    pub xpath_operations: usize,
    pub extension_operations: usize,
    pub pattern_evaluations: usize,
    pub template_applications: usize,
    pub sort_comparisons: usize,
    pub key_entries: usize,
    pub result_nodes: usize,
    pub serialized_bytes: usize,
    pub messages: usize,
}

#[derive(Debug)]
pub(crate) struct Meter {
    limits: ExecutionBudget,
    xpath_evaluations: usize,
    xpath_operations: usize,
    extension_operations: usize,
    pattern_evaluations: usize,
    template_applications: usize,
    sort_comparisons: usize,
    key_entries: usize,
    result_nodes: usize,
    serialized_bytes: usize,
    messages: usize,
    owned_bytes: usize,
    external_documents: usize,
    work: usize,
    work_ceiling: usize,
}

impl BudgetKind {
    /// Whether the kind counts toward an execution's aggregate work ceiling.
    #[must_use]
    pub const fn is_execution_work(self) -> bool {
        matches!(
            self,
            Self::XPathEvaluations
                | Self::XPathOperations
                | Self::ExtensionOperations
                | Self::PatternEvaluations
                | Self::TemplateApplications
                | Self::SortComparisons
                | Self::KeyEntries
                | Self::ResultNodes
                | Self::Messages
                | Self::SerializedBytes
        )
    }
}

impl Meter {
    pub(crate) fn new(limits: ExecutionBudget, source_bytes: usize) -> Result<Self> {
        ensure(BudgetKind::SourceBytes, limits.source_bytes, source_bytes)?;
        ensure(BudgetKind::OwnedBytes, limits.owned_bytes, source_bytes)?;
        Ok(Self {
            limits,
            xpath_evaluations: 0,
            xpath_operations: 0,
            extension_operations: 0,
            pattern_evaluations: 0,
            template_applications: 0,
            sort_comparisons: 0,
            key_entries: 0,
            result_nodes: 0,
            serialized_bytes: 0,
            messages: 0,
            owned_bytes: source_bytes,
            external_documents: 0,
            work: 0,
            work_ceiling: usize::MAX,
        })
    }

    /// Bound the kinds of [`BudgetKind::is_execution_work`] together.
    pub(crate) const fn with_work_ceiling(mut self, ceiling: usize) -> Self {
        self.work_ceiling = ceiling;
        self
    }

    /// Work the ceiling still admits, across every kind it bounds.
    pub(crate) const fn work_remaining(&self) -> usize {
        // Every admitted total stayed within the ceiling.
        self.work_ceiling - self.work
    }

    /// The ceiling over every kind of execution work together.
    pub(crate) const fn work_ceiling(&self) -> usize {
        self.work_ceiling
    }

    /// The aggregate work total after `amount` more, if the ceiling admits it.
    fn admit_work(&self, kind: BudgetKind, amount: usize) -> Result<usize> {
        if !kind.is_execution_work() {
            return Ok(self.work);
        }
        let actual = self.work.checked_add(amount).ok_or(Error::Budget {
            kind: BudgetKind::AggregateWork,
            limit: self.work_ceiling,
            actual: usize::MAX,
        })?;
        ensure(BudgetKind::AggregateWork, self.work_ceiling, actual)?;
        Ok(actual)
    }

    pub(crate) fn recursion(&self, depth: usize) -> Result<()> {
        ensure(
            BudgetKind::RecursionDepth,
            self.limits.recursion_depth,
            depth,
        )
    }

    pub(crate) fn recursion_with_ceiling(&self, depth: usize, ceiling: usize) -> Result<()> {
        ensure(
            BudgetKind::RecursionDepth,
            self.limits.recursion_depth.min(ceiling),
            depth,
        )
    }

    pub(crate) fn charge(&mut self, kind: BudgetKind, amount: usize) -> Result<()> {
        let work = self.admit_work(kind, amount)?;
        let (used, limit) = match kind {
            BudgetKind::ExternalDocuments => {
                (&mut self.external_documents, self.limits.external_documents)
            }
            BudgetKind::XPathEvaluations => {
                (&mut self.xpath_evaluations, self.limits.xpath_evaluations)
            }
            BudgetKind::XPathOperations => {
                (&mut self.xpath_operations, self.limits.xpath_operations)
            }
            BudgetKind::ExtensionOperations => (
                &mut self.extension_operations,
                self.limits.extension_operations,
            ),
            BudgetKind::PatternEvaluations => (
                &mut self.pattern_evaluations,
                self.limits.pattern_evaluations,
            ),
            BudgetKind::TemplateApplications => (
                &mut self.template_applications,
                self.limits.template_applications,
            ),
            BudgetKind::SortComparisons => {
                (&mut self.sort_comparisons, self.limits.sort_comparisons)
            }
            BudgetKind::KeyEntries => (&mut self.key_entries, self.limits.key_entries),
            BudgetKind::ResultNodes => (&mut self.result_nodes, self.limits.result_nodes),
            BudgetKind::SerializedBytes => {
                (&mut self.serialized_bytes, self.limits.serialized_bytes)
            }
            BudgetKind::Messages => (&mut self.messages, self.limits.messages),
            BudgetKind::OwnedBytes => (&mut self.owned_bytes, self.limits.owned_bytes),
            other => {
                return Err(Error::Dynamic(format!(
                    "{other:?} cannot be charged during execution"
                )));
            }
        };
        let Some(actual) = used.checked_add(amount) else {
            *used = usize::MAX;
            return Err(Error::Budget {
                kind,
                limit,
                actual: usize::MAX,
            });
        };
        ensure(kind, limit, actual)?;
        *used = actual;
        self.work = work;
        Ok(())
    }

    pub(crate) fn check_additional(&self, kind: BudgetKind, amount: usize) -> Result<()> {
        self.admit_work(kind, amount)?;
        let (used, limit) = self.usage(kind)?;
        ensure(kind, limit, used.saturating_add(amount))
    }

    pub(crate) fn release_owned_bytes(&mut self, amount: usize) {
        self.owned_bytes = self
            .owned_bytes
            .checked_sub(amount)
            .expect("released owned-byte reservation was previously charged");
    }

    pub(crate) fn usage(&self, kind: BudgetKind) -> Result<(usize, usize)> {
        match kind {
            BudgetKind::ExternalDocuments => {
                Ok((self.external_documents, self.limits.external_documents))
            }
            BudgetKind::XPathEvaluations => {
                Ok((self.xpath_evaluations, self.limits.xpath_evaluations))
            }
            BudgetKind::XPathOperations => {
                Ok((self.xpath_operations, self.limits.xpath_operations))
            }
            BudgetKind::ExtensionOperations => {
                Ok((self.extension_operations, self.limits.extension_operations))
            }
            BudgetKind::PatternEvaluations => {
                Ok((self.pattern_evaluations, self.limits.pattern_evaluations))
            }
            BudgetKind::TemplateApplications => Ok((
                self.template_applications,
                self.limits.template_applications,
            )),
            BudgetKind::SortComparisons => {
                Ok((self.sort_comparisons, self.limits.sort_comparisons))
            }
            BudgetKind::KeyEntries => Ok((self.key_entries, self.limits.key_entries)),
            BudgetKind::ResultNodes => Ok((self.result_nodes, self.limits.result_nodes)),
            BudgetKind::SerializedBytes => {
                Ok((self.serialized_bytes, self.limits.serialized_bytes))
            }
            BudgetKind::Messages => Ok((self.messages, self.limits.messages)),
            BudgetKind::OwnedBytes => Ok((self.owned_bytes, self.limits.owned_bytes)),
            other => Err(Error::Dynamic(format!(
                "{other:?} cannot be checked during execution"
            ))),
        }
    }

    pub(crate) const fn admitted_work(&self) -> ExecutionUsage {
        // A counter exceeds its limit only after an overflowing charge poisons it; that
        // refusal admitted the whole limit and nothing beyond it.
        const fn admitted(used: usize, limit: usize) -> usize {
            if used > limit { limit } else { used }
        }
        let limits = &self.limits;
        ExecutionUsage {
            external_documents: admitted(self.external_documents, limits.external_documents),
            xpath_evaluations: admitted(self.xpath_evaluations, limits.xpath_evaluations),
            xpath_operations: admitted(self.xpath_operations, limits.xpath_operations),
            extension_operations: admitted(self.extension_operations, limits.extension_operations),
            pattern_evaluations: admitted(self.pattern_evaluations, limits.pattern_evaluations),
            template_applications: admitted(
                self.template_applications,
                limits.template_applications,
            ),
            sort_comparisons: admitted(self.sort_comparisons, limits.sort_comparisons),
            key_entries: admitted(self.key_entries, limits.key_entries),
            result_nodes: admitted(self.result_nodes, limits.result_nodes),
            serialized_bytes: admitted(self.serialized_bytes, limits.serialized_bytes),
            messages: admitted(self.messages, limits.messages),
        }
    }

    pub(crate) fn remaining_owned_bytes(&self) -> usize {
        self.limits.owned_bytes.saturating_sub(self.owned_bytes)
    }

    pub(crate) const fn recursion_limit(&self) -> usize {
        self.limits.recursion_depth
    }
}

pub(crate) fn reserve_temporary_vec_slot<T>(
    items: &mut Vec<T>,
    meter: &mut Meter,
    reserved_owned_bytes: &mut usize,
) -> Result<()> {
    if std::mem::size_of::<T>() == 0 || items.len() < items.capacity() {
        return Ok(());
    }
    let old_capacity = items.capacity();
    let requested_slots = old_capacity.max(4);
    let target_capacity = old_capacity.saturating_add(requested_slots);
    let requested_bytes = target_capacity.saturating_mul(std::mem::size_of::<T>());
    meter.charge(BudgetKind::OwnedBytes, requested_bytes)?;
    let mut replacement = Vec::new();
    if let Err(error) = replacement.try_reserve_exact(target_capacity) {
        meter.release_owned_bytes(requested_bytes);
        return Err(Error::Dynamic(format!(
            "failed to reserve temporary execution storage: {error}"
        )));
    }
    let actual_bytes = replacement
        .capacity()
        .saturating_mul(std::mem::size_of::<T>());
    reconcile_replacement_growth(meter, requested_bytes, actual_bytes)?;

    replacement.append(items);
    std::mem::swap(items, &mut replacement);
    let old_bytes = replacement
        .capacity()
        .saturating_mul(std::mem::size_of::<T>());
    *reserved_owned_bytes = reserved_owned_bytes
        .checked_sub(old_bytes)
        .expect("temporary vector capacity was previously charged")
        .saturating_add(actual_bytes);
    meter.release_owned_bytes(old_bytes);
    Ok(())
}

pub(crate) fn append_metered_string(
    current: &mut String,
    suffix: &str,
    meter: &mut Meter,
) -> Result<()> {
    reserve_metered_string(current, suffix.len(), meter)?;
    current.push_str(suffix);
    Ok(())
}

pub(crate) fn reserve_metered_string(
    current: &mut String,
    additional: usize,
    meter: &mut Meter,
) -> Result<()> {
    let required = current
        .len()
        .checked_add(additional)
        .filter(|length| *length <= isize::MAX as usize)
        .ok_or_else(|| Error::Dynamic("text value is too large".into()))?;
    if required <= current.capacity() {
        return Ok(());
    }

    meter.check_additional(BudgetKind::OwnedBytes, required)?;
    debug_assert!(current.capacity() <= isize::MAX as usize);
    let target_capacity = (current.capacity() * 2)
        .max(required)
        .min(isize::MAX as usize)
        .min(meter.remaining_owned_bytes());
    meter.charge(BudgetKind::OwnedBytes, target_capacity)?;
    let mut replacement = String::new();
    if let Err(error) = replacement.try_reserve_exact(target_capacity) {
        meter.release_owned_bytes(target_capacity);
        return Err(Error::Dynamic(format!(
            "failed to reserve metered text storage: {error}"
        )));
    }
    let actual_capacity = replacement.capacity();
    reconcile_replacement_growth(meter, target_capacity, actual_capacity)?;
    replacement.push_str(current);
    let old_capacity = current.capacity();
    *current = replacement;
    meter.release_owned_bytes(old_capacity);
    Ok(())
}

pub(crate) fn reconcile_replacement_growth(
    meter: &mut Meter,
    requested_bytes: usize,
    actual_bytes: usize,
) -> Result<()> {
    if actual_bytes < requested_bytes {
        meter.release_owned_bytes(requested_bytes - actual_bytes);
    } else if actual_bytes > requested_bytes {
        let shortfall = actual_bytes - requested_bytes;
        if let Err(error) = meter.check_additional(BudgetKind::OwnedBytes, shortfall) {
            meter.release_owned_bytes(requested_bytes);
            return Err(error);
        }
        let charged_before_shortfall = meter.owned_bytes;
        if let Err(error) = meter.charge(BudgetKind::OwnedBytes, shortfall) {
            meter.owned_bytes = charged_before_shortfall;
            meter.release_owned_bytes(requested_bytes);
            return Err(error);
        }
    }
    Ok(())
}

pub(crate) fn retained_hash_storage<T>(capacity: usize) -> usize {
    capacity
        .saturating_mul(std::mem::size_of::<T>())
        .saturating_mul(2)
}

pub(crate) fn reserve_retained_hash_set_slot<T, S>(
    items: &mut HashSet<T, S>,
    meter: &mut Meter,
    reserved_owned_bytes: &mut usize,
) -> Result<()>
where
    T: Eq + Hash,
    S: BuildHasher + Clone,
{
    if items.len() < items.capacity() {
        return Ok(());
    }
    let old_capacity = items.capacity();
    let target_capacity = old_capacity.saturating_add(old_capacity.max(4));
    let requested_bytes = retained_hash_storage::<T>(target_capacity);
    meter.charge(BudgetKind::OwnedBytes, requested_bytes)?;
    let mut replacement = HashSet::with_hasher(items.hasher().clone());
    if let Err(error) = replacement.try_reserve(target_capacity) {
        meter.release_owned_bytes(requested_bytes);
        return Err(Error::Dynamic(format!(
            "failed to reserve retained hash-set storage: {error}"
        )));
    }
    let actual_bytes = retained_hash_storage::<T>(replacement.capacity());
    reconcile_replacement_growth(meter, requested_bytes, actual_bytes)?;
    replacement.extend(items.drain());
    std::mem::swap(items, &mut replacement);
    let old_bytes = retained_hash_storage::<T>(replacement.capacity());
    *reserved_owned_bytes = reserved_owned_bytes
        .checked_sub(old_bytes)
        .expect("retained hash-set capacity was previously charged")
        .saturating_add(actual_bytes);
    meter.release_owned_bytes(old_bytes);
    Ok(())
}

/// Grow retained vector storage through the same peak-allocation gate as temporary vectors.
pub(crate) fn reserve_retained_vec_slot<T>(items: &mut Vec<T>, meter: &mut Meter) -> Result<()> {
    let mut reserved_owned_bytes = items.capacity().saturating_mul(std::mem::size_of::<T>());
    reserve_temporary_vec_slot(items, meter, &mut reserved_owned_bytes)
}

pub(crate) fn reserve_retained_hash_map_slot<K, V, S>(
    items: &mut HashMap<K, V, S>,
    meter: &mut Meter,
    reserved_owned_bytes: &mut usize,
) -> Result<()>
where
    K: Eq + Hash,
    S: BuildHasher + Clone,
{
    if items.len() < items.capacity() {
        return Ok(());
    }
    let old_capacity = items.capacity();
    let target_capacity = old_capacity.saturating_add(old_capacity.max(4));
    let requested_bytes = retained_hash_storage::<(K, V)>(target_capacity);
    meter.charge(BudgetKind::OwnedBytes, requested_bytes)?;
    let mut replacement = HashMap::with_hasher(items.hasher().clone());
    if let Err(error) = replacement.try_reserve(target_capacity) {
        meter.release_owned_bytes(requested_bytes);
        return Err(Error::Dynamic(format!(
            "failed to reserve retained hash-map storage: {error}"
        )));
    }
    let actual_bytes = retained_hash_storage::<(K, V)>(replacement.capacity());
    reconcile_replacement_growth(meter, requested_bytes, actual_bytes)?;
    replacement.extend(items.drain());
    std::mem::swap(items, &mut replacement);
    let old_bytes = retained_hash_storage::<(K, V)>(replacement.capacity());
    *reserved_owned_bytes = reserved_owned_bytes
        .checked_sub(old_bytes)
        .expect("retained hash-map capacity was previously charged")
        .saturating_add(actual_bytes);
    meter.release_owned_bytes(old_bytes);
    Ok(())
}

pub(crate) fn ensure(kind: BudgetKind, limit: usize, actual: usize) -> Result<()> {
    if actual > limit {
        return Err(Error::Budget {
            kind,
            limit,
            actual,
        });
    }
    Ok(())
}

#[cfg(all(test, not(feature = "embedded")))]
mod tests;