Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: os/posix: port of the posix implementation to macOS #352

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
build/inc/
posix-macos-addons

3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,9 @@ target_include_directories(osal INTERFACE
${OSAL_API_INCLUDE_DIRECTORIES}
)

add_subdirectory(posix-macos-addons)
target_link_libraries(osal posix-macos-addons)

# Link the OSAL with the BSP
target_link_libraries(osal osal_bsp)

Expand Down
37 changes: 37 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@


test: test.unit test.integration
Copy link
Contributor Author

@stanislaw stanislaw Jan 22, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the first obvious candidate for removal or extraction. I have not found a single place in the osal from where I can run ALL unit and integration tests. I suspect that there is some private branch of nasa/osal where such script exists.

Having such a "one-click" solution for running all of the tests is essential for making sure that the osal is not broken when making changes and testing them on both Linux and macOS.

Please advise if I should remove this custom Makefile and rather use some existing test scripts.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like this will satisfy it: #403.


test.unit: build.cmake
cd build.commandline.dir && ./unit-tests/oscore-test/osal_core_UT
cd build.commandline.dir && ./unit-tests/osfile-test/osal_file_UT
cd build.commandline.dir && ./unit-tests/osfilesys-test/osal_filesys_UT
# There are module files that the binary expects in the output folder.
# Therefore cd'ing to the test binary's dir.
cd build.commandline.dir/unit-tests/osloader-test && ./osal_loader_UT
cd build.commandline.dir && ./unit-tests/ostimer-test/osal_timer_UT
cd build.commandline.dir && ./unit-tests/osnetwork-test/osal_network_UT

test.integration: build.cmake
cd build.commandline.dir && ./tests/bin-sem-flush-test
cd build.commandline.dir && ./tests/bin-sem-test
cd build.commandline.dir && ./tests/bin-sem-timeout-test
cd build.commandline.dir && ./tests/count-sem-test
cd build.commandline.dir && ./tests/file-api-test
cd build.commandline.dir && ./tests/mutex-test
cd build.commandline.dir && ./tests/osal-core-test
cd build.commandline.dir && ./tests/queue-timeout-test
cd build.commandline.dir && ./tests/sem-speed-test
cd build.commandline.dir && ./tests/symbol-api-test
cd build.commandline.dir && ./tests/timer-test

build.cmake:
mkdir -p build.commandline.dir
cd build.commandline.dir && cmake -G Ninja \
-DCMAKE_C_FLAGS="-Werror" \
-DENABLE_UNIT_TESTS=1 \
-DOSAL_SYSTEM_OSTYPE=posix \
-DOSAL_SYSTEM_BSPTYPE=pc-linux \
-DOSAL_INCLUDEDIR=src/bsp/pc-linux/config \
..
cd build.commandline.dir && ninja
4 changes: 2 additions & 2 deletions src/bsp/pc-linux/build_options.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ target_link_libraries(osal_bsp
# Note - although GCC understands the same flags for compile and link here, this may
# not be true on all platforms so the compile and link flags are specified separately.
if (NOT CMAKE_CROSSCOMPILING)
set(UT_COVERAGE_COMPILE_FLAGS -pg --coverage)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Opened here: #420.

set(UT_COVERAGE_LINK_FLAGS -pg --coverage)
set(UT_COVERAGE_COMPILE_FLAGS --coverage)
set(UT_COVERAGE_LINK_FLAGS --coverage)
endif()

# This indicates where to stage target binaries created during the build
Expand Down
33 changes: 32 additions & 1 deletion src/os/portable/os-impl-posix-dl.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
* dlsym()
*/

#include <dlfcn.h>

/****************************************************************************************
DEFINES
***************************************************************************************/
Expand Down Expand Up @@ -72,6 +74,26 @@ OS_impl_module_internal_record_t OS_impl_module_table[OS_MAX_MODULES];
DEFINES
***************************************************************************************/

/*
* Determine what to pass in for the first parameter of dlsym()
*
* If the "os-impl-loader.h" header already defined this, then use that.
*
* Otherwise, check if the C library provides an "RTLD_DEFAULT" symbol -
* This symbol is not POSIX standard but many implementations do provide it.
*
* Lastly, if nothing else works, use NULL. This is technically undefined
* behavior per POSIX, but most implementations do seem to interpret this
* as referring to the complete process (base executable + all loaded modules).
*/
#ifndef OSAL_DLSYM_DEFAULT_HANDLE
#ifdef RTLD_DEFAULT
#define OSAL_DLSYM_DEFAULT_HANDLE RTLD_DEFAULT
#else
#define OSAL_DLSYM_DEFAULT_HANDLE NULL
#endif
#endif

/****************************************************************************************
FUNCTION PROTOTYPES
***************************************************************************************/
Expand Down Expand Up @@ -123,7 +145,16 @@ int32 OS_SymbolLookup_Impl( cpuaddr *SymbolAddress, const char *SymbolName )
* call dlerror() to clear any prior error that might have occured.
*/
dlerror();
Function = dlsym((void *)0, SymbolName);

/// TODO-MAC:
/// [BEGIN] 04 OS_SymbolLookup
/// [ PASS] 04.001 ut_osloader_symtable_test.c:149 - #1 Invalid-pointer-arg-1
/// [ PASS] 04.002 ut_osloader_symtable_test.c:158 - #2 Invalid-pointer-arg-2
/// [ PASS] 04.003 ut_osloader_symtable_test.c:167 - #3 Symbol-not-found
/// Current working dir: /sandbox/cFS/osal/build.commandline.dir
/// [ FAIL] 04.004 ut_osloader_symtable_test.c:186 - #4 Nominal
/// [ END] 04 OS_SymbolLookup TOTAL::4 PASS::3 FAIL::1 MIR::0 TSF::0 N/A::0
Function = dlsym((void *)OSAL_DLSYM_DEFAULT_HANDLE, SymbolName);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See #214.

dlError = dlerror();
if( dlError == NULL )
{
Expand Down
10 changes: 10 additions & 0 deletions src/os/posix/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,13 @@ add_library(osal_posix_impl OBJECT
ostimer.c
)

target_link_libraries(osal_posix_impl posix-macos-addons)

# TODO-MAC: Defining this globally but can be made more focused.
# 1) In file included from /sandbox/cFS/osal/src/os/posix/osloader.c:32:
#/sandbox/cFS/osal/src/os/posix/../portable/os-impl-posix-dl.c:130:30: error: use of undeclared identifier 'RTLD_DEFAULT'
# Function = dlsym((void *)RTLD_DEFAULT, SymbolName);
# ^
# 2) /usr/include/sys/ucred.h:96:11: error: unknown type name 'u_long'; did you mean 'long'?
# volatile u_long cr_ref; /* reference count */
target_compile_definitions(osal_posix_impl PRIVATE -D_DARWIN_C_SOURCE)
6 changes: 6 additions & 0 deletions src/os/posix/osapi.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,14 @@

#include "os-posix.h"
#include "bsp-impl.h"

#include <sched.h>

#include <posix-macos-time.h>
#include <posix-macos-semaphore2-debug.h>
#include <posix-macos-stubs.h>
#include <posix-macos-pthread.h>

/*
* Defines
*/
Expand Down
7 changes: 6 additions & 1 deletion src/os/posix/osfilesys.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,12 @@
#include <sys/statvfs.h>
#include <sys/stat.h>
#include <sys/mount.h>
#include <sys/vfs.h>

/// TODO-MAC
/// I found somewhere that sys/vfs.h could be replaced with sys/mount.h.
/// I have no idea what the difference is, but now it works!
/// https://github.com/kartverket/fyba/issues/12
//#include <sys/vfs.h>

#include "common_types.h"
#include "osapi.h"
Expand Down
21 changes: 17 additions & 4 deletions src/os/posix/ostimer.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@

#include "os-posix.h"

#include <posix-macos-stubs.h>
#include <posix-macos-timer.h>

/****************************************************************************************
EXTERNAL FUNCTION PROTOTYPES
***************************************************************************************/
Expand Down Expand Up @@ -144,7 +147,10 @@ static uint32 OS_TimeBase_SigWaitImpl(uint32 timer_id)

local = &OS_impl_timebase_table[timer_id];

ret = sigwait(&local->sigset, &sig);
/// TODO-MAC: Replacing sigwait with a custom timer_poll to poll for timer
/// TODO-MAC: ticks.
/// ret = sigwait(&local->sigset, &sig);
ret = timer_poll(local->host_timerid);

if (ret != 0)
{
Expand Down Expand Up @@ -404,7 +410,13 @@ int32 OS_TimeBaseCreate_Impl(uint32 timer_id)
sigemptyset(&local->sigset);
sigaddset(&local->sigset, local->assigned_signal);

/*
/**
* TODO-MAC: Setting up signals below and above is not needed
* because they are not used by the implementation of timer_*.
* Keeping everything intact to be as minimally invasive as possible
* and to keep the diff smaller for now.

*
* Ensure that the chosen signal is NOT already pending.
*
* Perform a "sigtimedwait" with a zero timeout to poll the
Expand All @@ -417,20 +429,21 @@ int32 OS_TimeBaseCreate_Impl(uint32 timer_id)
*
* The output is irrelevant here; the objective is to just ensure
* that the signal is not already pending.
*/
*
i = sysconf( _SC_SIGQUEUE_MAX);
do
{
ts.tv_sec = 0;
ts.tv_nsec = 0;
if (sigtimedwait(&local->sigset, NULL, &ts) < 0)
{
/* signal is NOT pending */
/// signal is NOT pending
break;
}
--i;
}
while(i > 0);
*/

/*
** Initialize the sigevent structures for the handler.
Expand Down
3 changes: 2 additions & 1 deletion src/tests/osal-core-test/osal-core-test.c
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,8 @@ void TestTasks(void)
status = OS_TaskDelete( TaskData[tasknum].task_id );

UtDebug("Delete Status = %d, Id = %d\n",(int)status,(int)TaskData[tasknum].task_id);

/// TODO-MAC: 1 of 5 full runs of make this line fails:
Copy link
Contributor Author

@stanislaw stanislaw Apr 21, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reported here: #424.

/// [ FAIL] 01.205 osal-core-test.c:138 - OS_TaskDelete, self exiting task
UtAssert_True(status != OS_SUCCESS, "OS_TaskDelete, self exiting task");

}
Expand Down
1 change: 1 addition & 0 deletions src/unit-test-coverage/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ function (add_coverage_tests SETNAME)
${UT_COVERAGE_LINK_FLAGS}
${OSALCOVERAGE_STUB_LIB_LIST}
ut_assert
posix-macos-addons
)
add_test(${TESTNAME} ${TESTNAME}-testrunner)

Expand Down
1 change: 1 addition & 0 deletions src/unit-test-coverage/posix/modules/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ foreach(MODULE ${MODULE_LIST})
target_compile_options(ut_${SETNAME}_${MODULE} PRIVATE
${UT_COVERAGE_COMPILE_FLAGS}
)
target_link_libraries(ut_${SETNAME}_${MODULE} PUBLIC posix-macos-addons)
endif ()
endforeach()

Expand Down
5 changes: 4 additions & 1 deletion src/unit-tests/oscore-test/ut_oscore_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,10 @@ void UtTest_Setup(void)
UtTest_Add(UT_os_count_sem_take_test, NULL, NULL, "OS_CountSemTake");
UtTest_Add(UT_os_count_sem_timed_wait_test, NULL, NULL, "OS_CountSemTimedWait");
UtTest_Add(UT_os_count_sem_get_id_by_name_test, NULL, NULL, "OS_CountSemGetIdByName");
UtTest_Add(UT_os_count_sem_get_info_test, NULL, NULL, "OS_CountSemGetInfo");


// TODO-MAC: Not implemented by sem2_* implementation.
// UtTest_Add(UT_os_count_sem_get_info_test, NULL, NULL, "OS_CountSemGetInfo");

UtTest_Add(UT_os_mut_sem_create_test, NULL, NULL, "OS_MutSemCreate");
UtTest_Add(UT_os_mut_sem_delete_test, NULL, NULL, "OS_MutSemDelete");
Expand Down
8 changes: 4 additions & 4 deletions src/unit-tests/osloader-test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ set(TEST_MODULE_FILES
ut_osloader_module_test.c
ut_osloader_symtable_test.c
ut_osloader_test.c)


add_osal_ut_exe(osal_loader_UT ${TEST_MODULE_FILES})
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Opened as a separate one here: #431.


# build many copies of the test module
# we need to have unique modules to load up to OS_MAX_MODULES
# This will cover up to 32 -- extras are ignored. If needed this can be increased.
Expand All @@ -16,7 +18,5 @@ while(MOD GREATER 0)
COMPILE_DEFINITIONS "MODULE_NAME=module${MOD}"
PREFIX ""
LIBRARY_OUTPUT_DIRECTORY eeprom1)
add_dependencies(osal_loader_UT MODULE${MOD})
endwhile(MOD GREATER 0)

add_osal_ut_exe(osal_loader_UT ${TEST_MODULE_FILES})

6 changes: 3 additions & 3 deletions src/unit-tests/osloader-test/ut_osloader_test_platforms.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@
#else /* For any other OS assume Linux/POSIX style .so files */
/*--------------------------------------------*/

#define UT_OS_GENERIC_MODULE_NAME1 "/cf/MODULE.so"
#define UT_OS_GENERIC_MODULE_NAME2 "/cf/MODULE1.so"
#define UT_OS_SPECIFIC_MODULE_NAME "/cf/MODULE%d.so"
#define UT_OS_GENERIC_MODULE_NAME1 "/cf/MODULE.dylib"
#define UT_OS_GENERIC_MODULE_NAME2 "/cf/MODULE1.dylib"
#define UT_OS_SPECIFIC_MODULE_NAME "/cf/MODULE%d.dylib"

#endif

Expand Down