pmat 3.15.0

PMAT - Zero-config AI context generation and code quality toolkit (CLI, MCP, HTTP)
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
#![cfg_attr(coverage_nightly, coverage(off))]
use super::*;
use parking_lot::RwLock;
use std::alloc::{GlobalAlloc, Layout, System};
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::Arc;

type PressureCallbacks = Arc<RwLock<Vec<Box<dyn Fn(f32) + Send + Sync>>>>;
use sysinfo::System as SysInfo;

// Memory limiter with custom allocator
/// Limiter for controlling memory usage.
pub struct MemoryLimiter {
    limits: Arc<RwLock<MemoryLimits>>,
    allocated: Arc<AtomicUsize>,
    peak_allocated: Arc<AtomicUsize>,
    system: Arc<RwLock<SysInfo>>,
    pid: u32,
}

impl MemoryLimiter {
    #[provable_contracts_macros::contract("pmat-core.yaml", equation = "check_compliance")]
    /// Create a new instance.
    pub fn new(limits: MemoryLimits) -> Result<Self, ResourceError> {
        let mut system = SysInfo::new_all();
        system.refresh_all();

        let limiter = Self {
            limits: Arc::new(RwLock::new(limits.clone())),
            allocated: Arc::new(AtomicUsize::new(0)),
            peak_allocated: Arc::new(AtomicUsize::new(0)),
            system: Arc::new(RwLock::new(system)),
            pid: std::process::id(),
        };

        // Skip applying actual system limits during tests to avoid failures
        #[cfg(not(test))]
        limiter.apply_memory_limits(&limits)?;

        Ok(limiter)
    }

    fn apply_memory_limits(&self, limits: &MemoryLimits) -> Result<(), ResourceError> {
        // Apply RSS limit using setrlimit
        self.set_rss_limit(limits.max_bytes)?;

        // Apply heap limit if specified
        if let Some(heap_limit) = limits.max_heap_bytes {
            self.set_heap_limit(heap_limit)?;
        }

        // Apply stack limit if specified
        if let Some(stack_limit) = limits.max_stack_bytes {
            self.set_stack_limit(stack_limit)?;
        }

        // Apply swap limit if specified
        if let Some(swap_limit) = limits.swap_limit_bytes {
            self.set_swap_limit(swap_limit)?;
        }

        Ok(())
    }

    fn set_rss_limit(&self, limit_bytes: usize) -> Result<(), ResourceError> {
        #[cfg(unix)]
        {
            use libc::{rlimit, setrlimit, RLIMIT_AS};

            let limit = rlimit {
                rlim_cur: limit_bytes as libc::rlim_t,
                rlim_max: limit_bytes as libc::rlim_t,
            };

            // SAFETY: Setting address space (RSS) memory limit via libc system call.
            // This is safe because:
            // 1. setrlimit is a standard POSIX system call for resource limits
            // 2. The rlimit struct is properly initialized with valid values
            // 3. RLIMIT_AS is a valid resource limit constant
            // 4. We validate the return code and propagate errors appropriately
            unsafe {
                let result = setrlimit(RLIMIT_AS, &limit);
                if result != 0 {
                    return Err(ResourceError::MemoryError(format!(
                        "Failed to set RSS limit: {}",
                        std::io::Error::last_os_error()
                    )));
                }
            }
        }

        Ok(())
    }

    fn set_heap_limit(&self, limit_bytes: usize) -> Result<(), ResourceError> {
        #[cfg(unix)]
        {
            use libc::{rlimit, setrlimit, RLIMIT_DATA};

            let limit = rlimit {
                rlim_cur: limit_bytes as libc::rlim_t,
                rlim_max: limit_bytes as libc::rlim_t,
            };

            // SAFETY: Setting heap (data segment) memory limit via libc system call.
            // This is safe because:
            // 1. setrlimit is a standard POSIX system call for resource limits
            // 2. The rlimit struct is properly initialized with valid values
            // 3. RLIMIT_DATA is a valid resource limit constant for heap memory
            // 4. We validate the return code and propagate errors appropriately
            unsafe {
                let result = setrlimit(RLIMIT_DATA, &limit);
                if result != 0 {
                    return Err(ResourceError::MemoryError(format!(
                        "Failed to set heap limit: {}",
                        std::io::Error::last_os_error()
                    )));
                }
            }
        }

        Ok(())
    }

    fn set_stack_limit(&self, limit_bytes: usize) -> Result<(), ResourceError> {
        #[cfg(unix)]
        {
            use libc::{rlimit, setrlimit, RLIMIT_STACK};

            let limit = rlimit {
                rlim_cur: limit_bytes as libc::rlim_t,
                rlim_max: limit_bytes as libc::rlim_t,
            };

            // SAFETY: Setting stack memory limit via libc system call.
            // This is safe because:
            // 1. setrlimit is a standard POSIX system call for resource limits
            // 2. The rlimit struct is properly initialized with valid values
            // 3. RLIMIT_STACK is a valid resource limit constant for stack memory
            // 4. We validate the return code and propagate errors appropriately
            unsafe {
                let result = setrlimit(RLIMIT_STACK, &limit);
                if result != 0 {
                    return Err(ResourceError::MemoryError(format!(
                        "Failed to set stack limit: {}",
                        std::io::Error::last_os_error()
                    )));
                }
            }
        }

        Ok(())
    }

    fn set_swap_limit(&self, _limit_bytes: usize) -> Result<(), ResourceError> {
        // Swap limiting typically requires cgroup configuration
        #[cfg(target_os = "linux")]
        {
            let cgroup_path = format!("/sys/fs/cgroup/memory/agent_{}", self.pid);

            if std::path::Path::new(&cgroup_path).exists() {
                use std::fs;

                let swap_limit = _limit_bytes.to_string();
                fs::write(
                    format!("{}/memory.memsw.limit_in_bytes", cgroup_path),
                    swap_limit,
                )
                .map_err(|e| {
                    ResourceError::MemoryError(format!("Failed to set swap limit: {}", e))
                })?;
            }
        }

        Ok(())
    }

    #[provable_contracts_macros::contract("pmat-core.yaml", equation = "check_compliance")]
    /// Check allocation.
    pub fn check_allocation(&self, size: usize) -> Result<(), ResourceError> {
        let current = self.allocated.load(Ordering::Relaxed);
        let limit = self.limits.read().max_bytes;

        if current + size > limit {
            Err(ResourceError::MemoryError(format!(
                "Memory limit exceeded: {} + {} > {}",
                current, size, limit
            )))
        } else {
            Ok(())
        }
    }

    #[provable_contracts_macros::contract("pmat-core.yaml", equation = "check_compliance")]
    /// Record allocation.
    pub fn record_allocation(&self, size: usize) {
        let new_allocated = self.allocated.fetch_add(size, Ordering::SeqCst) + size;

        // Update peak if necessary
        let mut peak = self.peak_allocated.load(Ordering::Relaxed);
        while new_allocated > peak {
            match self.peak_allocated.compare_exchange_weak(
                peak,
                new_allocated,
                Ordering::SeqCst,
                Ordering::Relaxed,
            ) {
                Ok(_) => break,
                Err(current) => peak = current,
            }
        }
    }

    #[provable_contracts_macros::contract("pmat-core.yaml", equation = "check_compliance")]
    /// Record deallocation.
    pub fn record_deallocation(&self, size: usize) {
        self.allocated.fetch_sub(size, Ordering::SeqCst);
    }

    #[provable_contracts_macros::contract("pmat-core.yaml", equation = "check_compliance")]
    /// Get allocated.
    pub fn get_allocated(&self) -> usize {
        self.allocated.load(Ordering::Relaxed)
    }

    /// Get peak allocated.
    pub fn get_peak_allocated(&self) -> usize {
        self.peak_allocated.load(Ordering::Relaxed)
    }
}

impl ResourceController for MemoryLimiter {
    fn apply_limits(&self, limits: &ResourceLimits) -> Result<(), ResourceError> {
        *self.limits.write() = limits.memory.clone();
        self.apply_memory_limits(&limits.memory)
    }

    fn get_usage(&self) -> Result<ResourceUsage, ResourceError> {
        let mut system = self.system.write();
        system.refresh_processes(sysinfo::ProcessesToUpdate::All, true);

        let memory_bytes =
            if let Some(process) = system.process(sysinfo::Pid::from(self.pid as usize)) {
                process.memory() * 1024 // Convert from KB to bytes
            } else {
                self.allocated.load(Ordering::Relaxed) as u64
            };

        Ok(ResourceUsage {
            cpu_percent: 0.0,
            memory_bytes: memory_bytes as usize,
            gpu_memory_bytes: None,
            gpu_compute_percent: None,
            network_ingress_bytes: 0,
            network_egress_bytes: 0,
            disk_read_bytes: 0,
            disk_write_bytes: 0,
            timestamp: std::time::SystemTime::now(),
        })
    }

    fn release(&self) -> Result<(), ResourceError> {
        // Reset limits to system defaults
        #[cfg(unix)]
        {
            use libc::{rlimit, setrlimit, RLIMIT_AS, RLIM_INFINITY};

            let unlimited = rlimit {
                rlim_cur: RLIM_INFINITY,
                rlim_max: RLIM_INFINITY,
            };

            // SAFETY: Removing address space memory limit via libc system call.
            // This is safe because:
            // 1. setrlimit is a standard POSIX system call for resource limits
            // 2. The rlimit struct is properly initialized with RLIM_INFINITY
            // 3. RLIMIT_AS is a valid resource limit constant
            // 4. Error handling is intentionally omitted in Drop (best-effort cleanup)
            unsafe {
                setrlimit(RLIMIT_AS, &unlimited);
            }
        }

        Ok(())
    }
}

// Custom allocator that tracks and limits memory usage
/// Custom allocator with limited limits.
pub struct LimitedAllocator {
    limiter: Arc<MemoryLimiter>,
    inner: System,
}

impl LimitedAllocator {
    #[provable_contracts_macros::contract("pmat-core.yaml", equation = "check_compliance")]
    /// Create a new instance.
    pub fn new(limiter: Arc<MemoryLimiter>) -> Self {
        Self {
            limiter,
            inner: System,
        }
    }
}

// SAFETY: LimitedAllocator delegates to System allocator with additional memory tracking.
// All unsafe operations are properly contained in unsafe blocks per Rust 2024 requirements.
unsafe impl GlobalAlloc for LimitedAllocator {
    unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
        let size = layout.size();

        // Check if allocation would exceed limit
        if self.limiter.check_allocation(size).is_err() {
            return std::ptr::null_mut();
        }

        // SAFETY: Delegating to System allocator which is guaranteed to be sound
        let ptr = unsafe { self.inner.alloc(layout) };

        if !ptr.is_null() {
            self.limiter.record_allocation(size);
        }

        ptr
    }

    unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
        self.limiter.record_deallocation(layout.size());
        // SAFETY: Delegating to System allocator which is guaranteed to be sound
        unsafe { self.inner.dealloc(ptr, layout) };
    }

    unsafe fn realloc(&self, ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8 {
        let old_size = layout.size();

        if new_size > old_size {
            // Check if additional allocation would exceed limit
            let additional = new_size - old_size;
            if self.limiter.check_allocation(additional).is_err() {
                return std::ptr::null_mut();
            }
        }

        // SAFETY: Delegating to System allocator which is guaranteed to be sound
        let new_ptr = unsafe { self.inner.realloc(ptr, layout, new_size) };

        if !new_ptr.is_null() {
            if new_size > old_size {
                self.limiter.record_allocation(new_size - old_size);
            } else {
                self.limiter.record_deallocation(old_size - new_size);
            }
        }

        new_ptr
    }
}

// Memory pressure monitor
/// Monitor for memory resources.
pub struct MemoryMonitor {
    limiter: Arc<MemoryLimiter>,
    pressure_callbacks: PressureCallbacks,
}

impl MemoryMonitor {
    #[provable_contracts_macros::contract("pmat-core.yaml", equation = "check_compliance")]
    /// Create a new instance.
    pub fn new(limiter: Arc<MemoryLimiter>) -> Self {
        Self {
            limiter,
            pressure_callbacks: Arc::new(RwLock::new(Vec::new())),
        }
    }

    #[provable_contracts_macros::contract("pmat-core.yaml", equation = "check_compliance")]
    /// Add pressure callback.
    pub fn add_pressure_callback<F>(&self, callback: F)
    where
        F: Fn(f32) + Send + Sync + 'static,
    {
        self.pressure_callbacks.write().push(Box::new(callback));
    }

    #[provable_contracts_macros::contract("pmat-core.yaml", equation = "check_compliance")]
    /// Check memory pressure.
    pub fn check_memory_pressure(&self) -> f32 {
        let allocated = self.limiter.get_allocated();
        let limit = self.limiter.limits.read().max_bytes;

        let pressure = allocated as f32 / limit as f32;

        // Trigger callbacks if pressure is high
        if pressure > 0.8 {
            let callbacks = self.pressure_callbacks.read();
            for callback in callbacks.iter() {
                callback(pressure);
            }
        }

        pressure
    }

    #[provable_contracts_macros::contract("pmat-core.yaml", equation = "check_compliance")]
    pub async fn monitor_loop(&self) {
        loop {
            self.check_memory_pressure();
            tokio::time::sleep(Duration::from_secs(1)).await;
        }
    }
}

#[cfg_attr(coverage_nightly, coverage(off))]
#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_memory_limiter_creation() {
        let limits = MemoryLimits {
            max_bytes: 1024 * 1024, // 1MB for testing
            max_heap_bytes: None,   // Don't set heap limit in tests
            max_stack_bytes: None,  // Don't set stack limit in tests
            swap_limit_bytes: None,
        };

        let _limiter = MemoryLimiter::new(limits).unwrap();
    }

    #[test]
    fn test_allocation_tracking() {
        let limits = MemoryLimits {
            max_bytes: 1024 * 1024, // 1MB
            max_heap_bytes: None,
            max_stack_bytes: None,
            swap_limit_bytes: None,
        };

        let limiter = MemoryLimiter::new(limits).unwrap();

        assert!(limiter.check_allocation(512 * 1024).is_ok());
        limiter.record_allocation(512 * 1024);
        assert_eq!(limiter.get_allocated(), 512 * 1024);

        assert!(limiter.check_allocation(600 * 1024).is_err());

        limiter.record_deallocation(256 * 1024);
        assert_eq!(limiter.get_allocated(), 256 * 1024);
    }

    #[test]
    fn test_memory_pressure() {
        let limits = MemoryLimits {
            max_bytes: 1000,
            max_heap_bytes: None,
            max_stack_bytes: None,
            swap_limit_bytes: None,
        };

        let limiter = Arc::new(MemoryLimiter::new(limits).unwrap());
        let monitor = MemoryMonitor::new(limiter.clone());

        limiter.record_allocation(500);
        let pressure = monitor.check_memory_pressure();
        assert_eq!(pressure, 0.5);

        limiter.record_allocation(400);
        let pressure = monitor.check_memory_pressure();
        assert_eq!(pressure, 0.9);
    }
}