untagged pointer to tagged array

Post Reply
luowy
Posts: 234
Joined: Mon Oct 20, 2014 12:52 pm

untagged pointer to tagged array

Post by luowy »

untagged pointer to tagged array give a wrong result.

Code: Select all

StdCoder.Decode ..,, ..iP....3Qw7uP5PRPPNR9Rbf9b8R79FTvMf1GomCrlAy2xhX,Cb2x
 hXhC6FU1xhiZiVBhihgmRiioedhgrZcZRiXFfaqmSrtuGfa4700zdGrr8rmCLLCJuyKtYcZRiX
 7.2.s,sQB.0k,5TWyql.bnayKmKKqGomC5XzET1.PuP.MHT9N9ntumaU2,CJuyKtQC98P9PP7O
 NbXmb.2.As3k2kQL.,6.,U08J99SdfJHPNjvQCJuGKfaqmY6MwdONl1QCh0708T,U..w.Qg3.,
 sUGpmWbBxhYhAbndMHT9NY6Mw.sQq2Y6cwB.0.Pv.w,gg1E.0E.kY4.86.QC18RdfQHfMf9R9v
 Q7ONb17.,.D,0.p.0.4.0EJYjyC.6.VQ.kbk.8Mtr.2.02,.e,2.AU.Ue.E.mP,UK.D,i00kAk
 .O.o,IU0KyBU.2.S22.e,0E.m80.e0.,6jU02.4k.UlU,w,soE.EqHM,YqVI.3Qw7ONhvETPPP
 PMR9N9fQbf9b8RO3U.Ay2hgq,.RdJ.0EtD.2..c,6.,k8k9kzrobGoemoW0mb8KwKJrGrkSqnK
 Km0praKrGrm8rRqk4akYqIcyIdGJECJEenS0mdapdGpWqIK0Gayqn0GRq1d9NNuPDvCPM1H6Jn
 8I9O1HM0ROMPPN,ND,NEZeI1OK,dAfdB,tHB8658G1eIrN1HM0V8RZfH1PP91VuHHeHdOFZuKf
 fPdPMki0GeyIEuokqqmin4ak2qk2akeuKb4qq2242Qfppho,66,dAfdB..VWJphCBAEc.0GeyI
 EKJruoksCqk20JdyoVKIWKJdKIEGoreGEWmIin4aUKBcG3YBAV7AViJbUodVhBA,H6QRfC,7Id
 fQR0UBk2KLR0meuKb.6QffC,7Idne.UX3hu2Y1372YdJbUAdCZe3xc3Jevg,3OFDOGRO1HM0PU
 pND37O99PNvP3tCPM0H6QgbHpZKBcA3ZEZim,YZHpZ,ZcG3ZpBZdQbBA,NuPDf9b8RZPORvNF7
 QfP8r7HTvNR7HRvCPM0q.RnS8Go.EsuaxQei.cH.EramIin46HsI.uqIiHay4N0KIbGIEGorin
 4ak2q.PMFR8F8kb8KwK3Em03ELGorqk48ssHpmsETfPdfQT9PNPNZvQRtIGqVGLtmKWKqtCK.4
 Te..c95uPR9R.7ONbvM,kVkk.Um,..Unp3.6F6.ZD,6.636.M00U.2..AU0CyIVGhighgmRiiQ
 88pum470,Mwd0UnpZGhighA70,cw5.,6.QJw.QI2U.sU.ktumdsIdPSNPN7ONbH.4D.o3aLq.,
 cwD.0.E2Eh6.0.32.oZ,ZC.G20U2U...G00k.0.0.0mFf32Uwpr,6C5XsQA4.2.8Mtr.2..c4E
 .k.Ue.0.,6Y1.0.UA2Tm.mmBjZ92T,eUXDFTXhhAsET1.UG.,..W.0.A,,U.Rfn7c9tc0MyfU.
 az86kx8O0mIW79b1...
 --- end of encoding ---
do you think it's bug or not?
User avatar
Josef Templ
Posts: 2047
Joined: Tue Sep 17, 2013 6:50 am

Re: untagged pointer to tagged array

Post by Josef Templ »

This is a programming mistake and the result is basically random or what happens to be on the stack
on a particular location from previous calls.
It assumes a particular memory layout and this assumption is wrong.
If you use SYSTEM to convert between tagged and untagged pointers/arrays you have to know
about the memory layout used by the runtime system (GC, loader, compiler).

- Josef
luowy
Posts: 234
Joined: Mon Oct 20, 2014 12:52 pm

Re: untagged pointer to tagged array

Post by luowy »

Josef Templ wrote:This is a programming mistake and the result is basically random or what happens to be on the stack
on a particular location from previous calls.
It assumes a particular memory layout and this assumption is wrong.
If you use SYSTEM to convert between tagged and untagged pointers/arrays you have to know
about the memory layout used by the runtime system (GC, loader, compiler).

- Josef
yes, I think I know the runtime very well, but I cant used this type pointer correctly,it is a true problem;
until now, no one can use it correctly, no any piece of such code existed in the whole framework and any components,
a more simple example:

Code: Select all

MODULE ObxSimple;

	IMPORT S := SYSTEM;
	
	(*  ObxSimple.Do *)
	PROCEDURE Do*();
		TYPE 
			A = ARRAY[untagged] 256 OF CHAR;
			P = POINTER[untagged] TO A;
		VAR a: A; p: P; ch: CHAR;
	BEGIN
		a := "hello";
		p := S.VAL(P, S.ADR(a[0]));
		ch := p[0]; (* ch="h" *)
		HALT(100);
	END Do;
	
	(*  ObxSimple.Do2 *)
	PROCEDURE Do2*();
		TYPE 
			A = ARRAY 256 OF CHAR;
			P = POINTER[untagged] TO A;
		VAR a: A; p: P; ch: CHAR;
	BEGIN
		a := "hello";
		p := S.VAL(P, S.ADR(a[0]));
		ch := p[0]; (* what do you think it is? *)
		HALT(100);
	END Do2;
	

END ObxSimple.
we know, untagged array has a same memory map as tagged array on the stack;

luowy
User avatar
Josef Templ
Posts: 2047
Joined: Tue Sep 17, 2013 6:50 am

Re: untagged pointer to tagged array

Post by Josef Templ »

Luowy, please note that a normal pointer to a normal array does not point to the first array element.
In other words, the assumption that S.ADR(S.VAL(INTEGER, pn)) = S.ADR(pn^) is wrong.
There is some meta information packed into the array memory block and in your example
the first array element is 12 bytes larger than the pointer value.
Since Name is a tagged array the compiler must assume that a pointer pointing to it
points to something that conforms with the memory layout conventions of the runtime system.
The array allocated on the stack does not have the required memory layout.
The difference between a tagged and an untagged pointer is not relevant here,
what counts is if the pointer base type is tagged or not.

So what is inside the 12 header bytes? It is the array descriptor of the array block
describing the layout and, if open arrays are involved, the len-vector of the array block.
It also contains an iterator for the GC to iterate over all array elements.
See Kernel.Block.

If you increase "hello" to a longer string, you will see the effect.
e.g. n:="0123456789012345678901234567890123456789";
Only the first 6 characters (=12 bytes) will be skipped.

In Kernel.NewArr you can study the memory layout of array objects.
The header size is

Code: Select all

		headSize := 4 * nofdim + 12;
which is 12 in our example because there are no dynamic (open array) dimensions.

- Josef
User avatar
Robert
Posts: 1024
Joined: Sat Sep 28, 2013 11:04 am
Location: Edinburgh, Scotland

Re: untagged pointer to tagged array

Post by Robert »

I am moving this topic to Rejected. Please advise me if this is incorrect.
Post Reply