summaryrefslogtreecommitdiff
path: root/lib/sanitizer_common/sanitizer_symbolizer.h
diff options
context:
space:
mode:
Diffstat (limited to 'lib/sanitizer_common/sanitizer_symbolizer.h')
-rw-r--r--lib/sanitizer_common/sanitizer_symbolizer.h149
1 files changed, 87 insertions, 62 deletions
diff --git a/lib/sanitizer_common/sanitizer_symbolizer.h b/lib/sanitizer_common/sanitizer_symbolizer.h
index ef37fd387f987..886fed20db030 100644
--- a/lib/sanitizer_common/sanitizer_symbolizer.h
+++ b/lib/sanitizer_common/sanitizer_symbolizer.h
@@ -7,26 +7,21 @@
//
//===----------------------------------------------------------------------===//
//
-// Symbolizer is intended to be used by both
-// AddressSanitizer and ThreadSanitizer to symbolize a given
-// address. It is an analogue of addr2line utility and allows to map
-// instruction address to a location in source code at run-time.
+// Symbolizer is used by sanitizers to map instruction address to a location in
+// source code at run-time. Symbolizer either uses __sanitizer_symbolize_*
+// defined in the program, or (if they are missing) tries to find and
+// launch "llvm-symbolizer" commandline tool in a separate process and
+// communicate with it.
//
-// Symbolizer is planned to use debug information (in DWARF format)
-// in a binary via interface defined in "llvm/DebugInfo/DIContext.h"
-//
-// Symbolizer code should be called from the run-time library of
-// dynamic tools, and generally should not call memory allocation
-// routines or other system library functions intercepted by those tools.
-// Instead, Symbolizer code should use their replacements, defined in
-// "compiler-rt/lib/sanitizer_common/sanitizer_libc.h".
+// Generally we should try to avoid calling system library functions during
+// symbolization (and use their replacements from sanitizer_libc.h instead).
//===----------------------------------------------------------------------===//
#ifndef SANITIZER_SYMBOLIZER_H
#define SANITIZER_SYMBOLIZER_H
+#include "sanitizer_allocator_internal.h"
#include "sanitizer_internal_defs.h"
#include "sanitizer_libc.h"
-// WARNING: Do not include system headers here. See details above.
namespace __sanitizer {
@@ -42,8 +37,14 @@ struct AddressInfo {
AddressInfo() {
internal_memset(this, 0, sizeof(AddressInfo));
}
+
// Deletes all strings and sets all fields to zero.
- void Clear();
+ void Clear() {
+ InternalFree(module);
+ InternalFree(function);
+ InternalFree(file);
+ internal_memset(this, 0, sizeof(AddressInfo));
+ }
void FillAddressAndModuleInfo(uptr addr, const char *mod_name,
uptr mod_offset) {
@@ -62,60 +63,84 @@ struct DataInfo {
uptr size;
};
-// Fills at most "max_frames" elements of "frames" with descriptions
-// for a given address (in all inlined functions). Returns the number
-// of descriptions actually filled.
-// This function should NOT be called from two threads simultaneously.
-uptr SymbolizeCode(uptr address, AddressInfo *frames, uptr max_frames)
- SANITIZER_WEAK_ATTRIBUTE;
-bool SymbolizeData(uptr address, DataInfo *info);
-
-bool IsSymbolizerAvailable();
-void FlushSymbolizer(); // releases internal caches (if any)
-
-// Attempts to demangle the provided C++ mangled name.
-const char *Demangle(const char *Name);
-
-// Starts external symbolizer program in a subprocess. Sanitizer communicates
-// with external symbolizer via pipes.
-bool InitializeExternalSymbolizer(const char *path_to_symbolizer);
-
-class LoadedModule {
+class Symbolizer {
public:
- LoadedModule(const char *module_name, uptr base_address);
- void addAddressRange(uptr beg, uptr end);
- bool containsAddress(uptr address) const;
-
- const char *full_name() const { return full_name_; }
- uptr base_address() const { return base_address_; }
+ /// Returns platform-specific implementation of Symbolizer. The symbolizer
+ /// must be initialized (with init or disable) before calling this function.
+ static Symbolizer *Get();
+ /// Returns platform-specific implementation of Symbolizer, or null if not
+ /// initialized.
+ static Symbolizer *GetOrNull();
+ /// Returns platform-specific implementation of Symbolizer. Will
+ /// automatically initialize symbolizer as if by calling Init(0) if needed.
+ static Symbolizer *GetOrInit();
+ /// Initialize and return the symbolizer, given an optional path to an
+ /// external symbolizer. The path argument is only required for legacy
+ /// reasons as this function will check $PATH for an external symbolizer. Not
+ /// thread safe.
+ static Symbolizer *Init(const char* path_to_external = 0);
+ /// Initialize the symbolizer in a disabled state. Not thread safe.
+ static Symbolizer *Disable();
+ // Fills at most "max_frames" elements of "frames" with descriptions
+ // for a given address (in all inlined functions). Returns the number
+ // of descriptions actually filled.
+ virtual uptr SymbolizeCode(uptr address, AddressInfo *frames,
+ uptr max_frames) {
+ return 0;
+ }
+ virtual bool SymbolizeData(uptr address, DataInfo *info) {
+ return false;
+ }
+ virtual bool IsAvailable() {
+ return false;
+ }
+ virtual bool IsExternalAvailable() {
+ return false;
+ }
+ // Release internal caches (if any).
+ virtual void Flush() {}
+ // Attempts to demangle the provided C++ mangled name.
+ virtual const char *Demangle(const char *name) {
+ return name;
+ }
+ virtual void PrepareForSandboxing() {}
+
+ // Allow user to install hooks that would be called before/after Symbolizer
+ // does the actual file/line info fetching. Specific sanitizers may need this
+ // to distinguish system library calls made in user code from calls made
+ // during in-process symbolization.
+ typedef void (*StartSymbolizationHook)();
+ typedef void (*EndSymbolizationHook)();
+ // May be called at most once.
+ void AddHooks(StartSymbolizationHook start_hook,
+ EndSymbolizationHook end_hook);
private:
- struct AddressRange {
- uptr beg;
- uptr end;
+ /// Platform-specific function for creating a Symbolizer object.
+ static Symbolizer *PlatformInit(const char *path_to_external);
+ /// Create a symbolizer and store it to symbolizer_ without checking if one
+ /// already exists. Not thread safe.
+ static Symbolizer *CreateAndStore(const char *path_to_external);
+
+ static Symbolizer *symbolizer_;
+ static StaticSpinMutex init_mu_;
+
+ protected:
+ Symbolizer();
+
+ static LowLevelAllocator symbolizer_allocator_;
+
+ StartSymbolizationHook start_hook_;
+ EndSymbolizationHook end_hook_;
+ class SymbolizerScope {
+ public:
+ explicit SymbolizerScope(const Symbolizer *sym);
+ ~SymbolizerScope();
+ private:
+ const Symbolizer *sym_;
};
- char *full_name_;
- uptr base_address_;
- static const uptr kMaxNumberOfAddressRanges = 6;
- AddressRange ranges_[kMaxNumberOfAddressRanges];
- uptr n_ranges_;
};
-// Creates external symbolizer connected via pipe, user should write
-// to output_fd and read from input_fd.
-bool StartSymbolizerSubprocess(const char *path_to_symbolizer,
- int *input_fd, int *output_fd);
-
-// OS-dependent function that fills array with descriptions of at most
-// "max_modules" currently loaded modules. Returns the number of
-// initialized modules. If filter is nonzero, ignores modules for which
-// filter(full_name) is false.
-typedef bool (*string_predicate_t)(const char *);
-uptr GetListOfModules(LoadedModule *modules, uptr max_modules,
- string_predicate_t filter);
-
-void SymbolizerPrepareForSandboxing();
-
} // namespace __sanitizer
#endif // SANITIZER_SYMBOLIZER_H