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
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
use alloc::{boxed::Box, sync::Arc, vec::Vec};
use core::{any::Any, sync::atomic::AtomicI32};
use futures::channel::oneshot::{self, Receiver, Sender};
use hashbrown::HashMap;
use lock::Mutex;
use super::exception::{ExceptionChannelType, Exceptionate};
use super::job_policy::{JobPolicy, PolicyAction, PolicyCondition};
use super::{Job, Task, Thread, ThreadFn};
use crate::object::{Handle, HandleBasicInfo, HandleValue, INVALID_HANDLE};
use crate::object::{KObjectBase, KernelObject, KoID, Rights, Signal};
use crate::{define_count_helper, impl_kobject};
use crate::{signal::Futex, vm::VmAddressRegion, ZxError, ZxResult};
#[allow(dead_code)]
pub struct Process {
base: KObjectBase,
_counter: CountHelper,
job: Arc<Job>,
policy: JobPolicy,
vmar: Arc<VmAddressRegion>,
ext: Box<dyn Any + Send + Sync>,
exceptionate: Arc<Exceptionate>,
debug_exceptionate: Arc<Exceptionate>,
inner: Mutex<ProcessInner>,
}
impl_kobject!(Process
fn get_child(&self, id: KoID) -> ZxResult<Arc<dyn KernelObject>> {
let inner = self.inner.lock();
let thread = inner.threads.iter().find(|o| o.id() == id).ok_or(ZxError::NOT_FOUND)?;
Ok(thread.clone())
}
fn related_koid(&self) -> KoID {
self.job.id()
}
);
define_count_helper!(Process);
#[derive(Default)]
struct ProcessInner {
status: Status,
max_handle_id: u32,
handles: HashMap<HandleValue, (Handle, Vec<Sender<()>>)>,
futexes: HashMap<usize, Arc<Futex>>,
threads: Vec<Arc<Thread>>,
debug_addr: usize,
dyn_break_on_load: usize,
critical_to_job: Option<(Arc<Job>, bool)>,
}
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub enum Status {
Init,
Running,
Exited(i64),
}
impl Default for Status {
fn default() -> Self {
Status::Init
}
}
impl Process {
pub fn create(job: &Arc<Job>, name: &str) -> ZxResult<Arc<Self>> {
Self::create_with_ext(job, name, ())
}
pub fn create_with_ext(
job: &Arc<Job>,
name: &str,
ext: impl Any + Send + Sync,
) -> ZxResult<Arc<Self>> {
let proc = Arc::new(Process {
base: KObjectBase::with_name(name),
_counter: CountHelper::new(),
job: job.clone(),
policy: job.policy(),
vmar: VmAddressRegion::new_root(),
ext: Box::new(ext),
exceptionate: Exceptionate::new(ExceptionChannelType::Process),
debug_exceptionate: Exceptionate::new(ExceptionChannelType::Debugger),
inner: Mutex::new(ProcessInner::default()),
});
job.add_process(proc.clone())?;
Ok(proc)
}
pub fn start(
&self,
thread: &Arc<Thread>,
entry: usize,
stack: usize,
arg1: Option<Handle>,
arg2: usize,
thread_fn: ThreadFn,
) -> ZxResult {
let handle_value;
{
let mut inner = self.inner.lock();
if !inner.contains_thread(thread) {
return Err(ZxError::ACCESS_DENIED);
}
if inner.status != Status::Init {
return Err(ZxError::BAD_STATE);
}
inner.status = Status::Running;
handle_value = arg1.map_or(INVALID_HANDLE, |handle| inner.add_handle(handle));
}
thread.set_first_thread();
let res = thread.start_with_entry(entry, stack, handle_value as usize, arg2, thread_fn);
if res.is_err() && handle_value != INVALID_HANDLE {
self.inner.lock().remove_handle(handle_value).ok();
}
res
}
pub fn exit(&self, retcode: i64) {
let mut inner = self.inner.lock();
if let Status::Exited(_) = inner.status {
return;
}
inner.status = Status::Exited(retcode);
if inner.threads.is_empty() {
inner.handles.clear();
drop(inner);
self.terminate();
return;
}
for thread in inner.threads.iter() {
thread.kill();
}
inner.handles.clear();
}
fn terminate(&self) {
let mut inner = self.inner.lock();
let retcode = match inner.status {
Status::Exited(retcode) => retcode,
_ => {
inner.status = Status::Exited(0);
0
}
};
self.base.signal_set(Signal::PROCESS_TERMINATED);
self.exceptionate.shutdown();
self.debug_exceptionate.shutdown();
self.job.remove_process(self.base.id);
if let Some((job, retcode_nonzero)) = &inner.critical_to_job {
if !retcode_nonzero || retcode != 0 {
job.kill();
}
}
}
pub fn check_policy(&self, condition: PolicyCondition) -> ZxResult {
match self
.policy
.get_action(condition)
.unwrap_or(PolicyAction::Allow)
{
PolicyAction::Allow => Ok(()),
PolicyAction::Deny => Err(ZxError::ACCESS_DENIED),
_ => unimplemented!(),
}
}
pub fn set_critical_at_job(
&self,
critical_to_job: &Arc<Job>,
retcode_nonzero: bool,
) -> ZxResult {
let mut inner = self.inner.lock();
if inner.critical_to_job.is_some() {
return Err(ZxError::ALREADY_BOUND);
}
let mut job = self.job.clone();
loop {
if job.id() == critical_to_job.id() {
inner.critical_to_job = Some((job, retcode_nonzero));
return Ok(());
}
if let Some(p) = job.parent() {
job = p;
} else {
break;
}
}
Err(ZxError::INVALID_ARGS)
}
pub fn status(&self) -> Status {
self.inner.lock().status
}
pub fn exit_code(&self) -> Option<i64> {
if let Status::Exited(code) = self.status() {
Some(code)
} else {
None
}
}
pub fn ext(&self) -> &Box<dyn Any + Send + Sync> {
&self.ext
}
pub fn vmar(&self) -> Arc<VmAddressRegion> {
self.vmar.clone()
}
pub fn job(&self) -> Arc<Job> {
self.job.clone()
}
pub fn add_handle(&self, handle: Handle) -> HandleValue {
self.inner.lock().add_handle(handle)
}
pub fn add_handles(&self, handles: Vec<Handle>) -> Vec<HandleValue> {
let mut inner = self.inner.lock();
handles.into_iter().map(|h| inner.add_handle(h)).collect()
}
pub fn remove_handle(&self, handle_value: HandleValue) -> ZxResult<Handle> {
self.inner.lock().remove_handle(handle_value)
}
pub fn remove_handles(&self, handle_values: &[HandleValue]) -> ZxResult<Vec<Handle>> {
let mut inner = self.inner.lock();
handle_values
.iter()
.map(|h| inner.remove_handle(*h))
.collect()
}
pub fn remove_object<T: KernelObject>(&self, handle_value: HandleValue) -> ZxResult<Arc<T>> {
let handle = self.remove_handle(handle_value)?;
let object = handle
.object
.downcast_arc::<T>()
.map_err(|_| ZxError::WRONG_TYPE)?;
Ok(object)
}
fn get_handle(&self, handle_value: HandleValue) -> ZxResult<Handle> {
self.inner.lock().get_handle(handle_value)
}
pub fn get_futex(&self, addr: &'static AtomicI32) -> Arc<Futex> {
let mut inner = self.inner.lock();
inner
.futexes
.entry(addr as *const AtomicI32 as usize)
.or_insert_with(|| Futex::new(addr))
.clone()
}
pub fn dup_handle_operating_rights(
&self,
handle_value: HandleValue,
operation: impl FnOnce(Rights) -> ZxResult<Rights>,
) -> ZxResult<HandleValue> {
let mut inner = self.inner.lock();
let mut handle = match inner.handles.get(&handle_value) {
Some((h, _)) => h.clone(),
None => return Err(ZxError::BAD_HANDLE),
};
handle.rights = operation(handle.rights)?;
let new_handle_value = inner.add_handle(handle);
Ok(new_handle_value)
}
pub fn get_object_with_rights<T: KernelObject>(
&self,
handle_value: HandleValue,
desired_rights: Rights,
) -> ZxResult<Arc<T>> {
self.get_dyn_object_with_rights(handle_value, desired_rights)
.and_then(|obj| obj.downcast_arc::<T>().map_err(|_| ZxError::WRONG_TYPE))
}
pub fn get_object_and_rights<T: KernelObject>(
&self,
handle_value: HandleValue,
) -> ZxResult<(Arc<T>, Rights)> {
let (object, rights) = self.get_dyn_object_and_rights(handle_value)?;
let object = object
.downcast_arc::<T>()
.map_err(|_| ZxError::WRONG_TYPE)?;
Ok((object, rights))
}
pub fn get_dyn_object_with_rights(
&self,
handle_value: HandleValue,
desired_rights: Rights,
) -> ZxResult<Arc<dyn KernelObject>> {
let handle = self.get_handle(handle_value)?;
if !handle.rights.contains(desired_rights) {
return Err(ZxError::ACCESS_DENIED);
}
Ok(handle.object)
}
pub fn get_dyn_object_and_rights(
&self,
handle_value: HandleValue,
) -> ZxResult<(Arc<dyn KernelObject>, Rights)> {
let handle = self.get_handle(handle_value)?;
Ok((handle.object, handle.rights))
}
pub fn get_object<T: KernelObject>(&self, handle_value: HandleValue) -> ZxResult<Arc<T>> {
let handle = self.get_handle(handle_value)?;
let object = handle
.object
.downcast_arc::<T>()
.map_err(|_| ZxError::WRONG_TYPE)?;
Ok(object)
}
pub fn get_handle_info(&self, handle_value: HandleValue) -> ZxResult<HandleBasicInfo> {
let handle = self.get_handle(handle_value)?;
Ok(handle.get_info())
}
pub(super) fn add_thread(&self, thread: Arc<Thread>) -> ZxResult {
let mut inner = self.inner.lock();
if let Status::Exited(_) = inner.status {
return Err(ZxError::BAD_STATE);
}
inner.threads.push(thread);
Ok(())
}
pub(super) fn remove_thread(&self, tid: KoID) {
let mut inner = self.inner.lock();
inner.threads.retain(|t| t.id() != tid);
if inner.threads.is_empty() {
drop(inner);
self.terminate();
}
}
pub fn get_info(&self) -> ProcessInfo {
let mut info = ProcessInfo {
debugger_attached: self.debug_exceptionate.has_channel(),
..Default::default()
};
match self.inner.lock().status {
Status::Init => {
info.started = false;
info.has_exited = false;
}
Status::Running => {
info.started = true;
info.has_exited = false;
}
Status::Exited(ret) => {
info.return_code = ret;
info.has_exited = true;
info.started = true;
}
}
info
}
pub fn set_debug_addr(&self, addr: usize) {
self.inner.lock().debug_addr = addr;
}
pub fn get_debug_addr(&self) -> usize {
self.inner.lock().debug_addr
}
pub fn set_dyn_break_on_load(&self, addr: usize) {
self.inner.lock().dyn_break_on_load = addr;
}
pub fn get_dyn_break_on_load(&self) -> usize {
self.inner.lock().dyn_break_on_load
}
pub fn get_cancel_token(&self, handle_value: HandleValue) -> ZxResult<Receiver<()>> {
self.inner.lock().get_cancel_token(handle_value)
}
pub fn thread_ids(&self) -> Vec<KoID> {
self.inner.lock().threads.iter().map(|t| t.id()).collect()
}
pub async fn wait_for_exit(self: &Arc<Self>) -> i64 {
let object: Arc<dyn KernelObject> = self.clone();
object.wait_signal(Signal::PROCESS_TERMINATED).await;
let code = self.exit_code().expect("process not exited!");
info!(
"process {:?}({}) exited with code {:?}",
self.name(),
self.id(),
code
);
code
}
}
impl Task for Process {
fn kill(&self) {
self.exit(super::TASK_RETCODE_SYSCALL_KILL);
}
fn suspend(&self) {
let inner = self.inner.lock();
for thread in inner.threads.iter() {
thread.suspend();
}
}
fn resume(&self) {
let inner = self.inner.lock();
for thread in inner.threads.iter() {
thread.resume();
}
}
fn exceptionate(&self) -> Arc<Exceptionate> {
self.exceptionate.clone()
}
fn debug_exceptionate(&self) -> Arc<Exceptionate> {
self.debug_exceptionate.clone()
}
}
impl ProcessInner {
fn add_handle(&mut self, handle: Handle) -> HandleValue {
let key = (self.max_handle_id << 2) | 0x3u32;
info!("add handle: {:#x}, {:?}", key, handle.object);
self.max_handle_id += 1;
self.handles.insert(key, (handle, Vec::new()));
key
}
fn contains_thread(&self, thread: &Arc<Thread>) -> bool {
self.threads.iter().any(|t| Arc::ptr_eq(t, thread))
}
fn remove_handle(&mut self, handle_value: HandleValue) -> ZxResult<Handle> {
let (handle, queue) = self
.handles
.remove(&handle_value)
.ok_or(ZxError::BAD_HANDLE)?;
for sender in queue {
let _ = sender.send(());
}
Ok(handle)
}
fn get_cancel_token(&mut self, handle_value: HandleValue) -> ZxResult<Receiver<()>> {
let (_, queue) = self
.handles
.get_mut(&handle_value)
.ok_or(ZxError::BAD_HANDLE)?;
let (sender, receiver) = oneshot::channel();
queue.push(sender);
Ok(receiver)
}
fn get_handle(&mut self, handle_value: HandleValue) -> ZxResult<Handle> {
let (handle, _) = self.handles.get(&handle_value).ok_or(ZxError::BAD_HANDLE)?;
Ok(handle.clone())
}
}
#[allow(missing_docs)]
#[repr(C)]
#[derive(Default)]
pub struct ProcessInfo {
pub return_code: i64,
pub started: bool,
pub has_exited: bool,
pub debugger_attached: bool,
pub padding1: [u8; 5],
}
#[cfg(test)]
mod tests {
use super::*;
use crate::object::KernelObject;
use crate::task::*;
#[test]
fn create() {
let root_job = Job::root();
let proc = Process::create(&root_job, "proc").expect("failed to create process");
assert_eq!(proc.related_koid(), root_job.id());
assert!(Arc::ptr_eq(&root_job, &proc.job()));
}
#[test]
fn handle() {
let root_job = Job::root();
let proc = Process::create(&root_job, "proc").expect("failed to create process");
let handle = Handle::new(proc.clone(), Rights::DEFAULT_PROCESS);
let handle_value = proc.add_handle(handle);
let _info = proc.get_handle_info(handle_value).unwrap();
let object: Arc<Process> = proc
.get_object_with_rights(handle_value, Rights::DEFAULT_PROCESS)
.expect("failed to get object");
assert!(Arc::ptr_eq(&object, &proc));
let (object, rights) = proc
.get_object_and_rights::<Process>(handle_value)
.expect("failed to get object");
assert!(Arc::ptr_eq(&object, &proc));
assert_eq!(rights, Rights::DEFAULT_PROCESS);
assert_eq!(
proc.get_object_with_rights::<Process>(handle_value, Rights::MANAGE_JOB)
.err(),
Some(ZxError::ACCESS_DENIED)
);
assert_eq!(
proc.get_object_with_rights::<Job>(handle_value, Rights::DEFAULT_PROCESS)
.err(),
Some(ZxError::WRONG_TYPE)
);
proc.remove_handle(handle_value).unwrap();
assert_eq!(
proc.get_object_with_rights::<Process>(handle_value, Rights::DEFAULT_PROCESS)
.err(),
Some(ZxError::BAD_HANDLE)
);
let handle1 = Handle::new(proc.clone(), Rights::DEFAULT_PROCESS);
let handle2 = Handle::new(proc.clone(), Rights::DEFAULT_PROCESS);
let handle_values = proc.add_handles(vec![handle1, handle2]);
let object1: Arc<Process> = proc
.get_object_with_rights(handle_values[0], Rights::DEFAULT_PROCESS)
.expect("failed to get object");
assert!(Arc::ptr_eq(&object1, &proc));
proc.remove_handles(&handle_values).unwrap();
assert_eq!(
proc.get_object_with_rights::<Process>(handle_values[0], Rights::DEFAULT_PROCESS)
.err(),
Some(ZxError::BAD_HANDLE)
);
}
#[test]
fn handle_duplicate() {
let root_job = Job::root();
let proc = Process::create(&root_job, "proc").expect("failed to create process");
assert_eq!(
proc.dup_handle_operating_rights(0, |_| Ok(Rights::empty())),
Err(ZxError::BAD_HANDLE)
);
let rights = Rights::DUPLICATE;
let handle_value = proc.add_handle(Handle::new(proc.clone(), rights));
let new_handle_value = proc
.dup_handle_operating_rights(handle_value, |old_rights| Ok(old_rights))
.unwrap();
assert_eq!(proc.get_handle(new_handle_value).unwrap().rights, rights);
let new_handle_value = proc
.dup_handle_operating_rights(handle_value, |_| Ok(Rights::empty()))
.unwrap();
assert_eq!(
proc.get_handle(new_handle_value).unwrap().rights,
Rights::empty()
);
let handle_value = proc.add_handle(Handle::new(proc.clone(), Rights::empty()));
assert_eq!(
proc.dup_handle_operating_rights(handle_value, |handle_rights| {
if !handle_rights.contains(Rights::DUPLICATE) {
return Err(ZxError::ACCESS_DENIED);
}
Ok(handle_rights)
}),
Err(ZxError::ACCESS_DENIED)
);
}
#[test]
fn get_child() {
let root_job = Job::root();
let proc = Process::create(&root_job, "proc").expect("failed to create process");
let thread = Thread::create(&proc, "thread").expect("failed to create thread");
assert_eq!(proc.get_child(thread.id()).unwrap().id(), thread.id());
assert_eq!(proc.get_child(proc.id()).err(), Some(ZxError::NOT_FOUND));
let thread1 = Thread::create(&proc, "thread1").expect("failed to create thread");
assert_eq!(proc.thread_ids(), vec![thread.id(), thread1.id()]);
}
#[test]
fn properties() {
let root_job = Job::root();
let proc = Process::create(&root_job, "proc").expect("failed to create process");
proc.set_debug_addr(123);
assert_eq!(proc.get_debug_addr(), 123);
proc.set_dyn_break_on_load(2);
assert_eq!(proc.get_dyn_break_on_load(), 2);
}
#[test]
fn exit() {
let root_job = Job::root();
let proc = Process::create(&root_job, "proc").expect("failed to create process");
let thread = Thread::create(&proc, "thread").expect("failed to create thread");
let info = proc.get_info();
assert!(!info.has_exited && !info.started && info.return_code == 0);
proc.exit(666);
let info = proc.get_info();
assert!(info.has_exited && info.started && info.return_code == 666);
assert_eq!(thread.state(), ThreadState::Dying);
assert_eq!(
Thread::create(&proc, "thread1").err(),
Some(ZxError::BAD_STATE)
);
}
#[test]
fn contains_thread() {
let root_job = Job::root();
let proc = Process::create(&root_job, "proc").expect("failed to create process");
let thread = Thread::create(&proc, "thread").expect("failed to create thread");
let proc1 = Process::create(&root_job, "proc1").expect("failed to create process");
let thread1 = Thread::create(&proc1, "thread1").expect("failed to create thread");
let inner = proc.inner.lock();
assert!(inner.contains_thread(&thread) && !inner.contains_thread(&thread1));
}
#[test]
fn check_policy() {
let root_job = Job::root();
let policy1 = BasicPolicy {
condition: PolicyCondition::BadHandle,
action: PolicyAction::Allow,
};
let policy2 = BasicPolicy {
condition: PolicyCondition::NewChannel,
action: PolicyAction::Deny,
};
assert!(root_job
.set_policy_basic(SetPolicyOptions::Absolute, &[policy1, policy2])
.is_ok());
let proc = Process::create(&root_job, "proc").expect("failed to create process");
assert!(proc.check_policy(PolicyCondition::BadHandle).is_ok());
assert!(proc.check_policy(PolicyCondition::NewProcess).is_ok());
assert_eq!(
proc.check_policy(PolicyCondition::NewChannel).err(),
Some(ZxError::ACCESS_DENIED)
);
let _job = root_job.create_child().unwrap();
assert_eq!(
root_job
.set_policy_basic(SetPolicyOptions::Absolute, &[policy1, policy2])
.err(),
Some(ZxError::BAD_STATE)
);
}
}