Static library can't allocate memory
Posted: Mon Jan 27, 2020 9:17 am
I exported a block of Matlab code into C to run on my NANO54415. I tested the code as a stand alone application and it functioned correctly without errors. The exported code was bigger than my application so I decided to make a static library out of it. When my larger application called the new library the Netburner faulted and rebooted. I trimmed down my code until I realized that accessing variables caused the seg fault. I created the following code that reproduces the error 100% of the time on my system. In older systems I would have checked linker commands to verify there was memory allocated to the library, but those quaint old flags appear to have gone away.
The static library:
libTest.h:
libTest.cpp:
The calling application:
The static library:
libTest.h:
Code: Select all
#ifndef _LIB_TEST_H_
#define _LIB_TEST_H_
extern "C" void libTest(void);
#endif /* _LIB_TEST_H_ */
Code: Select all
#include <stdio.h>
#include "libTest.h"
uint32_t state[10];
void libTest(void)
{
iprintf("library test entry\n");
state[0] = 5489;
iprintf("library test exit\n");
}
The calling application:
Code: Select all
#include <predef.h>
#include <stdio.h>
#include <ctype.h>
#include <startnet.h>
#include <autoupdate.h>
#include <smarttrap.h>
#include <taskmon.h>
#include <NetworkDebug.h>
#include <string.h>
#include <serial.h>
#include <command.h>
#include "libTest.h"
extern "C" {
void UserMain(void * pd);
}
#define SERIALPORT_TO_USE (0) //0 for the main port, 1 for the 10pin aux serial port
#define BAUDRATE_TO_USE (115200)
#define STOP_BITS (1)
#define DATA_BITS (8)
const char * AppName="StatLibDriver";
void * ProcessConnect( FILE *fp )
{
const char *prompt;
fiprintf( fp, "\nFront end Command Program\n");
if ( ( int ) ( fp->_file ) == ( SERIAL_SOCKET_OFFSET ) )
{
prompt = "Serial0";
}
else if ( ( int ) ( fp->_file ) == ( SERIAL_SOCKET_OFFSET + 1 ) )
{
prompt = "Serial1";
}
else
{
prompt = "Telnet";
}
/* To test arbitray data tracking return the session number */
return ( void * ) prompt;
}
void ProcessPrompt( FILE *fp, void *pData )
{
/* For fun give each session its nmumber in the prompt */
fiprintf( fp, "\nTest:%s>", (char *)pData );
}
/* The command processing Callback */
int ProcessCommand( const char *command, FILE *fp, void *pData )
{
iprintf("Calling libTest\n");
libTest();
return CMD_OK;
}
/* The User Authentication Callback */
int ProcessAuth( const char *name, const char *pass )
{
/* For testing reject the set if they are the same */
if ( strcmp( name, pass ) == 0 )
{
return CMD_FAIL;
}
else
{
return CMD_OK;
}
}
void UserMain(void * pd) {
InitializeStack();
OSChangePrio(MAIN_PRIO);
EnableAutoUpdate();
StartHTTP();
EnableTaskMonitor();
#ifndef _DEBUG
EnableSmartTraps();
#endif
#ifdef _DEBUG
InitializeNetworkGDB_and_Wait();
#endif
// Setup console and ftp access
// Close the serial port in case it is already open.
SerialClose( SERIALPORT_TO_USE );
//Open the serial port
int fdserial = OpenSerial( SERIALPORT_TO_USE,
BAUDRATE_TO_USE,
STOP_BITS,
DATA_BITS,
eParityNone );
ReplaceStdio( 0, fdserial );
ReplaceStdio( 1, fdserial );
ReplaceStdio( 2, fdserial );
CmdAuthenticateFunc = ProcessAuth; /* No authentication to start */
CmdCmd_func = ProcessCommand;
CmdConnect_func = ProcessConnect;
CmdPrompt_func = ProcessPrompt;
CmdIdleTimeout = TICKS_PER_SECOND * 60;
if(CmdStartCommandProcessor( MAIN_PRIO - 4) != CMD_OK)
iprintf("CmdStartCommandProcessor failed\n");
CmdAddCommandFd( fdserial, FALSE, FALSE );
iprintf("Lib test application started\n");
while (1) {
OSTimeDly(TICKS_PER_SECOND);
}
}