DEV Community

Cover image for Use Valgrind in your CI / CD
DURAND Malo
DURAND Malo

Posted on

Use Valgrind in your CI / CD

Hello everyone !

Today I will show you how to use Valgrind to easily check for memory leaks on your code inside a GitHub Action.

Why ?

You have to use Valgrind to make sure that your code does not implement any memory leak, any unclosed file descriptor and more important : do not make bad free() calls.

Some people say that checking for memory leaks or unclosed file descriptor is not really useful as the Linux kernel will reclaim that memory anyway. But Valgrind is not only used for this purpose, even though it's well-known for it.

Valgrind is also useful because your program may be trying to read or write inside memory that it does not own, or it may be trying to free a memory you already free'd, and so on... causing your program to crash.

How ?

How to easily add Valgrind checks to your GitHub Action workflows ?

The easiest way is to check the Valgrind Action I released for that purpose. It will check for different kind of bad memory management / usage :

  • unfree'd memory,
  • invalid read or write operations,
  • unclose'd file descriptors (files and sockets),
  • invalid usage of free, delete, delete [] or realloc,
  • uninitialised syscall params,
  • overlaps between sources and destinations for memcpy, memmove, etc...,
  • fishy arguments (possibly negative values) for unsigned expected,
  • memory allocation with a size of 0,
  • invalid alignment values.

You can also check the errors description provided in the 4.2. Explanation of error messages from Memcheck section on Valgrind's documentation.

To use it, it's pretty simple :

name: "ci"

on:
    -   push

jobs:
    # <Some jobs to make sure every thing is alright>

    run-tests:
        runs-on: "ubuntu-latest"
        steps:
            -   name: "Checkout"
                uses: "actions/checkout@v4.1.1"

            # Outputs a binary containing all the unit tests to be
            # executed
            -   name: "Run tests"
                timeout-minutes: 1
                run: "make tests_run"

            # Perform the unit tests along with the Valgrind checks
            # over both tests and code implementation
            -   name: "Valgrind Checks"
                uses: "Ximaz/valgrind-action@v1.2.0"
                with:
                    binary_path: "./unit_tests"

    # <The rest of the code (deploy, webhook, etc...)>
Enter fullscreen mode Exit fullscreen mode

Thanks

Thanks for reading. If you enjoyed this article or think that this action will be helpful to you, consider checking it out at https://github.com/ximaz/valgrind-action and leaving a star to it. <3

Top comments (0)