Separate data and function address spaces

Conor P. Cahill cpcahil at virtech.uucp
Fri Nov 10 23:30:33 AEST 1989


In article <2559F3AE.9260 at ateng.com>, chip at ateng.com (Chip Salzenberg) writes:
> Recent processors also have this "feature".  When the '286 and '386
> processors are in protected mode -- i.e. when they're running Unix
> -- they do not permit program execution from any data segment. This
> restriction can be bypassed only by the subterfuge of pointing two
> segment descriptors at the same piece of memory. 

I don't know what unix you are using, but the System V/386 Unixs use the small
model for compiled programs which place the data and text portion into the 
same segment.  I have executed out of data space on these systems.  I have
even executed out of a shared memory segment.

> >	char *p;
> >	int fn();
> >	p = (char *)fn;

while this is non-portable, it can be done on the unixs I spoke about above. 
Try the following on your 386 system:

#include	<stdio.h>

main()
 {
	int		   a();
	int		   b();
	int		   errno;
	int		(* func )();
	void		 * malloc();
	char		 * shmaddr;
	char		 * test;

	if( (shmaddr=(char *)malloc(512)) == 0 )
	{
		printf("malloc failed, errno = %d\n", errno);
		exit(10);
	}
	cpy(shmaddr,a,b);

	func = (int (*)()) shmaddr;

	test = "If the word 'shared' appears here: ......  it works.";

	(* func)(test);

	printf("%s\n",test);

	exit(0);
}	

cpy(tgt,src,srcend)
 char * tgt;
 char * src;
 char * srcend;
 {
  while ( src != srcend )
    *tgt++ = *src++;
 }

a( s )
 char *s;
 {
  s[35] = 'S'; s[36] = 'H'; s[37] = 'A'; s[38] = 'R'; s[39] = 'E'; s[40] = 'D'; 
  return;
 }

b( s )
 char *s;
 {
  s[35] = 'N'; s[36] = 'O'; s[37] = 'R'; s[38] = 'M'; s[39] = 'A'; s[40] = 'L'; 
  return;
 }

-- 
+-----------------------------------------------------------------------+
| Conor P. Cahill     uunet!virtech!cpcahil      	703-430-9247	!
| Virtual Technologies Inc.,    P. O. Box 876,   Sterling, VA 22170     |
+-----------------------------------------------------------------------+



More information about the Comp.lang.c mailing list