Skip to content

Commit f70d383

Browse files
committed
micro-ROS Rolling patch
* micro-ROS changes over dashing Feature/add security directory (#1) * Added security directory * Updated security directory Feature/avoid filesystem and allocation (#2) * Included RCUTILS_NO_FILESYSTEM and RCUTILS_AVOID_DYNAMIC_ALLOCATION * Added no filesystem options * Default allocators write access * Avoid dynamic allocation and no filesytem on error handling * Typo * New flags for filesystem and avoid dynamic * Error handling template * New allocator approach Add test_security_directory test from rcl. (#3) Merge pull request #4 from micro-ROS/feature/zephyr_fixes Feature/zephyr fixes CMake refactor (#5) Update approach (#6) * Update approach * Remove target_compile_definitions and refactor flags install * Added RCUTILS_NO_FILESYSTEM on new functions * Added RCUTILS_NO_FILESYSTEM on new functions Co-authored-by: Pablo Garrido <[email protected]> Updates 17092020 Fix atomics 64bits (#9) * micro-ROS changes over dashing Feature/add security directory (#1) * Added security directory * Updated security directory Feature/avoid filesystem and allocation (#2) * Included RCUTILS_NO_FILESYSTEM and RCUTILS_AVOID_DYNAMIC_ALLOCATION * Added no filesystem options * Default allocators write access * Avoid dynamic allocation and no filesytem on error handling * Typo * New flags for filesystem and avoid dynamic * Error handling template * New allocator approach Add test_security_directory test from rcl. (#3) Merge pull request #4 from micro-ROS/feature/zephyr_fixes Feature/zephyr fixes CMake refactor (#5) Update approach (#6) * Update approach * Remove target_compile_definitions and refactor flags install * Added RCUTILS_NO_FILESYSTEM on new functions * Added RCUTILS_NO_FILESYSTEM on new functions Co-authored-by: Pablo Garrido <[email protected]> * Initial changes * Add hashing and lock pool * Updates Co-authored-by: Jose Antonio Moral <[email protected]> Fix atomics 64bits (#9) Updates 09102020 * Release micro-ROS Foxy (#8) Update Cleaning Update Update filesystem Updates Adjust logger level
1 parent befc608 commit f70d383

15 files changed

+379
-13
lines changed

CMakeLists.txt

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@ cmake_minimum_required(VERSION 3.12)
22

33
project(rcutils)
44

5+
option(RCUTILS_NO_THREAD_SUPPORT "Disable thread support." OFF)
6+
option(RCUTILS_NO_FILESYSTEM "Disable filesystem usage." OFF)
7+
option(RCUTILS_AVOID_DYNAMIC_ALLOCATION "Disable dynamic allocations." OFF)
8+
option(RCUTILS_NO_64_ATOMIC "Disable support for 64 bits atomic operations." OFF)
9+
option(RCUTILS_MICROROS "Flag for building micro-ROS." ON)
10+
511
# Default to C11
612
if(NOT CMAKE_C_STANDARD)
713
set(CMAKE_C_STANDARD 11)
@@ -30,7 +36,7 @@ if(UNIX AND NOT APPLE)
3036
endif()
3137
endif()
3238

33-
if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
39+
if(NOT RCUTILS_MICROROS AND (CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang"))
3440
# enables building a static library but later link it into a dynamic library
3541
add_compile_options(-fPIC)
3642
endif()
@@ -75,6 +81,7 @@ set(rcutils_sources
7581
src/time.c
7682
${time_impl_c}
7783
src/uint8_array.c
84+
$<$<BOOL:${RCUTILS_NO_64_ATOMIC}>:src/atomic_64bits.c>
7885
)
7986
set_source_files_properties(
8087
${rcutils_sources}
@@ -129,6 +136,10 @@ target_compile_definitions(${PROJECT_NAME} PRIVATE "RCUTILS_BUILDING_DLL")
129136
if(BUILD_TESTING AND NOT RCUTILS_DISABLE_FAULT_INJECTION)
130137
target_compile_definitions(${PROJECT_NAME} PUBLIC RCUTILS_ENABLE_FAULT_INJECTION)
131138
endif()
139+
configure_file(
140+
"${PROJECT_SOURCE_DIR}/include/rcutils/configuration_flags.h.in"
141+
"${PROJECT_BINARY_DIR}/include/rcutils/configuration_flags.h"
142+
)
132143

133144
target_link_libraries(${PROJECT_NAME} ${CMAKE_DL_LIBS})
134145

include/rcutils/allocator.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,21 @@ RCUTILS_WARN_UNUSED
8585
rcutils_allocator_t
8686
rcutils_get_zero_initialized_allocator(void);
8787

88+
/// Set rcutils default allocators.
89+
/**
90+
* <hr>
91+
* Attribute | Adherence
92+
* ------------------ | -------------
93+
* Allocates Memory | No
94+
* Thread-Safe | Yes
95+
* Uses Atomics | No
96+
* Lock-Free | Yes
97+
*/
98+
RCUTILS_PUBLIC
99+
RCUTILS_WARN_UNUSED
100+
bool
101+
rcutils_set_default_allocator(rcutils_allocator_t * allocator);
102+
88103
/// Return a properly initialized rcutils_allocator_t with default values.
89104
/**
90105
* This defaults to:
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
2+
#ifndef RCUTILS__CONFIGURATION_FLAGS_H_
3+
#define RCUTILS__CONFIGURATION_FLAGS_H_
4+
5+
#ifdef __cplusplus
6+
extern "C"
7+
{
8+
#endif
9+
10+
#cmakedefine RCUTILS_NO_FILESYSTEM
11+
#cmakedefine RCUTILS_AVOID_DYNAMIC_ALLOCATION
12+
#cmakedefine RCUTILS_NO_THREAD_SUPPORT
13+
14+
#ifdef __cplusplus
15+
}
16+
#endif
17+
18+
#endif // RCUTILS__CONFIGURATION_FLAGS_H_

include/rcutils/error_handling.h

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,19 +39,21 @@ extern "C"
3939
#include "rcutils/testing/fault_injection.h"
4040
#include "rcutils/types/rcutils_ret.h"
4141
#include "rcutils/visibility_control.h"
42+
#include "rcutils/configuration_flags.h"
4243

43-
#ifdef __STDC_LIB_EXT1__
44+
#if defined(__STDC_LIB_EXT1__) && !defined(RCUTILS_NO_FILESYSTEM)
4445
/// Write the given msg out to stderr, limiting the buffer size in the `fwrite`.
4546
/**
4647
* This ensures that there is an upper bound to a buffer overrun if `msg` is
4748
* non-null terminated.
4849
*/
4950
#define RCUTILS_SAFE_FWRITE_TO_STDERR(msg) \
5051
do {fwrite(msg, sizeof(char), strnlen_s(msg, 4096), stderr);} while (0)
51-
#else
52-
/// Write the given msg out to stderr.
52+
#elif !defined(RCUTILS_NO_FILESYSTEM)
5353
#define RCUTILS_SAFE_FWRITE_TO_STDERR(msg) \
5454
do {fwrite(msg, sizeof(char), strlen(msg), stderr);} while (0)
55+
#else
56+
#define RCUTILS_SAFE_FWRITE_TO_STDERR(msg)
5557
#endif
5658

5759
/// Set the error message to stderr using a format string and format arguments.
@@ -233,8 +235,12 @@ rcutils_set_error_state(const char * error_string, const char * file, size_t lin
233235
*
234236
* \param[in] msg The error message to be set.
235237
*/
238+
#ifdef RCUTILS_AVOID_DYNAMIC_ALLOCATION
239+
#define RCUTILS_SET_ERROR_MSG(msg)
240+
#else
236241
#define RCUTILS_SET_ERROR_MSG(msg) \
237242
do {rcutils_set_error_state(msg, __FILE__, __LINE__);} while (0)
243+
#endif // RCUTILS_AVOID_DYNAMIC_ALLOCATION
238244

239245
/// Set the error message using a format string and format arguments.
240246
/**
@@ -245,6 +251,9 @@ rcutils_set_error_state(const char * error_string, const char * file, size_t lin
245251
* \param[in] format_string The string to be used as the format of the error message.
246252
* \param[in] ... Arguments for the format string.
247253
*/
254+
#ifdef RCUTILS_AVOID_DYNAMIC_ALLOCATION
255+
#define RCUTILS_SET_ERROR_MSG_WITH_FORMAT_STRING(format_string, ...)
256+
#else
248257
#define RCUTILS_SET_ERROR_MSG_WITH_FORMAT_STRING(format_string, ...) \
249258
do { \
250259
char output_msg[RCUTILS_ERROR_MESSAGE_MAX_LENGTH]; \
@@ -255,6 +264,8 @@ rcutils_set_error_state(const char * error_string, const char * file, size_t lin
255264
RCUTILS_SET_ERROR_MSG(output_msg); \
256265
} \
257266
} while (0)
267+
#endif // RCUTILS_AVOID_DYNAMIC_ALLOCATION
268+
258269

259270
/// Indicate that the function intends to set an error message and return an error value.
260271
/**

include/rcutils/logging.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ extern "C"
4040
* \def RCUTILS_DEFAULT_LOGGER_DEFAULT_LEVEL
4141
* \brief The default severity level of the default logger.
4242
*/
43-
#define RCUTILS_DEFAULT_LOGGER_DEFAULT_LEVEL RCUTILS_LOG_SEVERITY_INFO
43+
#define RCUTILS_DEFAULT_LOGGER_DEFAULT_LEVEL RCUTILS_LOG_SEVERITY_UNSET
4444

4545
/// The flag if the logging system has been initialized.
4646
RCUTILS_PUBLIC

include/rcutils/macros.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ extern "C"
2222
{
2323
#endif
2424

25+
#include "rcutils/configuration_flags.h"
26+
2527
#ifndef _WIN32
2628
/// A macro to make the compiler warn when the return value of a function is not used.
2729
#define RCUTILS_WARN_UNUSED __attribute__((warn_unused_result))
@@ -32,7 +34,9 @@ extern "C"
3234

3335
/// @cond Doxygen_Suppress
3436
// This block either sets RCUTILS_THREAD_LOCAL or RCUTILS_THREAD_LOCAL_PTHREAD.
35-
#if defined _WIN32 || defined __CYGWIN__
37+
#if defined(RCUTILS_NO_THREAD_SUPPORT)
38+
#define RCUTILS_THREAD_LOCAL
39+
#elif defined _WIN32 || defined __CYGWIN__
3640
// Windows or Cygwin
3741
#define RCUTILS_THREAD_LOCAL __declspec(thread)
3842
#elif defined __APPLE__

include/rcutils/security_directory.h

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// Copyright 2018 Open Source Robotics Foundation, Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#ifndef RCUTILS__SECURITY_DIRECTORY_H_
16+
#define RCUTILS__SECURITY_DIRECTORY_H_
17+
18+
#ifdef __cplusplus
19+
extern "C"
20+
{
21+
#endif
22+
23+
#include "rcutils/allocator.h"
24+
#include "rcutils/visibility_control.h"
25+
26+
#ifndef ROS_SECURITY_NODE_DIRECTORY_VAR_NAME
27+
#define ROS_SECURITY_NODE_DIRECTORY_VAR_NAME "ROS_SECURITY_NODE_DIRECTORY"
28+
#endif
29+
30+
#ifndef ROS_SECURITY_ROOT_DIRECTORY_VAR_NAME
31+
#define ROS_SECURITY_ROOT_DIRECTORY_VAR_NAME "ROS_SECURITY_ROOT_DIRECTORY"
32+
#endif
33+
34+
#ifndef ROS_SECURITY_LOOKUP_TYPE_VAR_NAME
35+
#define ROS_SECURITY_LOOKUP_TYPE_VAR_NAME "ROS_SECURITY_LOOKUP_TYPE"
36+
#endif
37+
38+
/// Return the secure root directory associated with a node given its validated name and namespace.
39+
/**
40+
* E.g. for a node named "c" in namespace "/a/b", the secure root path will be
41+
* "a/b/c", where the delimiter "/" is native for target file system (e.g. "\\" for _WIN32).
42+
* If no exact match is found for the node name, a best match would be used instead
43+
* (by performing longest-prefix matching).
44+
*
45+
* However, this expansion can be overridden by setting the secure node directory environment
46+
* variable, allowing users to explicitly specify the exact secure root directory to be utilized.
47+
* Such an override is useful for where the FQN of a node is non-deterministic before runtime,
48+
* or when testing and using additional tools that may not otherwise be easily provisioned.
49+
*
50+
* \param[in] node_name validated node name (a single token)
51+
* \param[in] node_namespace validated, absolute namespace (starting with "/")
52+
* \param[in] allocator the allocator to use for allocation
53+
* \returns machine specific (absolute) node secure root path or NULL on failure
54+
* returned pointer must be deallocated by the caller of this function
55+
*/
56+
RCUTILS_PUBLIC
57+
char * rcutils_get_secure_root(
58+
const char * node_name,
59+
const char * node_namespace,
60+
const rcutils_allocator_t * allocator
61+
);
62+
63+
#ifdef __cplusplus
64+
}
65+
#endif
66+
67+
#endif // RCUTILS__SECURITY_DIRECTORY_H_

include/rcutils/testing/fault_injection.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ RCUTILS_WARN_UNUSED
8383
int_least64_t
8484
_rcutils_fault_injection_maybe_fail(void);
8585

86+
#ifdef RCUTILS_ENABLE_FAULT_INJECTION
87+
8688
/**
8789
* \def RCUTILS_FAULT_INJECTION_MAYBE_RETURN_ERROR
8890
* \brief This macro checks and decrements a static global variable atomic counter and returns
@@ -199,6 +201,17 @@ _rcutils_fault_injection_maybe_fail(void);
199201
rcutils_fault_injection_set_count(no_fault_injection_count); \
200202
} while (0)
201203

204+
#else
205+
206+
// Mocks for micro-ROS when fault injection not enabled
207+
208+
#define RCUTILS_FAULT_INJECTION_MAYBE_RETURN_ERROR(return_value_on_error)
209+
#define RCUTILS_FAULT_INJECTION_MAYBE_FAIL(failure_code)
210+
#define RCUTILS_FAULT_INJECTION_TEST(code)
211+
#define RCUTILS_NO_FAULT_INJECTION(code)
212+
213+
#endif
214+
202215
#ifdef __cplusplus
203216
}
204217
#endif

resource/logging_macros.h.em

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ extern "C"
4444
* Use RCUTILS_LOG_MIN_SEVERITY_NONE to compile out all macros.
4545
*/
4646
#ifndef RCUTILS_LOG_MIN_SEVERITY
47-
#define RCUTILS_LOG_MIN_SEVERITY RCUTILS_LOG_MIN_SEVERITY_DEBUG
47+
#define RCUTILS_LOG_MIN_SEVERITY RCUTILS_LOG_MIN_SEVERITY_NONE
4848
#endif
4949

5050
// TODO(dhood): optimise severity check via notifyLoggerLevelsChanged concept or similar.

src/allocator.c

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,16 +75,31 @@ rcutils_get_zero_initialized_allocator(void)
7575
return zero_allocator;
7676
}
7777

78-
rcutils_allocator_t
79-
rcutils_get_default_allocator()
80-
{
81-
static rcutils_allocator_t default_allocator = {
78+
static rcutils_allocator_t default_allocator = {
8279
.allocate = __default_allocate,
8380
.deallocate = __default_deallocate,
8481
.reallocate = __default_reallocate,
8582
.zero_allocate = __default_zero_allocate,
8683
.state = NULL,
8784
};
85+
86+
bool
87+
rcutils_set_default_allocator(rcutils_allocator_t * allocator){
88+
if (rcutils_allocator_is_valid(allocator))
89+
{
90+
default_allocator.allocate = allocator->allocate;
91+
default_allocator.deallocate = allocator->deallocate;
92+
default_allocator.reallocate = allocator->reallocate;
93+
default_allocator.zero_allocate = allocator->zero_allocate;
94+
default_allocator.state = NULL;
95+
return true;
96+
}
97+
return false;
98+
}
99+
100+
rcutils_allocator_t
101+
rcutils_get_default_allocator()
102+
{
88103
return default_allocator;
89104
}
90105

0 commit comments

Comments
 (0)