Skip to content

Improvements to array bounds inference #226

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Sep 7, 2020
Merged

Conversation

Machiry
Copy link
Collaborator

@Machiry Machiry commented Aug 15, 2020

  • Context-sensitive bounds:

    Consider the following example:

_Itype_for_any(T) void *somefunc(unsigned long size) : itype(_Array_ptr<T>) byte_count(size);
        
struct hash_node
{
  int *p_key;
  int *q_key;
  int *r_key;
  unsigned pqlen;
  unsigned r_len;
}; 
int foo() {
    unsigned i;
    struct hash_node *n ;
    i = 5*sizeof(int);
    n->pqlen = i;
    n->p_key = somefunc(i);
    n->r_key = somefunc(n->r_len);
    n->p_key[0] = 1;
    n->r_key[0] = 1;
    return 0;
}

Here, we need to keep track of the call-sites of somefunc. This allows us to keep track of the length association correctly.
That way, we can correctly infer the bounds as below:

struct hash_node
{
  _Array_ptr<int> p_key : count(pqlen);
  _Array_ptr<int> q_key : count(pqlen);
  _Array_ptr<int> r_key : count(r_len);
  unsigned pqlen;
  unsigned r_len;
}; 
  • Prioritized Bounds:

    When we have conflicting bounds, use bounds based on priority:
    Example:

p = malloc(n*sizeof(int));
...
memcpy(p, q, 8);

In the above case, even though p has conflicting bounds from malloc and memcpy we will infer the bounds of p to be count(n).

  • Invalidate bounds on pointer arithmetic:

    If there is pointer arithmetic on a pointer, then it cannot have count or byte_bounds.
    For example:

      _Array_ptr<int> p : count(n) = malloc(n*sizeof(int));
      ...
      ...
      p++;
    

The above bounds expressio for p ins invalid because count(n) means -> bounds(p, p+n) that means changing p (i.e., p++) will make the bounds invalid.
Ideally, we should do the following:

   _Ptr<int> pl = NULL;
   _Array_ptr<int> p : bounds(pl, pl+n) = malloc(n*sizeof(int));
   pl = p;

Making the above change is not straight forward. We will make another issue (enhancement) for that. As of now, we make the bounds invalid whenever we see a pointer modifying expression.

  • Bounds for return values:

We will emit bounds for function returns.

For example:

        int *sus() {
           int *z = malloc(5*sizeof(int));
          ...
          return z;
       }

will get converted to:

       _Array_ptr<int> sus() : count(5) {
           _Array_ptr<int> z = malloc(5*sizeof(int));
          ...
          return z;
       }

@Machiry Machiry linked an issue Aug 20, 2020 that may be closed by this pull request
@Machiry Machiry merged commit 7a47739 into BigRefactor Sep 7, 2020
@Machiry Machiry deleted the bytecount_reduction branch September 7, 2020 10:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Invalidate bounds on pointer arithmetic
1 participant