Welcome, Guest. Please login or register.

Author Topic: CreateNewProc bug in SASC ?  (Read 5586 times)

Description:

0 Members and 1 Guest are viewing this topic.

Offline JoseTopic starter

  • Hero Member
  • *****
  • Join Date: Feb 2002
  • Posts: 2871
    • Show only replies by Jose
CreateNewProc bug in SASC ?
« on: August 19, 2007, 12:56:26 PM »
Hi. I've shrinked a bug to the tiny piece of code bellow. Works well when compiled with VBCC but crashes on my bare A1200 with SASC ?!
Code: [Select]

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <utility/tagitem.h>
#include <exec/ports.h>
#include <dos/dos.h>
#include <dos/dostags.h>

/* Protos */
#include <clib/exec_protos.h>
#include <clib/dos_protos.h>
#include <clib/alib_protos.h>

void CllFnc(void);

int main (int argc, char **argv)
{ struct Process *Proc;
  struct TagItem PrcTgs[5];
  PrcTgs[0].ti_Tag = NP_Entry;
  PrcTgs[0].ti_Data = (ULONG)CllFnc;
  PrcTgs[1].ti_Tag = NP_Name;
  PrcTgs[1].ti_Data = (ULONG)&quot;TestProcessName&quot;;
  PrcTgs[2].ti_Tag = NP_Output;
  PrcTgs[2].ti_Data = Output();
  PrcTgs[3].ti_Tag = NP_CloseOutput;
  PrcTgs[3].ti_Data = FALSE;
  PrcTgs[4].ti_Tag = TAG_DONE;
 
  if (!(Proc = CreateNewProc(PrcTgs)))
    printf(&quot;Thread creation error!!!\n&quot;);
  else
    printf(&quot;Thread creation successfull\n&quot;);

Delay(200); /* Wait 4 seconds. Normally a WaitPort or Wait should be used, */
exit(0); /* but this is just a shrinked piece of code to reproduce the bug */
}


/* Function caller */
void CllFnc(void)
{ Forbid();
  printf(&quot;Test text working!!!\n&quot;);
}

\\"We made Amiga, they {bleep}ed it up\\"
 

Offline Piru

  • \' union select name,pwd--
  • Hero Member
  • *****
  • Join Date: Aug 2002
  • Posts: 6946
    • Show only replies by Piru
    • http://www.iki.fi/sintonen/
Re: CreateNewProc bug in SASC ?
« Reply #1 on: August 19, 2007, 12:59:08 PM »
Your bug: stdio is not thread safe.
 

Offline ChaosLord

  • Hero Member
  • *****
  • Join Date: Nov 2003
  • Posts: 2608
    • Show only replies by ChaosLord
    • http://totalchaoseng.dbv.pl/news.php
Re: CreateNewProc bug in SASC ?
« Reply #2 on: August 19, 2007, 01:05:14 PM »
You can't do a printf() inside of a Forbid();

and where is the Permit()?
Wanna try a wonderfull strategy game with lots of handdrawn anims,
Magic Spells and Monsters, Incredible playability and lastability,
English speech, etc. Total Chaos AGA
 

Offline Piru

  • \' union select name,pwd--
  • Hero Member
  • *****
  • Join Date: Aug 2002
  • Posts: 6946
    • Show only replies by Piru
    • http://www.iki.fi/sintonen/
Re: CreateNewProc bug in SASC ?
« Reply #3 on: August 19, 2007, 01:08:04 PM »
@ChaosLord

printf() works inside Forbid() just fine (tho it is bad style IMO). Forbid() is broken when needed.

Permit() is not needed at the end of a subprocess. In fact, it's often useful to avoid the parent being allowed to run before the subprocess has terminated.

The real problem is that stdio is not thread safe.
 

Offline ChaosLord

  • Hero Member
  • *****
  • Join Date: Nov 2003
  • Posts: 2608
    • Show only replies by ChaosLord
    • http://totalchaoseng.dbv.pl/news.php
Re: CreateNewProc bug in SASC ?
« Reply #4 on: August 19, 2007, 01:39:42 PM »
Can a regular Process with no children or parents, do a Forbid() without Permit() near the end?

For example a CLI command?

Wanna try a wonderfull strategy game with lots of handdrawn anims,
Magic Spells and Monsters, Incredible playability and lastability,
English speech, etc. Total Chaos AGA
 

Offline Piru

  • \' union select name,pwd--
  • Hero Member
  • *****
  • Join Date: Aug 2002
  • Posts: 6946
    • Show only replies by Piru
    • http://www.iki.fi/sintonen/
Re: CreateNewProc bug in SASC ?
« Reply #5 on: August 19, 2007, 01:45:08 PM »
@ChaosLord
Quote
Can a regular Process with no children or parents, do a Forbid() without Permit() near the end?

For example a CLI command?

No.

Well it can, but that is a bug (the CLI will then inherit the pending Forbid, and it will cause all sorts of nasty things).
 

Offline Georg

  • Jr. Member
  • **
  • Join Date: Feb 2002
  • Posts: 90
    • Show only replies by Georg
Re: CreateNewProc bug in SASC ?
« Reply #6 on: August 19, 2007, 02:07:42 PM »
Quote
Permit() is not needed at the end of a subprocess. In fact, it's often useful to avoid the parent being allowed to run before the subprocess has terminated.


If you are lucky enough not to be screwed by the process cleanup routines if the they do something which calls Wait() and breaks Forbid() state.

 

Offline itix

  • Hero Member
  • *****
  • Join Date: Oct 2002
  • Posts: 2380
    • Show only replies by itix
Re: CreateNewProc bug in SASC ?
« Reply #7 on: August 19, 2007, 02:25:59 PM »
Quote

If you are lucky enough not to be screwed by the process cleanup routines if the they do something which calls Wait() and breaks Forbid() state.


Since old Workbench program must end with Forbid() + ReplyMsg() pair it is not very likely...
My Amigas: A500, Mac Mini and PowerBook
 

Offline Piru

  • \' union select name,pwd--
  • Hero Member
  • *****
  • Join Date: Aug 2002
  • Posts: 6946
    • Show only replies by Piru
    • http://www.iki.fi/sintonen/
Re: CreateNewProc bug in SASC ?
« Reply #8 on: August 19, 2007, 03:31:09 PM »
@Georg
Quote
If you are lucky enough not to be screwed by the process cleanup routines if the they do something which calls Wait() and breaks Forbid() state.

Screwed how? The execution is inside the OS now, and will not return to the seglist, so the seglist can disappear as soon as the final 'rts' has executed.

The Forbid() is there only to ensure that the final couple of instructions (typically poking some flag or calling ReplyMsg()) can execute without the seglist being unloaded under it.
 

Offline Thomas

Re: CreateNewProc bug in SASC ?
« Reply #9 on: August 19, 2007, 04:49:04 PM »
@Jose:
Doesn't SASC use small data model by default ? Then your CllFnc has to be declared with __saveds, so that the base register (A4) is set correctly. May also be __geta4 or something, depends on the compiler.

Bye,
Thomas

Offline Piru

  • \' union select name,pwd--
  • Hero Member
  • *****
  • Join Date: Aug 2002
  • Posts: 6946
    • Show only replies by Piru
    • http://www.iki.fi/sintonen/
Re: CreateNewProc bug in SASC ?
« Reply #10 on: August 19, 2007, 05:43:50 PM »
@Thomas

Good point. That is the reason for outright crash.

However, even with __saveds it won't be safe... Neither SAS/C stdio, nor dos.library buffered I/O is thread safe.
 

Offline Georg

  • Jr. Member
  • **
  • Join Date: Feb 2002
  • Posts: 90
    • Show only replies by Georg
Re: CreateNewProc bug in SASC ?
« Reply #11 on: August 19, 2007, 07:52:12 PM »
Quote

Piru wrote:

Screwed how? The execution is inside the OS now, and will not return to the seglist, so the seglist can disappear as soon as the final 'rts' has executed.


Ah, yes, you're right. I used to know this things. Will do a self diagnosis and increase my lameness level.


 

Offline ChaosLord

  • Hero Member
  • *****
  • Join Date: Nov 2003
  • Posts: 2608
    • Show only replies by ChaosLord
    • http://totalchaoseng.dbv.pl/news.php
Re: CreateNewProc bug in SASC ?
« Reply #12 on: August 19, 2007, 09:07:12 PM »
Is stdio "Resident safe"?

If my cli command uses only local variables and stdio and someone makes it resident then runs the command multiple times simultaneously will it blow up because of stdio?

Wanna try a wonderfull strategy game with lots of handdrawn anims,
Magic Spells and Monsters, Incredible playability and lastability,
English speech, etc. Total Chaos AGA
 

Offline Piru

  • \' union select name,pwd--
  • Hero Member
  • *****
  • Join Date: Aug 2002
  • Posts: 6946
    • Show only replies by Piru
    • http://www.iki.fi/sintonen/
Re: CreateNewProc bug in SASC ?
« Reply #13 on: August 19, 2007, 10:01:31 PM »
Quote
Is stdio "Resident safe"?

By default it isn't.

How to create a residentable (or pure) application depends on the compiler. For example one SAS/C you link against LIB:cres.o. With gcc you use -resident switch (should work with ixemul at least). I'm not familiar with VBCC.

This applies to c-library stdio. If you use dos.library stuff and keep all variables local then these restrictions do not apply, obviously. That way it's possible to do pure applications even without any startup or stdlib code, for example.
 

Offline JoseTopic starter

  • Hero Member
  • *****
  • Join Date: Feb 2002
  • Posts: 2871
    • Show only replies by Jose
Re: CreateNewProc bug in SASC ?
« Reply #14 on: August 19, 2007, 10:54:54 PM »
It must be what Thomas suggested, I just moved this piece of code from VBCC to SASC to be able to use it's debugger and kept most of the default options.

As for stdio not being thread safe. I've debugged various multithreaded functions in the past and worst result I've had AFAIremember was the text output being mixed up, but no crashes.

Cheers for the replies
\\"We made Amiga, they {bleep}ed it up\\"