├── .gitignore ├── tests ├── fail_sleep.cpp ├── fail_usleep.cpp ├── fail_mutex_lock.cpp ├── fail_thread_join.cpp ├── fail_free.cpp ├── fail_valloc.cpp ├── fail_calloc.cpp ├── fail_malloc.cpp ├── fail_mutex_unlock.cpp ├── fail_realloc.cpp ├── fail_nanosleep.cpp ├── fail_posix_memalign.cpp ├── fail_vector.cpp ├── pass_nothrow_exception.cpp ├── fail_stdsleep.cpp ├── fail_thread_create.cpp ├── fail_custom_function.cpp ├── pass_small_atomic.cpp ├── fail_large_function.cpp ├── pass_small_function.cpp ├── ignoring │ ├── pass_malloc_ignore.cpp │ └── pass_memory_ignore.cpp ├── fail_reallocf.cpp ├── fail_file_size.cpp ├── fail_throw_exception.cpp ├── fail_large_thread_local.cpp ├── fail_large_atomic.cpp ├── fail_syscall.cpp ├── fail_mutex_unique_lock_contended.cpp ├── fail_false_mutex_unique_lock_uncontended.cpp ├── fail_shared_mutex_shared_lock_contended.cpp ├── fail_shared_mutex_shared_lock_uncontended.cpp ├── fail_mmap.cpp ├── fail_mutex_unique_try_lock_contended.cpp ├── fail_false_mutex_unique_try_lock_uncontended.cpp ├── fail_munmap.cpp ├── fail_open.cpp ├── fail_read_file.cpp ├── fail_fcntl.cpp ├── CMakeLists.txt └── pass_unit_tests.cpp ├── CMakeLists.txt ├── src ├── CMakeLists.txt ├── interception.h ├── rtcheck.h └── rtcheck.cpp ├── .github └── workflows │ └── build.yml ├── rtcheck_icon.svg ├── README.md └── LICENSE /.gitignore: -------------------------------------------------------------------------------- 1 | .idea 2 | build_* 3 | cmake-build* 4 | Temporary/ -------------------------------------------------------------------------------- /tests/fail_sleep.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() 5 | { 6 | rtc::realtime_context rc; 7 | sleep(1); 8 | 9 | return 0; 10 | } 11 | -------------------------------------------------------------------------------- /tests/fail_usleep.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() 5 | { 6 | rtc::realtime_context rc; 7 | usleep(100); 8 | 9 | return 0; 10 | } 11 | -------------------------------------------------------------------------------- /tests/fail_mutex_lock.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | 5 | int main() 6 | { 7 | std::mutex m; 8 | 9 | rtc::realtime_context rc; 10 | m.lock(); 11 | 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /tests/fail_thread_join.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() 5 | { 6 | std::thread t ([] {}); 7 | 8 | rtc::realtime_context rc; 9 | t.join(); 10 | 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /tests/fail_free.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() 5 | { 6 | volatile auto res = malloc (1024); 7 | 8 | rtc::realtime_context rc; 9 | free (res); 10 | 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /tests/fail_valloc.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() 5 | { 6 | rtc::realtime_context rc; 7 | 8 | [[ maybe_unused ]] volatile auto res = valloc (1024); 9 | 10 | return 0; 11 | } 12 | -------------------------------------------------------------------------------- /tests/fail_calloc.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() 5 | { 6 | rtc::realtime_context rc; 7 | 8 | [[ maybe_unused ]] volatile auto res = calloc (1024, 4); 9 | 10 | return 0; 11 | } 12 | -------------------------------------------------------------------------------- /tests/fail_malloc.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | 5 | int main() 6 | { 7 | rtc::realtime_context rc; 8 | 9 | [[ maybe_unused ]] volatile auto res = malloc (1024); 10 | 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /tests/fail_mutex_unlock.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | 5 | int main() 6 | { 7 | std::mutex m; 8 | m.lock(); 9 | 10 | rtc::realtime_context rc; 11 | m.unlock(); 12 | 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /tests/fail_realloc.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() 5 | { 6 | volatile auto res = malloc (1024); 7 | 8 | rtc::realtime_context rc; 9 | res = realloc (res, 1024 * 4); 10 | 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /tests/fail_nanosleep.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main() 6 | { 7 | timespec req; 8 | 9 | rtc::realtime_context rc; 10 | nanosleep(&req, nullptr); 11 | 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /tests/fail_posix_memalign.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() 5 | { 6 | rtc::realtime_context rc; 7 | 8 | void* p; 9 | [[ maybe_unused ]] auto res = posix_memalign (&p, 32, 128); 10 | 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /tests/fail_vector.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include 4 | 5 | 6 | int main() 7 | { 8 | std::vector vec; 9 | 10 | { 11 | rtc::realtime_context rc; 12 | vec.reserve (42); 13 | } 14 | 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /tests/pass_nothrow_exception.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() 5 | { 6 | rtc::realtime_context rc; 7 | 8 | try 9 | { 10 | } 11 | catch (std::runtime_error) 12 | { 13 | } 14 | 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /tests/fail_stdsleep.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main() 6 | { 7 | using namespace std::chrono_literals; 8 | 9 | rtc::realtime_context rc; 10 | std::this_thread::sleep_for (1ns); 11 | 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /tests/fail_thread_create.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() 5 | { 6 | std::thread t; 7 | 8 | { 9 | rtc::realtime_context rc; 10 | t = std::thread ([] {}); 11 | } 12 | 13 | t.join(); 14 | 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /tests/fail_custom_function.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void my_func() 4 | { 5 | rtc::log_function_if_realtime_context (__func__); 6 | } 7 | 8 | int main() 9 | { 10 | my_func(); 11 | 12 | rtc::realtime_context rc; 13 | my_func(); 14 | 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /tests/pass_small_atomic.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | 6 | int main() 7 | { 8 | std::atomic a; 9 | assert(a.is_lock_free()); 10 | 11 | rtc::realtime_context rc; 12 | a = 42.0f; 13 | 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /tests/fail_large_function.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct doubles 5 | { 6 | double d[8]; 7 | }; 8 | 9 | int main() 10 | { 11 | rtc::realtime_context rc; 12 | doubles d; 13 | std::function fn = [d] { }; 14 | 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /tests/pass_small_function.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct doubles 5 | { 6 | double d[2]; 7 | }; 8 | 9 | int main() 10 | { 11 | rtc::realtime_context rc; 12 | doubles d; 13 | std::function fn = [d] { }; 14 | 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /tests/ignoring/pass_malloc_ignore.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() 5 | { 6 | rtc::realtime_context rc; 7 | rtc::disable_checks_for_thread (rtc::check_flags::malloc); 8 | 9 | [[ maybe_unused ]] auto res = malloc (1024); 10 | 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /tests/fail_reallocf.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() 5 | { 6 | #if __APPLE__ 7 | auto res = malloc (1024); 8 | 9 | rtc::realtime_context rc; 10 | res = reallocf (res, 1024 * 4); 11 | 12 | return 0; 13 | #else 14 | return 1; 15 | #endif 16 | } 17 | -------------------------------------------------------------------------------- /tests/fail_file_size.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | 5 | int main() 6 | { 7 | rtc::realtime_context rc; 8 | 9 | std::filesystem::path awk_path ("/usr/bin/awk"); 10 | [[maybe_unused]] std::uintmax_t file_size = std::filesystem::file_size (awk_path); 11 | 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /tests/fail_throw_exception.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() 5 | { 6 | rtc::realtime_context rc; 7 | 8 | try 9 | { 10 | throw (std::runtime_error ("runtime_error")); 11 | } 12 | catch (std::runtime_error) 13 | { 14 | } 15 | 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /tests/ignoring/pass_memory_ignore.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() 5 | { 6 | rtc::realtime_context rc; 7 | rtc::disable_checks_for_thread (rtc::check_flags::memory); 8 | 9 | auto res = malloc (1024); 10 | res = realloc(res, 1024 * 2); 11 | free(res); 12 | 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /tests/fail_large_thread_local.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #if __APPLE__ 5 | int main() 6 | { 7 | rtc::realtime_context rc; 8 | thread_local std::array ten_megabytes; 9 | ten_megabytes[0] = std::byte {1}; 10 | 11 | return 0; 12 | } 13 | #else 14 | int main() 15 | { 16 | return 1; 17 | } 18 | #endif 19 | -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.20) 2 | project(rtcheck) 3 | 4 | set(CMAKE_CTEST_ARGUMENTS "--build-and-test;${CMAKE_SOURCE_DIR};${CMAKE_BINARY_DIR};--build-generator;${CMAKE_GENERATOR};--test-command;ctest") 5 | enable_testing() 6 | 7 | set(CMAKE_POSITION_INDEPENDENT_CODE ON) 8 | 9 | add_subdirectory(src) 10 | 11 | if(rtcheck_IS_TOP_LEVEL) 12 | add_subdirectory(tests) 13 | endif() -------------------------------------------------------------------------------- /src/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | #cmake_minimum_required(VERSION 3.20) 2 | #project(rtcheck) 3 | 4 | set(CMAKE_CXX_STANDARD 20) 5 | set(CMAKE_CXX_STANDARD_REQUIRED ON) 6 | 7 | #====================================== 8 | add_library(rtcheck SHARED 9 | rtcheck.cpp 10 | ) 11 | 12 | target_include_directories(rtcheck 13 | PUBLIC 14 | . 15 | ) 16 | 17 | target_link_libraries(rtcheck 18 | pthread 19 | dl 20 | ) -------------------------------------------------------------------------------- /tests/fail_large_atomic.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | struct doubles 7 | { 8 | double d1[4]; 9 | }; 10 | 11 | int main() 12 | { 13 | std::atomic a; 14 | assert (! a.is_lock_free()); 15 | 16 | doubles d { 1.0, 2.0, 3.0, 4.0 }; 17 | 18 | rtc::realtime_context rc; 19 | a.store (d); 20 | 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /tests/fail_syscall.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | 6 | int main() 7 | { 8 | rtc::realtime_context rc; 9 | 10 | #pragma clang diagnostic push 11 | // syscall is deprecated, but still in use in libc++ 12 | #pragma clang diagnostic ignored "-Wdeprecated-declarations" 13 | 14 | [[maybe_unused]] pid_t tid = syscall(SYS_gettid); 15 | 16 | #pragma clang diagnostic pop 17 | 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /tests/fail_mutex_unique_lock_contended.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | void sleep_non_realtime() 6 | { 7 | using namespace std::chrono_literals; 8 | 9 | rtc::non_realtime_context nrc; 10 | std::this_thread::sleep_for (1s); 11 | } 12 | 13 | int main() 14 | { 15 | std::mutex m; 16 | 17 | std::thread t1 ([&] 18 | { 19 | rtc::realtime_context rc; 20 | 21 | std::unique_lock l (m); 22 | sleep_non_realtime(); 23 | }); 24 | 25 | std::thread t2 ([&] 26 | { 27 | rtc::realtime_context rc; 28 | 29 | std::unique_lock l (m); 30 | sleep_non_realtime(); 31 | }); 32 | t1.join(); 33 | t2.join(); 34 | 35 | return 0; 36 | } 37 | -------------------------------------------------------------------------------- /.github/workflows/build.yml: -------------------------------------------------------------------------------- 1 | name: Build 2 | on: 3 | push: 4 | workflow_dispatch: 5 | 6 | env: 7 | BUILD_CONFIG: Debug 8 | BUILD_DIR: build 9 | 10 | jobs: 11 | test: 12 | strategy: 13 | fail-fast: false 14 | matrix: 15 | include: 16 | - name: linux 17 | os: ubuntu-latest 18 | generator: "Unix Makefiles" 19 | - name: macOS 20 | os: macos-latest 21 | generator: "Xcode" 22 | 23 | runs-on: ${{ matrix.os }} 24 | steps: 25 | - uses: actions/checkout@v4 26 | with: 27 | submodules: true 28 | 29 | - name: "Build and test" 30 | shell: bash 31 | env: 32 | GENERATOR: ${{ matrix.generator }} 33 | run: | 34 | ctest --build-and-test . ./build_$OSTYPE --build-generator "${{ matrix.generator }}" --test-command ctest -C ${{ env.BUILD_CONFIG }} --verbose 35 | -------------------------------------------------------------------------------- /tests/fail_false_mutex_unique_lock_uncontended.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | void sleep_non_realtime() 6 | { 7 | using namespace std::chrono_literals; 8 | 9 | rtc::non_realtime_context nrc; 10 | std::this_thread::sleep_for (1s); 11 | } 12 | 13 | int main() 14 | { 15 | std::mutex m; 16 | 17 | std::thread t1 ([&] 18 | { 19 | rtc::realtime_context rc; 20 | 21 | std::unique_lock l (m); 22 | sleep_non_realtime(); 23 | }); 24 | t1.join(); 25 | 26 | std::thread t2 ([&] 27 | { 28 | rtc::realtime_context rc; 29 | 30 | std::unique_lock l (m); 31 | sleep_non_realtime(); 32 | }); 33 | t2.join(); 34 | 35 | return 0; 36 | } 37 | -------------------------------------------------------------------------------- /tests/fail_shared_mutex_shared_lock_contended.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | void sleep_non_realtime() 6 | { 7 | using namespace std::chrono_literals; 8 | 9 | rtc::non_realtime_context nrc; 10 | std::this_thread::sleep_for (1s); 11 | } 12 | 13 | int main() 14 | { 15 | std::shared_mutex m; 16 | 17 | std::thread t1 ([&] 18 | { 19 | rtc::realtime_context rc; 20 | 21 | std::shared_lock l (m); 22 | sleep_non_realtime(); 23 | }); 24 | 25 | std::thread t2 ([&] 26 | { 27 | rtc::realtime_context rc; 28 | 29 | std::shared_lock l (m); 30 | sleep_non_realtime(); 31 | }); 32 | t1.join(); 33 | t2.join(); 34 | 35 | return 0; 36 | } 37 | -------------------------------------------------------------------------------- /tests/fail_shared_mutex_shared_lock_uncontended.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | void sleep_non_realtime() 6 | { 7 | using namespace std::chrono_literals; 8 | 9 | rtc::non_realtime_context nrc; 10 | std::this_thread::sleep_for (1s); 11 | } 12 | 13 | int main() 14 | { 15 | std::shared_mutex m; 16 | 17 | std::thread t1 ([&] 18 | { 19 | rtc::realtime_context rc; 20 | 21 | std::shared_lock l (m); 22 | sleep_non_realtime(); 23 | }); 24 | t1.join(); 25 | 26 | std::thread t2 ([&] 27 | { 28 | rtc::realtime_context rc; 29 | 30 | std::shared_lock l (m); 31 | sleep_non_realtime(); 32 | }); 33 | t2.join(); 34 | 35 | return 0; 36 | } 37 | -------------------------------------------------------------------------------- /tests/fail_mmap.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | int main() 10 | { 11 | int fd = open("/usr/bin/awk", O_RDONLY); 12 | 13 | if (fd == -1) 14 | { 15 | std::cout << "ERROR: open failed\n"; 16 | return 0; 17 | } 18 | 19 | struct stat s; 20 | int status = fstat (fd, &s); 21 | auto size = s.st_size; 22 | 23 | char* map = nullptr; 24 | 25 | { 26 | rtc::realtime_context rc; 27 | map = (char *) mmap (0, size, PROT_READ, MAP_PRIVATE, fd, 0); 28 | } 29 | 30 | if (map == MAP_FAILED) 31 | { 32 | std::cout << "ERROR: MAP_FAILED\n"; 33 | return 0; 34 | } 35 | 36 | if (munmap(map, size) == -1) 37 | { 38 | std::cout << "ERROR: munmap failed\n"; 39 | return 0; 40 | } 41 | 42 | close(fd); 43 | 44 | return 0; 45 | } 46 | -------------------------------------------------------------------------------- /tests/fail_mutex_unique_try_lock_contended.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | void sleep_non_realtime() 6 | { 7 | using namespace std::chrono_literals; 8 | 9 | rtc::non_realtime_context nrc; 10 | std::this_thread::sleep_for (1s); 11 | } 12 | 13 | int main() 14 | { 15 | std::mutex m; 16 | 17 | std::thread t1 ([&] 18 | { 19 | rtc::realtime_context rc; 20 | 21 | if (std::unique_lock l (m, std::try_to_lock); l.owns_lock()) 22 | sleep_non_realtime(); 23 | }); 24 | 25 | std::thread t2 ([&] 26 | { 27 | rtc::realtime_context rc; 28 | 29 | if (std::unique_lock l (m, std::try_to_lock); l.owns_lock()) 30 | sleep_non_realtime(); 31 | }); 32 | t1.join(); 33 | t2.join(); 34 | 35 | return 0; 36 | } 37 | -------------------------------------------------------------------------------- /tests/fail_false_mutex_unique_try_lock_uncontended.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | void sleep_non_realtime() 6 | { 7 | using namespace std::chrono_literals; 8 | 9 | rtc::non_realtime_context nrc; 10 | std::this_thread::sleep_for (1s); 11 | } 12 | 13 | int main() 14 | { 15 | std::mutex m; 16 | 17 | std::thread t1 ([&] 18 | { 19 | rtc::realtime_context rc; 20 | 21 | if (std::unique_lock l (m, std::try_to_lock); l.owns_lock()) 22 | sleep_non_realtime(); 23 | }); 24 | t1.join(); 25 | 26 | std::thread t2 ([&] 27 | { 28 | rtc::realtime_context rc; 29 | 30 | if (std::unique_lock l (m, std::try_to_lock); l.owns_lock()) 31 | sleep_non_realtime(); 32 | }); 33 | t2.join(); 34 | 35 | return 0; 36 | } 37 | -------------------------------------------------------------------------------- /tests/fail_munmap.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | int main() 10 | { 11 | int fd = open("/usr/bin/awk", O_RDONLY); 12 | 13 | if (fd == -1) 14 | { 15 | std::cout << "ERROR: open failed\n"; 16 | return 0; 17 | } 18 | 19 | struct stat s; 20 | int status = fstat (fd, &s); 21 | auto size = s.st_size; 22 | 23 | auto map = (char *) mmap (0, size, PROT_READ, MAP_PRIVATE, fd, 0); 24 | 25 | if (map == MAP_FAILED) 26 | { 27 | std::cout << "ERROR: MAP_FAILED\n"; 28 | return 0; 29 | } 30 | 31 | int res; 32 | 33 | { 34 | rtc::realtime_context rc; 35 | res = munmap(map, size); 36 | } 37 | 38 | if (res == -1) 39 | { 40 | std::cout << "ERROR: munmap failed\n"; 41 | return 0; 42 | } 43 | 44 | close(fd); 45 | 46 | return 0; 47 | } 48 | -------------------------------------------------------------------------------- /tests/fail_open.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | #pragma clang diagnostic push 13 | #pragma clang diagnostic ignored "-Wdeprecated-declarations" 14 | 15 | int main() 16 | { 17 | rtc::realtime_context rc; 18 | 19 | namespace fs = std::filesystem; 20 | const fs::path temp_file = std::tmpnam (nullptr); 21 | 22 | const int mode = S_IRGRP | S_IROTH | S_IRUSR | S_IWUSR; 23 | const int fd = open (temp_file.c_str(), O_CREAT | O_WRONLY, mode); 24 | assert(fd != -1); 25 | close(fd); 26 | 27 | struct stat st; 28 | assert(stat(temp_file.c_str(), &st) == 0); 29 | 30 | // Mask st_mode to get permission bits only 31 | assert((st.st_mode & 0777) == mode); 32 | 33 | auto res = fs::remove (temp_file); 34 | assert (res); 35 | 36 | return 0; 37 | } 38 | 39 | #pragma clang diagnostic pop 40 | -------------------------------------------------------------------------------- /tests/fail_read_file.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | #include 7 | 8 | 9 | int main() 10 | { 11 | rtc::realtime_context rc; 12 | 13 | namespace fs = std::filesystem; 14 | 15 | if (fs::path file_path ("/usr/bin/awk"); 16 | fs::exists (file_path)) 17 | { 18 | // Open the file in binary mode 19 | const auto file_size = fs::file_size (file_path); 20 | std::vector file_data (file_size); 21 | 22 | // Read the file into the vector 23 | std::ifstream file (file_path, std::ios::binary); 24 | file.read (file_data.data(), file_size); 25 | 26 | // Check if the file was read successfully 27 | if (file) 28 | std::cout << "File read successfully." << std::endl; 29 | else 30 | std::cout << "Error reading the file." << std::endl; 31 | 32 | file.close(); 33 | } 34 | else 35 | { 36 | std::cout << "File does not exist." << std::endl; 37 | } 38 | 39 | return 0; 40 | } 41 | -------------------------------------------------------------------------------- /tests/fail_fcntl.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | #pragma clang diagnostic push 13 | #pragma clang diagnostic ignored "-Wdeprecated-declarations" 14 | 15 | int main() 16 | { 17 | rtc::realtime_context rc; 18 | 19 | namespace fs = std::filesystem; 20 | const fs::path temp_file = std::tmpnam (nullptr); 21 | int fd = creat(temp_file.c_str(), S_IRUSR | S_IWUSR); 22 | assert(fd != -1); 23 | 24 | auto func = [fd] { 25 | struct flock lock{}; 26 | lock.l_type = F_RDLCK; 27 | lock.l_whence = SEEK_SET; 28 | lock.l_start = 0; 29 | lock.l_len = 0; 30 | lock.l_pid = ::getpid(); 31 | 32 | assert(fcntl(fd, F_GETLK, &lock) == 0); 33 | assert(lock.l_type == F_UNLCK); 34 | }; 35 | 36 | close(fd); 37 | std::remove (temp_file.c_str()); 38 | 39 | return 0; 40 | } 41 | 42 | #pragma clang diagnostic pop 43 | -------------------------------------------------------------------------------- /tests/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | #cmake_minimum_required(VERSION 3.20) 2 | #project(lib_rt_check_tests) 3 | 4 | set(CMAKE_CXX_STANDARD 20) 5 | set(CMAKE_CXX_STANDARD_REQUIRED ON) 6 | 7 | #====================================== 8 | message(CMAKE_CURRENT_SOURCE_DIR: ${CMAKE_CURRENT_SOURCE_DIR}) 9 | file(GLOB_RECURSE test_files LIST_DIRECTORIES false "${CMAKE_CURRENT_SOURCE_DIR}" pass_*.cpp fail_*.cpp) 10 | message(test_files: ${test_files}) 11 | 12 | #====================================== 13 | foreach(file ${test_files}) 14 | message(STATUS "Processing file: ${file}") 15 | get_filename_component(test_name ${file} NAME_WLE) 16 | message(STATUS "Creating test: ${test_name}") 17 | 18 | add_executable(${test_name} 19 | ${file} 20 | ) 21 | 22 | target_link_libraries(${test_name} 23 | rtcheck 24 | ) 25 | 26 | if (${CMAKE_SYSTEM_NAME} STREQUAL Linux) 27 | target_link_libraries(${test_name} 28 | atomic 29 | ) 30 | endif() 31 | 32 | target_link_options(${test_name} PRIVATE 33 | "-rdynamic" 34 | ) 35 | 36 | add_test (NAME ${test_name} COMMAND ${test_name}) 37 | 38 | if(${test_name} MATCHES "fail") 39 | set_property(TEST ${test_name} PROPERTY WILL_FAIL TRUE) 40 | endif() 41 | endforeach() 42 | -------------------------------------------------------------------------------- /tests/pass_unit_tests.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | #pragma clang diagnostic push 10 | #pragma clang diagnostic ignored "-Wdeprecated-declarations" 11 | 12 | void test_openCreatesFileWithProperMode() 13 | { 14 | namespace fs = std::filesystem; 15 | const fs::path temp_file = fs::temp_directory_path() / "test_file_XXXXXX"; 16 | 17 | const int mode = S_IRGRP | S_IROTH | S_IRUSR | S_IWUSR; 18 | const int fd = open (temp_file.c_str(), O_CREAT | O_WRONLY, mode); 19 | assert(fd != -1); 20 | close(fd); 21 | 22 | struct stat st; 23 | assert(stat(temp_file.c_str(), &st) == 0); 24 | 25 | // Mask st_mode to get permission bits only 26 | assert((st.st_mode & 0777) == mode); 27 | 28 | auto res = fs::remove (temp_file); 29 | assert (res); 30 | } 31 | 32 | void test_fcntlFlockDiesWhenRealtime() 33 | { 34 | namespace fs = std::filesystem; 35 | const fs::path temp_file = fs::temp_directory_path() / "test_file_XXXXXX"; 36 | int fd = creat(temp_file.c_str(), S_IRUSR | S_IWUSR); 37 | assert(fd != -1); 38 | 39 | auto func = [fd] { 40 | struct flock lock{}; 41 | lock.l_type = F_RDLCK; 42 | lock.l_whence = SEEK_SET; 43 | lock.l_start = 0; 44 | lock.l_len = 0; 45 | lock.l_pid = ::getpid(); 46 | 47 | assert(fcntl(fd, F_GETLK, &lock) == 0); 48 | assert(lock.l_type == F_UNLCK); 49 | }; 50 | 51 | func(); 52 | 53 | close(fd); 54 | std::remove (temp_file.c_str()); 55 | } 56 | 57 | #pragma clang diagnostic pop 58 | 59 | int main() 60 | { 61 | test_openCreatesFileWithProperMode(); 62 | test_fcntlFlockDiesWhenRealtime(); 63 | 64 | return 0; 65 | } 66 | 67 | -------------------------------------------------------------------------------- /src/interception.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | //============================================================================== 4 | // The following is extracted from LLVM: 5 | //============================================================================== 6 | 7 | //===-- interception.h ------------------------------------------*- C++ -*-===// 8 | // 9 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 10 | // See https://llvm.org/LICENSE.txt for license information. 11 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 12 | // 13 | //===----------------------------------------------------------------------===// 14 | // 15 | // This file is a part of AddressSanitizer, an address sanity checker. 16 | // 17 | // Machinery for providing replacements/wrappers for system functions. 18 | //===----------------------------------------------------------------------===// 19 | 20 | #ifdef __linux__ 21 | #define INTERCEPTOR_ATTRIBUTE 22 | 23 | #define INTERCEPTOR(ret_type, func, ...) \ 24 | extern "C" INTERCEPTOR_ATTRIBUTE ret_type func(__VA_ARGS__) 25 | 26 | #define INTERCEPT_FUNCTION(ret_type, func, ...) static auto real = (ret_type (*)(__VA_ARGS__))dlsym(RTLD_NEXT, #func); 27 | 28 | #define REAL(func, ...) real __VA_ARGS__ 29 | #elif __APPLE__ 30 | using uptr = size_t; 31 | 32 | struct interpose_substitution { 33 | const uptr replacement; 34 | const uptr original; 35 | }; 36 | 37 | // For a function foo() create a global pair of pointers { wrap_foo, foo } in 38 | // the __DATA,__interpose section. 39 | // As a result all the calls to foo() will be routed to wrap_foo() at runtime. 40 | #define INTERPOSER(func_name) __attribute__((used)) \ 41 | const interpose_substitution substitution_##func_name[] \ 42 | __attribute__((section("__DATA, __interpose"))) = { \ 43 | { reinterpret_cast(WRAP(func_name)), \ 44 | reinterpret_cast(func_name) } \ 45 | } 46 | 47 | #define WRAP(x) wrap_##x 48 | #define TRAMPOLINE(x) WRAP(x) 49 | #define INTERCEPTOR_ATTRIBUTE 50 | #define DECLARE_WRAPPER(ret_type, func, ...) 51 | 52 | // #define REAL(func, ...) WRAP(func) (__VA_ARGS__) 53 | #define REAL(x) x 54 | #define DECLARE_REAL(ret_type, func, ...) \ 55 | extern "C" ret_type func(__VA_ARGS__); 56 | #define ASSIGN_REAL(x, y) 57 | 58 | #define INTERCEPTOR_ZZZ(suffix, ret_type, func, ...) \ 59 | extern "C" ret_type func(__VA_ARGS__) suffix; \ 60 | extern "C" ret_type WRAP(func)(__VA_ARGS__); \ 61 | INTERPOSER(func); \ 62 | extern "C" INTERCEPTOR_ATTRIBUTE ret_type WRAP(func)(__VA_ARGS__) 63 | 64 | #define INTERCEPTOR(ret_type, func, ...) \ 65 | INTERCEPTOR_ZZZ(/*no symbol variants*/, ret_type, func, __VA_ARGS__) 66 | 67 | #define INTERCEPTOR_WITH_SUFFIX(ret_type, func, ...) \ 68 | INTERCEPTOR_ZZZ(__DARWIN_ALIAS_C(func), ret_type, func, __VA_ARGS__) 69 | 70 | #define INTERCEPT_FUNCTION(...) 71 | #endif 72 | -------------------------------------------------------------------------------- /rtcheck_icon.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![Build](https://github.com/Tracktion/rtcheck/actions/workflows/build.yml/badge.svg)](https://github.com/Tracktion/rtcheck/actions/workflows/build.yml) 2 | 3 | 4 | 5 | # rtcheck 6 | Dynamic library to catch run-time safety violations heavily inspired by [RADSan](https://github.com/realtime-sanitizer/radsan) 7 | 8 | ## Contents 9 | - [Adding rtcheck to a project](#adding-rtcheck-to-a-project) 10 | - [Using rtcheck](#using-rtcheck) 11 | - [Disabling checks](#disabling-checks) 12 | - [Catching your own violations](#catching-your-own-violations) 13 | - [Error modes](#error-modes) 14 | 15 | ## Adding rtcheck to a project 16 | ### CMake option 1: Git Submodule 17 | ``` 18 | git submodule add -b main https://github.com/Tracktion/rtcheck rtcheck 19 | ``` 20 | 21 | To update down the road: 22 | ``` 23 | git submodule update --remote --merge rtcheck 24 | ``` 25 | 26 | #### Build rtcheck as part of your CMakeLists.txt 27 | ```cmake 28 | add_subdirectory(rtcheck) 29 | ``` 30 | 31 | ### CMake option 2: FetchContent 32 | ```cmake 33 | Include(FetchContent) 34 | FetchContent_Declare(rtcheck 35 | GIT_REPOSITORY https://github.com/Tracktion/rtcheck.git 36 | GIT_TAG origin/main 37 | SOURCE_DIR ${CMAKE_CURRENT_BINARY_DIR}/rtcheck) 38 | FetchContent_MakeAvailable(rtcheck) 39 | ``` 40 | 41 | ### Link rtcheck to your CMake project 42 | ```cmake 43 | target_link_libraries("YourProject" PRIVATE rtcheck) 44 | ``` 45 | 46 | Then include rtcheck in your source files: 47 | ```c++ 48 | #include 49 | ``` 50 | 51 | ## Using rtcheck 52 | Simply add an instance of `rtc::realtime_context` at the start of the scope you want to check for real-time safety violations like this: 53 | ```c++ 54 | int main() 55 | { 56 | std::thread t ([] 57 | { 58 | rtc::realtime_context rc; 59 | malloc (1024); // Allocating memory is a real-time safety violation! 60 | }); 61 | 62 | t.join(); 63 | 64 | return 0; 65 | } 66 | ``` 67 | The call to malloc in the above code will then be caught and logged as so: 68 | ``` 69 | Real-time violation: intercepted call to real-time unsafe function malloc in real-time context! Stack trace: 70 | 0 librt_check.dylib 0x000000010543f7d4 _ZN3rtc14get_stacktraceEv + 84 71 | 1 librt_check.dylib 0x000000010543f450 _ZN3rtc32log_function_if_realtime_contextEPKc + 300 72 | 2 librt_check.dylib 0x000000010543fe00 _Z44log_function_if_realtime_context_and_enabledN3rtc11check_flagsEPKc + 64 73 | 3 librt_check.dylib 0x000000010543fe30 wrap_malloc + 32 74 | 4 fail_malloc 0x0000000104fcff14 main + 32 75 | 5 dyld 0x00000001901760e0 start + 2360 76 | ``` 77 | 78 | ## Disabling checks 79 | There are two ways to disable checks. You can either disable all checks, which can be useful if you 80 | know you'll be calling a potentially unsafe function but in a safe way (e.g. an un-contented lock), or you want to log something: 81 | ```c++ 82 | { 83 | rtc::non_realtime_context nrc; 84 | std::cout << "need to get this message on the screen!"; 85 | } 86 | ``` 87 | Or you can selectively disable checks: 88 | ```c++ 89 | { 90 | rtc::disable_checks_for_thread (check_flags::threads); 91 | mutex.lock(); // I know this is uncontended, don't for get to unlock! 92 | } 93 | ``` 94 | 95 | ## Catching your own violations 96 | If you have some code which you know is non-real-time safe e.g. an unbounded distribution function or some other async call, you can opt-in to let rtcheck catch it by calling the following function: 97 | ```c++ 98 | void my_unsafe_function() 99 | { 100 | log_function_if_realtime_context (__func__); 101 | } 102 | ``` 103 | This will then get logged if called whilst a `rtc::realtime_context` is alive. 104 | 105 | ## Error Modes 106 | There are two currently supported error modes 107 | - Exit with error code 1 (default) 108 | - Log and continue 109 | 110 | Exiting it useful for CI runs where you want the program to terminate in an obvious way (non-zero exit code) to fail the run. 111 | In normal use though, you may just want to log the violation and continue. 112 | 113 | You can change between these globally using the following function: 114 | ```c++ 115 | /** Sets the global error more to determine the behaviour when a real-time 116 | violation is detected. 117 | */ 118 | void set_error_mode (error_mode); 119 | ``` 120 | --- 121 | # Notes: 122 | ## Features 123 | - [x] Enable in scope 124 | - [x] Disable in scope 125 | - [x] Symbolicated stack-trace 126 | - [x] Run-time options for checks 127 | - [x] Opt-in for own code 128 | - [x] linux 129 | - [x] macOS (malloc unsupported) 130 | - [ ] Add option to realtime_context constructor to disable checks for that scope 131 | - [ ] Delay time 132 | 133 | ## Functions (test = ✔) 134 | - Time 135 | - [x] sleep ✔ 136 | - [x] nanosleep ✔ 137 | - [x] usleep ✔ 138 | - Memory 139 | - [x] malloc ✔ 140 | - [x] calloc ✔ 141 | - [x] realloc ✔ 142 | - [x] free ✔ 143 | - [x] reallocf (macOS) ✔ 144 | - [x] valloc ✔ 145 | - [x] posix_memalign ✔ 146 | - [x] mmap ✔ 147 | - [x] munmap ✔ 148 | - Threads 149 | - [x] pthread_create ✔ 150 | - [x] pthread_mutex_lock ✔ 151 | - [x] pthread_mutex_unlock ✔ 152 | - [x] pthread_join ✔ 153 | - [x] pthread_cond_signal 154 | - [x] pthread_cond_broadcast 155 | - [x] pthread_cond_wait 156 | - [x] pthread_cond_timedwait 157 | - [x] pthread_rwlock_rdlock ✔ 158 | - [x] pthread_rwlock_unlock ✔ 159 | - [x] pthread_rwlock_wrlock ✔ 160 | - [x] pthread_spin_lock (linux) 161 | - Files 162 | - [x] open ✔ 163 | - [x] openat 164 | - [ ] close 165 | - [x] fopen 166 | - [ ] fread 167 | - [ ] fwrite 168 | - [ ] fclose 169 | - [x] fcntl ✔ 170 | - [ ] creat 171 | - [ ] puts 172 | - [ ] fputs 173 | - [x] stat ✔ 174 | - [ ] stat64 175 | - [x] fstat 176 | - [ ] fstat64 177 | - IO 178 | - [ ] socket 179 | - [ ] send 180 | - [ ] sendmsg 181 | - [ ] sendto 182 | - [ ] recv 183 | - [ ] recvmsg 184 | - [ ] recvfrom 185 | - [ ] shutdown 186 | - System calls 187 | - [x] syscall ✔ 188 | - [x] schedule 189 | - [x] context_switch 190 | 191 | ## CI/Tests 192 | - Failures 193 | - [x] Throwing exceptions 194 | - [x] Large std::function 195 | - [x] Atomic 4*ptr size 196 | - [ ] Dynamic loading of a library 197 | - Passes 198 | - [x] Atomic double 199 | - [x] Small std::function 200 | - [x] thread_local? (pass on linux, fail on macOS) 201 | - [x] Running on CI Linux 202 | - [x] Running on CI macOS 203 | - [x] Tests via CTest (can catch failues) 204 | 205 | ## Packages 206 | - [ ] cpack 207 | - [ ] conan 208 | - [ ] vcpkg 209 | -------------------------------------------------------------------------------- /src/rtcheck.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | 5 | namespace rtc 6 | { 7 | //============================================================================== 8 | //============================================================================== 9 | /** Puts the current thread in to a real-time context. 10 | You'd generally put one of these at the start of your real-time thread which 11 | will enable checking of real-time safety violations. 12 | */ 13 | struct realtime_context 14 | { 15 | /** Enters the real-time context. */ 16 | realtime_context(); 17 | 18 | /** Exits the real-time context. */ 19 | ~realtime_context(); 20 | }; 21 | 22 | //============================================================================== 23 | /** Exits a real-time context for the life-time of the object. 24 | Placing one of these in the scope of a known real-time unsafe call can avoid 25 | flagging errors. This can enable you to progressively adopt real-time safety 26 | or to perform logging etc. in debug builds. 27 | */ 28 | struct non_realtime_context 29 | { 30 | /** Exits the real-time context. 31 | pre_condition: Thread is in a real-time context 32 | */ 33 | non_realtime_context(); 34 | 35 | /** Re-enters the real-time context. */ 36 | ~non_realtime_context(); 37 | }; 38 | 39 | //============================================================================== 40 | //============================================================================== 41 | /** Returns true if the current thread is in a real-time context. */ 42 | bool is_real_time_context(); 43 | 44 | /** If the current thead is in a real-time context, this will log and carry out 45 | the currently error_mode behaviour. 46 | This can be used in your own code to flag functions that are not real-time safe. 47 | */ 48 | void log_function_if_realtime_context (const char* function_name); 49 | 50 | //============================================================================== 51 | //============================================================================== 52 | /** Holds the various supported error modes. */ 53 | enum class error_mode 54 | { 55 | exit, /// Exit with value 1, default 56 | cont /// Continue execution 57 | }; 58 | 59 | /** Sets the global error more to determine the behaviour when a real-time 60 | violation is detected. 61 | */ 62 | void set_error_mode (error_mode); 63 | 64 | /** Returns the global error detection mode. */ 65 | error_mode get_error_mode(); 66 | 67 | 68 | //============================================================================== 69 | //============================================================================== 70 | /** Enum specifying the different checks which can be used to individually or as 71 | a group be disabled for the current thread. 72 | */ 73 | enum class check_flags : uint64_t 74 | { 75 | //============================================================================== 76 | // memory 77 | //============================================================================== 78 | malloc = 1 << 0, 79 | calloc = 1 << 1, 80 | realloc = 1 << 2, 81 | reallocf = 1 << 3, // apple only 82 | valloc = 1 << 4, 83 | free = 1 << 5, 84 | posix_memalign = 1 << 6, 85 | mmap = 1 << 7, 86 | munmap = 1 << 8, 87 | 88 | memory = malloc | calloc | realloc | reallocf | valloc 89 | | free | posix_memalign | mmap | munmap, 90 | 91 | //============================================================================== 92 | // threads 93 | //============================================================================== 94 | pthread_create = 1 << 9, 95 | pthread_mutex_lock = 1 << 10, 96 | pthread_mutex_unlock = 1 << 11, 97 | pthread_join = 1 << 12, 98 | pthread_cond_signal = 1 << 13, 99 | pthread_cond_broadcast = 1 << 14, 100 | pthread_cond_wait = 1 << 15, 101 | pthread_rwlock_init = 1 << 16, 102 | pthread_rwlock_destroy = 1 << 17, 103 | pthread_cond_timedwait = 1 << 18, 104 | pthread_rwlock_rdlock = 1 << 19, 105 | pthread_rwlock_unlock = 1 << 20, 106 | pthread_rwlock_wrlock = 1 << 21, 107 | pthread_spin_lock = 1 << 22, // linux only 108 | futex = 1 << 23, // linux only 109 | OSSpinLockLock = 1 << 24, // apple only 110 | os_unfair_lock_lock = 1 << 25, // apple only 111 | _os_nospin_lock_lock = 1 << 26, // apple only 112 | 113 | threads = pthread_create | pthread_mutex_lock | pthread_mutex_unlock | pthread_join 114 | | pthread_cond_signal | pthread_cond_broadcast | pthread_cond_wait 115 | | pthread_rwlock_init | pthread_rwlock_destroy | pthread_cond_timedwait 116 | | pthread_rwlock_rdlock | pthread_rwlock_unlock | pthread_rwlock_wrlock 117 | | pthread_spin_lock | futex | OSSpinLockLock | os_unfair_lock_lock 118 | | _os_nospin_lock_lock, 119 | 120 | //============================================================================== 121 | // sleeping 122 | //============================================================================== 123 | sleep = 1 << 27, 124 | usleep = 1 << 28, 125 | nanosleep = 1 << 29, 126 | 127 | sleeping = sleep | usleep | nanosleep, 128 | 129 | //============================================================================== 130 | // files 131 | //============================================================================== 132 | stat = 1 << 30, 133 | fstat = 1ull << 31, 134 | open = 1ull << 32, 135 | fopen = 1ull << 33, 136 | openat = 1ull << 34, 137 | fcntl = 1ull << 35, 138 | 139 | files = stat | fstat | open | fopen | openat, 140 | 141 | //============================================================================== 142 | // system 143 | //============================================================================== 144 | schedule = 1ull << 36, // linux only 145 | context_switch = 1ull << 37, // linux only 146 | syscall = 1ull << 38, 147 | 148 | sys = schedule | context_switch | syscall 149 | }; 150 | 151 | /** Disables a number of checks for the current thread. */ 152 | void disable_checks_for_thread (uint64_t flags); 153 | 154 | /** Disables a check for the current thread. */ 155 | void disable_checks_for_thread (check_flags); 156 | 157 | /** Returns true if the current check is enabled. */ 158 | [[nodiscard]] bool is_check_enabled_for_thread (check_flags); 159 | } 160 | -------------------------------------------------------------------------------- /src/rtcheck.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | #if __APPLE__ 13 | #include 14 | #include 15 | #include 16 | #endif 17 | 18 | #if __has_include () 19 | #include 20 | #endif 21 | 22 | #include "rtcheck.h" 23 | #include "interception.h" 24 | 25 | namespace rtc 26 | { 27 | auto to_underlying (auto e) 28 | { 29 | return static_cast> (e); 30 | } 31 | 32 | inline std::string demangle (const std::string& name) 33 | { 34 | #if __has_include() 35 | int status; 36 | std::unique_ptr demangled ( 37 | abi::__cxa_demangle (name.c_str(), nullptr, nullptr, &status), 38 | &std::free); 39 | 40 | if (demangled && status == 0) 41 | return std::string (demangled.get()); 42 | #endif 43 | 44 | return name; 45 | } 46 | 47 | inline std::string extract_symbol (const std::string& line) 48 | { 49 | // macOS/BSD format: "frame binary address symbol + offset" 50 | if (line.find ("0x") != std::string::npos) 51 | { 52 | std::regex macPattern (R"(^\s*\d+\s+\S+\s+0x[0-9a-fA-F]+\s+(.+?)(?:\s+\+\s+\d+)?$)"); 53 | std::smatch match; 54 | 55 | if (std::regex_match (line, match, macPattern)) 56 | return match[1].str(); 57 | } 58 | 59 | // Linux format: "binary(symbol+offset) [address]" 60 | { 61 | size_t lparen = line.find ('('); 62 | size_t plus = line.find ('+', lparen); 63 | 64 | if (lparen != std::string::npos && plus != std::string::npos) 65 | return line.substr (lparen + 1, plus - lparen - 1); 66 | } 67 | 68 | return line; 69 | } 70 | 71 | 72 | inline std::string get_stacktrace() 73 | { 74 | std::string result; 75 | 76 | void* stack[128]; 77 | auto frames = backtrace (stack, 128); 78 | char** frameStrings = backtrace_symbols (stack, frames); 79 | 80 | for (auto i = (decltype (frames)) 0; i < frames; ++i) 81 | (result.append (i, ' ') += demangle (extract_symbol (frameStrings[i]))) += "\n"; 82 | 83 | result += "\n"; 84 | ::free (frameStrings); 85 | 86 | return result; 87 | } 88 | 89 | 90 | //============================================================================== 91 | //============================================================================== 92 | static bool has_initialised = false; 93 | 94 | 95 | //============================================================================== 96 | //============================================================================== 97 | /** 98 | Used to enable intercepting of allocations using new, new[], delete and 99 | delete[] on the calling thread. 100 | */ 101 | struct realtime_context_state 102 | { 103 | realtime_context_state() = default; 104 | 105 | /// Enters a real-time context 106 | void realtime_enter() { realtime_flag.store (true); } 107 | 108 | /// Exits a real-time context 109 | void realtime_exit() { realtime_flag.store (false); } 110 | 111 | /// Returns true if this is in a real-time state 112 | bool is_realtime_context() const { return realtime_flag.load(); } 113 | 114 | //============================================================================== 115 | private: 116 | std::atomic realtime_flag { false }; 117 | }; 118 | 119 | 120 | //============================================================================== 121 | //============================================================================== 122 | #if __APPLE__ 123 | struct malloc_zone_wrapper 124 | { 125 | static void* internal_alloc (size_t size) 126 | { 127 | return malloc_zone_malloc (malloc_default_zone(), size); 128 | } 129 | 130 | static void internal_free (void *ptr) 131 | { 132 | malloc_zone_free (malloc_default_zone(), ptr); 133 | } 134 | }; 135 | 136 | template 137 | Type& get_thead_local_variable() 138 | { 139 | static pthread_key_t key; 140 | static pthread_once_t key_once = PTHREAD_ONCE_INIT; 141 | 142 | auto make_tls_key = [] 143 | { 144 | [[ maybe_unused ]]auto res = pthread_key_create (&key, malloc_zone_wrapper::internal_free); 145 | assert(res == 0); 146 | }; 147 | 148 | pthread_once (&key_once, make_tls_key); 149 | 150 | auto *ptr = static_cast (pthread_getspecific (key)); 151 | 152 | if (ptr == nullptr) 153 | { 154 | ptr = static_cast (malloc_zone_wrapper::internal_alloc(sizeof (Type))); 155 | new(ptr) Type(); 156 | pthread_setspecific (key, ptr); 157 | } 158 | 159 | return *ptr; 160 | } 161 | 162 | realtime_context_state& get_realtime_context_state() 163 | { 164 | return get_thead_local_variable(); 165 | } 166 | 167 | std::bitset<64>& get_disabled_flags_for_thread() 168 | { 169 | return get_thead_local_variable>(); 170 | } 171 | #else 172 | realtime_context_state& get_realtime_context_state() 173 | { 174 | thread_local realtime_context_state rcs; 175 | return rcs; 176 | } 177 | 178 | std::bitset<64>& get_disabled_flags_for_thread() 179 | { 180 | thread_local std::bitset<64> disabled_flags; 181 | return disabled_flags; 182 | } 183 | #endif 184 | 185 | realtime_context::realtime_context() 186 | { 187 | get_realtime_context_state().realtime_enter(); 188 | } 189 | 190 | realtime_context::~realtime_context() 191 | { 192 | get_realtime_context_state().realtime_exit(); 193 | } 194 | 195 | non_realtime_context::non_realtime_context() 196 | { 197 | assert (get_realtime_context_state().is_realtime_context()); 198 | get_realtime_context_state().realtime_exit(); 199 | } 200 | 201 | non_realtime_context::~non_realtime_context() 202 | { 203 | get_realtime_context_state().realtime_enter(); 204 | } 205 | 206 | bool is_real_time_context() 207 | { 208 | return get_realtime_context_state().is_realtime_context(); 209 | } 210 | 211 | void log_function_if_realtime_context (const char* function_name) 212 | { 213 | if (! has_initialised) 214 | return; 215 | 216 | if (! is_real_time_context()) 217 | return; 218 | 219 | non_realtime_context nrc; 220 | 221 | std::string_view name (function_name), wrap_prefix ("wrap_"); 222 | 223 | if (name.starts_with (wrap_prefix)) 224 | name = name.substr (wrap_prefix.length()); 225 | 226 | std::cerr << "Real-time violation: intercepted call to real-time unsafe function " << name << " in real-time context! Stack trace:\n" << get_stacktrace() << std::endl; 227 | 228 | if (get_error_mode() == error_mode::exit) 229 | std::exit (1); 230 | } 231 | 232 | //============================================================================== 233 | void disable_checks_for_thread (uint64_t flags) 234 | { 235 | get_disabled_flags_for_thread() = flags; 236 | } 237 | 238 | void disable_checks_for_thread (check_flags flags) 239 | { 240 | disable_checks_for_thread(static_cast (flags)); 241 | } 242 | 243 | bool are_all_bits_enabled (uint64_t flags, std::bitset<64> disabled_bits) 244 | { 245 | assert(static_cast (flags) <= std::numeric_limits::max()); 246 | const std::bitset<64> bits (flags); 247 | const auto disabled_bits_interested_in = disabled_bits & bits; 248 | 249 | return (bits ^ disabled_bits_interested_in) == bits; 250 | } 251 | 252 | #ifndef NDEBUG 253 | struct check_flags_tests 254 | { 255 | check_flags_tests() 256 | { 257 | assert(! are_all_bits_enabled (to_underlying (check_flags::malloc), 0b1)); 258 | assert(! are_all_bits_enabled (to_underlying (check_flags::malloc) + to_underlying (check_flags::realloc), 0b101)); 259 | assert(are_all_bits_enabled (to_underlying (check_flags::malloc) + to_underlying (check_flags::calloc), 0b100)); 260 | assert(are_all_bits_enabled (to_underlying (check_flags::syscall) + to_underlying (check_flags::openat), 0b0)); 261 | auto a1 = to_underlying (check_flags::syscall); 262 | auto a2 = to_underlying (check_flags::openat); 263 | assert(! are_all_bits_enabled (to_underlying (check_flags::syscall) + to_underlying (check_flags::openat), 0b100010000000000000000000000000000000000)); 264 | } 265 | }; 266 | 267 | static check_flags_tests check_flags_tests; 268 | #endif 269 | 270 | bool is_check_enabled_for_thread (check_flags check) 271 | { 272 | assert(std::bitset<64> (static_cast (check)).count() == 1 && "Only one flag can be check with this function"); 273 | return are_all_bits_enabled (static_cast (check), get_disabled_flags_for_thread()); 274 | } 275 | 276 | 277 | //============================================================================== 278 | // details 279 | //============================================================================== 280 | std::atomic& get_error_mode_flag() 281 | { 282 | static std::atomic em { error_mode::exit }; 283 | return em; 284 | } 285 | 286 | void set_error_mode (error_mode em) 287 | { 288 | get_error_mode_flag().store (em, std::memory_order_release); 289 | } 290 | 291 | error_mode get_error_mode() 292 | { 293 | return get_error_mode_flag().load (std::memory_order_acquire); 294 | } 295 | } 296 | 297 | void log_function_if_realtime_context_and_enabled (rtc::check_flags flag, const char* function_name) 298 | { 299 | if (! rtc::has_initialised) 300 | return; 301 | 302 | if (rtc::is_check_enabled_for_thread (flag)) 303 | rtc::log_function_if_realtime_context (function_name); 304 | } 305 | 306 | 307 | //============================================================================== 308 | // memory 309 | //============================================================================== 310 | INTERCEPTOR(void*, malloc, size_t size) 311 | { 312 | log_function_if_realtime_context_and_enabled (rtc::check_flags::malloc, __func__); 313 | INTERCEPT_FUNCTION(void*, malloc, size_t); 314 | 315 | return REAL(malloc)(size); 316 | } 317 | 318 | 319 | INTERCEPTOR(void*, calloc, size_t size, size_t item_size) 320 | { 321 | log_function_if_realtime_context_and_enabled (rtc::check_flags::calloc, __func__); 322 | 323 | INTERCEPT_FUNCTION(void*, calloc, size_t, size_t); 324 | return REAL(calloc)(size, item_size); 325 | } 326 | 327 | INTERCEPTOR(void*, realloc, void *ptr, size_t new_size) 328 | { 329 | log_function_if_realtime_context_and_enabled (rtc::check_flags::realloc, __func__); 330 | 331 | INTERCEPT_FUNCTION(void*, realloc, void*, size_t); 332 | return REAL(realloc)(ptr, new_size); 333 | } 334 | 335 | #ifdef __APPLE__ 336 | INTERCEPTOR(void *, reallocf, void *ptr, size_t size) 337 | { 338 | log_function_if_realtime_context_and_enabled (rtc::check_flags::reallocf, __func__); 339 | 340 | INTERCEPT_FUNCTION(void*, reallocf, void*, size_t); 341 | return REAL(reallocf)(ptr, size); 342 | } 343 | #endif 344 | 345 | INTERCEPTOR(void*, valloc, size_t size) 346 | { 347 | log_function_if_realtime_context_and_enabled (rtc::check_flags::valloc, __func__); 348 | 349 | INTERCEPT_FUNCTION(void*, valloc, size_t); 350 | return REAL(valloc)(size); 351 | } 352 | 353 | INTERCEPTOR(void, free, void* ptr) 354 | { 355 | if (ptr != nullptr) 356 | log_function_if_realtime_context_and_enabled (rtc::check_flags::free, __func__); 357 | 358 | INTERCEPT_FUNCTION(void, free, void*); 359 | return REAL(free)(ptr); 360 | } 361 | 362 | INTERCEPTOR(int, posix_memalign, void **memptr, size_t alignment, size_t size) 363 | { 364 | log_function_if_realtime_context_and_enabled (rtc::check_flags::posix_memalign, __func__); 365 | 366 | INTERCEPT_FUNCTION(int, posix_memalign, void**, size_t, size_t); 367 | return REAL(posix_memalign)(memptr, alignment, size); 368 | } 369 | 370 | INTERCEPTOR(void *, mmap, void* addr, size_t length, int prot, int flags, int fd, off_t offset) 371 | { 372 | log_function_if_realtime_context_and_enabled (rtc::check_flags::mmap, __func__); 373 | 374 | INTERCEPT_FUNCTION(void*, mmap, void*, size_t, int, int, int, off_t); 375 | return REAL(mmap)(addr, length, prot, flags, fd, offset); 376 | } 377 | 378 | INTERCEPTOR(int, munmap, void* addr, size_t length) 379 | { 380 | log_function_if_realtime_context_and_enabled (rtc::check_flags::munmap, __func__); 381 | 382 | INTERCEPT_FUNCTION(int, munmap, void*, size_t); 383 | return REAL(munmap)(addr, length); 384 | } 385 | 386 | 387 | //============================================================================== 388 | // threads 389 | //============================================================================== 390 | INTERCEPTOR(int, pthread_create, pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine)(void *), void *arg) 391 | { 392 | log_function_if_realtime_context_and_enabled (rtc::check_flags::pthread_create, __func__); 393 | INTERCEPT_FUNCTION(int, pthread_create, pthread_t *, const pthread_attr_t *, void *(*)(void *), void *); 394 | return REAL(pthread_create)(thread, attr, start_routine, arg); 395 | } 396 | 397 | INTERCEPTOR(int, pthread_mutex_lock, pthread_mutex_t *mutex) 398 | { 399 | log_function_if_realtime_context_and_enabled (rtc::check_flags::pthread_mutex_lock, __func__); 400 | 401 | INTERCEPT_FUNCTION(int, pthread_mutex_lock, pthread_mutex_t*); 402 | return REAL(pthread_mutex_lock)(mutex); 403 | } 404 | 405 | INTERCEPTOR(int, pthread_mutex_unlock, pthread_mutex_t *mutex) 406 | { 407 | log_function_if_realtime_context_and_enabled (rtc::check_flags::pthread_mutex_unlock, __func__); 408 | 409 | INTERCEPT_FUNCTION(int, pthread_mutex_unlock, pthread_mutex_t*); 410 | return REAL(pthread_mutex_unlock)(mutex); 411 | } 412 | 413 | INTERCEPTOR(int, pthread_join, pthread_t thread, void **value_ptr) 414 | { 415 | log_function_if_realtime_context_and_enabled (rtc::check_flags::pthread_join, __func__); 416 | 417 | INTERCEPT_FUNCTION(int, pthread_join, pthread_t, void **); 418 | return REAL(pthread_join)(thread, value_ptr); 419 | } 420 | 421 | INTERCEPTOR(int, pthread_cond_signal, pthread_cond_t *cond) 422 | { 423 | log_function_if_realtime_context_and_enabled (rtc::check_flags::pthread_cond_signal, __func__); 424 | 425 | INTERCEPT_FUNCTION(int, pthread_cond_signal, pthread_cond_t *); 426 | return REAL(pthread_cond_signal)(cond); 427 | } 428 | 429 | INTERCEPTOR(int, pthread_cond_broadcast, pthread_cond_t *cond) 430 | { 431 | log_function_if_realtime_context_and_enabled (rtc::check_flags::pthread_cond_broadcast, __func__); 432 | 433 | INTERCEPT_FUNCTION(int, pthread_cond_broadcast, pthread_cond_t *); 434 | return REAL(pthread_cond_broadcast)(cond); 435 | } 436 | 437 | INTERCEPTOR(int, pthread_cond_wait, pthread_cond_t *cond, pthread_mutex_t *mutex) 438 | { 439 | log_function_if_realtime_context_and_enabled (rtc::check_flags::pthread_cond_wait, __func__); 440 | 441 | INTERCEPT_FUNCTION(int, pthread_cond_wait, pthread_cond_t *, pthread_mutex_t *); 442 | return REAL(pthread_cond_wait)(cond, mutex); 443 | } 444 | 445 | INTERCEPTOR(int, pthread_rwlock_init, pthread_rwlock_t *rwlock, 446 | const pthread_rwlockattr_t *attr) 447 | { 448 | log_function_if_realtime_context_and_enabled (rtc::check_flags::pthread_rwlock_init, __func__); 449 | 450 | INTERCEPT_FUNCTION(int, pthread_rwlock_init, pthread_rwlock_t *, const pthread_rwlockattr_t *); 451 | return REAL(pthread_rwlock_init)(rwlock, attr); 452 | } 453 | 454 | INTERCEPTOR(int, pthread_rwlock_destroy, pthread_rwlock_t *rwlock) 455 | { 456 | log_function_if_realtime_context_and_enabled (rtc::check_flags::pthread_rwlock_destroy, __func__); 457 | 458 | INTERCEPT_FUNCTION(int, pthread_rwlock_destroy, pthread_rwlock_t *); 459 | return REAL(pthread_rwlock_destroy)(rwlock); 460 | } 461 | 462 | INTERCEPTOR(int, pthread_cond_timedwait, pthread_cond_t *cond, 463 | pthread_mutex_t *mutex, const timespec *ts) 464 | { 465 | log_function_if_realtime_context_and_enabled (rtc::check_flags::pthread_cond_timedwait, __func__); 466 | 467 | INTERCEPT_FUNCTION(int, pthread_cond_timedwait, pthread_cond_t *, 468 | pthread_mutex_t *, const timespec *); 469 | return REAL(pthread_cond_timedwait)(cond, mutex, ts); 470 | } 471 | 472 | INTERCEPTOR(int, pthread_rwlock_rdlock, pthread_rwlock_t *lock) 473 | { 474 | log_function_if_realtime_context_and_enabled (rtc::check_flags::pthread_rwlock_rdlock, __func__); 475 | 476 | INTERCEPT_FUNCTION(int, pthread_rwlock_rdlock, pthread_rwlock_t *); 477 | return REAL(pthread_rwlock_rdlock)(lock); 478 | } 479 | 480 | INTERCEPTOR(int, pthread_rwlock_unlock, pthread_rwlock_t *lock) 481 | { 482 | log_function_if_realtime_context_and_enabled (rtc::check_flags::pthread_rwlock_unlock, __func__); 483 | 484 | INTERCEPT_FUNCTION(int, pthread_rwlock_unlock, pthread_rwlock_t *); 485 | return REAL(pthread_rwlock_unlock)(lock); 486 | } 487 | 488 | INTERCEPTOR(int, pthread_rwlock_wrlock, pthread_rwlock_t *lock) 489 | { 490 | log_function_if_realtime_context_and_enabled (rtc::check_flags::pthread_rwlock_wrlock, __func__); 491 | 492 | INTERCEPT_FUNCTION(int, pthread_rwlock_wrlock, pthread_rwlock_t *); 493 | return REAL(pthread_rwlock_wrlock)(lock); 494 | } 495 | 496 | 497 | #ifndef __APPLE__ 498 | INTERCEPTOR(int, pthread_spin_lock, pthread_spinlock_t *spinlock) 499 | { 500 | log_function_if_realtime_context_and_enabled (rtc::check_flags::pthread_spin_lock, __func__); 501 | INTERCEPT_FUNCTION(int, pthread_spin_lock, pthread_spinlock_t*); 502 | return REAL(pthread_spin_lock)(spinlock); 503 | } 504 | 505 | INTERCEPTOR(int, futex, int *uaddr, int op, int val, const struct timespec *timeout, int *uaddr2, int val3) 506 | { 507 | log_function_if_realtime_context_and_enabled (rtc::check_flags::futex, __func__); 508 | 509 | INTERCEPT_FUNCTION(int, futex, int*, int, int, const struct timespec*, int*, int); 510 | return REAL(futex)(uaddr, op, val, timeout, uaddr2, val3); 511 | } 512 | #endif 513 | 514 | //============================================================================== 515 | // sleep 516 | //============================================================================== 517 | INTERCEPTOR(unsigned int, sleep, unsigned int seconds) 518 | { 519 | log_function_if_realtime_context_and_enabled (rtc::check_flags::sleep, __func__); 520 | 521 | static auto real = (unsigned int (*)(unsigned int))dlsym(RTLD_NEXT, "sleep"); 522 | return REAL(sleep)(seconds); 523 | } 524 | 525 | INTERCEPTOR(int, usleep, useconds_t useconds) 526 | { 527 | log_function_if_realtime_context_and_enabled (rtc::check_flags::usleep, __func__); 528 | 529 | INTERCEPT_FUNCTION(int, usleep, useconds_t); 530 | return REAL(usleep)(useconds); 531 | } 532 | 533 | INTERCEPTOR(int, nanosleep, const struct timespec *req, struct timespec * rem) 534 | { 535 | log_function_if_realtime_context_and_enabled (rtc::check_flags::nanosleep, __func__); 536 | 537 | INTERCEPT_FUNCTION(int, nanosleep, const struct timespec *, struct timespec *); 538 | return REAL(nanosleep)(req, rem); 539 | } 540 | 541 | //============================================================================== 542 | // files 543 | //============================================================================== 544 | INTERCEPTOR(int, stat, const char* pathname, struct stat* statbuf) 545 | { 546 | log_function_if_realtime_context_and_enabled (rtc::check_flags::stat, __func__); 547 | 548 | INTERCEPT_FUNCTION(int, stat, const char*, struct stat*); 549 | return REAL(stat)(pathname, statbuf); 550 | } 551 | 552 | INTERCEPTOR(int, fstat, int fd, struct stat *statbuf) 553 | { 554 | log_function_if_realtime_context_and_enabled (rtc::check_flags::fstat, __func__); 555 | 556 | INTERCEPT_FUNCTION(int, fstat, int, struct stat*); 557 | return REAL(fstat)(fd, statbuf); 558 | } 559 | 560 | INTERCEPTOR(int, open, const char *path, int oflag, ...) 561 | { 562 | log_function_if_realtime_context_and_enabled (rtc::check_flags::open, __func__); 563 | 564 | INTERCEPT_FUNCTION(int, open, const char*, int, ...); 565 | 566 | va_list args; 567 | va_start(args, oflag); 568 | const mode_t mode = va_arg(args, int); 569 | va_end(args); 570 | 571 | return REAL(open)(path, oflag, mode); 572 | } 573 | 574 | INTERCEPTOR(FILE*, fopen, const char *path, const char *mode) 575 | { 576 | log_function_if_realtime_context_and_enabled (rtc::check_flags::fopen, __func__); 577 | 578 | INTERCEPT_FUNCTION(FILE*, fopen, const char*, const char*); 579 | auto result = REAL(fopen)(path, mode); 580 | 581 | return result; 582 | } 583 | 584 | INTERCEPTOR(int, openat, int fd, const char *path, int oflag, ...) 585 | { 586 | log_function_if_realtime_context_and_enabled (rtc::check_flags::openat, __func__); 587 | 588 | INTERCEPT_FUNCTION(int, openat, int, const char*, int, ...); 589 | 590 | va_list args; 591 | va_start(args, oflag); 592 | mode_t mode = va_arg(args, int); 593 | va_end(args); 594 | 595 | return REAL(openat)(fd, path, oflag, mode); 596 | } 597 | 598 | INTERCEPTOR(int, fcntl, int filedes, int cmd, ...) 599 | { 600 | log_function_if_realtime_context_and_enabled (rtc::check_flags::fcntl, __func__); 601 | 602 | INTERCEPT_FUNCTION(int, fcntl, int, int, ...); 603 | 604 | va_list args; 605 | va_start(args, cmd); 606 | 607 | // From RTsan: 608 | // Following precedent here. The linux source (fcntl.c, do_fcntl) accepts the 609 | // final argument in a variable that will hold the largest of the possible 610 | // argument types (pointers and ints are typical in fcntl) It is then assumed 611 | // that the implementation of fcntl will cast it properly depending on cmd. 612 | // 613 | // This is also similar to what is done in 614 | // sanitizer_common/sanitizer_common_syscalls.inc 615 | const unsigned long arg = va_arg(args, unsigned long); 616 | int result = REAL(fcntl)(filedes, cmd, arg); 617 | 618 | va_end(args); 619 | 620 | return result; 621 | } 622 | 623 | //============================================================================== 624 | // system 625 | //============================================================================== 626 | #ifndef __APPLE__ 627 | INTERCEPTOR(long, schedule, void) 628 | { 629 | log_function_if_realtime_context_and_enabled (rtc::check_flags::schedule, __func__); 630 | 631 | INTERCEPT_FUNCTION(long, schedule, void); 632 | return REAL(schedule)(); 633 | } 634 | 635 | INTERCEPTOR(long, context_switch, struct task_struct *prev, struct task_struct *next) 636 | { 637 | log_function_if_realtime_context_and_enabled (rtc::check_flags::context_switch, __func__); 638 | 639 | INTERCEPT_FUNCTION(long, context_switch, struct task_struct *, struct task_struct *); 640 | return REAL(context_switch)(prev, next); 641 | } 642 | #endif 643 | 644 | #pragma clang diagnostic push 645 | // syscall is deprecated, but still in use in libc++ 646 | #pragma clang diagnostic ignored "-Wdeprecated-declarations" 647 | 648 | #define FORWARD_ARGS(func, ...) func(__VA_ARGS__) 649 | #define EXPAND_ARGS(...) (__VA_ARGS__) 650 | 651 | INTERCEPTOR(long int, syscall, long int sid, ...) 652 | { 653 | log_function_if_realtime_context_and_enabled (rtc::check_flags::syscall, __func__); 654 | 655 | INTERCEPT_FUNCTION(long, syscall, long, ...); 656 | 657 | va_list args; 658 | va_start(args, sid); 659 | va_end(args); 660 | 661 | return REAL(syscall)(sid); 662 | } 663 | 664 | #pragma clang diagnostic pop 665 | 666 | 667 | //============================================================================== 668 | // Apple 669 | //============================================================================== 670 | #if __APPLE__ 671 | 672 | #pragma clang diagnostic push 673 | // OSSpinLockLock is deprecated, but still in use in libc++ 674 | #pragma clang diagnostic ignored "-Wdeprecated-declarations" 675 | 676 | INTERCEPTOR(void, OSSpinLockLock, volatile OSSpinLock *lock) 677 | { 678 | log_function_if_realtime_context_and_enabled (rtc::check_flags::OSSpinLockLock, __func__); 679 | return REAL(OSSpinLockLock)(lock); 680 | } 681 | 682 | INTERCEPTOR(void, os_unfair_lock_lock, os_unfair_lock_t lock) 683 | { 684 | log_function_if_realtime_context_and_enabled (rtc::check_flags::os_unfair_lock_lock, __func__); 685 | return REAL(os_unfair_lock_lock)(lock); 686 | } 687 | 688 | #pragma clang diagnostic pop 689 | 690 | // Newer macOS versions use an internal _os_nospin_lock_lock which is interecpted as per LLVM's RTSan in this commit: 691 | // https://code.ornl.gov/llvm-doe/llvm-project/-/commit/481a55a3d9645a6bc1540d326319b78ad8ed8db1 692 | extern "C" { 693 | // A pointer to this type is in the interface for `_os_nospin_lock_lock`, but 694 | // it's an internal implementation detail of `os/lock.c` on Darwin, and 695 | // therefore not available in any headers. As a workaround, we forward declare 696 | // it here, which is enough to facilitate interception of _os_nospin_lock_lock. 697 | struct _os_nospin_lock_s; 698 | using _os_nospin_lock_t = _os_nospin_lock_s *; 699 | } 700 | 701 | INTERCEPTOR(void, _os_nospin_lock_lock, _os_nospin_lock_t lock) { 702 | log_function_if_realtime_context_and_enabled (rtc::check_flags::os_unfair_lock_lock, __func__); 703 | return REAL(_os_nospin_lock_lock)(lock); 704 | } 705 | 706 | #endif 707 | 708 | //============================================================================== 709 | // init 710 | //============================================================================== 711 | __attribute__((constructor)) 712 | void init() 713 | { 714 | printf ("Hello rtcheck!\n"); 715 | rtc::has_initialised = true; 716 | } 717 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | 635 | Copyright (C) 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | Copyright (C) 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | --------------------------------------------------------------------------------