Assertion `offset < 0x10000lu' failed

Using, learning, programming and modding the Gigatron and anything related.
Forum rules
Be nice. No drama.
jurkstas
Posts: 20
Joined: 05 Aug 2019, 17:06

Assertion `offset < 0x10000lu' failed

Post by jurkstas »

Hi,

I am trying to compile something and I get this output:

Code: Select all

rcc: ./build/gt1.c:4436: local: Assertion `offset < 0x10000lu' failed.
Utils/lcc/build/lcc: fatal error in Utils/lcc/build/rcc
Makefile:266: recipe for target '../nanotode/test/connectome.o' failed
make: *** [../nanotode/test/connectome.o] Error 1
snippet from gt1.c:

Code: Select all

static void local(Symbol p) {
	if (isfloat(p->type)) {
		p->sclass = AUTO;
	}
	if (askregvar(p, (*IR->x.rmap)(ttob(p->type))) == 0) {
		assert(p->sclass == AUTO);
		p->x.offset = offset;
		debug(fprint(stderr, "local %s of size %d @ offset %d\n", p->name, p->type->size, offset));
		p->x.name = stringd(offset);
		offset += p->type->size;
		assert(offset < 0x10000lu); // Catch only big overruns. Default stack is much smaller.
	}
}
How could I approach debugging this issue? Is there a way to enable output of the debug()?
User avatar
marcelk
Posts: 488
Joined: 13 May 2018, 08:26

Re: Assertion `offset < 0x10000lu' failed

Post by marcelk »

It means there are local variables or function arguments that need 64K or more. In the default setup, the C stack has space for 512 bytes (starting at 0x700 and growing down).
User avatar
marcelk
Posts: 488
Joined: 13 May 2018, 08:26

Re: Assertion `offset < 0x10000lu' failed

Post by marcelk »

I almost forgot that when you get this message, it's actually more likely there's the runaway register allocation at play:

Main GitHub issue:
#76 lcc: compiler hangs on complex expression

I replaced the resulting crashing/hanging with this assert, just to make it easier to detect:
https://github.com/kervinck/gigatron-ro ... e471f96214

Related GitHub issues, but probably it's all the same thing:
#73 lcc: compiler crash
#77 lcc: compiler hangs on simple statement

In the library we worked around this by using only simple code (until we find a proper solution).
jurkstas
Posts: 20
Joined: 05 Aug 2019, 17:06

Re: Assertion `offset < 0x10000lu' failed

Post by jurkstas »

Thanks. I modified the local() to print what is in the debug() call and this is what I got. Looks like a runaway as you said:

Code: Select all

[..]
local 32761 of size 2 @ offset 65518
local 32762 of size 2 @ offset 65520
local 32763 of size 2 @ offset 65522
local 32764 of size 2 @ offset 65524
local 32765 of size 2 @ offset 65526
local 32766 of size 2 @ offset 65528
local 32767 of size 2 @ offset 65530
local 32768 of size 2 @ offset 65532
local 32769 of size 2 @ offset 65534
rcc: ./build/gt1.c:4437: local: Assertion `offset < 0x10000lu' failed.
Utils/lcc/build/lcc: fatal error in Utils/lcc/build/rcc
Makefile:266: recipe for target '../nanotode/test/connectome.o' failed
make: *** [../nanotode/test/connectome.o] Error 1
Is there a way to identify what is causing it in the source I am trying to compile? I might be able to rework it to stop this.
User avatar
marcelk
Posts: 488
Joined: 13 May 2018, 08:26

Re: Assertion `offset < 0x10000lu' failed

Post by marcelk »

jurkstas wrote: 10 Aug 2019, 17:19 Is there a way to identify what is causing it in the source I am trying to compile? I might be able to rework it to stop this.
I isolated 'my' troublesome expression (it comes from mscp.c) by trial and error: using #if 0...#endif to disable large chunks of the C program until the error disappears or changes. The way I understand it, any nested expression with * / << or >> can potentially trigger this.

In new programs it's easy to workaround it. I still have hopes we can find a fix once we can put our mind to it. It will be a great boost for the memory card subproject. (But we always have cc65 as a backup for that.)
jurkstas
Posts: 20
Joined: 05 Aug 2019, 17:06

Re: Assertion `offset < 0x10000lu' failed

Post by jurkstas »

Played with #if 0 and found a line that causes it. int16_t is defined as int somewhere else.

Code: Select all

static void ctm_set_next_state(Connectome* const c, const uint16_t id, const int16_t val) {
  if(id < c->_neurons_tot) {
    if(val > 127) {
      c->_neuron_next[id] = 127;
    }
//    else if(val < -128) { //this here causes runaway and the one below does not
    else if(val + 128 < 0) {
      c->_neuron_next[id] = -128;
    }
    else {
      c->_neuron_next[id] = val;
    }
  }
  else {
    c->_muscle_next[id - c->_neurons_tot] = val;
  }
}
jurkstas
Posts: 20
Joined: 05 Aug 2019, 17:06

Re: Assertion `offset < 0x10000lu' failed

Post by jurkstas »

That being resolved, should I move to another topic or just rename this topic to something like "compiling nanotode library" and continue with other troubles?
Now I get this:

Code: Select all

Utils/lcc/build/lcc -ILibs Libs/test/main.o Libs/string/strcpy.o Libs/string/memset.o Libs/stdio/fflush.o Libs/stdio/getchar.o Libs/stdio/printf.o Libs/stdio/vprintf.o Libs/stdio/sprintf.o Libs/stdio/fscanf.o Libs/stdio/fclose.o Libs/stdio/fprintf.o Libs/stdio/vfprintf.o Libs/stdio/stdout.o Libs/stdio/ungetc.o Libs/stdio/vfscanf.o Libs/stdio/puts.o Libs/stdio/fopen.o Libs/stdio/scanf.o Libs/stdio/fputs.o Libs/stdio/snprintf.o Libs/stdio/stdin.o Libs/stdio/fgetc.o Libs/stdio/putchar.o Libs/stdio/vsprintf.o Libs/stdio/fputc.o Libs/stdio/vsnprintf.o Libs/errno/errno.o Libs/test/neural_rom.o Libs/test/muscles.o Libs/test/connectome.o Libs/ctype/isspace.o Libs/ctype/isdigit.o Libs/Gigatron/BusyWait.o Libs/Gigatron/WaitKey.o Libs/Gigatron/Random.o Libs/Gigatron/Newline.o Libs/Gigatron/ClearScreen.o Libs/Gigatron/PutChar.o -o "Libs/test/main.gt1"
Warning: unsafe thunk CALL in @blkcopy
Warning: unsafe RET in @blkcopy
Traceback (most recent call last):
  File "Utils/lcc/build/gtlink.py", line 27, in <module>
    sys.exit(main(sys.argv[1:]))
  File "Utils/lcc/build/gtlink.py", line 24, in main
    asm.link('@start', outf, logf)
  File "/home/user/gigatron-rom/Utils/lcc/build/asm.py", line 521, in link
    segment = segments[sidx]
IndexError: list index out of range
Makefile:273: recipe for target 'Libs/test/main.gt1' failed
make: *** [Libs/test/main.gt1] Error 1
sidx - 124
segment - <asm.Segment object at 0x7f150b781be0>
User avatar
marcelk
Posts: 488
Joined: 13 May 2018, 08:26

Re: Assertion `offset < 0x10000lu' failed

Post by marcelk »

Could it be that the compiled code doesn't fit in the memory available? There is a proposal to support 64K systems here:

#66 lcc: malloc, 64K support and memory control
jurkstas
Posts: 20
Joined: 05 Aug 2019, 17:06

Re: Assertion `offset < 0x10000lu' failed

Post by jurkstas »

marcelk wrote: 10 Aug 2019, 21:42 Could it be that the compiled code doesn't fit in the memory available?
Might be. Is there a way to check that? Is there a way to get something like this:

Code: Select all

Application uses 656 bytes (0%) of program storage space. Maximum is 253952 bytes.
Global variables use 9 bytes (0%) of dynamic memory, leaving 8183 bytes for local variables. Maximum is 8192 bytes.
User avatar
marcelk
Posts: 488
Joined: 13 May 2018, 08:26

Re: Assertion `offset < 0x10000lu' failed

Post by marcelk »

I just judge by glancing over the source code. When you're writing new code, it becomes evident along the way because you have the intermediate .gt1 files. You can dump these with Utils/gt1dump.py to see where you are. The author sure didn't add a lot of user messages, and when it crashes, it leaves nothing behind. Well, he did call it "rudimentary" for a reason.

656+9 bytes isn't too much by itself, but under 32K everything must be distributed over 96-byte segments as well. With code the compiler fixes that for you. With data it can't do that. I couldn't compile mscp.c yet for that reason: it needs the large unsegmented chunk above 32K.
Post Reply