diff --git a/CMakeLists.txt b/CMakeLists.txt index 2004b72e..41bba080 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,6 +2,11 @@ cmake_minimum_required(VERSION 3.5) project(rcutils) +option(RCUTILS_NO_THREAD_SUPPORT "Disable thread support." OFF) +option(RCUTILS_NO_FILESYSTEM "Disable filesystem usage." OFF) +option(RCUTILS_AVOID_DYNAMIC_ALLOCATION "Disable dynamic allocations." OFF) +option(RCUTILS_NO_64_ATOMIC "Disable support for 64 bits atomic operations." OFF) + # Default to C11 if(NOT CMAKE_C_STANDARD) set(CMAKE_C_STANDARD 11) @@ -74,6 +79,7 @@ set(rcutils_sources src/time.c ${time_impl_c} src/uint8_array.c + $<$:src/atomic_64bits.c> ) set_source_files_properties( ${rcutils_sources} @@ -128,6 +134,10 @@ target_compile_definitions(${PROJECT_NAME} PRIVATE "RCUTILS_BUILDING_DLL") if(BUILD_TESTING AND NOT RCUTILS_DISABLE_FAULT_INJECTION) target_compile_definitions(${PROJECT_NAME} PUBLIC RCUTILS_ENABLE_FAULT_INJECTION) endif() +configure_file( + "${PROJECT_SOURCE_DIR}/include/rcutils/configuration_flags.h.in" + "${PROJECT_BINARY_DIR}/include/rcutils/configuration_flags.h" +) target_link_libraries(${PROJECT_NAME} ${CMAKE_DL_LIBS}) diff --git a/include/rcutils/allocator.h b/include/rcutils/allocator.h index e84706cb..7089adf7 100644 --- a/include/rcutils/allocator.h +++ b/include/rcutils/allocator.h @@ -83,6 +83,21 @@ RCUTILS_WARN_UNUSED rcutils_allocator_t rcutils_get_zero_initialized_allocator(void); +/// Set rcutils default allocators. +/** + *
+ * Attribute | Adherence + * ------------------ | ------------- + * Allocates Memory | No + * Thread-Safe | Yes + * Uses Atomics | No + * Lock-Free | Yes + */ +RCUTILS_PUBLIC +RCUTILS_WARN_UNUSED +bool +rcutils_set_default_allocator(rcutils_allocator_t * allocator); + /// Return a properly initialized rcutils_allocator_t with default values. /** * This defaults to: diff --git a/include/rcutils/configuration_flags.h.in b/include/rcutils/configuration_flags.h.in new file mode 100644 index 00000000..346c6a33 --- /dev/null +++ b/include/rcutils/configuration_flags.h.in @@ -0,0 +1,18 @@ + +#ifndef RCUTILS__CONFIGURATION_FLAGS_H_ +#define RCUTILS__CONFIGURATION_FLAGS_H_ + +#ifdef __cplusplus +extern "C" +{ +#endif + +#cmakedefine RCUTILS_NO_FILESYSTEM +#cmakedefine RCUTILS_AVOID_DYNAMIC_ALLOCATION +#cmakedefine RCUTILS_NO_THREAD_SUPPORT + +#ifdef __cplusplus +} +#endif + +#endif // RCUTILS__CONFIGURATION_FLAGS_H_ \ No newline at end of file diff --git a/include/rcutils/error_handling.h b/include/rcutils/error_handling.h index 36603f77..f4494fda 100644 --- a/include/rcutils/error_handling.h +++ b/include/rcutils/error_handling.h @@ -39,15 +39,18 @@ extern "C" #include "rcutils/testing/fault_injection.h" #include "rcutils/types/rcutils_ret.h" #include "rcutils/visibility_control.h" +#include "rcutils/configuration_flags.h" -#ifdef __STDC_LIB_EXT1__ +#if defined(__STDC_LIB_EXT1__) && !defined(RCUTILS_NO_FILESYSTEM) // Limit the buffer size in the `fwrite` call to give an upper bound to buffer overrun in the case // of non-null terminated `msg`. #define RCUTILS_SAFE_FWRITE_TO_STDERR(msg) \ do {fwrite(msg, sizeof(char), strnlen_s(msg, 4096), stderr);} while (0) -#else +#elif !defined(RCUTILS_NO_FILESYSTEM) #define RCUTILS_SAFE_FWRITE_TO_STDERR(msg) \ do {fwrite(msg, sizeof(char), strlen(msg), stderr);} while (0) +#else + #define RCUTILS_SAFE_FWRITE_TO_STDERR(msg) #endif /// Set the error message to stderr using a format string and format arguments. @@ -220,8 +223,12 @@ rcutils_set_error_state(const char * error_string, const char * file, size_t lin * * \param[in] msg The error message to be set. */ +#ifdef RCUTILS_AVOID_DYNAMIC_ALLOCATION + #define RCUTILS_SET_ERROR_MSG(msg) +#else #define RCUTILS_SET_ERROR_MSG(msg) \ do {rcutils_set_error_state(msg, __FILE__, __LINE__);} while (0) +#endif // RCUTILS_AVOID_DYNAMIC_ALLOCATION /// Set the error message using a format string and format arguments. /** @@ -232,6 +239,9 @@ rcutils_set_error_state(const char * error_string, const char * file, size_t lin * \param[in] format_string The string to be used as the format of the error message. * \param[in] ... Arguments for the format string. */ +#ifdef RCUTILS_AVOID_DYNAMIC_ALLOCATION + #define RCUTILS_SET_ERROR_MSG_WITH_FORMAT_STRING(format_string, ...) +#else #define RCUTILS_SET_ERROR_MSG_WITH_FORMAT_STRING(format_string, ...) \ do { \ char output_msg[RCUTILS_ERROR_MESSAGE_MAX_LENGTH]; \ @@ -242,6 +252,8 @@ rcutils_set_error_state(const char * error_string, const char * file, size_t lin RCUTILS_SET_ERROR_MSG(output_msg); \ } \ } while (0) +#endif // RCUTILS_AVOID_DYNAMIC_ALLOCATION + /// Indicate that the function intends to set an error message and return an error value. /** diff --git a/include/rcutils/macros.h b/include/rcutils/macros.h index e2646212..d2b83f6d 100644 --- a/include/rcutils/macros.h +++ b/include/rcutils/macros.h @@ -20,6 +20,8 @@ extern "C" { #endif +#include "rcutils/configuration_flags.h" + #ifndef _WIN32 #define RCUTILS_WARN_UNUSED __attribute__((warn_unused_result)) #else @@ -28,7 +30,9 @@ extern "C" // Note: this block was migrated from rmw/macros.h // This block either sets RCUTILS_THREAD_LOCAL or RCUTILS_THREAD_LOCAL_PTHREAD. -#if defined _WIN32 || defined __CYGWIN__ +#if defined(RCUTILS_NO_THREAD_SUPPORT) + #define RCUTILS_THREAD_LOCAL +#elif defined _WIN32 || defined __CYGWIN__ // Windows or Cygwin #define RCUTILS_THREAD_LOCAL __declspec(thread) #elif defined __APPLE__ diff --git a/include/rcutils/security_directory.h b/include/rcutils/security_directory.h new file mode 100644 index 00000000..9d2c6806 --- /dev/null +++ b/include/rcutils/security_directory.h @@ -0,0 +1,67 @@ +// Copyright 2018 Open Source Robotics Foundation, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef RCUTILS__SECURITY_DIRECTORY_H_ +#define RCUTILS__SECURITY_DIRECTORY_H_ + +#ifdef __cplusplus +extern "C" +{ +#endif + +#include "rcutils/allocator.h" +#include "rcutils/visibility_control.h" + +#ifndef ROS_SECURITY_NODE_DIRECTORY_VAR_NAME + #define ROS_SECURITY_NODE_DIRECTORY_VAR_NAME "ROS_SECURITY_NODE_DIRECTORY" +#endif + +#ifndef ROS_SECURITY_ROOT_DIRECTORY_VAR_NAME + #define ROS_SECURITY_ROOT_DIRECTORY_VAR_NAME "ROS_SECURITY_ROOT_DIRECTORY" +#endif + +#ifndef ROS_SECURITY_LOOKUP_TYPE_VAR_NAME + #define ROS_SECURITY_LOOKUP_TYPE_VAR_NAME "ROS_SECURITY_LOOKUP_TYPE" +#endif + +/// Return the secure root directory associated with a node given its validated name and namespace. +/** + * E.g. for a node named "c" in namespace "/a/b", the secure root path will be + * "a/b/c", where the delimiter "/" is native for target file system (e.g. "\\" for _WIN32). + * If no exact match is found for the node name, a best match would be used instead + * (by performing longest-prefix matching). + * + * However, this expansion can be overridden by setting the secure node directory environment + * variable, allowing users to explicitly specify the exact secure root directory to be utilized. + * Such an override is useful for where the FQN of a node is non-deterministic before runtime, + * or when testing and using additional tools that may not otherwise be easily provisioned. + * + * \param[in] node_name validated node name (a single token) + * \param[in] node_namespace validated, absolute namespace (starting with "/") + * \param[in] allocator the allocator to use for allocation + * \returns machine specific (absolute) node secure root path or NULL on failure + * returned pointer must be deallocated by the caller of this function + */ +RCUTILS_PUBLIC +char * rcutils_get_secure_root( + const char * node_name, + const char * node_namespace, + const rcutils_allocator_t * allocator +); + +#ifdef __cplusplus +} +#endif + +#endif // RCUTILS__SECURITY_DIRECTORY_H_ diff --git a/include/rcutils/testing/fault_injection.h b/include/rcutils/testing/fault_injection.h index d397fd74..d74a49bf 100644 --- a/include/rcutils/testing/fault_injection.h +++ b/include/rcutils/testing/fault_injection.h @@ -83,6 +83,8 @@ RCUTILS_WARN_UNUSED int_least64_t _rcutils_fault_injection_maybe_fail(void); +#ifdef RCUTILS_ENABLE_FAULT_INJECTION + /** * \def RCUTILS_FAULT_INJECTION_MAYBE_RETURN_ERROR * \brief This macro checks and decrements a static global variable atomic counter and returns @@ -199,6 +201,17 @@ _rcutils_fault_injection_maybe_fail(void); rcutils_fault_injection_set_count(no_fault_injection_count); \ } while (0) +#else + +// Mocks for micro-ROS when fault injection not enabled + +#define RCUTILS_FAULT_INJECTION_MAYBE_RETURN_ERROR(return_value_on_error) +#define RCUTILS_FAULT_INJECTION_MAYBE_FAIL(failure_code) +#define RCUTILS_FAULT_INJECTION_TEST(code) +#define RCUTILS_NO_FAULT_INJECTION(code) + +#endif + #ifdef __cplusplus } #endif diff --git a/src/allocator.c b/src/allocator.c index 0309da95..886aee72 100644 --- a/src/allocator.c +++ b/src/allocator.c @@ -75,16 +75,31 @@ rcutils_get_zero_initialized_allocator(void) return zero_allocator; } -rcutils_allocator_t -rcutils_get_default_allocator() -{ - static rcutils_allocator_t default_allocator = { +static rcutils_allocator_t default_allocator = { .allocate = __default_allocate, .deallocate = __default_deallocate, .reallocate = __default_reallocate, .zero_allocate = __default_zero_allocate, .state = NULL, }; + +bool +rcutils_set_default_allocator(rcutils_allocator_t * allocator){ + if (rcutils_allocator_is_valid(allocator)) + { + default_allocator.allocate = allocator->allocate; + default_allocator.deallocate = allocator->deallocate; + default_allocator.reallocate = allocator->reallocate; + default_allocator.zero_allocate = allocator->zero_allocate; + default_allocator.state = NULL; + return true; + } + return false; +} + +rcutils_allocator_t +rcutils_get_default_allocator() +{ return default_allocator; } diff --git a/src/atomic_64bits.c b/src/atomic_64bits.c new file mode 100644 index 00000000..f11a39da --- /dev/null +++ b/src/atomic_64bits.c @@ -0,0 +1,92 @@ +// Copyright 2020 Proyectos y Sistemas de Mantenimiento SL (eProsima). +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifdef __cplusplus +extern "C" +{ +#endif + +#include +#include + +#define FLAGS_LEN 23 + +static bool * get_memory_lock(void *address) +{ + static bool memory_locks[FLAGS_LEN] = { 0 }; + uintptr_t a = (uintptr_t)(address); + + // Public domain hash function taken from http://burtleburtle.net/bob/hash/integer.html + a = (a ^ 61) ^ (a >> 16); + a = a + (a << 3); + a = a ^ (a >> 4); + a = a * 0x27d4eb2d; + a = a ^ (a >> 15); + + a = a % FLAGS_LEN; + return memory_locks + a; +} + +void lock_memory(uint64_t *address){ + bool * memory_lock = get_memory_lock(address); + + while (__atomic_test_and_set(memory_lock, __ATOMIC_ACQUIRE) == 1); +} + +void unlock_memory(uint64_t *address){ + bool * memory_lock = get_memory_lock(address); + + __atomic_clear(memory_lock, __ATOMIC_RELEASE); +} + +uint64_t __atomic_load_8(uint64_t *mem, int model) { + (void) model; + + lock_memory(mem); + uint64_t ret = *mem; + unlock_memory(mem); + return ret; +} + +void __atomic_store_8(uint64_t *mem, uint64_t val, int model) { + (void) model; + + lock_memory(mem); + *mem = val; + unlock_memory(mem); +} + +uint64_t __atomic_exchange_8(uint64_t *mem, uint64_t val, int model) { + (void) model; + + lock_memory(mem); + uint64_t ret = *mem; + *mem = val; + unlock_memory(mem); + return ret; +} + +uint64_t __atomic_fetch_add_8(uint64_t *mem, uint64_t val, int model) { + (void) model; + + lock_memory(mem); + uint64_t ret = *mem; + *mem += val; + unlock_memory(mem); + return ret; +} + +#ifdef __cplusplus +} +#endif diff --git a/src/filesystem.c b/src/filesystem.c index a17b0627..48b9cd40 100644 --- a/src/filesystem.c +++ b/src/filesystem.c @@ -22,9 +22,13 @@ extern "C" #include #include #include +#ifndef RCUTILS_NO_FILESYSTEM #include +#endif #ifndef _WIN32 +#ifndef RCUTILS_NO_FILESYSTEM #include +#endif #include #else // When building with MSVC 19.28.29333.0 on Windows 10 (as of 2020-11-11), @@ -57,6 +61,10 @@ extern "C" bool rcutils_get_cwd(char * buffer, size_t max_length) { +#ifdef RCUTILS_NO_FILESYSTEM + RCUTILS_SET_ERROR_MSG("not available filesystem"); + return false; +#else if (NULL == buffer || max_length == 0) { return false; } @@ -70,11 +78,16 @@ rcutils_get_cwd(char * buffer, size_t max_length) } #endif // _WIN32 return true; +#endif // _RCUTILS_NO_FILESYSTEM } bool rcutils_is_directory(const char * abs_path) { +#ifdef RCUTILS_NO_FILESYSTEM + RCUTILS_SET_ERROR_MSG("not available filesystem"); + return false; +#else struct stat buf; if (stat(abs_path, &buf) < 0) { return false; @@ -84,11 +97,16 @@ rcutils_is_directory(const char * abs_path) #else return S_ISDIR(buf.st_mode); #endif // _WIN32 +#endif // _RCUTILS_NO_FILESYSTEM } bool rcutils_is_file(const char * abs_path) { +#ifdef RCUTILS_NO_FILESYSTEM + RCUTILS_SET_ERROR_MSG("not available filesystem"); + return false; +#else struct stat buf; if (stat(abs_path, &buf) < 0) { return false; @@ -98,21 +116,31 @@ rcutils_is_file(const char * abs_path) #else return S_ISREG(buf.st_mode); #endif // _WIN32 +#endif // _RCUTILS_NO_FILESYSTEM } bool rcutils_exists(const char * abs_path) { +#ifdef RCUTILS_NO_FILESYSTEM + RCUTILS_SET_ERROR_MSG("not available filesystem"); + return false; +#else struct stat buf; if (stat(abs_path, &buf) < 0) { return false; } return true; +#endif // _RCUTILS_NO_FILESYSTEM } bool rcutils_is_readable(const char * abs_path) { +#ifdef RCUTILS_NO_FILESYSTEM + RCUTILS_SET_ERROR_MSG("not available filesystem"); + return false; +#else struct stat buf; if (stat(abs_path, &buf) < 0) { return false; @@ -125,11 +153,16 @@ rcutils_is_readable(const char * abs_path) return false; } return true; +#endif // _RCUTILS_NO_FILESYSTEM } bool rcutils_is_writable(const char * abs_path) { +#ifdef RCUTILS_NO_FILESYSTEM + RCUTILS_SET_ERROR_MSG("not available filesystem"); + return false; +#else struct stat buf; if (stat(abs_path, &buf) < 0) { return false; @@ -142,11 +175,16 @@ rcutils_is_writable(const char * abs_path) return false; } return true; +#endif // _RCUTILS_NO_FILESYSTEM } bool rcutils_is_readable_and_writable(const char * abs_path) { +#ifdef RCUTILS_NO_FILESYSTEM + RCUTILS_SET_ERROR_MSG("not available filesystem"); + return false; +#else struct stat buf; if (stat(abs_path, &buf) < 0) { return false; @@ -161,6 +199,7 @@ rcutils_is_readable_and_writable(const char * abs_path) return false; } return true; +#endif // _RCUTILS_NO_FILESYSTEM } char * @@ -335,6 +374,10 @@ rcutils_calculate_directory_size_with_recursion( uint64_t * size, rcutils_allocator_t allocator) { +#ifdef RCUTILS_NO_FILESYSTEM + RCUTILS_SET_ERROR_MSG("not available filesystem"); + return 0; +#else dir_list_t * dir_list = NULL; rcutils_ret_t ret = RCUTILS_RET_OK; @@ -348,6 +391,8 @@ rcutils_calculate_directory_size_with_recursion( return RCUTILS_RET_INVALID_ARGUMENT; } + size_t dir_size = 0; + if (!rcutils_is_directory(directory_path)) { RCUTILS_SAFE_FWRITE_TO_STDERR_WITH_FORMAT_STRING( "Path is not a directory: %s\n", directory_path); @@ -453,11 +498,18 @@ rcutils_calculate_directory_size_with_recursion( free_dir_list(dir_list, allocator); return ret; #endif + +#endif // _RCUTILS_NO_FILESYSTEM } size_t rcutils_get_file_size(const char * file_path) { + +#ifdef RCUTILS_NO_FILESYSTEM + RCUTILS_SET_ERROR_MSG("not available filesystem"); + return 0; +#else if (!rcutils_is_file(file_path)) { RCUTILS_SAFE_FWRITE_TO_STDERR_WITH_FORMAT_STRING( "Path is not a file: %s\n", file_path); @@ -467,6 +519,7 @@ rcutils_get_file_size(const char * file_path) struct stat stat_buffer; int rc = stat(file_path, &stat_buffer); return rc == 0 ? (size_t)(stat_buffer.st_size) : 0; +#endif // _RCUTILS_NO_FILESYSTEM } #ifdef __cplusplus diff --git a/src/process.c b/src/process.c index 2b438426..823fc9f0 100644 --- a/src/process.c +++ b/src/process.c @@ -61,7 +61,13 @@ char * rcutils_get_executable_name(rcutils_allocator_t allocator) #if defined __APPLE__ || defined __FreeBSD__ || (defined __ANDROID__ && __ANDROID_API__ >= 21) const char * appname = getprogname(); #elif defined __GNUC__ && !defined(__QNXNTO__) - const char * appname = program_invocation_name; + #if defined __linux__ || defined __linux || defined __gnu_linux__ || defined linux + const char * appname = program_invocation_name; + #else + // Some embedded OS compile with __GNUC__ but are not quite conformant with GNU-specific extensions. + // They may fake to have a GLIBC in their custom C library implementation. + const char * appname = ""; + #endif #elif defined _WIN32 || defined __CYGWIN__ char appname[MAX_PATH]; int32_t size = GetModuleFileNameA(NULL, appname, MAX_PATH); diff --git a/src/shared_library.c b/src/shared_library.c index 9bdfc7a2..c0200c6b 100644 --- a/src/shared_library.c +++ b/src/shared_library.c @@ -19,6 +19,10 @@ extern "C" #include #include +#include "rcutils/configuration_flags.h" + +#ifndef RCUTILS_NO_FILESYSTEM + #ifndef _WIN32 #if defined(__APPLE__) #include @@ -46,6 +50,8 @@ C_ASSERT(sizeof(void *) == sizeof(HINSTANCE)); C_ASSERT(sizeof(char) == sizeof(TCHAR)); #endif // _WIN32 +#endif //RCUTILS_NO_FILESYSTEM + #include "rcutils/error_handling.h" #include "rcutils/macros.h" #include "rcutils/shared_library.h" @@ -67,6 +73,7 @@ rcutils_load_shared_library( const char * library_path, rcutils_allocator_t allocator) { +#ifndef RCUTILS_NO_FILESYSTEM RCUTILS_CAN_RETURN_WITH_ERROR_OF(RCUTILS_RET_INVALID_ARGUMENT); RCUTILS_CAN_RETURN_WITH_ERROR_OF(RCUTILS_RET_BAD_ALLOC); RCUTILS_CAN_RETURN_WITH_ERROR_OF(RCUTILS_RET_ERROR); @@ -193,6 +200,11 @@ rcutils_load_shared_library( } return ret; #endif // _WIN32 + +#else + return RCUTILS_RET_ERROR; +#endif //RCUTILS_NO_FILESYSTEM + } void * diff --git a/src/time_unix.c b/src/time_unix.c index 47c996c8..35fed19e 100644 --- a/src/time_unix.c +++ b/src/time_unix.c @@ -39,7 +39,7 @@ extern "C" // This id an appropriate check for clock_gettime() according to: // http://man7.org/linux/man-pages/man2/clock_gettime.2.html # if !defined(_POSIX_TIMERS) || !_POSIX_TIMERS -# error no monotonic clock function available +# warning no monotonic clock function available # endif // !defined(_POSIX_TIMERS) || !_POSIX_TIMERS #endif // !defined(__MACH__)