Skip to content

Commit

Permalink
micro-ROS changes over dashing
Browse files Browse the repository at this point in the history
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
  • Loading branch information
pablogs9 committed May 22, 2020
1 parent e5dd0bd commit 4169a2c
Show file tree
Hide file tree
Showing 14 changed files with 711 additions and 9 deletions.
30 changes: 30 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ 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)

# Default to C11
if(NOT CMAKE_C_STANDARD)
set(CMAKE_C_STANDARD 11)
Expand All @@ -15,6 +19,7 @@ include(CheckLibraryExists)

find_package(ament_cmake_python REQUIRED)
find_package(ament_cmake_ros REQUIRED)
find_package(tinydir_vendor REQUIRED)

ament_python_install_package(${PROJECT_NAME})

Expand Down Expand Up @@ -53,7 +58,9 @@ set(rcutils_sources
src/time.c
${time_impl_c}
src/uint8_array.c
src/security_directory.c
)

set_source_files_properties(
${rcutils_sources}
PROPERTIES language "C")
Expand Down Expand Up @@ -91,6 +98,7 @@ add_custom_command(OUTPUT include/rcutils/logging_macros.h
)
list(APPEND rcutils_sources
include/rcutils/logging_macros.h)
include_directories("${CMAKE_CURRENT_BINARY_DIR}/include")

add_library(
${PROJECT_NAME}
Expand All @@ -104,6 +112,11 @@ target_include_directories(${PROJECT_NAME} PUBLIC
# which is appropriate when building the dll but not consuming it.
target_compile_definitions(${PROJECT_NAME} PRIVATE "RCUTILS_BUILDING_DLL")

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})

# Needed if pthread is used for thread local storage.
Expand All @@ -116,11 +129,18 @@ install(TARGETS ${PROJECT_NAME} EXPORT ${PROJECT_NAME}
LIBRARY DESTINATION lib
RUNTIME DESTINATION bin)

ament_target_dependencies(${PROJECT_NAME}
"tinydir_vendor"
)

if(BUILD_TESTING)
if(NOT WIN32)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14")
endif()

set(test_resources_dir_name "test/resources")
add_definitions(-DTEST_RESOURCES_DIRECTORY="${CMAKE_CURRENT_BINARY_DIR}/${test_resources_dir_name}")

find_package(ament_cmake_gmock REQUIRED)
find_package(ament_cmake_gtest REQUIRED)
find_package(ament_cmake_pytest REQUIRED)
Expand Down Expand Up @@ -428,6 +448,16 @@ if(BUILD_TESTING)
endif()


rcutils_custom_add_gtest(test_security_directory
test/test_security_directory.cpp
)
if(TARGET test_security_directory)
target_link_libraries(test_security_directory ${PROJECT_NAME} osrf_testing_tools_cpp::memory_tools)
endif()

install(DIRECTORY ${test_resources_dir_name}
DESTINATION ${CMAKE_CURRENT_BINARY_DIR}
)
endif()

ament_export_dependencies(ament_cmake)
Expand Down
15 changes: 15 additions & 0 deletions include/rcutils/allocator.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,21 @@ RCUTILS_WARN_UNUSED
rcutils_allocator_t
rcutils_get_zero_initialized_allocator(void);

/// Set rcutils default allocators.
/**
* <hr>
* 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:
Expand Down
18 changes: 18 additions & 0 deletions include/rcutils/configuration_flags.h.in
Original file line number Diff line number Diff line change
@@ -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_
16 changes: 14 additions & 2 deletions include/rcutils/error_handling.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,18 @@ extern "C"
#include "rcutils/snprintf.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

// fixed constraints
Expand Down Expand Up @@ -199,8 +202,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.
/**
Expand All @@ -211,6 +218,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]; \
Expand All @@ -221,6 +231,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


/// Return `true` if the error is set, otherwise `false`.
RCUTILS_PUBLIC
Expand Down
6 changes: 5 additions & 1 deletion include/rcutils/macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ extern "C"
{
#endif

#include "rcutils/configuration_flags.h"

#ifndef _WIN32
#define RCUTILS_WARN_UNUSED __attribute__((warn_unused_result))
#else
Expand All @@ -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__
Expand Down
67 changes: 67 additions & 0 deletions include/rcutils/security_directory.h
Original file line number Diff line number Diff line change
@@ -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_
3 changes: 3 additions & 0 deletions package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
<buildtool_depend>ament_cmake_ros</buildtool_depend>
<buildtool_depend>python3-empy</buildtool_depend>

<build_depend>tinydir_vendor</build_depend>
<build_export_depend>tinydir_vendor</build_export_depend>

<test_depend>ament_cmake_gmock</test_depend>
<test_depend>ament_cmake_gtest</test_depend>
<test_depend>ament_cmake_pytest</test_depend>
Expand Down
23 changes: 19 additions & 4 deletions src/allocator.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,16 +69,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;
}

Expand Down
Loading

0 comments on commit 4169a2c

Please sign in to comment.