How to get pointer to code segment?

We have a requirement to verify that a program’s code segment does not
become corrupt while the program is running.

The approach I’d like to take is have the program calculate a CRC over
its primary code segment as soon as it starts and then recalculate and
compare the CRC periodically after that.

How do I get a pointer to a program’s own code segment?

Thanks,
Ben

I’m not an advanced programmer, but I think it should work with a pointer to
the funtcion containing the code you want to calculate a CRC over. You have
to know the size of your code segment. I’ve tested with the following source
code, and got my executables HEX-code. This means &main is a pointer to the
beginning of your code segment.

Daniel

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

int main(int argc, char argv[])
{
int i;
char
func;
for(i=0;i<50;i++)
{
func =(char*) (&main +i);
printf("%X \n",*func);
};
return EXIT_SUCCESS;
}



“Ben Gardner” <bgardner@spam.wabtec.com> schrieb im Newsbeitrag
news:bekvqf$fhr$1@inn.qnx.com

We have a requirement to verify that a program’s code segment does not
become corrupt while the program is running.

The approach I’d like to take is have the program calculate a CRC over
its primary code segment as soon as it starts and then recalculate and
compare the CRC periodically after that.

How do I get a pointer to a program’s own code segment?

Thanks,
Ben

If you are working on a x86 architecture, I have something for you. If it
is not a x86, there is probably a similar method but I can’t help you.

The code segment is held in CS register. While the CPU is in protected
mode(QNX runs in pmode), CS is not a valid pointer to a memory location.
It is used by the CPU to obtain information about the segment. I suggest
you to look at http://developer.intel.com/design/pentium4/manuals/245472.htm
for exact information about that. It is not so complicated(but laborious)
to obtain de physical address of your code but you will need to read a lot.
No need to tell you must be root to succeed.
In any case, x86 architecture(in pmode) does not allow write access to code
segments. Considering that, how could it be corrupted? By strong EMI
maybe… If it is the case, be sure you will end up executing junk and your
CRC won’t be of any use.

Good luck!





“Ben Gardner” <bgardner@spam.wabtec.com> wrote in message
news:bekvqf$fhr$1@inn.qnx.com

We have a requirement to verify that a program’s code segment does not
become corrupt while the program is running.

The approach I’d like to take is have the program calculate a CRC over
its primary code segment as soon as it starts and then recalculate and
compare the CRC periodically after that.

How do I get a pointer to a program’s own code segment?

Thanks,
Ben

&main is not garenteed to be a pointer to your code segment. It depends on
your linker and compiler.

Indeed, &func should work to read the code of a given function, assuming you
know it’s size. And that’s were it starts to be complicated, especially if
you have many functions. To get the size, you will need to code a part in
assembly before and after your C code and link the whole thing in the right
order. It is possible if you have a very flexible linker and you know well
your compiler. From by point of view it is a real mess except for programs
written entirely in assembly.

If anyone knows an easier way, I would be pleased to hear it.

I wish you a very good luck.

“Daniel” <daniel-grimm@gmx.de> wrote in message
news:bg0cnd$pts$1@inn.qnx.com

I’m not an advanced programmer, but I think it should work with a pointer
to
the funtcion containing the code you want to calculate a CRC over. You
have
to know the size of your code segment. I’ve tested with the following
source
code, and got my executables HEX-code. This means &main is a pointer to
the
beginning of your code segment.

Daniel

#include <stdlib.h
#include <stdio.h

int main(int argc, char argv[])
{
int i;
char
func;
for(i=0;i<50;i++)
{
func =(char*) (&main +i);
printf("%X \n",*func);
};
return EXIT_SUCCESS;
}



“Ben Gardner” <> bgardner@spam.wabtec.com> > schrieb im Newsbeitrag
news:bekvqf$fhr$> 1@inn.qnx.com> …
We have a requirement to verify that a program’s code segment does not
become corrupt while the program is running.

The approach I’d like to take is have the program calculate a CRC over
its primary code segment as soon as it starts and then recalculate and
compare the CRC periodically after that.

How do I get a pointer to a program’s own code segment?

Thanks,
Ben

I’m not in front of a QNX system so I can’t give you a detailed example, but
I don’t think it is that difficult.

Write a small asm module (I think it can actually be empty) that is in the
CODE segment and in some group of your own creation (like “put_me_first”).
Write another small asm modual for CODE segment and another group (like
“put_me_last”).

Then make sure that you link in the first object modual first and the last
modual last. Now you can take the address of these two modules and subtract
them to get the size.


“Frédéric Paquet-Roy” <frederic.paquet-roy.1@ens.etsmtl.ca> wrote in message
news:bgdrpv$qlr$1@inn.qnx.com

&main is not garenteed to be a pointer to your code segment. It depends
on
your linker and compiler.

Indeed, &func should work to read the code of a given function, assuming
you
know it’s size. And that’s were it starts to be complicated, especially
if
you have many functions. To get the size, you will need to code a part in
assembly before and after your C code and link the whole thing in the
right
order. It is possible if you have a very flexible linker and you know
well
your compiler. From by point of view it is a real mess except for
programs
written entirely in assembly.

If anyone knows an easier way, I would be pleased to hear it.

I wish you a very good luck.

“Daniel” <> daniel-grimm@gmx.de> > wrote in message
news:bg0cnd$pts$> 1@inn.qnx.com> …
I’m not an advanced programmer, but I think it should work with a
pointer
to
the funtcion containing the code you want to calculate a CRC over. You
have
to know the size of your code segment. I’ve tested with the following
source
code, and got my executables HEX-code. This means &main is a pointer to
the
beginning of your code segment.

Daniel

#include <stdlib.h
#include <stdio.h

int main(int argc, char argv[])
{
int i;
char
func;
for(i=0;i<50;i++)
{
func =(char*) (&main +i);
printf("%X \n",*func);
};
return EXIT_SUCCESS;
}



“Ben Gardner” <> bgardner@spam.wabtec.com> > schrieb im Newsbeitrag
news:bekvqf$fhr$> 1@inn.qnx.com> …
We have a requirement to verify that a program’s code segment does not
become corrupt while the program is running.

The approach I’d like to take is have the program calculate a CRC over
its primary code segment as soon as it starts and then recalculate and
compare the CRC periodically after that.

How do I get a pointer to a program’s own code segment?

Thanks,
Ben

\

Memory can not be corrupted by accessing it through the SC register. But
the same physical memory can be accessed/modified via different means. I.E.
/dev/mem

“Frédéric Paquet-Roy” <frederic.paquet-roy.1@ens.etsmtl.ca> wrote in message
news:bgdr02$pmr$1@inn.qnx.com

If you are working on a x86 architecture, I have something for you. If it
is not a x86, there is probably a similar method but I can’t help you.

The code segment is held in CS register. While the CPU is in protected
mode(QNX runs in pmode), CS is not a valid pointer to a memory location.
It is used by the CPU to obtain information about the segment. I suggest
you to look at
http://developer.intel.com/design/pentium4/manuals/245472.htm
for exact information about that. It is not so complicated(but laborious)
to obtain de physical address of your code but you will need to read a
lot.
No need to tell you must be root to succeed.
In any case, x86 architecture(in pmode) does not allow write access to
code
segments. Considering that, how could it be corrupted? By strong EMI
maybe… If it is the case, be sure you will end up executing junk and
your
CRC won’t be of any use.

Good luck!





“Ben Gardner” <> bgardner@spam.wabtec.com> > wrote in message
news:bekvqf$fhr$> 1@inn.qnx.com> …
We have a requirement to verify that a program’s code segment does not
become corrupt while the program is running.

The approach I’d like to take is have the program calculate a CRC over
its primary code segment as soon as it starts and then recalculate and
compare the CRC periodically after that.

How do I get a pointer to a program’s own code segment?

Thanks,
Ben

/dev/mem. Very good point.

“Bill Caroselli” <QTPS@Earthlink.net> wrote in message
news:bge58h$3nr$1@inn.qnx.com

Memory can not be corrupted by accessing it through the SC register. But
the same physical memory can be accessed/modified via different means.
I.E.
/dev/mem

“Frédéric Paquet-Roy” <> frederic.paquet-roy.1@ens.etsmtl.ca> > wrote in
message
news:bgdr02$pmr$> 1@inn.qnx.com> …
If you are working on a x86 architecture, I have something for you. If
it
is not a x86, there is probably a similar method but I can’t help you.

The code segment is held in CS register. While the CPU is in protected
mode(QNX runs in pmode), CS is not a valid pointer to a memory
location.
It is used by the CPU to obtain information about the segment. I
suggest
you to look at
http://developer.intel.com/design/pentium4/manuals/245472.htm
for exact information about that. It is not so complicated(but
laborious)
to obtain de physical address of your code but you will need to read a
lot.
No need to tell you must be root to succeed.
In any case, x86 architecture(in pmode) does not allow write access to
code
segments. Considering that, how could it be corrupted? By strong EMI
maybe… If it is the case, be sure you will end up executing junk and
your
CRC won’t be of any use.

Good luck!





“Ben Gardner” <> bgardner@spam.wabtec.com> > wrote in message
news:bekvqf$fhr$> 1@inn.qnx.com> …
We have a requirement to verify that a program’s code segment does not
become corrupt while the program is running.

The approach I’d like to take is have the program calculate a CRC over
its primary code segment as soon as it starts and then recalculate and
compare the CRC periodically after that.

How do I get a pointer to a program’s own code segment?

Thanks,
Ben

\

Hmmm, wait. I am not sure you could do that. Is it really only by using CS
that you cannot do a write access to a code segment? This would mean that a
given program could screw up all other code segments and even its own just
by getting the physical address of those code regions and accessing them by
another segment with write permission. In this case, /dev/mem is that kind
of dangerous segment.

Anyway, there is always another peripheral that could burst something into
RAM(ie: DMA controler) and do bad things.

“Bill Caroselli” <QTPS@Earthlink.net> wrote in message
news:bge58h$3nr$1@inn.qnx.com

Memory can not be corrupted by accessing it through the SC register. But
the same physical memory can be accessed/modified via different means.
I.E.
/dev/mem

“Frédéric Paquet-Roy” <> frederic.paquet-roy.1@ens.etsmtl.ca> > wrote in
message
news:bgdr02$pmr$> 1@inn.qnx.com> …
If you are working on a x86 architecture, I have something for you. If
it
is not a x86, there is probably a similar method but I can’t help you.

The code segment is held in CS register. While the CPU is in protected
mode(QNX runs in pmode), CS is not a valid pointer to a memory
location.
It is used by the CPU to obtain information about the segment. I
suggest
you to look at
http://developer.intel.com/design/pentium4/manuals/245472.htm
for exact information about that. It is not so complicated(but
laborious)
to obtain de physical address of your code but you will need to read a
lot.
No need to tell you must be root to succeed.
In any case, x86 architecture(in pmode) does not allow write access to
code
segments. Considering that, how could it be corrupted? By strong EMI
maybe… If it is the case, be sure you will end up executing junk and
your
CRC won’t be of any use.

Good luck!





“Ben Gardner” <> bgardner@spam.wabtec.com> > wrote in message
news:bekvqf$fhr$> 1@inn.qnx.com> …
We have a requirement to verify that a program’s code segment does not
become corrupt while the program is running.

The approach I’d like to take is have the program calculate a CRC over
its primary code segment as soon as it starts and then recalculate and
compare the CRC periodically after that.

How do I get a pointer to a program’s own code segment?

Thanks,
Ben

\

“Bill Caroselli” <QTPS@Earthlink.net> wrote in message
news:bge58h$3nr$1@inn.qnx.com

Memory can not be corrupted by accessing it through the SC register. But

that’s “CS” register.

the same physical memory can be accessed/modified via different means.
I.E.
/dev/mem