Generation numbers to catch write back of stale copies

Oops when a stale copy is written back, i.e. the processor was yielded
since the copy was made.  Such bugs are difficult to spot.  Sequence
numbers catch them when they do actual harm (they also catch different
bugs).  Generation numbers catch them even when they don't.

New ef_generation to count generations.  Call new ef_make_stale() to
increment it whenever the processor may be yielded.

New struct emptypedstr member generation.  To conserve space, make it
a bit-field of twelve bits, i.e. generations are only recorded modulo
2^12.  Make sure all members of unit empobj_storage share it.  It is
only used in copies; its value on disk and in the cache is
meaningless.  Copies with generation other than ef_generation are
stale.  Stale copies that are a multiple of 2^12 generations old can't
be detected, but that is sufficiently improbable.

Set generation to ef_generation by calling new ef_mark_fresh() when
making copies in ef_read() and ef_blank().  nav_ship() and
fltp_to_list() make copies without going through ef_read(), and
therefore need to call ef_mark_fresh() as well.  Also call it in
obj_changed() to make check_sect_ok() & friends freshen their argument
when it is unchanged.

New must_be_fresh() oopses when its argument is stale.  Call it in
ef_write() to catch write back of stale copies.
This commit is contained in:
Markus Armbruster 2009-12-31 13:14:36 +01:00
parent 358aee203e
commit 2fa5f65257
22 changed files with 93 additions and 3 deletions

View file

@ -44,6 +44,7 @@ struct comstr {
/* initial part must match struct empobj */
signed ef_type: 8;
unsigned com_seqno: 12;
unsigned com_generation: 12;
int com_uid;
time_t com_timestamp;
natid com_owner;

View file

@ -57,6 +57,7 @@ struct empobj {
*/
signed ef_type: 8;
unsigned seqno: 12;
unsigned generation: 12;
int uid;
time_t timestamp;
/* end of part matching struct emptypedstr */

View file

@ -87,6 +87,7 @@ struct empfile {
struct emptypedstr {
signed ef_type: 8;
unsigned seqno: 12;
unsigned generation: 12;
int uid;
time_t timestamp;
};
@ -203,6 +204,8 @@ enum {
extern struct castr *ef_cadef(int);
extern int ef_read(int, int, void *);
extern void ef_make_stale(void);
extern void ef_mark_fresh(int, void *);
extern void *ef_ptr(int, int);
extern char *ef_nameof(int);
extern time_t ef_mtime(int);

View file

@ -40,6 +40,7 @@ struct gamestr {
/* initial part must match struct empobj */
signed ef_type: 8;
unsigned game_seqno: 12;
unsigned game_generation: 12;
int game_uid;
time_t game_timestamp;
/* end of part matching struct empobj */

View file

@ -51,6 +51,7 @@ struct lndstr {
/* initial part must match struct empobj */
signed ef_type: 8;
unsigned lnd_seqno: 12;
unsigned lnd_generation: 12;
int lnd_uid; /* unit id (land #) */
time_t lnd_timestamp; /* Last time this unit was touched */
natid lnd_own; /* owner's country num */

View file

@ -44,6 +44,7 @@ struct lonstr {
/* initial part must match struct empobj */
signed ef_type: 8;
unsigned l_seqno: 12;
unsigned l_generation: 12;
int l_uid;
time_t l_timestamp;
/* end of part matching struct empobj */

View file

@ -41,6 +41,7 @@ struct loststr {
/* initial part must match struct empobj */
signed ef_type: 8;
unsigned lost_seqno: 12;
unsigned lost_generation: 12;
int lost_uid;
time_t lost_timestamp; /* When it was lost */
natid lost_owner; /* Who lost it */

View file

@ -72,6 +72,7 @@ struct realmstr {
/* initial part must match struct empobj */
signed ef_type: 8;
unsigned r_seqno: 12;
unsigned r_generation: 12;
int r_uid; /* realm table index */
time_t r_timestamp; /* Last time this realm was touched */
natid r_cnum; /* country number */
@ -85,6 +86,7 @@ struct natstr {
/* initial part must match struct empobj */
signed ef_type: 8;
unsigned nat_seqno: 12;
unsigned nat_generation: 12;
int nat_uid; /* equals nat_cnum */
time_t nat_timestamp;
natid nat_cnum; /* our country number */

View file

@ -44,6 +44,7 @@ struct nukstr {
/* initial part must match struct empobj */
signed ef_type: 8;
unsigned nuk_seqno: 12;
unsigned nuk_generation: 12;
int nuk_uid; /* unit id (nuke #) */
time_t nuk_timestamp; /* Last time this nuke was touched */
natid nuk_own;

View file

@ -48,6 +48,7 @@ struct plnstr {
/* initial part must match struct empobj */
signed ef_type: 8;
unsigned pln_seqno: 12;
unsigned pln_generation: 12;
int pln_uid; /* unit id (plane #) */
time_t pln_timestamp; /* Last time this plane was touched */
natid pln_own; /* owning country */

View file

@ -46,6 +46,7 @@ struct sctstr {
/* initial part must match struct empobj */
signed ef_type: 8;
unsigned sct_seqno: 12;
unsigned sct_generation: 12;
int sct_uid; /* equals XYOFFSET(sct_x, sct_y) */
time_t sct_timestamp; /* Last time this sector was written to */
natid sct_own; /* owner's country num */

View file

@ -65,6 +65,7 @@ struct shpstr {
/* initial part must match struct empobj */
signed ef_type: 8;
unsigned shp_seqno: 12;
unsigned shp_generation: 12;
int shp_uid; /* unit it (ship #) */
time_t shp_timestamp; /* Last time this ship was touched. */
natid shp_own; /* owner's country num */

View file

@ -44,6 +44,7 @@ struct trdstr {
/* initial part must match struct empobj */
signed ef_type: 8;
unsigned trd_seqno: 12;
unsigned trd_generation: 12;
int trd_uid;
time_t trd_timestamp;
natid trd_owner;

View file

@ -41,6 +41,7 @@ struct trtstr {
/* initial part must match struct empobj */
signed ef_type: 8;
unsigned trt_seqno: 12;
unsigned trt_generation: 12;
int trt_uid;
time_t trt_timestamp;
/* end of part matching struct empobj */

View file

@ -57,9 +57,12 @@ static int do_read(struct empfile *, void *, int, int);
static int do_write(struct empfile *, void *, int, int);
static unsigned get_seqno(struct empfile *, int);
static void new_seqno(struct empfile *, void *);
static void must_be_fresh(struct empfile *, void *);
static void do_blank(struct empfile *, void *, int, int);
static int ef_check(int);
static unsigned ef_generation;
/*
* Open the file-backed table TYPE (EF_SECTOR, ...).
* HOW are flags to control operation. Naturally, immutable flags are
@ -360,6 +363,7 @@ ef_read(int type, int id, void *into)
cachep = ep->cache + (id - ep->baseid) * ep->size;
}
memcpy(into, cachep, ep->size);
ef_mark_fresh(type, into);
if (ep->postread)
ep->postread(id, into);
@ -522,9 +526,11 @@ ef_write(int type, int id, void *from)
if (ep->onresize && ep->onresize(type) < 0)
return 0;
}
if (id >= ep->baseid && id < ep->baseid + ep->cids)
if (id >= ep->baseid && id < ep->baseid + ep->cids) {
cachep = ep->cache + (id - ep->baseid) * ep->size;
else
if (cachep != from)
must_be_fresh(ep, from);
} else
cachep = NULL;
if (ep->prewrite)
ep->prewrite(id, cachep, from);
@ -607,6 +613,35 @@ new_seqno(struct empfile *ep, void *buf)
elt->seqno = old_seqno + 1;
}
void
ef_make_stale(void)
{
ef_generation++;
}
void
ef_mark_fresh(int type, void *buf)
{
struct empfile *ep;
if (ef_check(type) < 0)
return;
ep = &empfile[type];
if (!(ep->flags & EFF_TYPED))
return;
((struct emptypedstr *)buf)->generation = ef_generation;
}
static void
must_be_fresh(struct empfile *ep, void *buf)
{
struct emptypedstr *elt = buf;
if (!(ep->flags & EFF_TYPED))
return;
CANT_HAPPEN(elt->generation != (ef_generation & 0xfff));
}
/*
* Extend table TYPE by COUNT elements.
* Any pointers obtained from ef_ptr() become invalid.
@ -684,6 +719,7 @@ ef_blank(int type, int id, void *buf)
elt = buf;
elt->seqno = get_seqno(ep, elt->uid);
}
ef_mark_fresh(type, buf);
}
/*

View file

@ -51,6 +51,7 @@
#include <unistd.h>
#include "empio.h"
#include "empthread.h"
#include "file.h"
#include "ioqueue.h"
#include "misc.h"
#include "queue.h"
@ -196,6 +197,9 @@ io_output(struct iop *iop, int wait)
struct iovec iov[16];
int n, res, cc;
if (wait)
ef_make_stale();
if (!ioq_qsize(iop->output))
return 0;

View file

@ -37,6 +37,7 @@
#include <signal.h>
#include <time.h>
#include "empthread.h"
#include "file.h"
#include "misc.h"
/* Flags that were passed to empth_init() */
@ -65,6 +66,7 @@ empth_create(void (*entry)(void *), int size, int flags,
{
if (!flags)
flags = empth_flags;
ef_make_stale();
return lwpCreate(1, entry, size, flags, name, 0, NULL, ud);
}
@ -89,18 +91,21 @@ empth_set_name(empth_t *thread, char *name)
void
empth_exit(void)
{
ef_make_stale();
lwpExit();
}
void
empth_yield(void)
{
ef_make_stale();
lwpYield();
}
int
empth_select(int fd, int flags, struct timeval *timeout)
{
ef_make_stale();
return lwpSleepFd(fd, flags, timeout);
}
@ -113,6 +118,7 @@ empth_wakeup(empth_t *a)
int
empth_sleep(time_t until)
{
ef_make_stale();
return lwpSleepUntil(until);
}
@ -123,6 +129,7 @@ empth_wait_for_signal(void)
int sig, err;
time_t now;
ef_make_stale();
sigemptyset(&set);
sigaddset(&set, SIGHUP);
sigaddset(&set, SIGINT);
@ -153,12 +160,14 @@ empth_rwlock_destroy(empth_rwlock_t *rwlock)
void
empth_rwlock_wrlock(empth_rwlock_t *rwlock)
{
ef_make_stale();
lwp_rwlock_wrlock(rwlock);
}
void
empth_rwlock_rdlock(empth_rwlock_t *rwlock)
{
ef_make_stale();
lwp_rwlock_rdlock(rwlock);
}

View file

@ -56,6 +56,7 @@
#include <process.h>
#include "misc.h"
#include "empthread.h"
#include "file.h"
#include "prototypes.h"
#include "server.h"
#include "sys/socket.h"
@ -423,6 +424,7 @@ empth_create(void (*entry)(void *), int size, int flags,
empth_t *pThread = NULL;
loc_debug("creating new thread %s", name);
ef_make_stale();
pThread = malloc(sizeof(*pThread));
if (!pThread) {
@ -501,6 +503,7 @@ empth_exit(void)
empth_t *pThread = TlsGetValue(dwTLSIndex);
loc_debug("empth_exit");
ef_make_stale();
loc_BlockThisThread();
TlsSetValue(dwTLSIndex, NULL);
@ -516,6 +519,7 @@ empth_exit(void)
void
empth_yield(void)
{
ef_make_stale();
loc_BlockThisThread();
Sleep(0);
loc_RunThisThread(NULL);
@ -541,6 +545,7 @@ empth_select(int fd, int flags, struct timeval *timeout)
loc_debug("%s select on %d",
flags == EMPTH_FD_READ ? "read" : "write", fd);
ef_make_stale();
loc_BlockThisThread();
hEventObject[0] = WSACreateEvent();
@ -615,6 +620,7 @@ empth_sleep(time_t until)
empth_t *pThread = TlsGetValue(dwTLSIndex);
DWORD result;
ef_make_stale();
loc_BlockThisThread();
do {
@ -645,6 +651,7 @@ empth_request_shutdown(void)
int
empth_wait_for_signal(void)
{
ef_make_stale();
loc_BlockThisThread();
loc_RunThisThread(hShutdownEvent);
return SIGTERM;
@ -696,6 +703,7 @@ empth_rwlock_destroy(empth_rwlock_t *rwlock)
void
empth_rwlock_wrlock(empth_rwlock_t *rwlock)
{
ef_make_stale();
/* block any new readers */
ResetEvent(rwlock->can_read);
rwlock->nwrite++;
@ -707,6 +715,7 @@ empth_rwlock_wrlock(empth_rwlock_t *rwlock)
void
empth_rwlock_rdlock(empth_rwlock_t *rwlock)
{
ef_make_stale();
loc_BlockThisThread();
loc_RunThisThread(rwlock->can_read);
ResetEvent(rwlock->can_write);

View file

@ -51,6 +51,7 @@
#include <unistd.h>
#include "misc.h"
#include "empthread.h"
#include "file.h"
#include "prototypes.h"
struct empth_t {
@ -187,6 +188,7 @@ empth_create(void (*entry)(void *), int size, int flags,
int eno;
empth_status("creating new thread %s", name);
ef_make_stale();
ctx = malloc(sizeof(empth_t));
if (!ctx) {
@ -262,6 +264,7 @@ empth_exit(void)
empth_t *ctx = pthread_getspecific(ctx_key);
empth_status("empth_exit");
ef_make_stale();
pthread_mutex_unlock(&mtx_ctxsw);
free(ctx->name);
free(ctx);
@ -271,6 +274,7 @@ empth_exit(void)
void
empth_yield(void)
{
ef_make_stale();
pthread_mutex_unlock(&mtx_ctxsw);
pthread_mutex_lock(&mtx_ctxsw);
empth_restorectx();
@ -286,6 +290,7 @@ empth_select(int fd, int flags, struct timeval *timeout)
empth_t *ctx;
int res = 0;
ef_make_stale();
pthread_mutex_unlock(&mtx_ctxsw);
empth_status("select on %d for %d", fd, flags);
@ -353,6 +358,7 @@ empth_sleep(time_t until)
struct timeval tv;
int res;
ef_make_stale();
pthread_mutex_unlock(&mtx_ctxsw);
do {
now = time(NULL);
@ -373,6 +379,7 @@ empth_wait_for_signal(void)
sigset_t set;
int sig, err;
ef_make_stale();
sigemptyset(&set);
sigaddset(&set, SIGHUP);
sigaddset(&set, SIGINT);
@ -426,6 +433,7 @@ empth_rwlock_wrlock(empth_rwlock_t *rwlock)
{
empth_status("wrlock %s %d %d",
rwlock->name, rwlock->nread, rwlock->nwrite);
ef_make_stale();
rwlock->nwrite++;
while (rwlock->nread != 0 || rwlock->nwrite != 1) {
empth_status("waiting for wrlock %s", rwlock->name);
@ -441,6 +449,7 @@ empth_rwlock_rdlock(empth_rwlock_t *rwlock)
{
empth_status("rdlock %s %d %d",
rwlock->name, rwlock->nread, rwlock->nwrite);
ef_make_stale();
while (rwlock->nwrite) {
empth_status("waiting for rdlock %s", rwlock->name);
pthread_cond_wait(&rwlock->can_read, &mtx_ctxsw);

View file

@ -53,7 +53,11 @@ obj_changed(struct empobj *obj, size_t sz)
get_empobj(obj->ef_type, obj->uid, &old);
memcpy(&tobj, obj, sz);
old.gen.timestamp = tobj.gen.timestamp = 0;
return memcmp(&tobj, &old, sz);
old.gen.generation = tobj.gen.generation = 0;
if (memcmp(&tobj, &old, sz))
return 1;
ef_mark_fresh(obj->ef_type, obj);
return 0;
}
int

View file

@ -268,6 +268,7 @@ nav_ship(struct shpstr *sp)
mlp = malloc(sizeof(struct ulist));
mlp->chrp = (struct empobj_chr *)(mchr + sp->shp_type);
mlp->unit.ship = *sp;
ef_mark_fresh(EF_SHIP, &mlp->unit.ship);
mlp->mobil = sp->shp_mobil;
emp_insque(&mlp->queue, &ship_list);

View file

@ -347,6 +347,7 @@ fltp_to_list(struct fltheadstr *fltp, struct emp_qelem *list)
sp = getshipp(fe->num);
mlp->chrp = (struct empobj_chr *)(mchr + sp->shp_type);
mlp->unit.ship = *sp;
ef_mark_fresh(EF_SHIP, &mlp->unit.ship);
mlp->mobil = fe->mobil;
emp_insque(&mlp->queue, list);
}