mxc-sdk 1.2.0

Importable library for starting MXC sandboxes in-process, with streaming stdio.
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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

//! Request-scoped session and selected learning-mode attribution.

use std::cell::Cell;
use std::marker::PhantomData;
use std::rc::Rc;
use std::sync::Mutex;

use base64::{engine::general_purpose::STANDARD_NO_PAD, Engine as _};
use uuid::Uuid;

use super::LearningMode;
use crate::mxc_common::logger::Logger;
use crate::mxc_contract::ContractVersion;

thread_local! {
    static CURRENT: Cell<Option<SessionContext>> = const { Cell::new(None) };
}

#[derive(Debug, Clone, Copy)]
pub(crate) struct SessionContext {
    // Preserve transferable attribution even where telemetry emission is a no-op.
    #[cfg_attr(not(any(target_os = "windows", test)), allow(dead_code))]
    session_id: Option<Uuid>,
    #[cfg_attr(not(any(target_os = "windows", test)), allow(dead_code))]
    learning_mode: LearningMode,
    #[cfg_attr(not(any(target_os = "windows", test)), allow(dead_code))]
    critical_events: bool,
    disabled: bool,
}

impl SessionContext {
    pub(crate) fn suppressed() -> Self {
        Self {
            session_id: None,
            learning_mode: LearningMode::NotApplicable,
            critical_events: false,
            disabled: true,
        }
    }
}

static PROCESS_CONTEXT: Mutex<Option<SessionContext>> = Mutex::new(None);

/// Restores the calling thread's attribution on return or unwind.
///
/// This guard cannot move to another thread. Streaming handles retain only
/// the copyable context and install a new guard wherever they are subsequently used.
#[doc(hidden)]
#[derive(Debug)]
#[must_use = "retain the scope until invocation processing finishes"]
pub struct SessionScope {
    previous: Option<SessionContext>,
    _thread_bound: PhantomData<Rc<()>>,
}

impl Drop for SessionScope {
    fn drop(&mut self) {
        CURRENT.with(|slot| slot.set(self.previous));
    }
}

/// Start one authorized invocation, reusing the lifecycle vector's random root.
#[doc(hidden)]
pub fn start(
    active: bool,
    vector: &str,
    source_contract: Option<ContractVersion>,
    logger: &mut Logger,
) -> SessionScope {
    start_with_learning_mode(
        active,
        vector,
        source_contract,
        LearningMode::NotApplicable,
        logger,
    )
}

/// Start an invocation with its selected access-check mode.
#[doc(hidden)]
pub fn start_with_learning_mode(
    active: bool,
    vector: &str,
    source_contract: Option<ContractVersion>,
    learning_mode: LearningMode,
    logger: &mut Logger,
) -> SessionScope {
    if !active {
        return resume(SessionContext::suppressed());
    }
    let id = if vector.is_empty() {
        random_id()
    } else {
        from_vector(vector).ok_or("session_vector_invalid")
    };
    let id = match id {
        Ok(id) => Some(id),
        Err(code) => {
            logger.warning_line(&format!(
                "telemetry: session attribution unavailable ({code}); recording mxc.session_id=unknown"
            ));
            None
        }
    };
    resume(SessionContext {
        session_id: id,
        learning_mode,
        critical_events: super::uses_required_events(source_contract),
        disabled: false,
    })
}

fn random_id() -> Result<Uuid, &'static str> {
    let mut bytes = [0u8; 16];
    getrandom::getrandom(&mut bytes).map_err(|_| "session_randomness_unavailable")?;
    Ok(uuid::Builder::from_random_bytes(bytes).into_uuid())
}

/// The random CV base is persisted already; formatting it as UUIDv4 needs no
/// additional identifier, caller-provided field, or independently mutable file.
pub(crate) fn from_vector(vector: &str) -> Option<Uuid> {
    if !super::correlation_vector::is_relayable(vector) {
        return None;
    }
    let base = vector.split('.').next()?;
    let mut bytes = [0u8; 16];
    let length = STANDARD_NO_PAD.decode_slice(base, &mut bytes).ok()?;
    (length == bytes.len()).then(|| uuid::Builder::from_random_bytes(bytes).into_uuid())
}

pub(crate) fn capture() -> SessionContext {
    CURRENT.with(Cell::get).unwrap_or(SessionContext {
        session_id: None,
        learning_mode: LearningMode::NotApplicable,
        critical_events: false,
        disabled: false,
    })
}

#[cfg(any(target_os = "windows", test))]
pub(crate) fn current() -> Option<Uuid> {
    capture().session_id
}

pub(crate) fn invocation_disabled() -> bool {
    capture().disabled
}

#[cfg(any(target_os = "windows", test))]
pub(crate) fn learning_mode() -> LearningMode {
    capture().learning_mode
}

pub(crate) fn critical_events() -> bool {
    capture().critical_events
}

pub(crate) fn legacy_events_enabled() -> bool {
    !capture().critical_events
}

pub(crate) fn resume(context: SessionContext) -> SessionScope {
    SessionScope {
        previous: CURRENT.with(|slot| slot.replace(Some(context))),
        _thread_bound: PhantomData,
    }
}

#[cfg(any(target_os = "windows", test))]
pub(crate) fn wire_id(buffer: &mut [u8; 36]) -> &str {
    match current() {
        Some(id) => id.hyphenated().encode_lower(buffer),
        None => "unknown",
    }
}

pub(super) fn remember_process() {
    let mut slot = PROCESS_CONTEXT
        .lock()
        .unwrap_or_else(|error| error.into_inner());
    if slot.is_none() {
        *slot = Some(capture());
    }
}

pub(super) fn resume_process() -> SessionScope {
    let stored = PROCESS_CONTEXT.try_lock().ok().and_then(|slot| *slot);
    let context = CURRENT.with(Cell::get).or(stored).unwrap_or_else(capture);
    resume(context)
}

#[cfg(test)]
pub(super) fn reset_process() {
    *PROCESS_CONTEXT
        .lock()
        .unwrap_or_else(|error| error.into_inner()) = None;
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::mxc_common::logger::Mode;

    #[test]
    fn learning_mode_is_scoped_and_disabled_invocations_do_not_inherit_it() {
        let mut logger = Logger::new(Mode::Buffer);
        let _outer = start_with_learning_mode(true, "", None, LearningMode::Learning, &mut logger);
        let outer = current();
        {
            let _inner = start_with_learning_mode(
                true,
                "",
                None,
                LearningMode::PermissiveLearning,
                &mut logger,
            );
            assert_eq!(learning_mode(), LearningMode::PermissiveLearning);
            {
                let _disabled = start_with_learning_mode(
                    false,
                    "",
                    None,
                    LearningMode::PermissiveLearning,
                    &mut logger,
                );
                assert_eq!(learning_mode(), LearningMode::NotApplicable);
                assert!(invocation_disabled());
            }
            assert_eq!(learning_mode(), LearningMode::PermissiveLearning);
        }
        assert_eq!(learning_mode(), LearningMode::Learning);
        assert_eq!(current(), outer);
        let result = std::panic::catch_unwind(|| {
            let mut logger = Logger::new(Mode::Buffer);
            let _scope = start_with_learning_mode(
                true,
                "",
                None,
                LearningMode::PermissiveLearning,
                &mut logger,
            );
            panic!("synthetic invocation unwind");
        });
        assert!(result.is_err());
        assert_eq!(learning_mode(), LearningMode::Learning);
        assert_eq!(current(), outer);
    }

    #[test]
    fn every_phase_of_a_random_vector_has_one_v4_session() {
        let root = super::super::correlation_vector::seed();
        let id = from_vector(&root).expect("valid root");
        assert_eq!(id.get_version_num(), 4);
        assert_eq!(id.get_variant(), uuid::Variant::RFC4122);
        for _ in 0..8 {
            let child = super::super::correlation_vector::spin(&root);
            assert_eq!(from_vector(&child), Some(id));
        }
        assert_ne!(
            from_vector(&super::super::correlation_vector::seed()),
            Some(id)
        );
        for invalid in ["", "caller@example.invalid", "invalid!", "not-a-vector.0"] {
            assert_eq!(from_vector(invalid), None);
        }
    }

    #[test]
    fn nested_and_disabled_invocations_restore_the_outer_session() {
        let mut logger = Logger::new(Mode::Buffer);
        let _outer = start(true, "", None, &mut logger);
        let outer = current().expect("random session");
        {
            let _inner = start(true, "", None, &mut logger);
            assert_ne!(current(), Some(outer));
            {
                let _disabled = start(false, "invalid", None, &mut logger);
                assert_eq!(current(), None);
                assert!(invocation_disabled());
            }
            assert!(current().is_some());
        }
        assert_eq!(current(), Some(outer));
        assert!(!invocation_disabled());
    }

    #[test]
    fn a_streaming_session_can_be_resumed_on_another_thread() {
        for source_contract in [
            None,
            Some(ContractVersion::V0_9_0Alpha),
            Some(ContractVersion::V1_0_0),
            Some(ContractVersion::V1_1_0Alpha),
        ] {
            let mut logger = Logger::new(Mode::Buffer);
            let _scope = start(true, "", source_contract, &mut logger);
            let expected_critical = source_contract.is_some();
            assert_eq!(critical_events(), expected_critical);
            let id = current().expect("random session");
            let context = capture();
            std::thread::spawn(move || {
                assert_eq!(current(), None);
                assert!(!critical_events());
                {
                    let _scope = resume(context);
                    assert_eq!(current(), Some(id));
                    assert_eq!(critical_events(), expected_critical);
                    let mut buffer = [0; 36];
                    assert_eq!(wire_id(&mut buffer), id.to_string());
                }
                assert_eq!(current(), None);
                assert!(!critical_events());
            })
            .join()
            .expect("worker completed");
            assert_eq!(current(), Some(id));
            assert_eq!(critical_events(), expected_critical);
        }
    }

    #[test]
    fn context_transfer_preserves_suppression_separately_from_missing_attribution() {
        for active in [false, true] {
            let mut logger = Logger::new(Mode::Buffer);
            let _scope = start_with_learning_mode(
                active,
                "invalid",
                Some(ContractVersion::V1_1_0Alpha),
                LearningMode::Learning,
                &mut logger,
            );
            assert_eq!(current(), None);
            let context = capture();
            std::thread::spawn(move || {
                let mut logger = Logger::new(Mode::Buffer);
                let _outer = start_with_learning_mode(
                    true,
                    "",
                    None,
                    LearningMode::PermissiveLearning,
                    &mut logger,
                );
                let outer_id = current();
                {
                    let _restored = resume(context);
                    assert_eq!(current(), None);
                    assert_eq!(invocation_disabled(), !active);
                    assert_eq!(critical_events(), active);
                    assert_eq!(
                        learning_mode(),
                        if active {
                            LearningMode::Learning
                        } else {
                            LearningMode::NotApplicable
                        }
                    );
                    assert_eq!(wire_id(&mut [0; 36]), "unknown");
                }
                assert_eq!(current(), outer_id);
                assert!(!invocation_disabled());
                assert_eq!(learning_mode(), LearningMode::PermissiveLearning);
                assert!(!critical_events());
            })
            .join()
            .unwrap();
        }
    }

    #[test]
    fn process_recovery_preserves_an_installed_invocation_without_attribution() {
        let _lock = super::super::events::test_sink::TEST_LOCK
            .lock()
            .unwrap_or_else(|error| error.into_inner());
        reset_process();
        let mut logger = Logger::new(Mode::Buffer);
        {
            let _owner = start_with_learning_mode(
                true,
                "",
                Some(ContractVersion::V1_1_0Alpha),
                LearningMode::Learning,
                &mut logger,
            );
            remember_process();
        }
        for active in [false, true] {
            let _invocation = start_with_learning_mode(
                active,
                "invalid",
                None,
                LearningMode::PermissiveLearning,
                &mut logger,
            );
            let expected_mode = learning_mode();
            let _recovered = resume_process();
            assert_eq!(current(), None);
            assert_eq!(invocation_disabled(), !active);
            assert_eq!(learning_mode(), expected_mode);
            assert!(!critical_events());
        }
        reset_process();
    }

    #[test]
    fn concurrent_invocations_have_distinct_ids_and_restore_context_after_unwind() {
        let barrier = std::sync::Arc::new(std::sync::Barrier::new(8));
        let workers: Vec<_> = (0..8)
            .map(|worker| {
                let barrier = barrier.clone();
                std::thread::spawn(move || {
                    let mut logger = Logger::new(Mode::Buffer);
                    let expected_critical = worker % 2 == 0;
                    let contract = if expected_critical {
                        Some(ContractVersion::V1_0_0)
                    } else {
                        None
                    };
                    let _scope = start(true, "", contract, &mut logger);
                    let id = current().unwrap();
                    barrier.wait();
                    assert_eq!(current(), Some(id));
                    assert_eq!(critical_events(), expected_critical);
                    let failed = std::panic::catch_unwind(|| {
                        let mut logger = Logger::new(Mode::Buffer);
                        let _nested = start(true, "", None, &mut logger);
                        assert_ne!(current(), Some(id));
                        assert!(!critical_events());
                        panic!("synthetic invocation unwind");
                    });
                    assert!(failed.is_err());
                    assert_eq!(current(), Some(id));
                    assert_eq!(critical_events(), expected_critical);
                    id
                })
            })
            .collect();
        let ids: std::collections::HashSet<_> = workers
            .into_iter()
            .map(|worker| worker.join().unwrap())
            .collect();
        assert_eq!(ids.len(), 8);
    }
}