Skip to content

Commit

Permalink
Add guards to some functions that modify object headers.
Browse files Browse the repository at this point in the history
  • Loading branch information
rbehrends committed Feb 7, 2020
1 parent 1d541f9 commit 3af3664
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 27 deletions.
6 changes: 6 additions & 0 deletions src/boehm_gc.c
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,9 @@ void RetypeBagIntern(Bag bag, UInt new_type)
if (old_type == new_type)
return;

#if defined(HPCGAP) && defined(USE_HPC_GUARDS)
WriteGuard(bag);
#endif
/* change the size-type word */
header->type = new_type;
{
Expand Down Expand Up @@ -456,6 +459,9 @@ UInt ResizeBag(Bag bag, UInt new_size)
CollectBags(0, 0);
#endif

#if defined(HPCGAP) && defined(USE_HPC_GUARDS)
WriteGuard(bag);
#endif
BagHeader * header = BAG_HEADER(bag);

/* get type and old size of the bag */
Expand Down
67 changes: 40 additions & 27 deletions src/gasman.h
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,46 @@ EXPORT_INLINE UInt TNUM_BAG(Bag bag)
return BAG_HEADER(bag)->type;
}

/****************************************************************************
**
*F ReadCheck(<bag>) . . . . . . . . . . . . . is the bag readable in HPCGAP?
*F WriteCheck(<bag>) . . . . . . . . . . . . is the bag writable in HPCGAP?
*F HandleReadGuardError(<bag>) . . . . . . . . . . signal read access error
*F HandleWriteGuardError(<bag>) . . . . . . . . . signal write access error
**
** These funtion handle access checks in HPCGAP, i.e. whether a bag can
** safely be read or modified without causing race conditions. These are
** called in functions such as PTR_BAG() and CONST_PTR_BAG(), which should
** implicitly or explicitly guard all object accesses. In order to access
** objects without triggering checks, alternative functions UNSAFE_PTR_BAG()
** and UNSAFE_CONST_PTR_BAG() are provided.
*/
#ifdef HPCGAP
extern int ExtendedGuardCheck(Bag) PURE_FUNC;

EXPORT_INLINE int ReadCheck(Bag bag)
{
Region *region;
region = REGION(bag);
if (!region)
return 1;
if (region->owner == GetTLS())
return 1;
if (region->readers[TLS(threadID)])
return 1;
return ExtendedGuardCheck(bag);
}

EXPORT_INLINE int WriteCheck(Bag bag)
{
Region * region;
region = REGION(bag);
return !region || region->owner == GetTLS() || ExtendedGuardCheck(bag);
}

extern void HandleReadGuardError(Bag) NORETURN;
extern void HandleWriteGuardError(Bag) NORETURN;
#endif // HPCGAP

/****************************************************************************
**
Expand Down Expand Up @@ -298,33 +338,6 @@ EXPORT_INLINE UInt SIZE_BAG_CONTENTS(const void *ptr) {
** the application must inform {\Gasman} that it has changed the bag, by
** calling 'CHANGED_BAG(old)' in the above example (see "CHANGED_BAG").
*/
#ifdef HPCGAP
extern int ExtendedGuardCheck(Bag) PURE_FUNC;

EXPORT_INLINE int ReadCheck(Bag bag)
{
Region *region;
region = REGION(bag);
if (!region)
return 1;
if (region->owner == GetTLS())
return 1;
if (region->readers[TLS(threadID)])
return 1;
return ExtendedGuardCheck(bag);
}

EXPORT_INLINE int WriteCheck(Bag bag)
{
Region * region;
region = REGION(bag);
return !region || region->owner == GetTLS() || ExtendedGuardCheck(bag);
}

extern void HandleReadGuardError(Bag) NORETURN;
extern void HandleWriteGuardError(Bag) NORETURN;
#endif // HPCGAP

EXPORT_INLINE Bag *PTR_BAG(Bag bag)
{
GAP_ASSERT(bag != 0);
Expand Down

0 comments on commit 3af3664

Please sign in to comment.