Skip to content

Commit

Permalink
Примеры из документации JetBrains по CLion.
Browse files Browse the repository at this point in the history
  • Loading branch information
gusenov committed Sep 14, 2019
1 parent a7d8350 commit da483fd
Show file tree
Hide file tree
Showing 60 changed files with 862 additions and 0 deletions.
15 changes: 15 additions & 0 deletions ASan/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# AddressSanitizer

[AddressSanitizer (ASan)](https://github.com/google/sanitizers/wiki/AddressSanitizer) -
детектор порчи памяти. Может находить следующие типы ошибок:

- Heap-, stack-, and global buffer overflow
- Use-after-free (dangling pointer dereference)
- Use-after-scope ```-fsanitize-address-use-after-scope```
- Use-after-return (pass ```detect_stack_use_after_return=1``` to ```ASAN_OPTIONS```)
- Double free, invalid free
- Initialization order bugs

ASan приостанавливается на первой обнаруженной ошибке.
Чтобы изменить это поведение нужно добавить флаг компиляции ```-fsanitize-recover=address```
и ```halt_on_error=false``` в ```ASAN_OPTIONS```.
73 changes: 73 additions & 0 deletions ASan/global-buffer-overflow/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# ---------------------------------------------------------------------------
# https://github.com/github/gitignore/blob/master/Global/JetBrains.gitignore

# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and WebStorm
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839

# User-specific stuff
.idea/**/workspace.xml
.idea/**/tasks.xml
.idea/**/usage.statistics.xml
.idea/**/dictionaries
.idea/**/shelf

# Generated files
.idea/**/contentModel.xml

# Sensitive or high-churn files
.idea/**/dataSources/
.idea/**/dataSources.ids
.idea/**/dataSources.local.xml
.idea/**/sqlDataSources.xml
.idea/**/dynamic.xml
.idea/**/uiDesigner.xml
.idea/**/dbnavigator.xml

# Gradle
.idea/**/gradle.xml
.idea/**/libraries

# Gradle and Maven with auto-import
# When using Gradle or Maven with auto-import, you should exclude module files,
# since they will be recreated, and may cause churn. Uncomment if using
# auto-import.
# .idea/modules.xml
# .idea/*.iml
# .idea/modules
# *.iml
# *.ipr

# CMake
cmake-build-*/

# Mongo Explorer plugin
.idea/**/mongoSettings.xml

# File-based project format
*.iws

# IntelliJ
out/

# mpeltonen/sbt-idea plugin
.idea_modules/

# JIRA plugin
atlassian-ide-plugin.xml

# Cursive Clojure plugin
.idea/replstate.xml

# Crashlytics plugin (for Android Studio and IntelliJ)
com_crashlytics_export_strings.xml
crashlytics.properties
crashlytics-build.properties
fabric.properties

# Editor-based Rest Client
.idea/httpRequests

# Android studio 3.1+ serialized cache file
.idea/caches/build_file_checksums.ser

# ---------------------------------------------------------------------------
2 changes: 2 additions & 0 deletions ASan/global-buffer-overflow/.idea/global-buffer-overflow.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions ASan/global-buffer-overflow/.idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions ASan/global-buffer-overflow/.idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions ASan/global-buffer-overflow/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
cmake_minimum_required(VERSION 3.14)
project(global-buffer-overflow)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=address -fno-omit-frame-pointer -O1")

add_executable(
global-buffer-overflow
main.cpp
)
4 changes: 4 additions & 0 deletions ASan/global-buffer-overflow/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
![Снимок экрана 2019-09-10 в 17.48.12](%D0%A1%D0%BD%D0%B8%D0%BC%D0%BE%D0%BA%20%D1%8D%D0%BA%D1%80%D0%B0%D0%BD%D0%B0%202019-09-10%20%D0%B2%2017.48.12.png)

![Снимок экрана 2019-09-10 в 18.57.15](%D0%A1%D0%BD%D0%B8%D0%BC%D0%BE%D0%BA%20%D1%8D%D0%BA%D1%80%D0%B0%D0%BD%D0%B0%202019-09-10%20%D0%B2%2018.57.15.png)

5 changes: 5 additions & 0 deletions ASan/global-buffer-overflow/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
int global_array[100] = { -1 };

int main(int argc, const char * argv[]) {
return global_array[argc + 100]; // global buffer overflow
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 13 additions & 0 deletions LSan/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# LeakSanitizer

[LeakSanitizer (LSan)](https://github.com/google/sanitizers/wiki/AddressSanitizerLeakSanitizer)
детектор утечек памяти. В stand-alone режиме это run-time инструмент
не требующий инструментов компиляции. LSan интегрирован в AddressSanitizer
поэтому можно их совместить для обнаружения ошибок памяти и утечек памяти.

Чтобы включить LeakSanitizer как часть AddressSanitizer нужно передать
```detect_leaks=1``` в переменную ```ASAN_OPTIONS```. Чтобы выключить
```detect_leaks=0```.

Чтобы запустить только LSan нужно использовать ```-fsanitize=leak``` вместо
```-fsanitize=address```.
73 changes: 73 additions & 0 deletions LSan/no-del-of-heap-alloc-obj/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# ---------------------------------------------------------------------------
# https://github.com/github/gitignore/blob/master/Global/JetBrains.gitignore

# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and WebStorm
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839

# User-specific stuff
.idea/**/workspace.xml
.idea/**/tasks.xml
.idea/**/usage.statistics.xml
.idea/**/dictionaries
.idea/**/shelf

# Generated files
.idea/**/contentModel.xml

# Sensitive or high-churn files
.idea/**/dataSources/
.idea/**/dataSources.ids
.idea/**/dataSources.local.xml
.idea/**/sqlDataSources.xml
.idea/**/dynamic.xml
.idea/**/uiDesigner.xml
.idea/**/dbnavigator.xml

# Gradle
.idea/**/gradle.xml
.idea/**/libraries

# Gradle and Maven with auto-import
# When using Gradle or Maven with auto-import, you should exclude module files,
# since they will be recreated, and may cause churn. Uncomment if using
# auto-import.
# .idea/modules.xml
# .idea/*.iml
# .idea/modules
# *.iml
# *.ipr

# CMake
cmake-build-*/

# Mongo Explorer plugin
.idea/**/mongoSettings.xml

# File-based project format
*.iws

# IntelliJ
out/

# mpeltonen/sbt-idea plugin
.idea_modules/

# JIRA plugin
atlassian-ide-plugin.xml

# Cursive Clojure plugin
.idea/replstate.xml

# Crashlytics plugin (for Android Studio and IntelliJ)
com_crashlytics_export_strings.xml
crashlytics.properties
crashlytics-build.properties
fabric.properties

# Editor-based Rest Client
.idea/httpRequests

# Android studio 3.1+ serialized cache file
.idea/caches/build_file_checksums.ser

# ---------------------------------------------------------------------------
4 changes: 4 additions & 0 deletions LSan/no-del-of-heap-alloc-obj/.idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions LSan/no-del-of-heap-alloc-obj/.idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions LSan/no-del-of-heap-alloc-obj/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
cmake_minimum_required(VERSION 3.14)
project(no-del-of-heap-alloc-obj)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=leak")

add_executable(
no-del-of-heap-alloc-obj
main.cpp
)
4 changes: 4 additions & 0 deletions LSan/no-del-of-heap-alloc-obj/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
![Снимок экрана 2019-09-10 в 21.16.27](%D0%A1%D0%BD%D0%B8%D0%BC%D0%BE%D0%BA%20%D1%8D%D0%BA%D1%80%D0%B0%D0%BD%D0%B0%202019-09-10%20%D0%B2%2021.16.27.png)

![Снимок экрана 2019-09-10 в 21.17.03](%D0%A1%D0%BD%D0%B8%D0%BC%D0%BE%D0%BA%20%D1%8D%D0%BA%D1%80%D0%B0%D0%BD%D0%B0%202019-09-10%20%D0%B2%2021.17.03.png)

4 changes: 4 additions & 0 deletions LSan/no-del-of-heap-alloc-obj/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
int main(int argc, const char * argv[]) {
int *x = new int(10);
return 0;
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
57 changes: 57 additions & 0 deletions LSan/small-app-with-mem-leak/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# ---------------------------------------------------------------------------
# https://github.com/github/gitignore/blob/master/C.gitignore

# Prerequisites
*.d

# Object files
*.o
*.ko
*.obj
*.elf

# Linker output
*.ilk
*.map
*.exp

# Precompiled Headers
*.gch
*.pch

# Libraries
*.lib
*.a
*.la
*.lo

# Shared objects (inc. Windows DLLs)
*.dll
*.so
*.so.*
*.dylib

# Executables
*.exe
*.out
*.app
*.i*86
*.x86_64
*.hex

# Debug files
*.dSYM/
*.su
*.idb
*.pdb

# Kernel Module Compile Results
*.mod*
*.cmd
.tmp_versions/
modules.order
Module.symvers
Mkfile.old
dkms.conf

# ---------------------------------------------------------------------------
19 changes: 19 additions & 0 deletions LSan/small-app-with-mem-leak/REFERENCES.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Stack Overflow

- [macos - Mac OS: Leaks Sanitizer](https://stackoverflow.com/a/55778432)
- [Build LLVM / Clang on MacOS](https://gist.github.com/datlife/c754535f18b422f6b8d59028c7f31bac)

If we are using XCode 10, you will notice that if you navigate to /usr in the
Finder, you will not see a folder called 'include' anymore which is why the
terminal complains of the absence of the header files which is contained inside
the 'include' folder. In this [release statement](https://developer.apple.com/documentation/xcode_release_notes/xcode_10_release_notes),
(you navigate to _/Library/Developer/CommandLineTools/Packages/macOS_SDK_headers_for_macOS_10.14.pkg_
and run that package to have the 'include' folder installed). Then you should be
good to go.

- [c++ - Getting llvm/clang (from brew) working on OSX](https://stackoverflow.com/a/54659928)
- [macos - Can't compile C program on a Mac after upgrade to Mojave](https://stackoverflow.com/questions/52509602/cant-compile-c-program-on-a-mac-after-upgrade-to-mojave/52530212#comment91963866_52509602)

# Homebrew

- [Clang can no longer find /usr/include header files? fatal error: 'stdlib.h' file not found](https://discourse.brew.sh/t/clang-can-no-longer-find-usr-include-header-files-fatal-error-stdlib-h-file-not-found/4523/2)
20 changes: 20 additions & 0 deletions LSan/small-app-with-mem-leak/build-and-run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/usr/bin/env bash


# clang -fsanitize=address -g mleak.c; ASAN_OPTIONS=detect_leaks=1 ./a.out

# ==11499==AddressSanitizer: detect_leaks is not supported on this platform.
# ./build-and-run.sh: line 3: 11499 Abort trap: 6 ASAN_OPTIONS=detect_leaks=1 ./a.out


export PATH="/usr/local/opt/llvm/bin:$PATH"
which clang

# https://discourse.brew.sh/t/clang-can-no-longer-find-usr-include-header-files-fatal-error-stdlib-h-file-not-found/4523
# clang -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include \
# -fsanitize=address -g mleak.c \
# && ASAN_OPTIONS=detect_leaks=1 ./a.out

clang \
-fsanitize=address -g mleak.c \
&& ASAN_OPTIONS=detect_leaks=1 ./a.out
9 changes: 9 additions & 0 deletions LSan/small-app-with-mem-leak/mleak.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#include <stdlib.h>

void *p;

int main() {
p = malloc(7);
p = 0; // The memory is leaked here.
return 0;
}
12 changes: 12 additions & 0 deletions MSan/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# MemorySanitizer

[MemorySanitizer (MSan)](https://github.com/google/sanitizers/wiki/MemorySanitizer)
обнаруживает чтения неинициализированной памяти. Ищутся случаи, когда
stack- или heap-allocated память читается перед записью. MSan может также
отслеживать неинициализированные биты в bitfield.

**MSan доступен только в Clang для Linux x86_64 targets.**

MSan может отследить происхождение неинициализированного значения в том месте,
где оно было создано, и сообщить эту информацию.
Флаг ```-fsanitize-memory-track-origins``` включает эту функциональность.
Loading

0 comments on commit da483fd

Please sign in to comment.