Home | History | Annotate | Download | only in asan
      1 //===-- asan_process.h ------------------------------------------*- C++ -*-===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is distributed under the University of Illinois Open Source
      6 // License. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 //
     10 // This file is a part of AddressSanitizer, an address sanity checker.
     11 //
     12 // Information about the process mappings.
     13 //===----------------------------------------------------------------------===//
     14 #ifndef ASAN_PROCMAPS_H
     15 #define ASAN_PROCMAPS_H
     16 
     17 #include "asan_internal.h"
     18 
     19 namespace __asan {
     20 
     21 class AsanProcMaps {
     22  public:
     23   AsanProcMaps();
     24   bool Next(uintptr_t *start, uintptr_t *end, uintptr_t *offset,
     25             char filename[], size_t filename_size);
     26   void Reset();
     27   // Gets the object file name and the offset in that object for a given
     28   // address 'addr'. Returns true on success.
     29   bool GetObjectNameAndOffset(uintptr_t addr, uintptr_t *offset,
     30                               char filename[], size_t filename_size);
     31   ~AsanProcMaps();
     32  private:
     33   // Default implementation of GetObjectNameAndOffset.
     34   // Quite slow, because it iterates through the whole process map for each
     35   // lookup.
     36   bool IterateForObjectNameAndOffset(uintptr_t addr, uintptr_t *offset,
     37                                      char filename[], size_t filename_size) {
     38     Reset();
     39     uintptr_t start, end, file_offset;
     40     for (int i = 0; Next(&start, &end, &file_offset, filename, filename_size);
     41          i++) {
     42       if (addr >= start && addr < end) {
     43         // Don't subtract 'start' for the first entry. Don't ask me why.
     44         *offset = (addr - (i ? start : 0)) + file_offset;
     45         return true;
     46       }
     47     }
     48     if (filename_size)
     49       filename[0] = '\0';
     50     return false;
     51   }
     52 
     53 #if defined __linux__
     54   char *proc_self_maps_buff_;
     55   size_t proc_self_maps_buff_mmaped_size_;
     56   size_t proc_self_maps_buff_len_;
     57   char *current_;
     58 #elif defined __APPLE__
     59   template<uint32_t kLCSegment, typename SegmentCommand>
     60   bool NextSegmentLoad(uintptr_t *start, uintptr_t *end, uintptr_t *offset,
     61                        char filename[], size_t filename_size);
     62   int current_image_;
     63   uint32_t current_magic_;
     64   int current_load_cmd_count_;
     65   char *current_load_cmd_addr_;
     66 #endif
     67 };
     68 
     69 }  // namespace __asan
     70 
     71 #endif  // ASAN_PROCMAPS_H
     72