Wrapping assignments in function call for simulation

Hi All,

I would like to perform for simulation experiments on a C codebase where based on the type of the variable, an assignment to a pointer of this type is followed by some additional instruction (lets say an external counter increment — just to create a simple scenario). I have implemented a RecuriveASTVisitor that visits BinaryOps (after checking they are assignments) and rewrites the source by replacement.

However, there are issues with multiple assignments within the same source line (such as ptr1 = ptr2 = NULL), as well as some macro expansions. I would like a robust way of doing so without breaking the code.

Could someone kindly answer the following:

  1. Is there some existing libTool to do so?

if not, then:
2. My wrapper function is of the form: (listing a simplified version)
void * wrapAssgnmt (void **ptrToLhsPtr, void *rhsPtr){
*ptrToLhsPtr = rhsPtr;
// do other things for simulation
return *ptrToLhsPtr; // so that NULL based checks work.
}
compilation gives me: “warning: comparison between pointer and integer” for cases of pattern:
type *ptr; // original code is if( (ptr = malloc ( … )) = NULL) …
if((wrapAssgnmt( &ptr , malloc( … ) ) = NULL)

I understand the warning, but I am not sure about the cause/reason. Could someone please suggest the correction?

Thanks!

Hi All,

I would like to perform for simulation experiments on a C codebase where
based on the type of the variable, an assignment to a pointer of this
type is followed by some additional instruction (lets say an external
counter increment --- just to create a simple scenario). I have
implemented a RecuriveASTVisitor that visits BinaryOps (after checking
they are assignments) and rewrites the source by replacement.

However, there are issues with multiple assignments within the same
source line (such as ptr1 = ptr2 = NULL), as well as some macro
expansions. I would like a robust way of doing so without breaking the code.

Could someone kindly answer the following:
1. Is there some existing libTool to do so?

if not, then:
2. My wrapper function is of the form: (listing a simplified version)
      void * wrapAssgnmt (void **ptrToLhsPtr, void *rhsPtr){
              *ptrToLhsPtr = rhsPtr;
              // do other things for simulation
              return *ptrToLhsPtr; // so that NULL based checks work.
      }
    compilation gives me: "warning: comparison between pointer and
integer" for cases of pattern:
    type *ptr; // original code is if( (ptr = malloc ( ... )) = NULL) ...
    if((wrapAssgnmt( &ptr , malloc( ... ) ) = NULL)

    I understand the warning, but I am not sure about the cause/reason.
Could someone please suggest the correction?

This happens because your translation is not precise enough. It needs to be something like:

   if ((wrapAssgnmt(&ptr, malloc( ... ), ptr) = NULL)

Jon

Thanks Jon!

So you suggest the change of function to:

void *wrapAssgnmt(void **,void *, void *ptr){
// assgn using first two args

// return the actual ptr
return ptr;
}

Thanks Jon!

So you suggest the change of function to:

void *wrapAssgnmt(void **,void *, void *ptr){
   // assgn using first two args
   ...
  // return the actual ptr
  return ptr;
}

Oh, oops. I put the parens in the wrong spot. I meant to write this:

   if ((wrapAssgnmt(&ptr, malloc( ... )), ptr) = NULL)

as-in: use the comma operator to perform the assignment of NULL into 'ptr'.

Jon

ah okay!

Thanks!