Skip to content
Merged
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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,5 @@ Sources for the [examples section](https://docs.conan.io/2/examples.html) of the
### [Libraries examples](examples/libraries)

### [Graph examples](examples/graph)

### [Security examples](examples/security)
3 changes: 3 additions & 0 deletions examples/security/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Conan security examples

### [Using Compiler Sanitizers with Conan](sanitizers)
5 changes: 5 additions & 0 deletions examples/security/sanitizers/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Using Compiler Sanitizers with Conan

This example follows the documented page https://docs.conan.io/2/examples/security/sanitizers.html about using compiler sanitizers with Conan.

For more information, please refer to the [C, C++ Compiler Sanitizers¶](https://docs.conan.io/2/security/sanitizers.html) documentation page.
22 changes: 22 additions & 0 deletions examples/security/sanitizers/ci_test_example.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
@echo off
setlocal enabledelayedexpansion

echo Setup settings user
for /f "usebackq delims=" %%H in (conan config home) do set "CONAN_HOME=%%H"
copy /Y settings_user.yml "%CONAN_HOME%"

echo Conan Examples 2: Compiler Sanitizers - Index Out of Bounds

CD index_out_of_bounds/
CALL conan build . -pr ../profiles/msvc_asan -c tools.compilation:verbosity=verbose
CALL build/Debug/index_out_of_bounds 2>nul || echo Process completed with errors (expected for sanitizer demo)
CD ..

echo Conan Examples 2: Compiler Sanitizers - Signed Integer Overflow

CD signed_integer_overflow/
CALL conan build . -pr ../profiles/msvc_asan -c tools.compilation:verbosity=verbose
CALL build/Debug/signed_integer_overflow 2>nul || echo Process completed with errors (expected for sanitizer demo)
CD ..

exit /b 0
21 changes: 21 additions & 0 deletions examples/security/sanitizers/ci_test_example.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/bin/bash

set -e
set -x

echo "Setup settings user"
cp -f settings_user.yml $(conan config home)

echo "Conan Examples 2: Compiler Sanitizers - Index Out of Bounds"

pushd index_out_of_bounds/
conan build . -pr ../profiles/clang_asan -c tools.compilation:verbosity=verbose
build/Debug/index_out_of_bounds || true
popd

echo "Conan Examples 2: Compiler Sanitizers - Signed Integer Overflow"

pushd signed_integer_overflow/
conan build . -pr ../profiles/clang_asan_ubsan -c tools.compilation:verbosity=verbose
build/Debug/signed_integer_overflow || true
popd
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
cmake_minimum_required(VERSION 3.15)
project(index_out_of_bounds LANGUAGES CXX)

add_executable(index_out_of_bounds main.cpp)
target_compile_features(index_out_of_bounds PUBLIC cxx_std_11)

include(GNUInstallDirs)
install(TARGETS index_out_of_bounds
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
28 changes: 28 additions & 0 deletions examples/security/sanitizers/index_out_of_bounds/conanfile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from conan import ConanFile
from conan.tools.cmake import CMake, cmake_layout, CMakeToolchain

required_conan_version = ">=2.1.0"

class IndexOutOfBoundsConan(ConanFile):
name = "index_out_of_bounds"
version = "0.1.0"
settings = "os", "arch", "compiler", "build_type"
exports_sources = "CMakeLists.txt", "main.cpp"
package_type = "application"
languages = ["C++"]

def layout(self):
cmake_layout(self)

def generate(self):
tc = CMakeToolchain(self)
tc.generate()

def build(self):
cmake = CMake(self)
cmake.configure()
cmake.build()

def package(self):
cmake = CMake(self)
cmake.install()
15 changes: 15 additions & 0 deletions examples/security/sanitizers/index_out_of_bounds/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#include <iostream>
#include <cstdlib>

int main() {
#ifdef __SANITIZE_ADDRESS__
std::cout << "Address sanitizer enabled\n";
#else
std::cout << "Address sanitizer not enabled\n";
#endif

int foo[100];
foo[100] = 42; // Out-of-bounds write

return EXIT_SUCCESS;
}
14 changes: 14 additions & 0 deletions examples/security/sanitizers/profiles/clang_asan
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
include(default)

[settings]
build_type=Debug
compiler.sanitizer=Address

[conf]
tools.build:cflags=['-fsanitize=address']
tools.build:cxxflags=['-fsanitize=address']
tools.build:exelinkflags=['-fsanitize=address']
tools.build:sharedlinkflags+=["-fsanitize=address"]

[runenv]
ASAN_OPTIONS="halt_on_error=1:detect_leaks=1"
11 changes: 11 additions & 0 deletions examples/security/sanitizers/profiles/clang_asan_ubsan
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
include(default)

[settings]
build_type=Debug
compiler.sanitizer=AddressUndefinedBehavior

[conf]
tools.build:cflags=['-fsanitize=address,undefined']
tools.build:cxxflags=['-fsanitize=address,undefined']
tools.build:exelinkflags=['-fsanitize=address,undefined']
tools.build:sharedlinkflags+=["-fsanitize=address"]
9 changes: 9 additions & 0 deletions examples/security/sanitizers/settings_user.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
compiler:
gcc:
sanitizer: [null, Address, Leak, Thread, UndefinedBehavior, HardwareAssistanceAddress, KernelAddress, AddressUndefinedBehavior, ThreadUndefinedBehavior]
clang:
sanitizer: [null, Address, Leak, Thread, Memory, UndefinedBehavior, HardwareAssistanceAddress, KernelAddress, AddressUndefinedBehavior, ThreadUndefinedBehavior]
apple-clang:
sanitizer: [null, Address, Leak, Thread, Memory, UndefinedBehavior, HardwareAssistanceAddress, KernelAddress, AddressUndefinedBehavior, ThreadUndefinedBehavior]
msvc:
sanitizer: [null, Address, KernelAddress]
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
cmake_minimum_required(VERSION 3.15)
project(signed_integer_overflow LANGUAGES CXX)

add_executable(signed_integer_overflow main.cpp)
target_compile_features(signed_integer_overflow PUBLIC cxx_std_11)

include(GNUInstallDirs)
install(TARGETS signed_integer_overflow
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
28 changes: 28 additions & 0 deletions examples/security/sanitizers/signed_integer_overflow/conanfile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from conan import ConanFile
from conan.tools.cmake import CMake, cmake_layout, CMakeToolchain

required_conan_version = ">=2.1.0"

class SignedIntegerOverflowConan(ConanFile):
name = "signed_integer_overflow"
version = "0.1.0"
settings = "os", "arch", "compiler", "build_type"
exports_sources = "CMakeLists.txt", "main.cpp"
package_type = "application"
languages = ["C++"]

def layout(self):
cmake_layout(self)

def generate(self):
tc = CMakeToolchain(self)
tc.generate()

def build(self):
cmake = CMake(self)
cmake.configure()
cmake.build()

def package(self):
cmake = CMake(self)
cmake.install()
16 changes: 16 additions & 0 deletions examples/security/sanitizers/signed_integer_overflow/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include <iostream>
#include <cstdlib>
#include <cstdint>

int main(int argc, char* argv[]) {
#ifdef __SANITIZE_ADDRESS__
std::cout << "Address sanitizer enabled\n";
#else
std::cout << "Address sanitizer not enabled\n";
#endif

int foo = 0x7fffffff;
foo += argc; // Signed integer overflow

return EXIT_SUCCESS;
}