Convert Figma logo to code with AI

google logosanitizers

AddressSanitizer, ThreadSanitizer, MemorySanitizer

11,332
1,020
11,332
627

Top Related Projects

14,889

A static analyzer for Java, C, C++, and Objective-C

3,519

Microsoft SEAL is an easy-to-use and powerful homomorphic encryption library.

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.

10,307

OSS-Fuzz - continuous fuzzing for open source software.

3,568

Research programming language for concurrent ownership

96,644

Empowering everyone to build reliable and efficient software.

Quick Overview

The google/sanitizers repository is a collection of dynamic testing tools developed by Google to detect various types of bugs and vulnerabilities in C/C++ code. These tools, known as sanitizers, include AddressSanitizer, ThreadSanitizer, MemorySanitizer, and others, which help developers identify and fix issues related to memory corruption, data races, and undefined behavior.

Pros

  • Highly effective at detecting hard-to-find bugs and security vulnerabilities
  • Integrated into popular compilers like GCC and Clang, making them easy to use
  • Low runtime overhead compared to other dynamic analysis tools
  • Actively maintained and supported by Google and the open-source community

Cons

  • Primarily focused on C/C++ languages, limiting their use for other programming languages
  • Can increase compilation time and binary size
  • May produce false positives in some cases, requiring manual verification
  • Learning curve for interpreting and addressing complex issues reported by the sanitizers

Code Examples

  1. Using AddressSanitizer to detect a buffer overflow:
#include <cstdio>

int main() {
    int array[10];
    array[10] = 0;  // Buffer overflow
    return 0;
}

Compile with: clang++ -fsanitize=address -g example.cpp

  1. Using ThreadSanitizer to detect a data race:
#include <thread>
#include <cstdio>

int shared_var = 0;

void increment() {
    shared_var++;  // Data race
}

int main() {
    std::thread t1(increment);
    std::thread t2(increment);
    t1.join();
    t2.join();
    return 0;
}

Compile with: clang++ -fsanitize=thread -g example.cpp -pthread

  1. Using UndefinedBehaviorSanitizer to detect integer overflow:
#include <cstdint>

int main() {
    int32_t x = INT32_MAX;
    x++;  // Integer overflow
    return 0;
}

Compile with: clang++ -fsanitize=undefined -g example.cpp

Getting Started

To use the sanitizers in your C/C++ project:

  1. Install a compatible compiler (GCC 4.8+ or Clang 3.1+)
  2. Add the appropriate -fsanitize flag to your compilation command:
    • AddressSanitizer: -fsanitize=address
    • ThreadSanitizer: -fsanitize=thread
    • MemorySanitizer: -fsanitize=memory
    • UndefinedBehaviorSanitizer: -fsanitize=undefined
  3. Compile and run your program as usual
  4. Analyze the output for any reported issues

Example:

clang++ -fsanitize=address -g myprogram.cpp -o myprogram
./myprogram

Competitor Comparisons

14,889

A static analyzer for Java, C, C++, and Objective-C

Pros of Infer

  • Supports multiple languages (C, C++, Objective-C, Java)
  • Performs inter-procedural analysis for more comprehensive bug detection
  • Integrates well with CI/CD pipelines and provides detailed reports

Cons of Infer

  • Slower analysis compared to Sanitizers
  • May produce more false positives due to its static analysis approach
  • Steeper learning curve and more complex setup process

Code Comparison

Sanitizers (example usage in C++):

g++ -fsanitize=address -g example.cpp -o example
./example

Infer (example usage):

infer run -- gcc -c example.c
infer run -- javac Example.java

Key Differences

  • Sanitizers focus on runtime detection of memory errors and undefined behavior
  • Infer performs static analysis to find potential bugs before runtime
  • Sanitizers are generally faster but limited to C/C++, while Infer supports more languages but may be slower
  • Infer can detect a wider range of issues, including concurrency problems and resource leaks

Use Cases

  • Sanitizers: Ideal for catching memory-related issues during development and testing
  • Infer: Better suited for large codebases and detecting complex, inter-procedural bugs in CI/CD pipelines
3,519

Microsoft SEAL is an easy-to-use and powerful homomorphic encryption library.

Pros of SEAL

  • Focuses on homomorphic encryption, providing advanced privacy-preserving computation capabilities
  • Offers a more specialized and targeted functionality for secure data processing
  • Actively maintained with regular updates and improvements

Cons of SEAL

  • Narrower scope compared to the broader range of memory error detection tools in sanitizers
  • Steeper learning curve due to the complexity of homomorphic encryption concepts
  • May have higher computational overhead for certain operations

Code Comparison

SEAL (C++):

Encryptor encryptor(context, public_key);
Ciphertext encrypted = encryptor.encrypt(plaintext);

sanitizers (C/C++):

#include <sanitizer/asan_interface.h>
ASAN_POISON_MEMORY_REGION(address, size);

Summary

SEAL focuses on homomorphic encryption, offering advanced privacy-preserving computation capabilities. It's more specialized but has a steeper learning curve. sanitizers provides a broader range of memory error detection tools, making it more versatile for general debugging purposes. SEAL is better suited for secure data processing, while sanitizers excels in identifying and preventing memory-related bugs during development.

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.

Pros of llvm-project

  • Comprehensive compiler infrastructure with multiple frontends and backends
  • Extensive toolchain including Clang, LLDB, and LLD
  • Active development with frequent updates and broad community support

Cons of llvm-project

  • Larger codebase and more complex project structure
  • Steeper learning curve for contributors
  • May require more resources to build and run

Code Comparison

sanitizers:

#include "sanitizer_common/sanitizer_common.h"

void __asan_init() {
  // Initialize AddressSanitizer
}

llvm-project:

#include "llvm/IR/Module.h"
#include "llvm/Pass.h"

struct MyPass : public ModulePass {
  static char ID;
  MyPass() : ModulePass(ID) {}
  bool runOnModule(Module &M) override {
    // Custom pass implementation
    return false;
  }
};

Summary

sanitizers focuses specifically on runtime error detection tools, while llvm-project is a comprehensive compiler infrastructure. sanitizers offers a more targeted approach to memory error detection, while llvm-project provides a complete toolchain for compiler development. The code examples illustrate the difference in scope, with sanitizers implementing specific sanitizer functions and llvm-project demonstrating the creation of custom compiler passes.

10,307

OSS-Fuzz - continuous fuzzing for open source software.

Pros of oss-fuzz

  • Broader scope: Focuses on continuous fuzzing of multiple open-source projects
  • Automated infrastructure: Provides a complete fuzzing pipeline for easier integration
  • Community-driven: Encourages collaboration and contributions from various projects

Cons of oss-fuzz

  • Higher complexity: Requires more setup and configuration compared to sanitizers
  • Resource-intensive: Needs dedicated infrastructure for continuous fuzzing
  • Limited to specific programming languages and build systems

Code comparison

sanitizers:

#include <sanitizer/asan_interface.h>

void *ptr = malloc(10);
ASAN_UNPOISON_MEMORY_REGION(ptr, 10);
free(ptr);

oss-fuzz:

extern "C" int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
  // Fuzz target code here
  return 0;
}

The sanitizers project provides low-level APIs for memory sanitization, while oss-fuzz focuses on integrating fuzzing into the development workflow. sanitizers offers more fine-grained control over memory checks, whereas oss-fuzz simplifies the process of continuous fuzzing for open-source projects.

3,568

Research programming language for concurrent ownership

Pros of Verona

  • Focuses on memory safety and concurrency for large-scale software
  • Introduces novel concepts like concurrent ownership and regions
  • Aims to provide a more comprehensive solution for safe systems programming

Cons of Verona

  • Still in early research stages, not production-ready
  • Limited documentation and community support compared to Sanitizers
  • Requires learning a new programming language and paradigm

Code Comparison

Verona (example of concurrent ownership):

class Counter
{
  var count: U64;

  public create(): Counter & mut
  {
    Counter{ count: 0 }
  }

  public increment(self: Counter & mut)
  {
    self.count = self.count + 1;
  }
}

Sanitizers (example of using AddressSanitizer in C++):

#include <sanitizer/asan_interface.h>

int main() {
    int *array = new int[100];
    delete[] array;
    return *(array + 10);  // Use-after-free, detected by AddressSanitizer
}

Key Differences

  • Sanitizers provide tools for existing languages (C, C++), while Verona introduces a new language
  • Sanitizers focus on detecting specific types of errors, while Verona aims to prevent them by design
  • Verona explores new concepts in concurrent programming, whereas Sanitizers primarily address memory and threading issues in sequential code
96,644

Empowering everyone to build reliable and efficient software.

Pros of Rust

  • Comprehensive language ecosystem with built-in memory safety features
  • Active community and extensive documentation
  • Supports multiple programming paradigms (functional, imperative, concurrent)

Cons of Rust

  • Steeper learning curve, especially for developers new to systems programming
  • Longer compilation times compared to C/C++
  • Less mature tooling ecosystem for certain specialized tasks

Code Comparison

Sanitizers (C++):

#include <sanitizer/asan_interface.h>

void *ptr = malloc(100);
ASAN_POISON_MEMORY_REGION(ptr, 100);
// Use ptr...
ASAN_UNPOISON_MEMORY_REGION(ptr, 100);
free(ptr);

Rust:

let mut data = vec![0; 100];
// Use data...
drop(data); // Automatic memory deallocation

Summary

Sanitizers focuses on providing runtime sanitization tools for C/C++ codebases, while Rust is a full-fledged programming language with built-in safety features. Rust offers a more comprehensive solution for memory safety but may require more initial investment in learning. Sanitizers provides powerful tools for existing C/C++ projects without requiring a complete language switch, but may not catch all issues at compile-time like Rust does.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

sanitizers

This project is the home for Sanitizers: AddressSanitizer, MemorySanitizer, ThreadSanitizer, LeakSanitizer, and more The actual code resides in the LLVM repository. Here we keep extended documentation, bugfixes and some helper code.

The documentation for our tools:

  • AddressSanitizer (detects addressability issues) and LeakSanitizer (detects memory leaks)
  • ThreadSanitizer (detects data races and deadlocks) for C++ and Go
  • MemorySanitizer (detects use of uninitialized memory)
  • HWASAN, or Hardware-assisted AddressSanitizer, a newer variant of AddressSanitizer that consumes much less memory
  • UBSan, or UndefinedBehaviorSanitizer

Some of the sanitizers are also available for different OS Kernels: