Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 82 additions & 0 deletions .github/workflows/allocator.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
name: Memory allocator selection

on:
pull_request:
branches: [ develop ]

jobs:
# Build cbmc with each CBMC_ALLOCATOR value and confirm the resulting binary
# links (or does not link) the expected allocator.
cmake-allocator:
runs-on: ubuntu-24.04
strategy:
fail-fast: false
matrix:
include:
- allocator: system
packages: ""
expect: none
- allocator: tcmalloc
packages: libgoogle-perftools-dev
expect: tcmalloc
- allocator: jemalloc
packages: libjemalloc-dev
expect: jemalloc
- allocator: auto
packages: libgoogle-perftools-dev
expect: tcmalloc
steps:
- uses: actions/checkout@v6
with:
submodules: recursive
- name: Fetch dependencies
env:
DEBIAN_FRONTEND: noninteractive
run: |
sudo apt-get update
sudo apt-get install --no-install-recommends -yq cmake ninja-build g++ flex bison ccache ${{ matrix.packages }}
- name: Configure with CBMC_ALLOCATOR=${{ matrix.allocator }}
run: cmake -S . -Bbuild -G Ninja -DWITH_JBMC=OFF -DCBMC_ALLOCATOR=${{ matrix.allocator }}
- name: Build cbmc
run: cmake --build build --target cbmc -j$(nproc)
- name: Confirm the binary links the expected allocator
run: |
echo "Allocators linked into build/bin/cbmc:"
ldd build/bin/cbmc | grep -iE "tcmalloc|jemalloc" || true
case "${{ matrix.expect }}" in
tcmalloc)
ldd build/bin/cbmc | grep -q tcmalloc \
|| { echo "ERROR: tcmalloc not linked"; exit 1; } ;;
jemalloc)
ldd build/bin/cbmc | grep -q jemalloc \
|| { echo "ERROR: jemalloc not linked"; exit 1; } ;;
none)
if ldd build/bin/cbmc | grep -qiE "tcmalloc|jemalloc"; then
echo "ERROR: an alternative allocator was unexpectedly linked"
exit 1
fi ;;
esac

# The Makefile build does not auto-detect an allocator; confirm the documented
# manual approach (LIBS=-ltcmalloc_minimal) produces a binary linking tcmalloc.
makefile-tcmalloc:
runs-on: ubuntu-24.04
steps:
- uses: actions/checkout@v6
with:
submodules: recursive
- name: Fetch dependencies
env:
DEBIAN_FRONTEND: noninteractive
run: |
sudo apt-get update
sudo apt-get install --no-install-recommends -yq g++ flex bison ccache libgoogle-perftools-dev
- name: Build cbmc with make, linking tcmalloc via LIBS
run: |
make -C src minisat2-download
make -C src CXX="ccache g++" LIBS="-ltcmalloc_minimal" -j$(nproc) cbmc.dir
- name: Confirm the binary links tcmalloc
run: |
ldd src/cbmc/cbmc | grep -iE "tcmalloc" || true
ldd src/cbmc/cbmc | grep -q tcmalloc \
|| { echo "ERROR: tcmalloc not linked"; exit 1; }
1 change: 1 addition & 0 deletions .github/workflows/build-and-test-Linux.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ jobs:
sudo apt-get install --no-install-recommends -y clang-19 clang++-19 flex bison cmake ninja-build maven jq libxml2-utils dpkg-dev ccache
sudo apt-get install --no-install-recommends -y libssl-dev libelf-dev libudev-dev libpci-dev libiberty-dev autoconf
sudo apt-get install --no-install-recommends -y gawk jq
sudo apt-get install --no-install-recommends -y libgoogle-perftools-dev

- name: Restore ccache
id: restore-ccache
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/coverage.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ jobs:
DEBIAN_FRONTEND: noninteractive
run: |
sudo apt-get update
sudo apt-get install --no-install-recommends -y g++ gcc gdb binutils flex bison cmake maven jq libxml2-utils openjdk-11-jdk-headless lcov ccache z3
sudo apt-get install --no-install-recommends -y g++ gcc gdb binutils flex bison cmake maven jq libxml2-utils openjdk-11-jdk-headless lcov ccache z3 libgoogle-perftools-dev
- name: Confirm z3 solver is available and log the version installed
run: z3 --version
- name: Install cvc5
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/performance.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ jobs:
DEBIAN_FRONTEND: noninteractive
run: |
sudo apt-get update
sudo apt-get install --no-install-recommends -yq cmake ninja-build gcc g++ maven flex bison ccache
sudo apt-get install --no-install-recommends -yq cmake ninja-build gcc g++ maven flex bison ccache libgoogle-perftools-dev

- name: Prepare ccache
uses: actions/cache/restore@v5
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/pull-request-checks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:
DEBIAN_FRONTEND: noninteractive
run: |
sudo apt-get update
sudo apt-get install --no-install-recommends -yq gcc gdb g++ maven jq flex bison libxml2-utils ccache cmake z3
sudo apt-get install --no-install-recommends -yq gcc gdb g++ maven jq flex bison libxml2-utils ccache cmake z3 libgoogle-perftools-dev

Copilot AI Apr 27, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Installing libgoogle-perftools-dev in CI will likely cause the default allocator=auto to select tcmalloc, changing the allocator used in most CI runs versus developer environments and historical CI baselines. Consider either (a) explicitly configuring CI builds with -Dallocator=system to keep CI consistent, or (b) gating installation/usage to a dedicated performance job that validates allocator linking, rather than affecting all build/test jobs.

Suggested change
sudo apt-get install --no-install-recommends -yq gcc gdb g++ maven jq flex bison libxml2-utils ccache cmake z3 libgoogle-perftools-dev
sudo apt-get install --no-install-recommends -yq gcc gdb g++ maven jq flex bison libxml2-utils ccache cmake z3

Copilot uses AI. Check for mistakes.
- name: Confirm z3 solver is available and log the version installed
run: z3 --version
- name: Install cvc5
Expand Down Expand Up @@ -237,7 +237,7 @@ jobs:
DEBIAN_FRONTEND: noninteractive
run: |
sudo apt-get update
sudo apt-get install --no-install-recommends -yq cmake ninja-build gcc gdb g++ maven flex bison libxml2-utils dpkg-dev ccache doxygen graphviz z3
sudo apt-get install --no-install-recommends -yq cmake ninja-build gcc gdb g++ maven flex bison libxml2-utils dpkg-dev ccache doxygen graphviz z3 libgoogle-perftools-dev
- name: Confirm z3 solver is available and log the version installed
run: z3 --version
- name: Install cvc5
Expand Down Expand Up @@ -506,7 +506,7 @@ jobs:
DEBIAN_FRONTEND: noninteractive
run: |
sudo apt-get update
sudo apt-get install --no-install-recommends -yq cmake ninja-build gcc gdb g++ maven flex bison libxml2-utils dpkg-dev ccache doxygen graphviz z3
sudo apt-get install --no-install-recommends -yq cmake ninja-build gcc gdb g++ maven flex bison libxml2-utils dpkg-dev ccache doxygen graphviz z3 libgoogle-perftools-dev
- name: Confirm z3 solver is available and log the version installed
run: z3 --version
- name: Install cvc5
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/release-packages.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
- name: Fetch dependencies
run: |
sudo apt-get update
sudo apt-get install --no-install-recommends -y g++ gdb flex bison cmake ninja-build maven jq libxml2-utils dpkg-dev ccache z3
sudo apt-get install --no-install-recommends -y g++ gdb flex bison cmake ninja-build maven jq libxml2-utils dpkg-dev ccache z3 libgoogle-perftools-dev
- name: Confirm z3 solver is available and log the version installed
run: z3 --version
- name: Install cvc5
Expand Down Expand Up @@ -88,7 +88,7 @@ jobs:
- name: Fetch dependencies
run: |
sudo apt-get update
sudo apt-get install --no-install-recommends -y g++ gdb flex bison cmake ninja-build maven jq libxml2-utils dpkg-dev ccache z3
sudo apt-get install --no-install-recommends -y g++ gdb flex bison cmake ninja-build maven jq libxml2-utils dpkg-dev ccache z3 libgoogle-perftools-dev
- name: Confirm z3 solver is available and log the version installed
run: z3 --version
- name: Install cvc5
Expand Down Expand Up @@ -159,7 +159,7 @@ jobs:
- name: Fetch dependencies
run: |
sudo apt-get update
sudo apt-get install --no-install-recommends -y g++ gdb flex bison cmake ninja-build maven jq libxml2-utils dpkg-dev ccache z3
sudo apt-get install --no-install-recommends -y g++ gdb flex bison cmake ninja-build maven jq libxml2-utils dpkg-dev ccache z3 libgoogle-perftools-dev
- name: Confirm z3 solver is available and log the version installed
run: z3 --version
- name: Install cvc5
Expand Down
64 changes: 64 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,70 @@ if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang" OR
endif()
endif()

# Alternative memory allocator for improved performance.
# tcmalloc and jemalloc give 18-25% speedup on typical CBMC workloads
# due to better handling of many small allocations (irept tree nodes).
# Set to "auto" to detect, or "tcmalloc", "jemalloc", or "system".
set(CBMC_ALLOCATOR "auto" CACHE STRING
"Memory allocator to link: auto, tcmalloc, jemalloc, or system")
set_property(CACHE CBMC_ALLOCATOR PROPERTY STRINGS auto tcmalloc jemalloc system)
if(NOT CBMC_ALLOCATOR MATCHES "^(auto|tcmalloc|jemalloc|system)$")
message(FATAL_ERROR
"Invalid CBMC_ALLOCATOR='${CBMC_ALLOCATOR}'; "
"expected one of: auto, tcmalloc, jemalloc, system")
endif()

if(NOT CBMC_ALLOCATOR STREQUAL "system")
if(CBMC_ALLOCATOR STREQUAL "auto" OR CBMC_ALLOCATOR STREQUAL "tcmalloc")
find_library(CBMC_TCMALLOC_LIB NAMES tcmalloc_minimal tcmalloc)
mark_as_advanced(CBMC_TCMALLOC_LIB)
endif()
if(CBMC_ALLOCATOR STREQUAL "auto" OR CBMC_ALLOCATOR STREQUAL "jemalloc")
find_library(CBMC_JEMALLOC_LIB NAMES jemalloc)
mark_as_advanced(CBMC_JEMALLOC_LIB)
endif()

if(CBMC_ALLOCATOR STREQUAL "tcmalloc")
if(NOT CBMC_TCMALLOC_LIB)
message(FATAL_ERROR "tcmalloc requested but not found")
endif()
set(CBMC_ALLOCATOR_LIB ${CBMC_TCMALLOC_LIB})
message(STATUS "Using tcmalloc: ${CBMC_TCMALLOC_LIB}")
elseif(CBMC_ALLOCATOR STREQUAL "jemalloc")
if(NOT CBMC_JEMALLOC_LIB)
message(FATAL_ERROR "jemalloc requested but not found")
endif()
set(CBMC_ALLOCATOR_LIB ${CBMC_JEMALLOC_LIB})
message(STATUS "Using jemalloc: ${CBMC_JEMALLOC_LIB}")
elseif(CBMC_ALLOCATOR STREQUAL "auto")
if(CBMC_TCMALLOC_LIB)
set(CBMC_ALLOCATOR_LIB ${CBMC_TCMALLOC_LIB})
message(STATUS "Auto-detected tcmalloc: ${CBMC_TCMALLOC_LIB}")
elseif(CBMC_JEMALLOC_LIB)
set(CBMC_ALLOCATOR_LIB ${CBMC_JEMALLOC_LIB})
message(STATUS "Auto-detected jemalloc: ${CBMC_JEMALLOC_LIB}")
else()
message(STATUS "No alternative allocator found, using system malloc")
endif()
endif()
endif()

# Expose the chosen allocator via an interface library. It is attached to the
# `util` library (see src/util/CMakeLists.txt), which every CBMC/JBMC
# executable and the unit-test binary transitively link, so the allocator is
# applied uniformly without per-executable boilerplate. When no alternative
# allocator is selected this is an empty interface library, i.e. a no-op.
add_library(cprover_allocator INTERFACE)
if(CBMC_ALLOCATOR_LIB)
target_link_libraries(cprover_allocator INTERFACE ${CBMC_ALLOCATOR_LIB})
# gperftools recommends building with these flags so the compiler does not
# inline or special-case the libc malloc family at call sites, ensuring all
# allocations are routed through the replacement allocator.
target_compile_options(cprover_allocator INTERFACE
-fno-builtin-malloc -fno-builtin-calloc
-fno-builtin-realloc -fno-builtin-free)
endif()

function(cprover_default_properties)
set(CBMC_CXX_STANDARD ${CMAKE_CXX_STANDARD})
set(CBMC_CXX_STANDARD_REQUIRED ${CMAKE_CXX_STANDARD_REQUIRED})
Expand Down
28 changes: 28 additions & 0 deletions COMPILING.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,26 @@ files.
Development Kit and Maven is difficult, you can avoid needing these
dependencies by not building JBMC. Just pass `-DWITH_JBMC=OFF` to `cmake`.

**Memory allocator**: CBMC allocates large numbers of small objects, so it
benefits from a faster `malloc`. The CMake build accepts
`-DCBMC_ALLOCATOR=<value>`:

- `auto` (the default): link [tcmalloc](https://github.com/gperftools/gperftools)
if found, otherwise [jemalloc](https://github.com/jemalloc/jemalloc),
otherwise the system allocator;
- `tcmalloc` or `jemalloc`: require that allocator (configuration fails with a
fatal error if it is not found);
- `system`: always use the system allocator.

The selected allocator is linked into every executable (CBMC, JBMC, the
unit-test binary, ...). On Linux, install tcmalloc with
`apt-get install libgoogle-perftools-dev` (or jemalloc with
`apt-get install libjemalloc-dev`) before configuring. On macOS and Windows
the system allocator is already competitive, so `auto` typically resolves to
`system`. Note that CMake caches the library lookup: if you install tcmalloc
or jemalloc *after* a first `cmake` run, delete `CMakeCache.txt` (or pass
`-DCBMC_TCMALLOC_LIB=<path>`) so the new library is picked up.

Finally, to enable building universal binaries on macOS, you can pass the
flag `-DCMAKE_OSX_ARCHITECTURES=i386;x86_64`. If you don't supply this flag,
the built binaries will only work on the architecture of the machine being
Expand Down Expand Up @@ -182,6 +202,14 @@ We assume that you have a Debian/Ubuntu or Red Hat-like distribution.
See doc/architectural/compilation-and-development.md for instructions on how
to use a SAT solver other than MiniSat 2.

Unlike the CMake build (see "Building using CMake"), the Makefile build does
not auto-detect tcmalloc/jemalloc. To link tcmalloc, install it
(`apt-get install libgoogle-perftools-dev`) and pass it via `LIBS`, which is
appended to the link command:
```
make -C src LIBS="-ltcmalloc_minimal"
```

4. To compile JBMC, do
```
make -C jbmc/src setup-submodules
Expand Down
5 changes: 5 additions & 0 deletions src/util/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ add_dependencies(util generate_version_cpp)
generic_includes(util)

target_link_libraries(util big-int langapi)
# Link the selected memory allocator (see CBMC_ALLOCATOR in the top-level
# CMakeLists.txt) via util, so every executable and test binary that links
# util picks it up. cprover_allocator is an empty interface library when the
# system allocator is used.
target_link_libraries(util cprover_allocator)
if(WIN32)
target_link_libraries(util dbghelp)
endif()
Loading