Skip to content

Commit 7154fed

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
1 parent 666241e commit 7154fed

13 files changed

+373
-10
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.5)
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)
@@ -28,7 +34,7 @@ if(UNIX AND NOT APPLE)
2834
endif()
2935
endif()
3036

31-
if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
37+
if(NOT RCUTILS_MICROROS AND (CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang"))
3238
# enables building a static library but later link it into a dynamic library
3339
add_compile_options(-fPIC)
3440
endif()
@@ -74,6 +80,7 @@ set(rcutils_sources
7480
src/time.c
7581
${time_impl_c}
7682
src/uint8_array.c
83+
$<$<BOOL:${RCUTILS_NO_64_ATOMIC}>:src/atomic_64bits.c>
7784
)
7885
set_source_files_properties(
7986
${rcutils_sources}
@@ -128,6 +135,10 @@ target_compile_definitions(${PROJECT_NAME} PRIVATE "RCUTILS_BUILDING_DLL")
128135
if(BUILD_TESTING AND NOT RCUTILS_DISABLE_FAULT_INJECTION)
129136
target_compile_definitions(${PROJECT_NAME} PUBLIC RCUTILS_ENABLE_FAULT_INJECTION)
130137
endif()
138+
configure_file(
139+
"${PROJECT_SOURCE_DIR}/include/rcutils/configuration_flags.h.in"
140+
"${PROJECT_BINARY_DIR}/include/rcutils/configuration_flags.h"
141+
)
131142

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

include/rcutils/allocator.h

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

86+
/// Set rcutils default allocators.
87+
/**
88+
* <hr>
89+
* Attribute | Adherence
90+
* ------------------ | -------------
91+
* Allocates Memory | No
92+
* Thread-Safe | Yes
93+
* Uses Atomics | No
94+
* Lock-Free | Yes
95+
*/
96+
RCUTILS_PUBLIC
97+
RCUTILS_WARN_UNUSED
98+
bool
99+
rcutils_set_default_allocator(rcutils_allocator_t * allocator);
100+
86101
/// Return a properly initialized rcutils_allocator_t with default values.
87102
/**
88103
* 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 & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,18 @@ 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
// Limit the buffer size in the `fwrite` call to give an upper bound to buffer overrun in the case
4546
// of non-null terminated `msg`.
4647
#define RCUTILS_SAFE_FWRITE_TO_STDERR(msg) \
4748
do {fwrite(msg, sizeof(char), strnlen_s(msg, 4096), stderr);} while (0)
48-
#else
49+
#elif !defined(RCUTILS_NO_FILESYSTEM)
4950
#define RCUTILS_SAFE_FWRITE_TO_STDERR(msg) \
5051
do {fwrite(msg, sizeof(char), strlen(msg), stderr);} while (0)
52+
#else
53+
#define RCUTILS_SAFE_FWRITE_TO_STDERR(msg)
5154
#endif
5255

5356
/// 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
220223
*
221224
* \param[in] msg The error message to be set.
222225
*/
226+
#ifdef RCUTILS_AVOID_DYNAMIC_ALLOCATION
227+
#define RCUTILS_SET_ERROR_MSG(msg)
228+
#else
223229
#define RCUTILS_SET_ERROR_MSG(msg) \
224230
do {rcutils_set_error_state(msg, __FILE__, __LINE__);} while (0)
231+
#endif // RCUTILS_AVOID_DYNAMIC_ALLOCATION
225232

226233
/// Set the error message using a format string and format arguments.
227234
/**
@@ -232,6 +239,9 @@ rcutils_set_error_state(const char * error_string, const char * file, size_t lin
232239
* \param[in] format_string The string to be used as the format of the error message.
233240
* \param[in] ... Arguments for the format string.
234241
*/
242+
#ifdef RCUTILS_AVOID_DYNAMIC_ALLOCATION
243+
#define RCUTILS_SET_ERROR_MSG_WITH_FORMAT_STRING(format_string, ...)
244+
#else
235245
#define RCUTILS_SET_ERROR_MSG_WITH_FORMAT_STRING(format_string, ...) \
236246
do { \
237247
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
242252
RCUTILS_SET_ERROR_MSG(output_msg); \
243253
} \
244254
} while (0)
255+
#endif // RCUTILS_AVOID_DYNAMIC_ALLOCATION
256+
245257

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

include/rcutils/macros.h

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

23+
#include "rcutils/configuration_flags.h"
24+
2325
#ifndef _WIN32
2426
#define RCUTILS_WARN_UNUSED __attribute__((warn_unused_result))
2527
#else
@@ -28,7 +30,9 @@ extern "C"
2830

2931
// Note: this block was migrated from rmw/macros.h
3032
// This block either sets RCUTILS_THREAD_LOCAL or RCUTILS_THREAD_LOCAL_PTHREAD.
31-
#if defined _WIN32 || defined __CYGWIN__
33+
#if defined(RCUTILS_NO_THREAD_SUPPORT)
34+
#define RCUTILS_THREAD_LOCAL
35+
#elif defined _WIN32 || defined __CYGWIN__
3236
// Windows or Cygwin
3337
#define RCUTILS_THREAD_LOCAL __declspec(thread)
3438
#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

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

src/atomic_64bits.c

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
// Copyright 2020 Proyectos y Sistemas de Mantenimiento SL (eProsima).
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+
#ifdef __cplusplus
16+
extern "C"
17+
{
18+
#endif
19+
20+
#include <stdint.h>
21+
#include <stdbool.h>
22+
23+
#define FLAGS_LEN 23
24+
25+
static bool * get_memory_lock(void *address)
26+
{
27+
static bool memory_locks[FLAGS_LEN] = { 0 };
28+
uintptr_t a = (uintptr_t)(address);
29+
30+
// Public domain hash function taken from http://burtleburtle.net/bob/hash/integer.html
31+
a = (a ^ 61) ^ (a >> 16);
32+
a = a + (a << 3);
33+
a = a ^ (a >> 4);
34+
a = a * 0x27d4eb2d;
35+
a = a ^ (a >> 15);
36+
37+
a = a % FLAGS_LEN;
38+
return memory_locks + a;
39+
}
40+
41+
void lock_memory(uint64_t *address){
42+
bool * memory_lock = get_memory_lock(address);
43+
44+
while (__atomic_test_and_set(memory_lock, __ATOMIC_ACQUIRE) == 1);
45+
}
46+
47+
void unlock_memory(uint64_t *address){
48+
bool * memory_lock = get_memory_lock(address);
49+
50+
__atomic_clear(memory_lock, __ATOMIC_RELEASE);
51+
}
52+
53+
uint64_t __atomic_load_8(uint64_t *mem, int model) {
54+
(void) model;
55+
56+
lock_memory(mem);
57+
uint64_t ret = *mem;
58+
unlock_memory(mem);
59+
return ret;
60+
}
61+
62+
void __atomic_store_8(uint64_t *mem, uint64_t val, int model) {
63+
(void) model;
64+
65+
lock_memory(mem);
66+
*mem = val;
67+
unlock_memory(mem);
68+
}
69+
70+
uint64_t __atomic_exchange_8(uint64_t *mem, uint64_t val, int model) {
71+
(void) model;
72+
73+
lock_memory(mem);
74+
uint64_t ret = *mem;
75+
*mem = val;
76+
unlock_memory(mem);
77+
return ret;
78+
}
79+
80+
uint64_t __atomic_fetch_add_8(uint64_t *mem, uint64_t val, int model) {
81+
(void) model;
82+
83+
lock_memory(mem);
84+
uint64_t ret = *mem;
85+
*mem += val;
86+
unlock_memory(mem);
87+
return ret;
88+
}
89+
90+
#ifdef __cplusplus
91+
}
92+
#endif

0 commit comments

Comments
 (0)