ristretto_classfile 0.31.0

A library for reading, writing and verifying Java classfiles.
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
//! # Verification Cache
//!
//! This module provides caching for verification artifacts to avoid
//! redundant parsing and computation.
//!
//! # Cached Artifacts
//!
//! - Decoded `StackMapTable` per method
//! - Parsed method descriptors
//! - Per-method verification results
//! - Constant pool decoding helpers
//!
//! # Thread Safety
//!
//! The cache uses `RwLock` for thread-safe access when enabled.
//!
//! # References
//!
//! - [JVMS ยง4.10 - Verification of class Files](https://docs.oracle.com/javase/specs/jvms/se25/html/jvms-4.html#jvms-4.10)

use ahash::AHashMap;
use hashbrown::Equivalent;
use std::borrow::Cow;
use std::hash::{Hash, Hasher};
use std::sync::RwLock;

use crate::FieldType;
use crate::java_string::{JavaStr, JavaString};

/// Cached verification result for a method.
#[derive(Debug, Clone)]
pub enum CachedResult {
    /// Verification succeeded.
    Success,
    /// Verification failed with the given error message.
    Failed(String),
}

/// Key for identifying a method in the cache.
///
/// Uses `Cow<'a, JavaStr>` fields so that lookups can borrow directly from
/// the constant pool (`MethodKey<'borrowed>`) while the cache stores
/// fully owned keys (`MethodKey<'static>`).
#[derive(Debug, Clone, Eq, PartialEq, Hash)]
pub struct MethodKey<'a> {
    /// Class name.
    pub class_name: Cow<'a, JavaStr>,
    /// Method name.
    pub method_name: Cow<'a, JavaStr>,
    /// Method descriptor.
    pub descriptor: Cow<'a, JavaStr>,
}

/// Wrapper type for zero-allocation cache lookups.
///
/// Uses a separate type to avoid coherence conflicts with the blanket
/// `impl<Q, K> Equivalent<K> for Q where Q: Eq, K: Borrow<Q>` impl
/// in the `equivalent` crate.
struct MethodKeyLookup<'a, 'b>(&'a MethodKey<'b>);

impl Hash for MethodKeyLookup<'_, '_> {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.0.hash(state);
    }
}

impl Equivalent<MethodKey<'static>> for MethodKeyLookup<'_, '_> {
    fn equivalent(&self, key: &MethodKey<'static>) -> bool {
        self.0.class_name == key.class_name
            && self.0.method_name == key.method_name
            && self.0.descriptor == key.descriptor
    }
}

impl MethodKey<'static> {
    /// Creates a new owned method key (allocates three `JavaString`s).
    pub fn new(
        class_name: impl Into<JavaString>,
        method_name: impl Into<JavaString>,
        descriptor: impl Into<JavaString>,
    ) -> Self {
        Self {
            class_name: Cow::Owned(class_name.into()),
            method_name: Cow::Owned(method_name.into()),
            descriptor: Cow::Owned(descriptor.into()),
        }
    }
}

impl<'a> MethodKey<'a> {
    /// Creates a method key by borrowing from `&JavaStr` references.
    pub fn borrowed(
        class_name: &'a JavaStr,
        method_name: &'a JavaStr,
        descriptor: &'a JavaStr,
    ) -> Self {
        Self {
            class_name: Cow::Borrowed(class_name),
            method_name: Cow::Borrowed(method_name),
            descriptor: Cow::Borrowed(descriptor),
        }
    }

    /// Converts this key into an owned `MethodKey<'static>` suitable for cache storage.
    /// Only allocates when the `Cow` fields are borrowed; owned fields are moved.
    pub fn into_owned(self) -> MethodKey<'static> {
        MethodKey {
            class_name: Cow::Owned(self.class_name.into_owned()),
            method_name: Cow::Owned(self.method_name.into_owned()),
            descriptor: Cow::Owned(self.descriptor.into_owned()),
        }
    }
}

/// Parsed method descriptor cache entry.
#[derive(Debug, Clone)]
pub struct ParsedDescriptor {
    /// Parameter types.
    pub parameters: Vec<FieldType>,
    /// Return type (None for void).
    pub return_type: Option<FieldType>,
}

/// Verification cache for reusing parsed artifacts.
///
/// This cache stores verification-related artifacts that are expensive
/// to compute, allowing them to be reused across multiple verification
/// passes or class loads.
#[derive(Debug, Default)]
pub struct VerificationCache {
    /// Whether caching is enabled.
    enabled: bool,

    /// Cached verification results keyed by `MethodKey<'static>`.
    ///
    /// Uses `hashbrown::HashMap` with `Equivalent` trait to allow
    /// zero-allocation lookups using a borrowed `MethodKey<'_>`.
    results: RwLock<hashbrown::HashMap<MethodKey<'static>, CachedResult, ahash::RandomState>>,

    /// Cached parsed descriptors.
    descriptors: RwLock<AHashMap<String, ParsedDescriptor>>,

    /// Cache statistics.
    stats: RwLock<CacheStats>,
}

/// Cache statistics for monitoring.
#[derive(Debug, Default, Clone)]
pub struct CacheStats {
    /// Number of cache hits for results.
    pub result_hits: u64,
    /// Number of cache misses for results.
    pub result_misses: u64,
    /// Number of cache hits for descriptors.
    pub descriptor_hits: u64,
    /// Number of cache misses for descriptors.
    pub descriptor_misses: u64,
}

impl VerificationCache {
    /// Creates a new verification cache.
    #[must_use]
    pub fn new(enabled: bool) -> Self {
        Self {
            enabled,
            results: RwLock::new(hashbrown::HashMap::with_hasher(
                ahash::RandomState::default(),
            )),
            descriptors: RwLock::new(AHashMap::default()),
            stats: RwLock::new(CacheStats::default()),
        }
    }

    /// Creates a disabled cache (no-op).
    #[must_use]
    pub fn disabled() -> Self {
        Self::new(false)
    }

    /// Returns whether caching is enabled.
    #[must_use]
    pub fn is_enabled(&self) -> bool {
        self.enabled
    }

    /// Gets a cached verification result.
    ///
    /// Uses the `Equivalent` trait to look up a `MethodKey<'static>` in the cache
    /// using a borrowed `MethodKey<'_>`, achieving zero-allocation lookups.
    #[must_use]
    pub fn get_result(&self, key: &MethodKey<'_>) -> Option<CachedResult> {
        if !self.enabled {
            return None;
        }

        let guard = self.results.read().ok()?;
        let result = guard.get(&MethodKeyLookup(key)).cloned();

        drop(guard);

        // Update stats
        if let Ok(mut stats) = self.stats.write() {
            if result.is_some() {
                stats.result_hits += 1;
            } else {
                stats.result_misses += 1;
            }
        }

        result
    }

    /// Stores a verification result in the cache.
    ///
    /// Converts the key to an owned `MethodKey<'static>` for storage.
    /// This allocation only occurs on cache misses.
    pub fn put_result(&self, key: &MethodKey<'_>, result: CachedResult) {
        if !self.enabled {
            return;
        }

        let owned_key = key.clone().into_owned();
        if let Ok(mut guard) = self.results.write() {
            guard.insert(owned_key, result);
        }
    }

    /// Gets a cached parsed descriptor.
    #[must_use]
    pub fn get_descriptor(&self, descriptor: &str) -> Option<ParsedDescriptor> {
        if !self.enabled {
            return None;
        }

        let guard = self.descriptors.read().ok()?;
        let result = guard.get(descriptor).cloned();

        drop(guard);

        // Update stats
        if let Ok(mut stats) = self.stats.write() {
            if result.is_some() {
                stats.descriptor_hits += 1;
            } else {
                stats.descriptor_misses += 1;
            }
        }

        result
    }

    /// Parses and caches a method descriptor.
    pub fn parse_descriptor(&self, descriptor: &str) -> Option<ParsedDescriptor> {
        // Check cache first
        if let Some(cached) = self.get_descriptor(descriptor) {
            return Some(cached);
        }

        // Parse the descriptor
        let java_descriptor = JavaStr::cow_from_str(descriptor);
        let (parameters, return_type) =
            FieldType::parse_method_descriptor(&java_descriptor).ok()?;

        let parsed = ParsedDescriptor {
            parameters,
            return_type,
        };

        // Cache it
        if self.enabled
            && let Ok(mut guard) = self.descriptors.write()
        {
            guard.insert(descriptor.to_string(), parsed.clone());
        }

        Some(parsed)
    }

    /// Gets cache statistics.
    #[must_use]
    pub fn stats(&self) -> CacheStats {
        self.stats.read().map(|s| s.clone()).unwrap_or_default()
    }

    /// Clears all cached data.
    pub fn clear(&self) {
        if let Ok(mut guard) = self.results.write() {
            guard.clear();
        }
        if let Ok(mut guard) = self.descriptors.write() {
            guard.clear();
        }
        if let Ok(mut stats) = self.stats.write() {
            *stats = CacheStats::default();
        }
    }

    /// Returns the number of cached results.
    #[must_use]
    pub fn result_count(&self) -> usize {
        self.results.read().map_or(0, |g| g.len())
    }

    /// Returns the number of cached descriptors.
    #[must_use]
    pub fn descriptor_count(&self) -> usize {
        self.descriptors.read().map_or(0, |g| g.len())
    }
}

/// Arena-style allocator for verification frames.
///
/// This provides a pool of reusable frame buffers to reduce allocation
/// overhead during verification.
#[derive(Debug)]
pub struct FramePool {
    /// Pool of reusable local variable buffers.
    locals_pool: Vec<Vec<crate::verifiers::bytecode::type_system::VerificationType>>,
    /// Pool of reusable stack buffers.
    stack_pool: Vec<Vec<crate::verifiers::bytecode::type_system::VerificationType>>,
    /// Maximum pool size.
    max_size: usize,
}

impl FramePool {
    /// Creates a new frame pool with the given maximum size.
    #[must_use]
    pub fn new(max_size: usize) -> Self {
        Self {
            locals_pool: Vec::with_capacity(max_size),
            stack_pool: Vec::with_capacity(max_size),
            max_size,
        }
    }

    /// Acquires a locals buffer from the pool.
    pub fn acquire_locals(
        &mut self,
        capacity: usize,
    ) -> Vec<crate::verifiers::bytecode::type_system::VerificationType> {
        if let Some(mut buffer) = self.locals_pool.pop() {
            buffer.clear();
            if buffer.capacity() < capacity {
                buffer.reserve(capacity);
            }
            buffer
        } else {
            Vec::with_capacity(capacity)
        }
    }

    /// Returns a locals buffer to the pool.
    pub fn return_locals(
        &mut self,
        buffer: Vec<crate::verifiers::bytecode::type_system::VerificationType>,
    ) {
        if self.locals_pool.len() < self.max_size {
            self.locals_pool.push(buffer);
        }
    }

    /// Acquires a stack buffer from the pool.
    pub fn acquire_stack(
        &mut self,
        capacity: usize,
    ) -> Vec<crate::verifiers::bytecode::type_system::VerificationType> {
        if let Some(mut buffer) = self.stack_pool.pop() {
            buffer.clear();
            if buffer.capacity() < capacity {
                buffer.reserve(capacity);
            }
            buffer
        } else {
            Vec::with_capacity(capacity)
        }
    }

    /// Returns a stack buffer to the pool.
    pub fn return_stack(
        &mut self,
        buffer: Vec<crate::verifiers::bytecode::type_system::VerificationType>,
    ) {
        if self.stack_pool.len() < self.max_size {
            self.stack_pool.push(buffer);
        }
    }

    /// Clears the pool.
    pub fn clear(&mut self) {
        self.locals_pool.clear();
        self.stack_pool.clear();
    }
}

impl Default for FramePool {
    fn default() -> Self {
        Self::new(32)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_cache_disabled() {
        let cache = VerificationCache::disabled();
        assert!(!cache.is_enabled());

        let key = MethodKey::new("Test", "foo", "()V");
        cache.put_result(&key, CachedResult::Success);

        // Should not cache when disabled
        assert!(cache.get_result(&key).is_none());
    }

    #[test]
    fn test_cache_enabled() {
        let cache = VerificationCache::new(true);
        assert!(cache.is_enabled());

        let key = MethodKey::new("Test", "foo", "()V");
        cache.put_result(&key, CachedResult::Success);

        let result = cache.get_result(&key);
        assert!(matches!(result, Some(CachedResult::Success)));
    }

    #[test]
    fn test_descriptor_cache() {
        let cache = VerificationCache::new(true);

        let parsed = cache.parse_descriptor("(II)V");
        assert!(parsed.is_some());

        let parsed = parsed.unwrap();
        assert_eq!(parsed.parameters.len(), 2);
        assert!(parsed.return_type.is_none());

        // Should be cached now
        assert_eq!(cache.descriptor_count(), 1);

        // Second lookup should hit cache
        let _ = cache.parse_descriptor("(II)V");
        let stats = cache.stats();
        assert_eq!(stats.descriptor_hits, 1);
    }

    #[test]
    fn test_cache_clear() {
        let cache = VerificationCache::new(true);

        let key = MethodKey::new("Test", "foo", "()V");
        cache.put_result(&key, CachedResult::Success);
        cache.parse_descriptor("(II)V");

        assert_eq!(cache.result_count(), 1);
        assert_eq!(cache.descriptor_count(), 1);

        cache.clear();

        assert_eq!(cache.result_count(), 0);
        assert_eq!(cache.descriptor_count(), 0);
    }

    #[test]
    fn test_frame_pool() {
        let mut pool = FramePool::new(4);

        // Acquire buffers
        let locals = pool.acquire_locals(10);
        let stack = pool.acquire_stack(5);

        assert!(locals.capacity() >= 10);
        assert!(stack.capacity() >= 5);

        // Return to pool
        pool.return_locals(locals);
        pool.return_stack(stack);

        // Acquire again; should reuse
        let locals2 = pool.acquire_locals(5);
        assert!(locals2.capacity() >= 10); // Still has old capacity
    }

    #[test]
    fn test_method_key_traits() {
        let key1 = MethodKey::new("Class", "method", "()V");
        let key2 = MethodKey::new("Class", "method", "()V");
        let key3 = MethodKey::new("Class", "other", "()V");

        assert_eq!(key1, key2);
        assert_ne!(key1, key3);

        // Test Debug
        let debug_str = format!("{key1:?}");
        assert!(debug_str.contains("MethodKey"));
        assert!(debug_str.contains("Class"));

        // Test Clone
        let key_clone = key1.clone();
        assert_eq!(key1, key_clone);
    }

    #[test]
    fn test_cached_result_traits() {
        let success = CachedResult::Success;
        let failed = CachedResult::Failed("error".to_string());

        // Test Debug
        assert!(format!("{success:?}").contains("Success"));
        assert!(format!("{failed:?}").contains("Failed"));

        // Test Clone
        let success_clone = success.clone();
        assert!(matches!(success_clone, CachedResult::Success));
    }

    #[test]
    fn test_parsed_descriptor_traits() {
        let desc = ParsedDescriptor {
            parameters: vec![],
            return_type: None,
        };

        // Test Debug
        assert!(format!("{desc:?}").contains("ParsedDescriptor"));

        // Test Clone
        let desc_clone = desc.clone();
        assert!(desc_clone.parameters.is_empty());
        assert!(desc_clone.return_type.is_none());
    }

    #[test]
    fn test_cache_default() {
        let cache = VerificationCache::default();
        assert!(!cache.is_enabled()); // Default bool is false
    }

    #[test]
    fn test_cache_stats_misses() {
        let cache = VerificationCache::new(true);
        let key = MethodKey::new("Test", "foo", "()V");

        // Result miss
        assert!(cache.get_result(&key).is_none());

        // Descriptor miss (via get_descriptor directly)
        assert!(cache.get_descriptor("()V").is_none());

        let stats = cache.stats();
        assert_eq!(stats.result_misses, 1);
        assert_eq!(stats.descriptor_misses, 1);
        assert_eq!(stats.result_hits, 0);
        assert_eq!(stats.descriptor_hits, 0);
    }

    #[test]
    fn test_parse_descriptor_invalid() {
        let cache = VerificationCache::new(true);
        let result = cache.parse_descriptor("invalid");
        assert!(result.is_none());
    }

    #[test]
    fn test_parse_descriptor_disabled() {
        let cache = VerificationCache::disabled();
        let result = cache.parse_descriptor("(II)V");
        assert!(result.is_some()); // Should still parse

        // But not cache
        assert_eq!(cache.descriptor_count(), 0);
    }

    #[test]
    fn test_frame_pool_default() {
        let pool = FramePool::default();
        assert_eq!(pool.max_size, 32);
    }

    #[test]
    fn test_frame_pool_resize() {
        let mut pool = FramePool::new(1);

        // Acquire and return a small buffer
        let locals = pool.acquire_locals(5);
        pool.return_locals(locals);

        // Acquire a larger buffer; should reuse and resize
        let locals = pool.acquire_locals(10);
        assert!(locals.capacity() >= 10);

        pool.return_locals(locals);

        // Same for stack
        let stack = pool.acquire_stack(5);
        pool.return_stack(stack);

        let stack = pool.acquire_stack(10);
        assert!(stack.capacity() >= 10);
    }

    #[test]
    fn test_frame_pool_limit() {
        let mut pool = FramePool::new(1);

        let l1 = pool.acquire_locals(1);
        let l2 = pool.acquire_locals(1);

        pool.return_locals(l1);
        // Pool is now full (size 1)
        pool.return_locals(l2);
        // Should be dropped, not added to pool

        assert_eq!(pool.locals_pool.len(), 1);

        // Same for stack
        let s1 = pool.acquire_stack(1);
        let s2 = pool.acquire_stack(1);

        pool.return_stack(s1);
        pool.return_stack(s2);

        assert_eq!(pool.stack_pool.len(), 1);
    }

    #[test]
    fn test_frame_pool_clear() {
        let mut pool = FramePool::new(5);
        let l = pool.acquire_locals(1);
        pool.return_locals(l);
        let s = pool.acquire_stack(1);
        pool.return_stack(s);

        assert_eq!(pool.locals_pool.len(), 1);
        assert_eq!(pool.stack_pool.len(), 1);

        pool.clear();

        assert_eq!(pool.locals_pool.len(), 0);
        assert_eq!(pool.stack_pool.len(), 0);
    }

    #[test]
    fn test_cache_stats_traits() {
        let stats = CacheStats::default();
        let debug_str = format!("{stats:?}");
        assert!(debug_str.contains("CacheStats"));

        let stats_clone = stats.clone();
        assert_eq!(stats.result_hits, stats_clone.result_hits);
    }

    #[test]
    fn test_verification_cache_debug() {
        let cache = VerificationCache::new(true);
        let debug_str = format!("{cache:?}");
        assert!(debug_str.contains("VerificationCache"));
    }
}