This is the mail archive of the ecos-discuss@sources.redhat.com mailing list for the eCos project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Problems allocating memory in main()


Hello,

probably it's a typical newbie-problem, but it's confusing anyway.
The following test-program instantiates a thread-object in main and
allocates a stack for this thread as well. 
main() allocates a stack-array (lStack1) for another, not yet
instantiated thread, also. If lStack1-allocation is commented out, then
the program works as expected.
The actual platform is i386PC made with the eCos-i386PC-template.

#include <pkgconf/kernel.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <cyg/kernel/thread.hxx>
#include <cyg/kernel/thread.inl>
#include <cyg/kernel/kapi.h>

//
//
//
void work ( cyg_addrword_t aDataAddress) {
  int lSize1 = 16150; 
  int lSize2 = 1;
  char* lMem1;
  char* lMem2;
  char* lName;
  
  lName = (char*) aDataAddress;
  
  printf ( "%s starts work ...\n", lName);
  lMem1 = new char [lSize1];
  printf ( "%s allocated %i chars ...\n", lName, lSize1);
  lMem2 = new char [lSize2];
  printf ( "%s allocated %i chars ...\n", lName, lSize2);

  Cyg_Thread::self()->exit ();  
}


/*
#ifdef __cplusplus
extern "C" {
#endif
*/
//void cyg_user_start ( void) {
int main () {
  Cyg_Thread* lThread0; 
  Cyg_Thread* lThread1;
  char* lPing = "ping";
  char* lPong = "pong";
  char lStack0 [4096];
  char lStack1 [4096]; // uncomment this line and it works
  
  lThread0 = new Cyg_Thread ( CYG_SCHED_DEFAULT_INFO,
	                      work,
	                      (cyg_addrword_t) lPing,
                              lPing,
                              (cyg_addrword_t) lStack0,
                              4096);
  printf ( "thread0 created ...\n");
  /*
  lThread1 = new Cyg_Thread ( CYG_SCHED_DEFAULT_INFO,
	                      work,
                              (cyg_addrword_t) lPong,
                              lPong,
                              (cyg_addrword_t) lStack1,
                              4096);
  printf ( "thread1 created ...\n");
  */
  //lThread->set_priority ( 2);
  lThread0->resume ();
  printf ( "thread0 resumed ...\n");
}
/*
#ifdef __cplusplus
}
#endif
*/

The results of this program are unpredictable as shown below. The
program binary is always the same and the platform has been hard-reset
before. There are three effects: 1.) the program stops, 2.) the program
catches SIGILL and 3.) the program catches SIGSEGV.

0x37dc in ?? ()
Loading section .text, size 0x10ef4 lma 0x108000
Loading section .rodata, size 0x680 lma 0x118ef8
Loading section .gcc_except_table, size 0xf8 lma 0x119578
Loading section .data, size 0x1230 lma 0x119670
Start address 0x108000 , load size 75932
Transfer rate: 18983 bits/sec, 74 bytes/write.
thread0 created ...
thread0 resumed ...
[New thread 2]

Program received signal SIGILL, Illegal instruction.
[Switching to thread 2]
0x46454443 in ?? ()
0x37dc in ?? ()
Loading section .text, size 0x10ef4 lma 0x108000
Loading section .rodata, size 0x680 lma 0x118ef8
Loading section .gcc_except_table, size 0xf8 lma 0x119578
Loading section .data, size 0x1230 lma 0x119670
Start address 0x108000 , load size 75932
Transfer rate: 19595 bits/sec, 74 bytes/write.
thread0 created ...
thread0 resumed ...
0x37dc in ?? ()
Loading section .text, size 0x10ef4 lma 0x108000
Loading section .rodata, size 0x680 lma 0x118ef8
Loading section .gcc_except_table, size 0xf8 lma 0x119578
Loading section .data, size 0x1230 lma 0x119670
Start address 0x108000 , load size 75932
Transfer rate: 18983 bits/sec, 74 bytes/write.
thread0 created ...
thread0 resumed ...
0x37dc in ?? ()
Loading section .text, size 0x10ef4 lma 0x108000
Loading section .rodata, size 0x680 lma 0x118ef8
Loading section .gcc_except_table, size 0xf8 lma 0x119578
Loading section .data, size 0x1230 lma 0x119670
Start address 0x108000 , load size 75932
Transfer rate: 19595 bits/sec, 74 bytes/write.
thread0 created ...
thread0 resumed ...
0x37dc in ?? ()
Loading section .text, size 0x10ef4 lma 0x108000
Loading section .rodata, size 0x680 lma 0x118ef8
Loading section .gcc_except_table, size 0xf8 lma 0x119578
Loading section .data, size 0x1230 lma 0x119670
Start address 0x108000 , load size 75932
Transfer rate: 18983 bits/sec, 74 bytes/write.
thread0 created ...
thread0 resumed ...
[New thread 2]

Program received signal SIGILL, Illegal instruction.
[Switching to thread 2]
0x46454443 in ?? ()
0x37dc in ?? ()
Loading section .text, size 0x10ef4 lma 0x108000
Loading section .rodata, size 0x680 lma 0x118ef8
Loading section .gcc_except_table, size 0xf8 lma 0x119578
Loading section .data, size 0x1230 lma 0x119670
Start address 0x108000 , load size 75932
Transfer rate: 18407 bits/sec, 74 bytes/write.
thread0 created ...
ping starts work ...
ping allocated 16150 chars ...
ping allocated 1 chars ...
thread0 resumed ...
[New thread 2]

Program received signal SIGSEGV, Segmentation fault.
[Switching to thread 2]
0x1115e1 in hal_thread_load_context () at
/opt/ecos/ecos-1.3.1/packages/kernel/v1_3_1/src/sync/mbox.cxx:156

Thanks for any help.

-- 
--------------------------------------------------
Andreas Bürgel   Software Engineer
ab@genologic.de  GenoLogic GmbH, Dortmund, Germany

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]