delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2001/04/19/22:00:26

Message-ID: <3ADF9680.94240320@jps.net>
From: Dennis Yelle <dennis51 AT jps DOT net>
X-Mailer: Mozilla 4.75 [en] (Win98; U)
X-Accept-Language: en
MIME-Version: 1.0
Newsgroups: comp.os.msdos.djgpp
Subject: Protected mode????
Lines: 81
Date: Thu, 19 Apr 2001 18:53:04 -0700
NNTP-Posting-Host: 216.119.48.98
X-Complaints-To: abuse AT onemain DOT com
X-Trace: nntp1.onemain.com 987731306 216.119.48.98 (Thu, 19 Apr 2001 21:48:26 EDT)
NNTP-Posting-Date: Thu, 19 Apr 2001 21:48:26 EDT
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

I was surprised when I compiled and ran this program:
----------------------------------------
#include <cassert>
#include <iostream>
#include <sys/nearptr.h>

unsigned junk( unsigned address)
{
  return *reinterpret_cast<unsigned*>( address);
}

unsigned peeku( unsigned address)
{
  return *reinterpret_cast<unsigned*>( address);
}

void poke( unsigned address, unsigned data)
{
  *reinterpret_cast<unsigned*>(address) = data;
}

int main()
{
  cout << hex << peeku << '\n';
  cout << hex << poke << '\n';
  unsigned a;
  for( a=1; a<0x10000; a*=4) {
    cout << "at " << hex << a << " I found " << hex << peeku(a) << '\n';
  }
  typedef unsigned (*fp)(unsigned);
  fp p = junk;
  unsigned* up = *reinterpret_cast< unsigned**>(&p);
  cout << "up is: " << hex << up << '\n';
  a = reinterpret_cast<unsigned>(up);
  cout << "at " << hex << a << " I found " << hex << peeku(a) << '\n';
  unsigned data = peeku( a);
  poke( a, data+1);
  cout << "at " << hex << a << " I found " << hex << peeku(a) << '\n';
}
------------------------------------

It produced this output:
----------------------------------
1
1
at 1 I found 3e8b0e7e
at 4 I found fcc0303e
at 10 I found 4ad089cb
at 40 I found c6000197
at 100 I found 8dffffff
at 400 I found 6e697270
at 1000 I found 0
at 4000 I found 1e750000
up is: 0x15c0
at 15c0 I found 8be58955
at 15c0 I found 8be58956
---------------------------------

I was surprised by many things here,
but mostly I was surprised that I could
read AND WRITE at small addresses, and that
those addresses seemed to contain the actual
program that was running.  That is,
DJGPP neither read protects nor write protects
the instructions of the running program.

Has it always been this way?
Is there any way to change this?
I would prefer that the instructions of
my program be read and write protected.
It seems much safer to me.
Also, if this cannot be done, can the program
be put in some other address that is less likely
to be trashed by wild stores thru pointers containing
small numbers?

Dennis Yelle
-- 
I am a computer programmer and I am looking for a job.
There is a link to my resume here:  
http://table.jps.net/~vert/

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019