inline void push(int d) {
	int oldTop, newTop,ret;
	__VERIFIER_atomic_index_malloc(&newTop);
	if(newTop == 0)
		exit(-1);
	INDIR(newTop,0) = d;
	while (1) {
		oldTop = top;
		INDIR(newTop,1) = oldTop;
		__VERIFIER_atomic_CAS(&top,oldTop,newTop,&ret);
		if(ret)	return;
	}
}
void push(int d) {
  int oldTop, newTop;
  __VERIFIER_atomic_index_malloc(&newTop);
  if(newTop == 0)
    exit(-1);
  INDIR(newTop,0) = d;
  while (1) {
    oldTop = top;
    INDIR(newTop,1) = oldTop;
    if(__sync_bool_compare_and_swap(&top,oldTop,newTop))
      return;
  }
}
inline void push(int d) {
	int oldTop = -1, newTop = -1;

	__VERIFIER_atomic_index_malloc(&newTop);
	if(newTop == 0)
		exit(-1);
	else{
		INDIR(newTop,0) = d;
		__VERIFIER_atomic_acquire();
		oldTop = top;
		INDIR(newTop,1) = oldTop;
		top = newTop; 
		__VERIFIER_atomic_release();
	}
}
int push(int d) {
  int oldTop = -1, newTop = -1;

  newTop = index_malloc();
  if(newTop == 0){
    return 0;
  }else{
    INDIR(newTop,0) = d;
    pthread_mutex_lock(&m);
    oldTop = top;
    INDIR(newTop,1) = oldTop;
    top = newTop;
    pthread_mutex_unlock(&m);
    return 1;
  }
}
inline int push(int d) {
	int oldTop = -1, newTop = -1;

	newTop = index_malloc();
	if(newTop == 0){
		return 0;
	}else{
		INDIR(newTop,0) = d;

		__VERIFIER_atomic_acquire(&m);
		oldTop = top;
		INDIR(newTop,1) = oldTop;
		top = newTop; 
		__VERIFIER_atomic_release(&m);
		return 1;
	}
}
int push(int d) {
  int oldTop = -1, newTop = -1;
  
  newTop = index_malloc();
  if(newTop == 0){
    return 0;
  }else{
    INDIR(newTop,0) = d;
    while (1) {
      oldTop = top;
      INDIR(newTop,1) = oldTop;
      if(__sync_bool_compare_and_swap(&top,oldTop,newTop)){
	return 1;
      }
      
    }
  }
}
inline int push(int d) {
	int oldTop = -1, newTop = -1, casret = -1;

	newTop = index_malloc();
	if(newTop == 0){
		return 0;
	}else{
		INDIR(newTop,0) = d;
		while (1) {
			oldTop = top;
			INDIR(newTop,1) = oldTop;
			__VERIFIER_atomic_CAS(&top,oldTop,newTop,&casret);
			if(casret==1){
				return 1;
			}

		}
	}
}
示例#8
0
文件: main.c 项目: olivo/BP
int push(E d) {
	WORDT_Ptr oldTop = -1, newTop = -1;
	WORDT_Ptr chTop = -1;

#if (APRED >= 1)
	__CPROVER_predicate(s.top == 0);
	__CPROVER_predicate(newTop == 0);
	__CPROVER_predicate(s.top == oldTop);
#endif

	newTop = index_malloc();
	if(newTop == WORDT_NULL){
		assume(newTop == WORDT_NULL);
		return 0;
	}else{
		assume(!(newTop == WORDT_NULL));
		INDIR(newTop,0) = d;
		while (1) {
			oldTop = s.top;
			INDIR(newTop,1) = oldTop;
			//inlining of cas
			//atomic_begin();
			if (s.top == oldTop) {
				assume(s.top == oldTop);
				atomic_begin();
					chTop = s.top;
					s.top = newTop; 
				atomic_end();
				
				//atomic_end();
				
				unsafe_assert(chTop == oldTop);
				
				return 1;
			}else{
				assume(!(s.top == oldTop));
				//atomic_end();
			}
		}
	}
}
示例#9
0
文件: main.c 项目: olivo/BP
int push(E d) {
	WORDT_Ptr oldTop = -1, newTop = -1;

	newTop = index_malloc();
	if(newTop == WORDT_NULL){
#ifdef USE_BRANCHING_ASSUMES
		__CPROVER_assume(newTop == WORDT_NULL);
#endif
		return 0;
	}else{
#ifdef USE_BRANCHING_ASSUMES
		__CPROVER_assume(!(newTop == WORDT_NULL));
#endif
		INDIR(newTop,0) = d;

		//__CPROVER_atomic_begin();
		oldTop = s.top;
		INDIR(newTop,1) = oldTop;
		s.top = newTop; 
		//__CPROVER_atomic_end();
		return 1;
	}
}
示例#10
0
/*
 * "Resolve" a pathname.
 *
 * Makes sure that 'path' falls within the 'dir' after application of
 * realpath to resolve "..", symlinks, etc. If 'create' is set, it will
 * create missing intermediate directories. In detail:
 *
 * The path (and dir) must be absolute, or it is an error.
 * If 'create' is zero, then all components of the path must exist
 *   or it is an error.
 * If 'create' is non-zero, then missing components except the last
 *   will be created.
 * If 'dir' is non-null, the existing part of the path must resolve
 *   within this directory or it is an error.
 * If 'dir' is null, then the path is resolved as far as it exists
 *  ('create' is ignored and assumed to be zero).
 *   
 * Returns NULL on an error, a malloc'ed pointer to a canonicalized
 * path otherwise.
 */
char *
resolvepath(char *path, char *dir, int create)
{
	char rpath[PATH_MAX], *next, *ep, *pathcopy;
	struct stat sb;
	int pathlen = strlen(path);
	int dirlen = 0;
	int rpathlen;
	mode_t omask, cmask;
	char *npath;

	if (debug > 1)
		info("resolvepath '%s' in dir '%s'", path, dir);
	/* validate path */
	if (path == NULL) {
		if (debug > 1)
			info(" null path");
		return NULL;
	}
	if (path[0] != '/') {
		if (debug > 1)
			info(" path is not absolute");
		return NULL;
	}
	if (pathlen >= sizeof rpath) {
		if (debug > 1)
			info(" path is too long");
		return NULL;
	}

	/* validate dir */
	if (dir) {
		dirlen = strlen(dir);
		if (dir[0] != '/') {
			if (debug > 1)
				info(" path and dir (%s) must be absolute",
				     dir);
			return NULL;
		}
		/* XXX make dir=='/' work */
		if (dirlen == 1)
			dirlen = 0;
	} else {
		create = 0;
	}

	/*
	 * If the full path resolves, make sure it falls in dir (if given)
	 * and is a regular file.
	 */
	if (myrealpath(path, rpath) != NULL) {
		if (dir && !INDIR(dir, dirlen, rpath)) {
			if (debug > 1)
				info(" resolved path (%s) not in dir (%s)",
				     rpath, dir);
			return NULL;
		}
		if (stat(rpath, &sb) < 0 || !S_ISREG(sb.st_mode)) {
			if (debug > 1)
				info(" not a regular file");
			return NULL;
		}
		return strdup(rpath);
	}
	if (debug > 1) {
		int oerrno = errno;
		info(" partially resolved to '%s' (errno %d)", rpath, errno);
		errno = oerrno;
	}

	/*
	 * If create is not set or we failed for any reason other than
	 * a non-existent component, we return an error.
	 */
	if (!create || errno != ENOENT) {
		if (debug > 1)
			info(" realpath failed at %s with %d",
			     rpath, errno);
		return NULL;
	}

	/*
	 * Need to create intermediate directories.
	 *
	 * First, check that what does exist of the path falls within
	 * the directory given.
	 */
	assert(dir != NULL);
	if (!INDIR(dir, dirlen, rpath)) {
		if (debug > 1)
			info(" resolved path (%s) not in dir (%s)",
			     rpath, dir);
		return NULL;
	}

	/*
	 * Establish permission (mode) for new directories.
	 * Start with permission of 'dir'; this will get updated with
	 * every component that resolves, so that new directories will
	 * get created with the permission of the most recent ancestor.
	 */
	if (stat(dir, &sb) < 0) {
		if (debug > 1)
			info(" stat failed on dir (%s)?!", dir);
		return NULL;
	}
	cmask = (sb.st_mode & (S_IRWXU|S_IRWXG|S_IRWXO)) | S_IRWXU;
	omask = umask(0);
	if (debug > 1)
		info(" umask=0 (was 0%o), initial cmask=0%o", omask, cmask);

	/*
	 * Find the first component of the original path that does not
	 * exist (i.e., what part of the original path maps to what realpath
	 * returned) and create everything from there on.
	 *
	 * Note that if what realpath returned is a prefix of the original
	 * path then nothing was translated and we are already at the first
	 * component that needs creation. So we find the appropriate location
	 * in the original string and go from there.
	 */
	npath = NULL;
	if ((pathcopy = strdup(path)) == NULL)
		goto done;
	rpathlen = strlen(rpath);
	assert(rpathlen <= pathlen);
	if (rpathlen > 1 && strncmp(pathcopy, rpath, rpathlen) == 0 &&
	    (pathcopy[rpathlen] == '\0' || pathcopy[rpathlen] == '/')) {
		/* same string, start at last slash in path */
		if (pathcopy[rpathlen] == '\0') {
			next = rindex(pathcopy, '/');
			assert(next != NULL);
		}
		/* rpath is a prefix, start at last slash in that prefix */
		else {
			next = rindex(rpath, '/');
			assert(next != NULL);
			next = &pathcopy[next-rpath];
		}
	}
	/* rpath is not a prefix, must scan entire path */
	else {
		next = pathcopy;
	}
	assert(next >= pathcopy && next < &pathcopy[pathlen]);
	assert(*next == '/');
	next++;
	if (debug > 1)
		info(" pathscan: path='%s', rpath='%s', start at '%s'",
		     pathcopy, rpath, next);

	while ((ep = index(next, '/')) != NULL) {
		*ep = '\0';
		if (debug > 1)
			info(" testing: %s", pathcopy);

		/*
		 * We use realpath here instead of just stat to make
		 * sure someone isn't actively tweaking the filesystem
		 * and messing with our head.
		 *
		 * If realpath fails, it should fail with ENOENT and
		 * the realpath-ified part should fall in our directory.
		 * Otherwise, someone really is playing games.
		 */
		if (myrealpath(pathcopy, rpath) == NULL) {
			if (errno != ENOENT ||
			    (dir && !INDIR(dir, dirlen, rpath))) {
				if (debug > 1)
					info("  resolves bad (%s)\n",
					     rpath);
				goto done;
			}
			/*
			 * We have hit a missing component.
			 * Create the component and carry on.
			 */
			if (mkdir(rpath, cmask) < 0) {
				if (debug > 1)
					info("  create failed (%s)\n",
					     rpath);
				goto done;
			}
			if (debug > 1)
				info("  created (%s)", rpath);
		} else {
			if (debug > 1)
				info("  exists (%s)", rpath);
			/*
			 * Update the creation permission; see comment above.
			 */
			if (stat(rpath, &sb) < 0) {
				if (debug > 1)
					info(" stat failed (%s)?!", rpath);
				goto done;
			}
			cmask = (sb.st_mode & (S_IRWXU|S_IRWXG|S_IRWXO)) |
				S_IRWXU;
		}
		*ep = '/';
		next = ep+1;
	}

	/*
	 * We are down to the final component of the original path.
	 * It should either exist and be a regular file or not
	 * exist at all.
	 */
	if (myrealpath(pathcopy, rpath) == NULL &&
	    (errno != ENOENT || !INDIR(dir, dirlen, rpath))) {
		if (debug > 1)
			info(" final resolved path (%s) bad (%d)",
			     rpath, errno);
		goto done;
	}

	/*
	 * We are in our happy place. rpath is the canonicalized path.
	 */
	npath = strdup(rpath);
	if (debug > 1)
		info("resolvepath: '%s' resolved to '%s'", path, npath);

 done:
	umask(omask);
	if (pathcopy)
		free(pathcopy);
	return npath;
}