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
//! #libunwind-rs
//!
//! `libunwind-rs`  is a library providing safe Rust API for retrieving backtrace from local and
//! remote process, and also from coredumps. Crate is build on a top of [libunwind] library.
//! 
//! [libunwind]: http://www.nongnu.org/libunwind/
extern crate num_derive;

use libunwind_sys::*;
use std::fmt; 
use std::ffi::CStr;
use foreign_types::{foreign_type, ForeignType};
use std::path::Path;
use std::ffi::CString;
use std::mem::MaybeUninit;
use libc::{c_void, c_char, c_ulong};
use num_derive::FromPrimitive;
use num_traits::FromPrimitive;

/// Error codes.  The unwind routines return the *negated* values of 
/// these error codes on error and a non-negative value on success.
#[derive(Copy, Clone, Debug, PartialEq, Eq,FromPrimitive)]
 pub enum Error {
    ///no error 
    Succsess = 0,
    /// unspecified (general) error 
    Unspec  = -1,        
    /// out of memory 
    NoMem  = -2,     
    /// bad register number 
    BadReg = -3, 
    /// attempt to write read-only register 
    ReadOnlyReg = -4,  
    /// stop unwinding       
    StopUnwind  = -5, 
    /// invalid IP 
    InvalidIp = -6, 
    ///bad frame 
    BadFrame = -7,  
    /// unsupported operation or bad value 
    InVal = -8, 
    /// unwind info has unsupported version 
    BadVersion = -9,  
    /// no unwind info found   
    NoInfo = -10         
}    

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        unsafe {
            let e = CStr::from_ptr(unw_strerror(self.clone() as i32));
            write!(f,"{}", e.to_string_lossy())
        }
    }
}

///These are backend callback routines that provide access to the
///state of a "remote" process.  This can be used, for example, to
///unwind another process through the ptrace() interface.
pub struct Accessors(unw_accessors_t);

impl Accessors {
    /// Method returns Accessors for ptrace()
    #[cfg(feature = "ptrace")]
    pub fn ptrace() -> &'static   Accessors {
        unsafe { &*(&_UPT_accessors as *const unw_accessors_t as *const Accessors) }
    }
    /// Method returns Accessors for coredump
    pub fn coredump() -> &'static   Accessors {
        unsafe { &*(&_UCD_accessors as *const unw_accessors_t as *const Accessors) }
    }
}
///The endianness (byte order) of a stream of bytes
pub enum Byteorder {
    Default = 0,
    LitleEndian = 1234,
    BigEndian = 3214,
    PdpEndian = 3412
}

foreign_type! { 
/// Struct represents an address space of unwinding procces
    pub unsafe type AddressSpace  {
        type CType = libunwind_sys::unw_addr_space;
        fn drop = unw_destroy_addr_space;
    }
}

impl AddressSpace {
    /// Method constructs `AddressSpace` from given accessors and byteorder 
    /// # Arguments
    ///
    /// * `accessors` - Bunch of Accessors functions (Ptrace, Coredump)
    ///
    /// * `byteorder` - Endianess  of target machine
    pub fn new(accessors: &Accessors, byteorder: Byteorder) -> Result<AddressSpace, Error> {
        unsafe {
            let ptr = unw_create_addr_space(
                &accessors.0 as *const unw_accessors_t as *mut unw_accessors_t,
                byteorder as i32,
            );
            if ptr.is_null() {
                Err(Error::Unspec)
            } else {
                Ok(AddressSpace::from_ptr(ptr))
            }
        }
    }
}

foreign_type! { 
    ///This state is used by accessors 
    pub unsafe type CoredumpState {
        type CType = libunwind_sys::UCD_info;
        fn drop = _UCD_destroy;
    }
}

impl CoredumpState {
    /// Method constructs new CoredumpState from path to core file.  
    /// # Arguments
    ///
    /// * `accessors` - Bunch of Accessors functions (Ptrace, Coredump)
    ///
    /// * `byteorder` - Endianess  of target machine
    pub fn new(core_path: &Path) -> Result<CoredumpState, Error> {
        unsafe {
            let core_path = CString::new(core_path.to_str().unwrap()).unwrap();
            let ui = _UCD_create(core_path.as_ptr());
            if ui.is_null() {
                Err(Error::NoMem)
            } else {
                Ok(CoredumpState::from_ptr(ui))
            }
        }
    }
    
    /// Method maps executable to specified  virtual address.  
    /// # Arguments
    ///
    /// * `file_path` - path to executable
    ///
    /// * `vaddr` - address to map
    pub fn load_file_at_vaddr(&mut self, file_path: &Path, vaddr: usize) {
        unsafe {
            let file_path = CString::new(file_path.to_str().unwrap()).unwrap();
            _UCD_add_backing_file_at_vaddr(self.0.as_ptr(), vaddr as c_ulong, file_path.as_ptr());
        }
    }
    /// Method returns current thread id
    pub fn pid(&mut self) -> i32 {
        unsafe {
            _UCD_get_pid(self.0.as_ptr())
         }
    }
    /// Method returns  the number of threads 
    pub fn num_threads(&mut self) -> i32 {
        unsafe {
            _UCD_get_num_threads(self.0.as_ptr())
         }
    }
    /// Method selects thread by provided thread id
    /// # Arguments
    ///
    /// * `id` - thread identifier
    pub fn select_thread(&mut self, id : i32) {
        unsafe {
            _UCD_select_thread(self.0.as_ptr(), id);
        }
    }

    /// Method gets value for memory address
    /// # Arguments
    ///
    /// * `asp` - AddressSpace struct
    /// 
    /// * `address` - memory address to access
    pub fn access_mem(&mut self, asp: &AddressSpace, address: usize) -> Result<usize, Error> {
        unsafe {
            let mut val: unw_word_t = 0;
            let ret = _UCD_access_mem(asp.0.as_ptr(), address as unw_word_t, &mut val, 0, self.0.as_ptr() as * mut libc::c_void);
            if ret == (Error::Succsess as i32) {
                Ok(val as usize)
            } else {
                Err(FromPrimitive::from_i32(ret).unwrap())
            }
        }
    }
}

#[cfg(feature = "ptrace")]
foreign_type! { 
    ///This state is used by accessors 
    pub unsafe type PtraceState {
        type CType = libc::c_void;
        fn drop = _UPT_destroy;
    }
}

#[cfg(feature = "ptrace")]
impl PtraceState {
    /// Method constructs constructs new CoredumpState from path to core file.  
    /// # Arguments
    ///
    /// * `pid` - Pid for remote proccess
    pub fn new(pid: u32) -> Result<PtraceState, Error> {
         unsafe {
            let ptr = _UPT_create(pid as _);
            if ptr.is_null() {
                Err(Error::NoMem)
            } else {
                Ok(PtraceState::from_ptr(ptr))
            }
        }
    }
}

///Information about called procedure
#[derive(Clone, Copy)]
pub struct ProcInfo {
    start_ip: usize,
    end_ip: usize 
}

impl ProcInfo {
    ///Method returns start address of procedure
    pub fn start(&self) -> usize {
        self.start_ip 
    }

    ///Method returns end address of procedure
    pub fn end(&self) -> usize {
        self.end_ip 
    }
}

#[derive(Clone)]
pub struct Cursor(unw_cursor_t);
impl Cursor {
    /// Method constructs cursor for coredump unwinding.  
    /// # Arguments
    ///
    /// * `address_space` - configured AddressSpace 
    ///
    /// * `state` - Configured CoredumpState
    pub fn coredump(address_space: &mut AddressSpace, state: &CoredumpState) -> Result<Cursor, Error> {
        unsafe {
            let mut cursor = MaybeUninit::uninit();
            let ret = unw_init_remote(
                cursor.as_mut_ptr(),
                address_space.0.as_ptr(),
                state.0.as_ptr() as *mut c_void,
            );
            if ret == (Error::Succsess as i32) {
                Ok(Cursor(cursor.assume_init()))
         } else {
                Err(FromPrimitive::from_i32(ret).unwrap())
            }
        }
    }
    
    /// Method constructs cursor for remote  unwinding.  
    /// # Arguments
    ///
    /// * `address_space` - configured AddressSpace 
    ///
    /// * `state` - Configured CoredumpState
    #[cfg(feature = "ptrace")]
    pub fn ptrace(address_space: &mut AddressSpace, state: &PtraceState) -> Result<Cursor, Error> {
        unsafe {
            let mut cursor = MaybeUninit::uninit();
            let ret = unw_init_remote(
                cursor.as_mut_ptr(),
                address_space.0.as_ptr(),
                state.0.as_ptr() as *mut c_void,
            );
            if ret == (Error::Succsess as i32) {
                Ok(Cursor(cursor.assume_init()))
         } else {
                Err(FromPrimitive::from_i32(ret).unwrap())
            }
        }
    }

    /// Method constructs cursor for local  unwinding.  
    /// # Arguments
    ///
    /// * `f` - function to work with local cursor 
    pub fn local<F, T>(f: F) -> Result<T, Error>
    where
        F: FnOnce(Cursor) -> Result<T, Error>,
    {
        unsafe {
            let mut context = MaybeUninit::uninit();
            let ret = unw_getcontext(context.as_mut_ptr());
            if ret != (Error::Succsess as i32) {
                return   Err(FromPrimitive::from_i32(ret).unwrap());
            }
            let mut context = context.assume_init();

            let mut cursor = MaybeUninit::uninit();
            let ret = unw_init_local(cursor.as_mut_ptr(), &mut context);
            if ret != (Error::Succsess as i32) {
                return   Err(FromPrimitive::from_i32(ret).unwrap());
            }

            f(Cursor(cursor.assume_init()))
        }
    }

    /// Method executes step on cursor.  
    /// # Return
    ///
    /// * `true`  - if step is executed
    ///
    /// * `false` - if cursor ends
    ///
    /// * `Error` - if error while steping is occured
    pub fn step(&mut self) -> Result<bool, Error> {
        unsafe {
            let ret = unw_step(&mut self.0);
            if ret > 0 {
                Ok(true)
            } else if ret == 0 {
                Ok(false)
            } else {
                Err(FromPrimitive::from_i32(ret).unwrap())
            }
        }
    }
    
    /// Method returns register value  
    /// # Arguments
    ///
    /// * `id`  - register's identifier
    pub fn register(&mut self, id: i32) -> Result<usize, Error> {
        unsafe {
            let mut value = 0;
            let ret = unw_get_reg(&self.0 as *const _ as *mut _, id, &mut value);
            if ret == (Error::Succsess as i32) {
                Ok(value as usize)
            } else {
                Err(FromPrimitive::from_i32(ret).unwrap())
            }
        }
    }
    
    /// Method returns instructions pointer value 
    pub fn ip(&mut self) ->  Result<usize, Error> {
        unsafe {
            let mut value = 0;
            let ret = unw_get_reg(&self.0 as *const _ as *mut _, libunwind_sys::UNW_TDEP_IP as i32, &mut value);
            if ret == (Error::Succsess as i32) {
                Ok(value as usize)
            } else {
                Err(FromPrimitive::from_i32(ret).unwrap())
            }
        }
    }
    
    /// Method returns stack pointer value 
    pub fn sp(&mut self) ->  Result<usize, Error> {
        unsafe {
            let mut value = 0;
            let ret = unw_get_reg(&self.0 as *const _ as *mut _, libunwind_sys::UNW_TDEP_SP as i32, &mut value);
            if ret == (Error::Succsess as i32) {
                Ok(value as usize)
            } else {
                Err(FromPrimitive::from_i32(ret).unwrap())
            }
        }
    }

    /// Method returns procedure information at crurrent stack frame
    pub fn proc_info(&mut self) -> Result<ProcInfo,Error> {
        unsafe {
            let mut info = MaybeUninit::uninit();
            let ret = unw_get_proc_info(&self.0 as *const _ as *mut _, info.as_mut_ptr());
            if ret == (Error::Succsess as i32) {
                let info = info.assume_init();
                Ok(ProcInfo {
                    start_ip: info.start_ip as usize,
                    end_ip: info.end_ip as usize,
                })
            } else {
                Err(FromPrimitive::from_i32(ret).unwrap())
            }
        }
    }
    
    /// Method returns procedure information at crurrent stack frame
    pub fn proc_name(&mut self) -> Result<String, Error> {
        unsafe {
            let mut name_vec = vec![0;256];
            let mut offset = 0;
            let ret = unw_get_proc_name(& self.0 as *const _ as * mut _, name_vec.as_mut_ptr() as * mut c_char, name_vec.len(), &mut offset);
            if ret == (Error::Succsess as i32) {
                let name = CStr::from_ptr(name_vec.as_mut_ptr());
                Ok(name.to_str().unwrap().to_string())
            } else {
                Err(FromPrimitive::from_i32(ret).unwrap())
            }
        }
    }
    
    /// Method returns true if frame is signal frame
    pub fn is_signal_frame(&mut self) -> Result<bool, Error> {
        unsafe {
            let ret = unw_is_signal_frame(&self.0 as *const _ as *mut _);
            if ret < 0 {
                Err(FromPrimitive::from_i32(ret).unwrap())
            } else {
                Ok(ret != 0)
            }
        }
    }

}
#[cfg(test)]
mod tests {
    use std::path::PathBuf;
    use crate::*;
    #[test]
    #[cfg(target_arch = "x86_64")]
    fn test_core_unwind() {
        let mut libc_path_buf  = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
        libc_path_buf.push("data/libc-2.23.so");
        let mut test_callstack_path_buf  = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
        test_callstack_path_buf.push("data/test_callstack");
        let mut core_path_buf  = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
        core_path_buf.push("data/core.test_callstack");
        let test_callstack_start:usize = 0x400000;
        let libc_start:usize = 0x00007f9ac7468000;
   
        let mut  state = CoredumpState::new(&core_path_buf).unwrap();
        state.load_file_at_vaddr(&test_callstack_path_buf, test_callstack_start);
        state.load_file_at_vaddr(&libc_path_buf, libc_start);
        let mut address_space = AddressSpace::new(Accessors::coredump(), Byteorder::Default).unwrap();
        let mut  cursor = Cursor::coredump(&mut address_space, &state).unwrap();
        
        let mut backtrace = String::new();
        loop { 
            let  ip = cursor.ip().unwrap();
            let sp = cursor.sp().unwrap();
            if let Err(_e) = state.access_mem(&address_space, sp) {
                assert!(false);
            }
            let  name = cursor.proc_name().unwrap();
            backtrace.push_str(&format!("0x{:x} in {:?} ()\n", ip, name));
            let  ret = cursor.step().unwrap();
                if ret == false  {
                    break;
                }
        }
        assert!(backtrace.contains("main"), true);
        assert!(backtrace.contains("first"), true);
        assert!(backtrace.contains("second"), true);
        assert!(backtrace.contains("third"), true);
    }

    #[test]
    #[cfg(target_arch = "x86_64")]
    fn test_core_unwind_heap_error() {
        let mut libc_path_buf  = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
        libc_path_buf.push("data/libc-2.23.so");
        let mut test_heap_path_buf  = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
        test_heap_path_buf.push("data/test_heapError");
        let mut core_path_buf  = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
        core_path_buf.push("data/core.test_heapError");
        let test_heap_start:usize = 0x000055b7b218c000;
        let libc_start:usize = 0x00007f90e058b000;
        
        let mut  state = CoredumpState::new(&core_path_buf).unwrap();
        state.load_file_at_vaddr(&test_heap_path_buf, test_heap_start);
        state.load_file_at_vaddr(&libc_path_buf, libc_start);
        let mut address_space = AddressSpace::new(Accessors::coredump(), Byteorder::Default).unwrap();
        let mut  cursor = Cursor::coredump(&mut address_space, &state).unwrap();
        
        let mut backtrace = String::new();
        loop { 
            let  ip = cursor.ip().unwrap();
            let sp = cursor.sp().unwrap();
            if let Err(_e) = state.access_mem(&address_space, sp) {
                assert!(false);
            }
            let  name = cursor.proc_name().unwrap();
            backtrace.push_str(&format!("0x{:x} in {:?} ()\n", ip, name));
            let  ret = cursor.step().unwrap();
            if ret == false  {
                break;
            }
        }   
        assert!(backtrace.contains("main"), true);
        assert!(backtrace.contains("cfree"), true);
    }
    #[test]
    #[cfg(target_arch = "x86_64")]
    fn test_core_unwind_canary() {
        let mut libc_path_buf  = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
        libc_path_buf.push("data/libc-2.23.so");
        let mut test_canary_path_buf  = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
        test_canary_path_buf.push("data/test_canary");
        let mut core_path_buf  = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
        core_path_buf.push("data/core.test_canary");
        let test_canary_start:usize = 0x0000558672376000;
        let libc_start:usize = 0x00007fc14b336000;

        let mut  state = CoredumpState::new(&core_path_buf).unwrap();
        state.load_file_at_vaddr(&test_canary_path_buf, test_canary_start);
        state.load_file_at_vaddr(&libc_path_buf, libc_start);
        let mut address_space = AddressSpace::new(Accessors::coredump(), Byteorder::Default).unwrap();
        let mut  cursor = Cursor::coredump(&mut address_space, &state).unwrap();
        
        let mut backtrace = String::new();
        loop { 
            let  ip = cursor.ip().unwrap();
            let sp = cursor.sp().unwrap();
            if let Err(_e) = state.access_mem(&address_space, sp) {
                assert!(false);
            }
            let  name = cursor.proc_name().unwrap();
            backtrace.push_str(&format!("0x{:x} in {:?} ()\n", ip, name));
            let  ret = cursor.step().unwrap();
            if ret == false  {
                break;
            }
        }   
        assert!(backtrace.contains("main"), true);
        assert!(backtrace.contains("fortify_fail"), true);
    }
    
    #[test]
    #[cfg(all(target_arch = "x86_64", feature = "ptrace"))]
    fn test_remote_unwind() {
        use std::process::Command;
        use libc::c_void;
        use std::ptr;
        use std::thread;
        use std::time::Duration;
        use std::io;
        
        let mut test_callstack_path_buf  = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
        test_callstack_path_buf.push("data/test_callstack_remote");
        let mut child = Command::new(test_callstack_path_buf.to_str().unwrap())
            .spawn()
            .expect("failed to execute child");
            thread::sleep(Duration::from_millis(10));
        unsafe {
            let ret = libc::ptrace(
            libc::PTRACE_ATTACH,
            child.id() as libc::pid_t,
            ptr::null_mut::<c_void>(),
            ptr::null_mut::<c_void>(),
            );
            if ret != 0 {
                panic!("{}", io::Error::last_os_error());
            }
            loop {
                let mut status = 0;
                let ret = libc::waitpid(child.id() as libc::pid_t, &mut status, 0);
                if ret < 0 {
                    panic!("{}", io::Error::last_os_error());
                }
                if libc::WIFSTOPPED(status) {
                    break;
                }
            }
        }
        let state = PtraceState::new(child.id()).unwrap();
        let mut address_space = AddressSpace::new(Accessors::ptrace(), Byteorder::Default).unwrap();
        let mut  cursor = Cursor::ptrace(&mut address_space, &state).unwrap();
        
        let mut backtrace = String::new();
        loop { 
            let  ip = cursor.ip().unwrap();
            let  name = cursor.proc_name().unwrap();
            backtrace.push_str(&format!("0x{:x} in {:?} ()\n", ip, name));
            let  ret = cursor.step().unwrap();
            if ret == false  {
                break;
            }
        }   
        assert!(backtrace.contains("main"), true);
        assert!(backtrace.contains("first"), true);
        assert!(backtrace.contains("second"), true);
        assert!(backtrace.contains("third"), true);
        child.kill().unwrap();
    }
    #[test]
    #[cfg(target_arch = "x86_64")]
    fn test_local_unwind() {
        let backtrace = Cursor::local(|mut cursor| {

        let mut backtrace = String::new();
            loop { 
                let  ip = cursor.ip().unwrap();
                let  name = cursor.proc_name().unwrap();
                backtrace.push_str(&format!("0x{:x} in {:?} ()\n", ip, name));
                let  ret = cursor.step().unwrap();
                if ret == false  {
                    break;
                }
            }
        Ok(backtrace)
        }).unwrap();
        
        assert!(backtrace.contains("__rust_maybe_catch_panic"), true);
        assert!(backtrace.contains("start_thread") || backtrace.contains("start"), true);
    }
}