IsBadWritePtr equivalent?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • rohit

    #1

    IsBadWritePtr equivalent?

    I am porting a C code from Windows base to Linux/Macintosh OS and need
    an implementation of "IsBadWrite Ptr" for gcc to run the app on
    Mac/Linux.

    Below is the detailed info:
    IsBadWritePtr in Windows, basically verifies that the calling process
    has write access to the specified range of memory.

    But unfortunately I am not finding any way to verify the memory on Mac
    (using gcc 4.0)

    I have a structure called DevCon, and I check if the validity of the
    same in windows using the following macro:

    #define IsValid(DevCon) IsBadWritePtr(D evCon,4)

    So if the calling process has write access to all bytes in the
    specified memory range, then it returns 0 else 1. This way I easily get
    to know the validity.

    In Linux/Mac, I tried to simulate the same using below code, but it
    gave me the segmentation fault.

    int IsValid(DEV_CON DevCon){

    if(DevCon->MyFirstEleme nt == "FIXEDVALUE ") ===I get the segmentation
    fault.
    {
    }

    }

    Please Note: The app is designed in such a way that the pointer
    pointing to the structure always have some value. (it is never null) On
    the other hand the structure it is pointing to is only freed. So this
    is the reason we used IsBadWritePtr in Windows.

    1) Do we have try catch throws in gcc ? So that it can be possible to
    suppress the crash ?
    2) Is there readily available API of IsBadWritePtr in gcc ? If not, can
    anybody show the implementaion for the same?

    Please let me know for any clarification in this regards,
    Rohit

  • Richard Heathfield

    #2
    Re: IsBadWritePtr equivalent?

    rohit said:

    <snip>
    1) Do we have try catch throws in gcc ?
    We don't have them in C.
    So that it can be possible to suppress the crash ?
    Instead of suppressing it, why not find and fix the cause?
    2) Is there readily available API of IsBadWritePtr in gcc ?
    There is none in the C language.
    If not, can
    anybody show the implementaion for the same?
    The implementation in C is kind of distributed. Here it is:

    1) When you define the pointer, ensure that it's either valid (by pointing
    it at a valid object or function) or NULL.

    2) Whenever you change the pointer value, ensure that the new value is
    either valid or NULL.

    3) Check that a pointer value is not NULL before relying on that value.

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999

    email: rjh at above domain (but drop the www, obviously)

    Comment

    • robertwessel2@yahoo.com

      #3
      Re: IsBadWritePtr equivalent?


      rohit wrote:
      I am porting a C code from Windows base to Linux/Macintosh OS and need
      an implementation of "IsBadWrite Ptr" for gcc to run the app on
      Mac/Linux.
      >
      Below is the detailed info:
      IsBadWritePtr in Windows, basically verifies that the calling process
      has write access to the specified range of memory.
      >
      But unfortunately I am not finding any way to verify the memory on Mac
      (using gcc 4.0)
      >
      I have a structure called DevCon, and I check if the validity of the
      same in windows using the following macro:
      >
      #define IsValid(DevCon) IsBadWritePtr(D evCon,4)
      >
      So if the calling process has write access to all bytes in the
      specified memory range, then it returns 0 else 1. This way I easily get
      to know the validity.
      >
      In Linux/Mac, I tried to simulate the same using below code, but it
      gave me the segmentation fault.
      >
      int IsValid(DEV_CON DevCon){
      >
      if(DevCon->MyFirstEleme nt == "FIXEDVALUE ") ===I get the segmentation
      fault.
      {
      }
      >
      }
      >
      Please Note: The app is designed in such a way that the pointer
      pointing to the structure always have some value. (it is never null) On
      the other hand the structure it is pointing to is only freed. So this
      is the reason we used IsBadWritePtr in Windows.
      >
      1) Do we have try catch throws in gcc ? So that it can be possible to
      suppress the crash ?
      2) Is there readily available API of IsBadWritePtr in gcc ? If not, can
      anybody show the implementaion for the same?
      >
      Please let me know for any clarification in this regards,

      While entirely OT...

      To expand on what Richard said... You can't do this in standard C, and
      even in Windows, IsBadXxxPtr() is probably *not* actually doing what
      you want.

      The IsBadXxxPtr() functions check to see if the memory being pointed to
      can be accessed. This is based on the OS's mapping/allocation of pages
      into the address space. This has little to do with C's notion of
      allocated and freed memory. Almost never will free() actually return
      the page to the OS, and the pointer, even though pointing to an area
      free()'d, will still be pointing to addressable storage, and IsBad will
      return OK. Even worse, the very next malloc(), of any type of object,
      can trivially reuse the area freed.

      It's extremely rare the IsBadXxxPtr() is actually useful. If you need
      it, you more likely have a bug in your program, because it DOES NOT
      tell you if a pointer is actually good. In fact, in most cases
      isBadXxxPtr() will forever return true once the malloc() has been done,
      since most of the time the CRT will never return heap pages to the OS.

      Let me repeat that. IsBadXxxPtr() does not, can not, will not, with
      even a small degree of reliability, determine if a pointer is pointing
      to a freed area. It does not do that, it will not work.

      Comment

      • Richard Tobin

        #4
        Re: IsBadWritePtr equivalent?

        In article <wLKdnQ_cRIYxF7 HYnZ2dnUVZ8qCdn Z2d@bt.com>,
        Richard Heathfield <invalid@invali d.invalidwrote:
        >The implementation in C is kind of distributed. Here it is:
        >
        >1) When you define the pointer, ensure that it's either valid (by pointing
        >it at a valid object or function) or NULL.
        >
        >2) Whenever you change the pointer value, ensure that the new value is
        >either valid or NULL.
        >
        >3) Check that a pointer value is not NULL before relying on that value.
        You missed

        4) Do not use a pointer after the lifetime of the memory it points to.

        -- Richard

        Comment

        • Richard Heathfield

          #5
          Re: IsBadWritePtr equivalent?

          Richard Tobin said:
          In article <wLKdnQ_cRIYxF7 HYnZ2dnUVZ8qCdn Z2d@bt.com>,
          Richard Heathfield <invalid@invali d.invalidwrote:
          >
          >>The implementation in C is kind of distributed. Here it is:
          >>
          >>1) When you define the pointer, ensure that it's either valid (by pointing
          >>it at a valid object or function) or NULL.
          >>
          >>2) Whenever you change the pointer value, ensure that the new value is
          >>either valid or NULL.
          >>
          >>3) Check that a pointer value is not NULL before relying on that value.
          >
          You missed
          >
          4) Do not use a pointer after the lifetime of the memory it points to.
          You're right that you should not use such a pointer, but I don't agree that
          I missed it. When the object pointed to ceases to exist, the value of the
          pointer changes. (Not the object representation; the *value*.) And so my
          point (2) covers it - "ensure that the new value is either valid or NULL".
          We know it's not valid (because the object it pointed to just died), so we
          should set it to NULL.

          --
          Richard Heathfield
          "Usenet is a strange place" - dmr 29/7/1999

          email: rjh at above domain (but drop the www, obviously)

          Comment

          • Lew Pitcher

            #6
            Re: IsBadWritePtr equivalent?

            -----BEGIN PGP SIGNED MESSAGE-----
            Hash: SHA1


            Richard Heathfield wrote:
            rohit said:
            >
            <snip>
            >
            1) Do we have try catch throws in gcc ?
            >
            We don't have them in C.
            But, we /do/ have signals and signal handlers

            Although the circumstances may vary, environment to environment, the
            standard SIGSEGV signal should be issued on "an invalid access to
            storage". This is one of the signals defined in the standard, and a
            signal handler function should be able to intercept it.
            So that it can be possible to suppress the crash ?
            Yes, for a limited solution. But, you want to do more than "suppress
            the crash"; you want to repair the cause of the crash, and prevent any
            further crashes. You could do some of this with signal handler code,
            but the general case would be pretty convoluted. Better to follow RH's
            advice and just "find and fix the cause" before it causes a crash.
            Instead of suppressing it, why not find and fix the cause?
            Agreed.

            [snip]

            HTH
            - --
            Lew Pitcher

            -----BEGIN PGP SIGNATURE-----
            Version: GnuPG v1.4.3 (MingW32) - WinPT 0.11.12

            iD8DBQFFLOeyagV FX4UWr64RAo22AK DzRnZKYP6sQjZvk qbHjburlk28DACf egFI
            4EuPVr7LmRHpMnk Mi2SvmoE=
            =O43a
            -----END PGP SIGNATURE-----

            Comment

            • rohit

              #7
              Re: IsBadWritePtr equivalent?

              How about following code:
              #include <setjmp.h>
              #include <signal.h>

              static jmp_buf hndbuf;

              static void handler(int ignored)
              {
              (void)ignored;
              longjmp(hndbuf, 1);
              }

              static BOOL IsBadWritePtr(c onst void *lp, size_t ucb)
              {
              struct sigaction act;
              struct sigaction oact;
              char *base = (void*)lp;
              char *over = base + ucb;
              volatile char *test;

              act.sa_handler = handler;
              sigemptyset(&ac t.sa_mask);
              act.sa_flags |= SA_RESTART;

              if (sigaction(SIGS EGV, &act, &oact) < 0) {
              return FALSE; /* Debatable action */
              }

              switch (setjmp(hndbuf) ) {
              case 0:
              for (test = base; test < over; test++) {
              *test = *test;
              }

              (void)sigaction (SIGSEGV, &oact, NULL);
              return FALSE;
              default:
              break;
              }

              (void)sigaction (SIGSEGV, &oact, NULL);
              return TRUE;
              }
              Rohit
              Lew Pitcher wrote:
              -----BEGIN PGP SIGNED MESSAGE-----
              Hash: SHA1
              >
              >
              Richard Heathfield wrote:
              rohit said:

              <snip>
              1) Do we have try catch throws in gcc ?
              We don't have them in C.
              >
              But, we /do/ have signals and signal handlers
              >
              Although the circumstances may vary, environment to environment, the
              standard SIGSEGV signal should be issued on "an invalid access to
              storage". This is one of the signals defined in the standard, and a
              signal handler function should be able to intercept it.
              >
              So that it can be possible to suppress the crash ?
              >
              Yes, for a limited solution. But, you want to do more than "suppress
              the crash"; you want to repair the cause of the crash, and prevent any
              further crashes. You could do some of this with signal handler code,
              but the general case would be pretty convoluted. Better to follow RH's
              advice and just "find and fix the cause" before it causes a crash.
              >
              Instead of suppressing it, why not find and fix the cause?
              >
              Agreed.
              >
              [snip]
              >
              HTH
              - --
              Lew Pitcher
              >
              -----BEGIN PGP SIGNATURE-----
              Version: GnuPG v1.4.3 (MingW32) - WinPT 0.11.12
              >
              iD8DBQFFLOeyagV FX4UWr64RAo22AK DzRnZKYP6sQjZvk qbHjburlk28DACf egFI
              4EuPVr7LmRHpMnk Mi2SvmoE=
              =O43a
              -----END PGP SIGNATURE-----

              Comment

              • Chris Dollin

                #8
                Re: IsBadWritePtr equivalent?

                rohit wrote:
                How about following code:
                #include <setjmp.h>
                #include <signal.h>
                >
                static jmp_buf hndbuf;
                >
                static void handler(int ignored)
                {
                (void)ignored;
                longjmp(hndbuf, 1);
                }
                If this signal handler is invoked other than via `abort` or
                `raise`, the effect is undefined, because it calls a library
                function other than `signal`. (At least according to this
                here C89 draft.)

                I think you need a stronger standard than C. (OT does Posix
                allow this reliably?)

                --
                Chris "Essen -7 and counting" Dollin
                "We did not have time to find out everything we wanted to know."
                - James Blish, /A Clash of Cymbals/

                Comment

                • CBFalconer

                  #9
                  Re: IsBadWritePtr equivalent?

                  rohit wrote:
                  >
                  How about following code:
                  #include <setjmp.h>
                  #include <signal.h>
                  ....
                  top-posted article ignored. See following links.

                  --
                  Some informative links:
                  <news:news.anno unce.newusers
                  <http://www.geocities.c om/nnqweb/>
                  <http://www.catb.org/~esr/faqs/smart-questions.html>
                  <http://www.caliburn.nl/topposting.html >
                  <http://www.netmeister. org/news/learn2quote.htm l>
                  <http://cfaj.freeshell. org/google/>


                  Comment

                  • Kenny McCormack

                    #10
                    Re: IsBadWritePtr equivalent?

                    In article <452E45EB.81F1A 83A@yahoo.com>,
                    CBFalconer <cbfalconer@mai neline.netwrote :
                    >rohit wrote:
                    >>
                    >How about following code:
                    >#include <setjmp.h>
                    >#include <signal.h>
                    >...
                    >top-posted article ignored. See following links.
                    But it wasn't ignored. You clearly read it, interpreted it, and, most
                    significantly of all, responded to it.
                    Some informative links:
                    <news:news.anno unce.newusers
                    <http://www.geocities.c om/nnqweb/>
                    <http://www.catb.org/~esr/faqs/smart-questions.html>
                    <http://www.caliburn.nl/topposting.html >
                    <http://www.netmeister. org/news/learn2quote.htm l>
                    <http://cfaj.freeshell. org/google/>
                    Those are good, but these are better - should be required first reading
                    for new posters here:





                    Comment

                    • Bart

                      #11
                      Re: IsBadWritePtr equivalent?

                      robertwessel2@y ahoo.com wrote:
                      While entirely OT...
                      Indeed.
                      Let me repeat that. IsBadXxxPtr() does not, can not, will not, with
                      even a small degree of reliability, determine if a pointer is pointing
                      to a freed area.
                      "cannot" is too strong. It is not meant to be used in conjunction with
                      malloc/free, but can work in programs that allocate using the Win32
                      directly. But I agree that it's superfluous in reliable programs.

                      Regards,
                      Bart.

                      Comment

                      • robertwessel2@yahoo.com

                        #12
                        Re: IsBadWritePtr equivalent?


                        Bart wrote:
                        robertwessel2@y ahoo.com wrote:
                        While entirely OT...
                        >
                        Indeed.
                        >
                        Let me repeat that. IsBadXxxPtr() does not, can not, will not, with
                        even a small degree of reliability, determine if a pointer is pointing
                        to a freed area.
                        >
                        "cannot" is too strong. It is not meant to be used in conjunction with
                        malloc/free, but can work in programs that allocate using the Win32
                        directly. But I agree that it's superfluous in reliable programs.

                        Only if you're looking at storage directly allocated with
                        VirtualAlloc() and friends. Using the Win32 HeapXxxx() functions has
                        the same problem as using the CRT's heap (assuming the CRT isn't just
                        using a Win32 heap). With any sort of conventional heap structure,
                        IsBadXxxPtr() simply can't do what we're assuming the OP wants it to
                        do. As I pointed out, in most cases it will never return false for a
                        pointer to a block that's been allocated, even if it has been freed
                        (since the heap will not usually release the underlying pages).

                        Implementing such a function requires some access to the heap
                        implementation, but that's fairly commonly available (if entirely
                        implementation specific). For example, in MSVC you could use the
                        _heapwalk() function to validate the pointer (not very quickly, but the
                        implementation is trivial). The CRTs used with GCC usually have a
                        similar HeapWalk() function. Even then this does nothing to detect
                        reuse of that memory area in a new allocation.

                        Comment

                        • Spiro Trikaliotis

                          #13
                          Re: IsBadWritePtr equivalent?

                          Hello,

                          Bart wrote:
                          >Let me repeat that. IsBadXxxPtr() does not, can not, will not, with
                          >even a small degree of reliability, determine if a pointer is pointing
                          >to a freed area.
                          >
                          "cannot" is too strong. It is not meant to be used in conjunction with
                          malloc/free, but can work in programs that allocate using the Win32
                          directly. But I agree that it's superfluous in reliable programs.
                          Although it is totally OT here, let's see what a Microsoft employee,
                          Raymond Chen, has to tell about it:

                          "IsBadXxxPt r should really be called CrashProgramRan domly"
                          http://blogs.msdn.com/oldnewthing/ar...27/773741.aspx

                          Regards,
                          Spiro.

                          --
                          Spiro R. Trikaliotis http://opencbm.sf.net/
                          http://www.trikaliotis.net/ http://www.viceteam.org/

                          Comment

                          • Tom St Denis

                            #14
                            Re: IsBadWritePtr equivalent?

                            Kenny McCormack wrote:
                            Those are good, but these are better - should be required first reading
                            for new posters here:
                            How about the CLC FAQ?

                            You seem to be upset that the regulars aren't doing all in their power
                            to help off topic discussions flourish.

                            Newsflash. Life isn't always going to turn out how you want. This is
                            comp.lang.c and nothing else. If you want to get upset, get upset at
                            the script monkeys, slackers [students] and others who make up the
                            majority[*] of off topic posts.

                            Tom
                            [*] there are the occasional innoncent posters who just don't know
                            better but most questions here seem to be either homework or
                            professionally related. So they deserve neither patience or anything
                            more than the most concise reply.

                            Comment

                            • Keith Thompson

                              #15
                              Re: IsBadWritePtr equivalent?

                              "Tom St Denis" <tomstdenis@gma il.comwrites:
                              Kenny McCormack wrote:
                              [snip]
                              >
                              How about the CLC FAQ?
                              >
                              You seem to be upset that the regulars aren't doing all in their power
                              to help off topic discussions flourish.
                              Tom, Kenny McCormack is simply a troll, nothing more, nothing less.
                              Please ignore him.

                              --
                              Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
                              San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
                              We must do something. This is something. Therefore, we must do this.

                              Comment

                              Working...