# MemoryCheckCommand for CTest Memcheck with \[Address/Thread/Memory\]Sanitizer

**URL:** https://discourse.cmake.org/t/memorycheckcommand-for-ctest-memcheck-with-address-thread-memory-sanitizer/1019
**Category:** Usage
**Created:** [April 14, 2020, 1:07pm UTC](https://discourse.cmake.org/t/memorycheckcommand-for-ctest-memcheck-with-address-thread-memory-sanitizer/1019 "2020-04-14T13:07:57Z")
**Posts on this page:** 14
**Page:** 1

<div class="post-metadata">

### Author: ![shaq](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/s/ee59a6/32.png) [@shaq](https://discourse.cmake.org/u/shaq)
#### Post date: [April 14, 2020, 1:07pm UTC](https://discourse.cmake.org/t/memorycheckcommand-for-ctest-memcheck-with-address-thread-memory-sanitizer/1019/1 "2020-04-14T13:07:57Z")

</div>

Hi,

I’m trying to setup CTest memcheck for build with sanitizers. I have an issue with missing MemoryCheckCommand/CTEST\_MEMORYCHECK\_COMMAND setting:

> Memory checker (MemoryCheckCommand) not set, or cannot find the specified program.  
> Errors while running CTest

I don’t know how to set CTEST\_MEMORYCHECK\_COMMAND, because sanitizers do not require any special command and documentation isn’t really helpful ([https://cmake.org/cmake/help/latest/variable/CTEST\_MEMORYCHECK\_COMMAND.html#variable:CTEST\_MEMORYCHECK\_COMMAND](https://cmake.org/cmake/help/latest/variable/CTEST_MEMORYCHECK_COMMAND.html#variable:CTEST_MEMORYCHECK_COMMAND)). I expected that if CTEST\_MEMORYCHECK\_TYPE is set to one of sanitizers ([https://cmake.org/cmake/help/latest/variable/CTEST\_MEMORYCHECK\_TYPE.html#variable:CTEST\_MEMORYCHECK\_TYPE](https://cmake.org/cmake/help/latest/variable/CTEST_MEMORYCHECK_TYPE.html#variable:CTEST_MEMORYCHECK_TYPE)) and sanitizer options are passed to CTEST\_MEMORYCHECK\_SANITIZER\_OPTIONS, it should be enough to successful run tests.

Is there any documentation how to setup CTest with AddressSanitizer or any other sanitizer? I’ve searched for examples on GitHub, but none of them is really helpful. Most devs use just valgrind with ctest or set their on custom targets.

CMake version: 3.17  
CTest command used: `ctest --output-on-failure -D ExperimentalMemCheck`  
ASAN settings I’m using in CMakeLists.txt:

```auto
set(CTEST_MEMORYCHECK_TYPE "AddressSanitizer")
set(CTEST_MEMORYCHECK_SANITIZER_OPTIONS "verbosity=1:symbolize=1:abort_on_error=1:detect_leaks=1")
add_compile_options("-fsanitize=address")
add_link_options("-fsanitize=address")

```

Br,  
Shaq

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [April 14, 2020, 1:25pm UTC](https://discourse.cmake.org/t/memorycheckcommand-for-ctest-memcheck-with-address-thread-memory-sanitizer/1019/2 "2020-04-14T13:25:47Z")

</div>

> [@shaq](#):
>
> ```cmake
> set(CTEST_MEMORYCHECK_TYPE "AddressSanitizer")
> set(CTEST_MEMORYCHECK_SANITIZER_OPTIONS "verbosity=1:symbolize=1:abort_on_error=1:detect_leaks=1")
> 
> ```

These only apply in CTest scripts; they have no effect in `CMakeLists.txt`. There, you need to use `MEMORYCHECK_TYPE` and `MEMORYCHECK_SANITIZER_OPTIONS` (as per the `DartConfiguration.tcl.in` template file).

---

<div class="post-metadata">

### Author: ![shaq](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/s/ee59a6/32.png) [@shaq](https://discourse.cmake.org/u/shaq)
#### Post date: [April 14, 2020, 2:09pm UTC](https://discourse.cmake.org/t/memorycheckcommand-for-ctest-memcheck-with-address-thread-memory-sanitizer/1019/3 "2020-04-14T14:09:52Z")

</div>

Thanks for anwser. With `MEMORYCHECK_TYPE` and `MEMORYCHECK_SANITIZER_OPTIONS` in CMakeLists.txt I got same results:

> Memory checker (MemoryCheckCommand) not set, or cannot find the specified program.
> 
> Errors while running CTest

Any other ideas?

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [April 14, 2020, 2:18pm UTC](https://discourse.cmake.org/t/memorycheckcommand-for-ctest-memcheck-with-address-thread-memory-sanitizer/1019/4 "2020-04-14T14:18:41Z")

</div>

What does the relevant section of your `DartConfiguration.tcl` look like?

---

<div class="post-metadata">

### Author: ![shaq](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/s/ee59a6/32.png) [@shaq](https://discourse.cmake.org/u/shaq)
#### Post date: [April 14, 2020, 3:19pm UTC](https://discourse.cmake.org/t/memorycheckcommand-for-ctest-memcheck-with-address-thread-memory-sanitizer/1019/5 "2020-04-14T15:19:07Z")

</div>

my DartConfiguration.tcl looks like:

```auto
# Dynamic analysis (MemCheck)
PurifyCommand: 
ValgrindCommand: 
ValgrindCommandOptions: 
DrMemoryCommand: 
DrMemoryCommandOptions: 
MemoryCheckType: AddressSanitizer
MemoryCheckSanitizerOptions: verbosity=1:symbolize=1:abort_on_error=1:detect_leaks=1
MemoryCheckCommand: MEMORYCHECK_COMMAND-NOTFOUND
MemoryCheckCommandOptions: 
MemoryCheckSuppressionFile: 

```

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [April 14, 2020, 6:01pm UTC](https://discourse.cmake.org/t/memorycheckcommand-for-ctest-memcheck-with-address-thread-memory-sanitizer/1019/6 "2020-04-14T18:01:31Z")

</div>

Hmm. Maybe just setting `MEMORYCHECK_COMMAND` to the empty string explicitly is necessary?

---

<div class="post-metadata">

### Author: ![shaq](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/s/ee59a6/32.png) [@shaq](https://discourse.cmake.org/u/shaq)
#### Post date: [April 14, 2020, 7:00pm UTC](https://discourse.cmake.org/t/memorycheckcommand-for-ctest-memcheck-with-address-thread-memory-sanitizer/1019/7 "2020-04-14T19:00:19Z")

</div>

Hi,

With: `set(MEMORYCHECK_COMMAND "")`

`DartConfiguration.tcl`:

```auto
# Dynamic analysis (MemCheck)
PurifyCommand:
ValgrindCommand:
ValgrindCommandOptions:
DrMemoryCommand:
DrMemoryCommandOptions:
MemoryCheckType: AddressSanitizer
MemoryCheckSanitizerOptions: verbosity=1:symbolize=1:abort_on_error=1:detect_leaks=1
MemoryCheckCommand:
MemoryCheckCommandOptions:
MemoryCheckSuppressionFile:

```

But result is the same:

```auto
Memory checker (MemoryCheckCommand) not set, or cannot find the specified program.
Errors while running CTest

```

I can reproduce it on macOs and Linux.

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [April 14, 2020, 7:31pm UTC](https://discourse.cmake.org/t/memorycheckcommand-for-ctest-memcheck-with-address-thread-memory-sanitizer/1019/8 "2020-04-14T19:31:36Z")

</div>

Hmm. This seems like a defect in CMake. It shouldn’t require a memcheck command if an instrumented build is being used. It seems that I’m getting lucky where `valgrind` ends up being used as the command because it happens to be installed. Please file an issue (in the meantime, you can just set the command to `/usr/bin/false` or something.

---

<div class="post-metadata">

### Author: ![shaq](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/s/ee59a6/32.png) [@shaq](https://discourse.cmake.org/u/shaq)
#### Post date: [April 14, 2020, 8:09pm UTC](https://discourse.cmake.org/t/memorycheckcommand-for-ctest-memcheck-with-address-thread-memory-sanitizer/1019/9 "2020-04-14T20:09:09Z")

</div>

Hi,

With `set(MEMORYCHECK_COMMAND "/usr/bin/false")` issue is still present.

`DartConfiguration.tcl` :

```auto
# Dynamic analysis (MemCheck)
PurifyCommand:
ValgrindCommand:
ValgrindCommandOptions:
DrMemoryCommand:
DrMemoryCommandOptions:
MemoryCheckType: AddressSanitizer
MemoryCheckSanitizerOptions: verbosity=1:symbolize=1:abort_on_error=1:detect_leaks=1
MemoryCheckCommand: /usr/bin/false
MemoryCheckCommandOptions:
MemoryCheckSuppressionFile:

```

/usr/bin/false exists:

```auto
$ ls /usr/bin/false
/usr/bin/false
$ /usr/bin/false
$ echo $? 
1

```

```auto
$ ctest --output-on-failure -D ExperimentalMemCheck ...
Memory checker (MemoryCheckCommand) not set, or cannot find the specified program.
Errors while running CTest

```

I’ve prepared minimal example: [https://bitbucket.org/shaqmz/cmake\_sanitizer/](https://bitbucket.org/shaqmz/cmake_sanitizer/)  
Steps to reproduce:

- mkdir build && cd build
- cmake …
- make
- ctest --output-on-failure -D ExperimentalMemCheck

---

<div class="post-metadata">

### Author: ![shaq](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/s/ee59a6/32.png) [@shaq](https://discourse.cmake.org/u/shaq)
#### Post date: [April 14, 2020, 9:00pm UTC](https://discourse.cmake.org/t/memorycheckcommand-for-ctest-memcheck-with-address-thread-memory-sanitizer/1019/10 "2020-04-14T21:00:32Z")

</div>

Any ideas worth trying to workaround CMake issue?

Bug report filed: [https://gitlab.kitware.com/cmake/cmake/-/issues/20584](https://gitlab.kitware.com/cmake/cmake/-/issues/20584)

---

<div class="post-metadata">

### Author: ![craig.scott](https://discourse.cmake.org/user_avatar/discourse.cmake.org/craig.scott/32/20_2.png) [@craig.scott](https://discourse.cmake.org/u/craig.scott)
#### Post date: [April 14, 2020, 10:11pm UTC](https://discourse.cmake.org/t/memorycheckcommand-for-ctest-memcheck-with-address-thread-memory-sanitizer/1019/11 "2020-04-14T22:11:08Z")

</div>

To use one of the sanitizers, set `MEMORYCHECK_TYPE` to one of the following strings (`MEMORYCHECK_COMMAND` will then be ignored):

- `AddressSanitizer`
- `LeakSanitizer`
- `MemorySanitizer`
- `ThreadSanitizer`
- `UndefinedBehaviorSanitizer`

I have some recollection that you may also need to add the `-fno-omit-frame-pointer` compiler flag as well (at least in some cases), but I don’t recall the details.

**Edit:** Just saw your earlier reply where you said you tried this. If it isn’t working for you, it would be interesting if you could try an earlier CMake version. It’s possible there has been a regression.

---

<div class="post-metadata">

### Author: ![shaq](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/s/ee59a6/32.png) [@shaq](https://discourse.cmake.org/u/shaq)
#### Post date: [April 15, 2020, 12:48pm UTC](https://discourse.cmake.org/t/memorycheckcommand-for-ctest-memcheck-with-address-thread-memory-sanitizer/1019/12 "2020-04-15T12:48:47Z")

</div>

@craig.scott @ben.boeckel to allow better understanding of problem I’ve setup CI verifivation like you’ve asked. I’ve tested latest minor version of all cmake major version starting from 3.1.3. With unset `MEMORYCHECK_COMMAND`, `MEMORYCHECK_COMMAND` set to `""` and `MEMORYCHECK_COMMAND` set to `"/usr/bin/false"`. Each job prints `DartConfiguration.tcl` content and performs test if `/usr/bin/false` exists in file system. Results are the same in all cases:

```auto
Memory checker (MemoryCheckCommand) not set, or cannot find the specified program.
Errors while running CTest

```

Please check CMakeLists.txt for obvious errors, but I couldn’t find any: [https://bitbucket.org/shaqmz/cmake\_sanitizer/src/master/CMakeLists.txt](https://bitbucket.org/shaqmz/cmake_sanitizer/src/master/CMakeLists.txt)

If you have some additional tips what could be worth trying, please share anything.

repo: [https://bitbucket.org/shaqmz/cmake\_sanitizer/src/master/](https://bitbucket.org/shaqmz/cmake_sanitizer/src/master/)  
CI jobs: [https://travis-ci.com/bitbucket/shaqmz/cmake\_sanitizer/builds/160373047](https://travis-ci.com/bitbucket/shaqmz/cmake_sanitizer/builds/160373047)

example jobs:  
unset MEMORYCHECK\_COMMAND: [https://travis-ci.com/bitbucket/shaqmz/cmake\_sanitizer/jobs/319589253](https://travis-ci.com/bitbucket/shaqmz/cmake_sanitizer/jobs/319589253)  
MEMORYCHECK\_COMMAND set to “”: [https://travis-ci.com/bitbucket/shaqmz/cmake\_sanitizer/jobs/319589263](https://travis-ci.com/bitbucket/shaqmz/cmake_sanitizer/jobs/319589263)  
MEMORYCHECK\_COMMAND set to dummy app: [https://travis-ci.com/bitbucket/shaqmz/cmake\_sanitizer/jobs/319589274](https://travis-ci.com/bitbucket/shaqmz/cmake_sanitizer/jobs/319589274)

Additionally I’ve check CTest sources and couldn’t find regression test validating CTest behaviour with sanitizers, so I guess there is some mistake in CTest source code. I’ve tried simple code analysis with grep and find and couldn’t find “CMakeCommand” defined from [https://gitlab.kitware.com/cmake/cmake/-/blob/master/Source/CTest/cmCTestMemCheckHandler.cxx](https://gitlab.kitware.com/cmake/cmake/-/blob/master/Source/CTest/cmCTestMemCheckHandler.cxx) line 493. Also other handlers are obtaining `ActualCommand` using other methods.

---

<div class="post-metadata">

### Author: ![shaq](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/s/ee59a6/32.png) [@shaq](https://discourse.cmake.org/u/shaq)
#### Post date: [April 15, 2020, 2:32pm UTC](https://discourse.cmake.org/t/memorycheckcommand-for-ctest-memcheck-with-address-thread-memory-sanitizer/1019/13 "2020-04-15T14:32:38Z")

</div>

I made mistake in tests with dummy app. `/usr/bin/false` was not present in CI vm. I’ve repeated test with `/bin/false` instead and added check for availability of passed application on filesystem.

Results are the same ☹.

Changes made: hxxps://bitbucket.org/shaqmz/cmake\_sanitizer/commits/6a0f6f7afb362fed27b3825d8a29a585cc37c806 (please change xx -\> tt)

Results: [https://travis-ci.com/bitbucket/shaqmz/cmake\_sanitizer/builds/160390649](https://travis-ci.com/bitbucket/shaqmz/cmake_sanitizer/builds/160390649)

---

<div class="post-metadata">

### Author: ![craig.scott](https://discourse.cmake.org/user_avatar/discourse.cmake.org/craig.scott/32/20_2.png) [@craig.scott](https://discourse.cmake.org/u/craig.scott)
#### Post date: [April 15, 2020, 9:42pm UTC](https://discourse.cmake.org/t/memorycheckcommand-for-ctest-memcheck-with-address-thread-memory-sanitizer/1019/14 "2020-04-15T21:42:49Z")

</div>

Since this currently looks like a bug, let’s keep discussion in the gitlab issue from this point rather than duplicating comments in both places. We can record any final outcome back here at the end once we resolve what’s happening.
