Skip to main content
Redhat Developers  Logo
  • Products

    Platforms

    • Red Hat Enterprise Linux
      Red Hat Enterprise Linux Icon
    • Red Hat AI
      Red Hat AI
    • Red Hat OpenShift
      Openshift icon
    • Red Hat Ansible Automation Platform
      Ansible icon
    • View All Red Hat Products

    Featured

    • Red Hat build of OpenJDK
    • Red Hat Developer Hub
    • Red Hat JBoss Enterprise Application Platform
    • Red Hat OpenShift Dev Spaces
    • Red Hat OpenShift Local
    • Red Hat Developer Sandbox

      Try Red Hat products and technologies without setup or configuration fees for 30 days with this shared Openshift and Kubernetes cluster.
    • Try at no cost
  • Technologies

    Featured

    • AI/ML
      AI/ML Icon
    • Linux
      Linux Icon
    • Kubernetes
      Cloud icon
    • Automation
      Automation Icon showing arrows moving in a circle around a gear
    • View All Technologies
    • Programming Languages & Frameworks

      • Java
      • Python
      • JavaScript
    • System Design & Architecture

      • Red Hat architecture and design patterns
      • Microservices
      • Event-Driven Architecture
      • Databases
    • Developer Productivity

      • Developer productivity
      • Developer Tools
      • GitOps
    • Automated Data Processing

      • AI/ML
      • Data Science
      • Apache Kafka on Kubernetes
    • Platform Engineering

      • DevOps
      • DevSecOps
      • Ansible automation for applications and services
    • Secure Development & Architectures

      • Security
      • Secure coding
  • Learn

    Featured

    • Kubernetes & Cloud Native
      Openshift icon
    • Linux
      Rhel icon
    • Automation
      Ansible cloud icon
    • AI/ML
      AI/ML Icon
    • View All Learning Resources

    E-Books

    • GitOps Cookbook
    • Podman in Action
    • Kubernetes Operators
    • The Path to GitOps
    • View All E-books

    Cheat Sheets

    • Linux Commands
    • Bash Commands
    • Git
    • systemd Commands
    • View All Cheat Sheets

    Documentation

    • Product Documentation
    • API Catalog
    • Legacy Documentation
  • Developer Sandbox

    Developer Sandbox

    • Access Red Hat’s products and technologies without setup or configuration, and start developing quicker than ever before with our new, no-cost sandbox environments.
    • Explore Developer Sandbox

    Featured Developer Sandbox activities

    • Get started with your Developer Sandbox
    • OpenShift virtualization and application modernization using the Developer Sandbox
    • Explore all Developer Sandbox activities

    Ready to start developing apps?

    • Try at no cost
  • Blog
  • Events
  • Videos

Improvements in memstomp

October 7, 2014
Jeff Law
Related topics:
Developer Tools
Related products:
Developer Tools

Share:

    memstomp is an interposition library to detect cases where applications may exhibit undefined behaviour when calling routines within the C library (glibc).

    The first version of memstomp was focused on detecting cases where source and destination memory regions passed to C library routines such as memcpy overlapped in ways not allowed by the ISO C standard.  Matt Newsome's blog post shows how to utilize memstomp to find that class of bugs.

    For many years, GCC has attempted to eliminate unnecessary NULL pointer checks.  GCC 4.9 (now available in Developer Toolset 3) improves on this optimization by utilizing guarantees made by the ISO C standards about the validity of certain pointer arguments passed to library functions.  In particular the standard mandates that certain arguments to certain functions must have a non-NULL value.   For example, the source and destination pointers passed to memcpy must not be NULL, even if the count of bytes to copy is zero. So for example:

    int copy (int* dest, int* src, size_t nbytes) {
      memcopy (dest, src, nbytes);
      if (src != NULL)
        return *src;
      return 0;
    }
    

    Invokes undefined behavior at runtime if "src" or "dest" is ever NULL. Thus, GCC can validly transform that function into:

    int copy (int* dest, int* src, size_t nbytes) {
      memcpy (dest, src, nbytes);
      return *src;
    }

     

    During the development and early deployment of GCC 4.9, we encountered several instances where code violated the ISO C standards in this way. As a result we enhanced memstomp to detect these cases. As a trivial example consider:

    #include <stdlib.h>
    #include <string.h>
    
    char *src = NULL;
    size_t size = 0;
    char *dest;
    main() {
        dest = malloc (size);
        memcpy (dest, src, size);
    }
    [law ~]# gcc foo.c
    [law ~]# memstomp -q ./a.out
    memmove NULL pointer a.out(18279)
        /lib64/libmemstomp.so(memmove+0x7c) [0x7f0309539c3c]
        ./a.out() [0x4005d6]
        /lib64/libc.so.6(__libc_start_main+0xf4) [0x36fb021d64]
        ./a.out() [0x4004d8]
    

    As desired, memstomp identified that this code passed a NULL pointer to memcpy and issued an error. What's particularly useful about memstomp is that it can be used on existing applications without recompiling them and that it's significantly faster than valgrind.

    Just for fun, on my Fedora 21-Alpha machine:

    [law ~]# ( memstomp -q vi foo.c > /dev/null ) > & ERR
    :q
    [law ~]# cat ERR
    memmove NULL pointer vi(18283)
        /lib64/libmemstomp.so(memmove+0x7c) [0x7fb5caf2ac3c]
        vi() [0x433225]
        vi() [0x4337e6]
        vi() [0x433f00]
        vi() [0x42608b]
        vi() [0x4074e5]
        vi() [0x4039d4]
        /lib64/libc.so.6(__libc_start_main+0xf4) [0x36fb021d64]
        vi() [0x404bd8]
    

    So clearly there's a bug in VIM that I need to get filed.

    This version of memstomp is part of Fedora 21 and not in RHEL at this time.  As usual, we'd welcome your feedback!

    Last updated: April 5, 2018

    Recent Posts

    • Profiling vLLM Inference Server with GPU acceleration on RHEL

    • Network performance in distributed training: Maximizing GPU utilization on OpenShift

    • Clang bytecode interpreter update

    • How Red Hat has redefined continuous performance testing

    • Simplify OpenShift installation in air-gapped environments

    Red Hat Developers logo LinkedIn YouTube Twitter Facebook

    Platforms

    • Red Hat AI
    • Red Hat Enterprise Linux
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform
    • See all products

    Build

    • Developer Sandbox
    • Developer Tools
    • Interactive Tutorials
    • API Catalog

    Quicklinks

    • Learning Resources
    • E-books
    • Cheat Sheets
    • Blog
    • Events
    • Newsletter

    Communicate

    • About us
    • Contact sales
    • Find a partner
    • Report a website issue
    • Site Status Dashboard
    • Report a security problem

    RED HAT DEVELOPER

    Build here. Go anywhere.

    We serve the builders. The problem solvers who create careers with code.

    Join us if you’re a developer, software engineer, web designer, front-end designer, UX designer, computer scientist, architect, tester, product manager, project manager or team lead.

    Sign me up

    Red Hat legal and privacy links

    • About Red Hat
    • Jobs
    • Events
    • Locations
    • Contact Red Hat
    • Red Hat Blog
    • Inclusion at Red Hat
    • Cool Stuff Store
    • Red Hat Summit
    © 2025 Red Hat

    Red Hat legal and privacy links

    • Privacy statement
    • Terms of use
    • All policies and guidelines
    • Digital accessibility

    Report a website issue