I’m working on running the Core Flight System with RTEMS6 on a Beaglebone Black using this repository rtems-cfs-demo for a personal project.
I’ve followed all the steps provided, and everything worked perfectly until I made some additional changes. I tried add a simple library for the run-time loader from this repository sample-lib.
PROBLEM
I encounter an error that I called “string shifting”, because I have never met this before. Some strings from other places get passed unreasonably into dev_mutex.c
, where I made a system call OS_MutSemCreate
(this is equal to rtems_semaphore_create
or pthread_mutex_init
). The error could happen to any function that received the string literal directly.
For example:
strncpy(SAMPLE_Buffer, "SaMpLe", sizeof(SAMPLE_Buffer))
#define SAMPLE_LIB_VERSION_STRING "Sample Lib DEVELOPMENT BUILD "
OS_printf("SAMPLE Lib Initialized.%sn", SAMPLE_LIB_VERSION_STRING);
Here is the console result when running the program on the board.
// fsw/mutex/dev_mutex.c
OS_MutSemCreate(0 - SaM): 0 /* The corrected string should be AAA */
OS_MutSemCreate(1 - Sam): 0 /* The corrected string should be AAB */
OS_MutSemCreate(2 - SAM): 0 /* The corrected string should be AAC */
OS_MutSemCreate(3 - AAD): 0
OS_MutSemCreate(4 - AAE): 0
WORKAROUND SOLUTION
After that I made some modifications for library above and it works properly. The modifications include avoiding using the string literal macro directly, but instead declare a pointer to point to these literals.
fsw/mutex/dev_mutex.c
const char *dev_strs[] = {"AAA", "AAB", "AAC", "AAD", "AAE", "AAF", "AAG", "AAH", "AAI", "AAJ", "AAK",
"AAL", "AAM", "AAN", "AAO", "AAP", "AAQ", "AAR", "AAS", "AAT", "AAU", "AAV",
"AAW", "AAX", "AAY", "AAZ", "ABA", "ABB", "ABC", "ABD", "ABE"};
static void getDeviceString(DeviceId id, char *deviceID)
{
if (id >= DEV_0 && id <= DEV_30)
{
// In the sample_lib library, I pass the string literal macro directly
CFE_PSP_MemCpy(deviceID, dev_strs[id], strlen(dev_strs[id])); // Abstraction of memcpy
deviceID[strlen(dev_strs[id])] = '';
}
else
{
deviceID[0] = '';
}
}
int32 mutexInitialize(MutexHandler_t *hwMutex, DeviceId ID)
{
int32 Status = OS_SUCCESS;
char mutex_name[OS_MAX_API_NAME];
char deviceID[OS_MAX_API_NAME];
CFE_PSP_MemSet(deviceID, 0, OS_MAX_API_NAME); // Abstraction of memset
getDeviceString(ID, deviceID);
if (deviceID == NULL)
{
return OS_ERROR;
}
CFE_PSP_MemSet(mutex_name, 0, OS_MAX_API_NAME); // Abstraction of memset
CFE_PSP_MemCpy(mutex_name, deviceID, strlen(deviceID)); // Abstraction of memcpy
mutex_name[strlen(deviceID)] = '';
Status = OS_MutSemCreate(&hwMutex->mutex, mutex_name, 0);
printf("OS_MutSemCreate(%p - %s): %dn", (void *)mutex_name, mutex_name, Status);
return Status;
}
QUESTION
Has anyone encountered a similar issue before? Any suggestions on where I should start investigating? Thank you in advance!