From 75c32f3a99b4189225402f143e71d78cfb177c58 Mon Sep 17 00:00:00 2001 From: Rodrigo Arias Date: Tue, 7 Dec 2021 19:52:48 +0100 Subject: [PATCH] Transform all aserts into unconditional checks --- chan.c | 65 ++++++++++++++------------- emu.c | 123 ++++++++++++++++++++++++++++----------------------- emu_kernel.c | 5 ++- emu_nanos6.c | 11 +++-- emu_nosv.c | 90 ++++++++++++++++++++++++++----------- emu_openmp.c | 10 +++-- emu_ovni.c | 102 ++++++++++++++++++++++++++++++------------ emu_tampi.c | 5 ++- ovni.c | 1 - ovnisync.c | 1 - prv.c | 17 +++---- sort.c | 20 +++++---- trace.c | 11 ++--- 13 files changed, 285 insertions(+), 176 deletions(-) diff --git a/chan.c b/chan.c index d6612e3..a6792ac 100644 --- a/chan.c +++ b/chan.c @@ -17,8 +17,6 @@ #include "chan.h" -#include - #include "emu.h" #include "prv.h" #include "utlist.h" @@ -43,8 +41,11 @@ chan_init(struct ovni_chan *chan, enum chan_track track, int row, int type, FILE static void mark_dirty(struct ovni_chan *chan, enum chan_dirty dirty) { - assert(chan->dirty == CHAN_CLEAN); - assert(dirty != CHAN_CLEAN); + if(chan->dirty != CHAN_CLEAN) + die("mark_dirty: chan %d already dirty\n", chan->id); + + if(dirty == CHAN_CLEAN) + die("mark_dirty: cannot use CHAN_CLEAN\n"); dbg("adding dirty chan %d to list\n", chan->id); chan->dirty = dirty; @@ -170,14 +171,12 @@ chan_set(struct ovni_chan *chan, int st) dbg("chan_set chan %d st=%d\n", chan->id, st); - assert(chan->enabled); + if(!chan->enabled) + die("chan_set: chan %d not enabled\n", chan->id); /* Only chan_set can set the 0 state */ if(st < 0) - { - err("chan_set: cannot set a negative state %d\n", st); - abort(); - } + die("chan_set: cannot set a negative state %d\n", st); /* Don't enforce this check if we are dirty because the channel was * just enabled; it may collide with a new state 0 set via chan_set() @@ -222,7 +221,8 @@ chan_push(struct ovni_chan *chan, int st) { dbg("chan_push chan %d st=%d\n", chan->id, st); - assert(chan->enabled); + if(!chan->enabled) + die("chan_push: chan %d not enabled\n", chan->id); if(st <= 0) { @@ -231,7 +231,8 @@ chan_push(struct ovni_chan *chan, int st) } /* Cannot be dirty */ - assert(chan->dirty == 0); + if(chan->dirty != CHAN_CLEAN) + die("chan_push: chan %d not clean", chan->id); if(chan->lastst >= 0 && chan->lastst == st) { @@ -259,10 +260,12 @@ chan_pop(struct ovni_chan *chan, int expected_st) dbg("chan_pop chan %d expected_st=%d\n", chan->id, expected_st); - assert(chan->enabled); + if(!chan->enabled) + die("chan_pop: chan %d not enabled\n", chan->id); /* Cannot be dirty */ - assert(chan->dirty == 0); + if(chan->dirty != CHAN_CLEAN) + die("chan_pop: chan %d not clean", chan->id); if(chan->n <= 0) { @@ -310,23 +313,19 @@ chan_ev(struct ovni_chan *chan, int ev) { dbg("chan_ev chan %d ev=%d\n", chan->id, ev); - assert(chan->enabled); + if(!chan->enabled) + die("chan_ev: chan %d not enabled\n", chan->id); /* Cannot be dirty */ - assert(chan->dirty == 0); + if(chan->dirty != CHAN_CLEAN) + die("chan_ev: chan %d is dirty\n", chan->id); if(ev <= 0) - { - err("chan_ev: cannot emit non-positive state %d\n", ev); - abort(); - } + die("chan_ev: cannot emit non-positive state %d\n", ev); if(chan->lastst >= 0 && chan->lastst == ev) - { - err("chan_ev id=%d cannot emit the state %d twice\n", + die("chan_ev id=%d cannot emit the state %d twice\n", chan->id, ev); - abort(); - } chan->ev = ev; chan->t = *chan->clock; @@ -343,14 +342,17 @@ chan_get_st(struct ovni_chan *chan) if(chan->n == 0) return 0; - assert(chan->n > 0); + if(chan->n < 0) + die("chan_get_st: chan %d has negative n\n", chan->id); + return chan->stack[chan->n-1]; } static void emit(struct ovni_chan *chan, int64_t t, int state) { - assert(chan->dirty != CHAN_CLEAN); + if(chan->dirty == CHAN_CLEAN) + die("emit: chan %d is not dirty\n", chan->id); /* A channel can only emit the same state as lastst if is dirty because * it has been enabled or disabled. Otherwise is a bug (ie. you have two @@ -360,9 +362,8 @@ emit(struct ovni_chan *chan, int64_t t, int state) && chan->lastst == state) { /* TODO: Print the raw clock of the offending event */ - err("chan: id=%d cannot emit the same state %d twice\n", + die("emit: chan %d cannot emit the same state %d twice\n", chan->id, state); - abort(); } if(chan->lastst != state) @@ -378,8 +379,11 @@ emit_ev(struct ovni_chan *chan) { int new, last; - assert(chan->enabled); - assert(chan->ev != -1); + if(!chan->enabled) + die("emit_ev: chan %d is not enabled\n", chan->id); + + if(chan->ev == -1) + die("emit_ev: chan %d cannot emit -1 ev\n", chan->id); new = chan->ev; last = chan_get_st(chan); @@ -413,7 +417,8 @@ chan_emit(struct ovni_chan *chan) if(chan->enabled == 0) { /* No punctual events allowed when disabled */ - assert(chan->ev == -1); + if(chan->ev != -1) + die("chan_emit: no punctual event allowed when disabled\n"); emit_st(chan); goto shower; diff --git a/emu.c b/emu.c index eb8c31b..28fba74 100644 --- a/emu.c +++ b/emu.c @@ -26,7 +26,6 @@ #include #include #include -#include #include #include @@ -85,8 +84,6 @@ cpu_update_tracking_chan(struct ovni_chan *cpu_chan, struct ovni_ethread *th) int th_enabled, cpu_enabled, st; struct ovni_chan *th_chan; - assert(th); - switch (cpu_chan->track) { case CHAN_TRACK_TH_RUNNING: @@ -179,7 +176,8 @@ emu_cpu_update_chan(struct ovni_cpu *cpu, struct ovni_chan *cpu_chan) } else if(count == 1) { - assert(th); + if(th == NULL) + die("emu_cpu_update_chan: tracking thread is NULL\n"); /* A unique thread found: copy the state */ dbg("cpu_update_chan: unique thread %d found, updating chan %d\n", @@ -210,7 +208,8 @@ propagate_channels(struct ovni_emu *emu) DL_FOREACH_SAFE(emu->th_chan, th_chan, tmp) { - assert(th_chan->thread); + if(th_chan->thread == NULL) + die("propagate_channels: channel thread is NULL\n"); thread = th_chan->thread; @@ -241,14 +240,12 @@ emit_channels(struct ovni_emu *emu) DL_FOREACH(emu->th_chan, th_chan) { dbg("emu emits th chan %d\n", th_chan->id); - //assert(th_chan->dirty); chan_emit(th_chan); } DL_FOREACH(emu->cpu_chan, cpu_chan) { dbg("emu emits cpu chan %d\n", cpu_chan->id); - //assert(cpu_chan->dirty); chan_emit(cpu_chan); } @@ -406,7 +403,8 @@ next_event(struct ovni_emu *emu) stream = heap_elem(node, struct ovni_stream, hh); - assert(stream); + if(stream == NULL) + die("next_event: heap_elem returned NULL\n"); set_current(emu, stream); @@ -433,7 +431,7 @@ next_event(struct ovni_emu *emu) emu->lastclock, stream->lastclock, stream->tid); if(emu->enable_linter) - exit(EXIT_FAILURE); + abort(); } emu->lastclock = stream->lastclock; @@ -470,11 +468,13 @@ emu_load_first_events(struct ovni_emu *emu) if(emu_step_stream(emu, stream) < 0) { - dbg("warning: empty stream for tid %d\n", stream->tid); + err("warning: empty stream for tid %d\n", stream->tid); + + if(emu->enable_linter) + abort(); + continue; } - - assert(stream->active); } } @@ -545,13 +545,12 @@ add_new_cpu(struct ovni_emu *emu, struct ovni_loom *loom, int i, int phyid) cpu = &loom->cpu[i]; if(i < 0 || i >= (int) loom->ncpus) - { - err("CPU with index %d in loom %s is out of bounds\n", i, - loom->hostname); - abort(); - } + die("CPU with index %d in loom %s is out of bounds\n", + i, loom->hostname); - assert(cpu->state == CPU_ST_UNKNOWN); + if(cpu->state != CPU_ST_UNKNOWN) + die("new cpu %d in unexpected state in loom %s\n", + i, loom->hostname); cpu->state = CPU_ST_READY; cpu->i = i; @@ -562,56 +561,49 @@ add_new_cpu(struct ovni_emu *emu, struct ovni_loom *loom, int i, int phyid) dbg("new cpu %d at phyid=%d\n", cpu->gindex, phyid); } -static void -proc_load_cpus(struct ovni_emu *emu, struct ovni_loom *loom, struct ovni_eproc *proc) +static int +proc_load_cpus(struct ovni_emu *emu, struct ovni_loom *loom, + struct ovni_eproc *proc, + struct ovni_eproc *metadata_proc) { - JSON_Array *cpuarray; - JSON_Object *cpu; - JSON_Object *meta; - size_t i, index, phyid; - struct ovni_cpu *vcpu; + JSON_Object *meta = json_value_get_object(proc->meta); + if(meta == NULL) + die("json_value_get_object() failed\n"); - meta = json_value_get_object(proc->meta); - - assert(meta); - - cpuarray = json_object_get_array(meta, "cpus"); + JSON_Array *cpuarray = json_object_get_array(meta, "cpus"); /* This process doesn't have the cpu list */ if(cpuarray == NULL) - return; + return -1; - assert(loom->ncpus == 0); + if(metadata_proc) + die("duplicated metadata for proc %d and %d in loom %s\n", + metadata_proc->pid, proc->pid, + loom->hostname); + + if(loom->ncpus != 0) + die("loom %s already has CPUs\n", loom->hostname); loom->ncpus = json_array_get_count(cpuarray); - if(loom->ncpus <= 0) - { - err("loom %s proc %d has read %ld cpus, but required > 0\n", - loom->hostname, proc->pid, loom->ncpus); - abort(); - } + if(loom->ncpus == 0) + die("loom %s proc %d has metadata but no CPUs\n", + loom->hostname, proc->pid); loom->cpu = calloc(loom->ncpus, sizeof(struct ovni_cpu)); if(loom->cpu == NULL) - { - perror("calloc failed"); - abort(); - } + die("calloc failed: %s\n", strerror(errno)); - for(i = 0; i < loom->ncpus; i++) + for(size_t i = 0; i < loom->ncpus; i++) { - cpu = json_array_get_object(cpuarray, i); + JSON_Object *cpu = json_array_get_object(cpuarray, i); if(cpu == NULL) - { - err("json_array_get_object returned NULL\n"); - abort(); - } + die("proc_load_cpus: json_array_get_object() failed\n"); - index = (int) json_object_get_number(cpu, "index"); - phyid = (int) json_object_get_number(cpu, "phyid"); + size_t index = (int) json_object_get_number(cpu, "index"); + size_t phyid = (int) json_object_get_number(cpu, "phyid"); add_new_cpu(emu, loom, index, phyid); } @@ -619,8 +611,10 @@ proc_load_cpus(struct ovni_emu *emu, struct ovni_loom *loom, struct ovni_eproc * /* If we reach this point, all CPUs are in the ready state */ /* Init the vcpu as well */ - vcpu = &loom->vcpu; - assert(vcpu->state == CPU_ST_UNKNOWN); + struct ovni_cpu *vcpu = &loom->vcpu; + if(vcpu->state != CPU_ST_UNKNOWN) + die("unexpected virtual CPU state in loom %s\n", + loom->hostname); vcpu->state = CPU_ST_READY; vcpu->i = -1; @@ -629,6 +623,8 @@ proc_load_cpus(struct ovni_emu *emu, struct ovni_loom *loom, struct ovni_eproc * vcpu->loom = loom; dbg("new vcpu %d\n", vcpu->gindex); + + return 0; } /* Obtain CPUs in the metadata files and other data */ @@ -647,16 +643,29 @@ load_metadata(struct ovni_emu *emu) loom = &trace->loom[i]; loom->offset_ncpus = emu->total_ncpus; + struct ovni_eproc *metadata_proc = NULL; + for(j=0; jnprocs; j++) { proc = &loom->proc[j]; - proc_load_cpus(emu, loom, proc); + if(proc_load_cpus(emu, loom, proc, metadata_proc) < 0) + continue; + + if(metadata_proc) + die("duplicated metadata found in pid %d and %d\n", + metadata_proc->pid, + proc->pid); + + metadata_proc = proc; } /* One of the process must have the list of CPUs */ - /* FIXME: The CPU list should be at the loom dir */ - assert(loom->ncpus > 0); + if(metadata_proc == NULL) + die("no metadata found in loom %s\n", loom->hostname); + + if(loom->ncpus == 0) + die("no CPUs found in loom %s\n", loom->hostname); } } @@ -677,7 +686,9 @@ destroy_metadata(struct ovni_emu *emu) { proc = &loom->proc[j]; - assert(proc->meta); + if(proc->meta == NULL) + die("cannot destroy metadata: is NULL\n"); + json_value_free(proc->meta); } diff --git a/emu_kernel.c b/emu_kernel.c index f6f82a2..2c78787 100644 --- a/emu_kernel.c +++ b/emu_kernel.c @@ -15,7 +15,6 @@ * along with this program. If not, see . */ -#include #include "uthash.h" #include "ovni.h" @@ -91,7 +90,9 @@ context_switch(struct ovni_emu *emu) void hook_pre_kernel(struct ovni_emu *emu) { - assert(emu->cur_ev->header.model == 'K'); + if(emu->cur_ev->header.model != 'K') + die("hook_pre_kernel: unexpected event with model %c\n", + emu->cur_ev->header.model); switch(emu->cur_ev->header.category) { diff --git a/emu_nanos6.c b/emu_nanos6.c index d9dac4d..fa880c2 100644 --- a/emu_nanos6.c +++ b/emu_nanos6.c @@ -15,7 +15,6 @@ * along with this program. If not, see . */ -#include #include "uthash.h" #include "ovni.h" @@ -89,9 +88,13 @@ pre_subsystem(struct ovni_emu *emu, int st) void hook_pre_nanos6(struct ovni_emu *emu) { - // Ensure that the thread is running - assert(emu->cur_thread->is_running != 0); - assert(emu->cur_ev->header.model == 'L'); + if(emu->cur_ev->header.model != 'L') + die("hook_pre_nanos6: unexpected event with model %c\n", + emu->cur_ev->header.model); + + if(!emu->cur_thread->is_running) + die("hook_pre_nanos6: current thread %d not running\n", + emu->cur_thread->tid); switch(emu->cur_ev->header.category) { diff --git a/emu_nosv.c b/emu_nosv.c index 78e6af0..2a48677 100644 --- a/emu_nosv.c +++ b/emu_nosv.c @@ -15,7 +15,6 @@ * along with this program. If not, see . */ -#include #include "uthash.h" #include "ovni.h" @@ -119,9 +118,14 @@ pre_task_execute(struct ovni_emu *emu) HASH_FIND_INT(emu->cur_proc->tasks, &taskid, task); - assert(task != NULL); - assert(emu->cur_thread->state == TH_ST_RUNNING); - assert(emu->cur_thread->task == NULL); + if(task == NULL) + die("cannot find task with id %d\n", taskid); + + if(emu->cur_thread->state != TH_ST_RUNNING) + die("thread state is not running\n"); + + if(emu->cur_thread->task != NULL) + die("thread already has a task\n"); task->state = TASK_ST_RUNNING; task->thread = emu->cur_thread; @@ -142,11 +146,20 @@ pre_task_pause(struct ovni_emu *emu) HASH_FIND_INT(emu->cur_proc->tasks, &taskid, task); - assert(task != NULL); - assert(task->state == TASK_ST_RUNNING); - assert(emu->cur_thread->state == TH_ST_RUNNING); - assert(emu->cur_thread->task == task); - assert(emu->cur_thread == task->thread); + if(task == NULL) + die("cannot find task with id %d\n", taskid); + + if(task->state != TASK_ST_RUNNING) + die("task state is not running\n"); + + if(emu->cur_thread->state != TH_ST_RUNNING) + die("thread state is not running\n"); + + if(emu->cur_thread->task != task) + die("thread has assigned a different task\n"); + + if(emu->cur_thread != task->thread) + die("task is assigned to a different thread\n"); task->state = TASK_ST_PAUSED; @@ -165,11 +178,20 @@ pre_task_resume(struct ovni_emu *emu) HASH_FIND_INT(emu->cur_proc->tasks, &taskid, task); - assert(task != NULL); - assert(task->state == TASK_ST_PAUSED); - assert(emu->cur_thread->state == TH_ST_RUNNING); - assert(emu->cur_thread->task == task); - assert(emu->cur_thread == task->thread); + if(task == NULL) + die("cannot find task with id %d\n", taskid); + + if(task->state != TASK_ST_PAUSED) + die("task state is not paused\n"); + + if(emu->cur_thread->state != TH_ST_RUNNING) + die("thread is not running\n"); + + if(emu->cur_thread->task != task) + die("thread has assigned a different task\n"); + + if(emu->cur_thread != task->thread) + die("task is assigned to a different thread\n"); task->state = TASK_ST_RUNNING; @@ -189,11 +211,20 @@ pre_task_end(struct ovni_emu *emu) HASH_FIND_INT(emu->cur_proc->tasks, &taskid, task); - assert(task != NULL); - assert(task->state == TASK_ST_RUNNING); - assert(emu->cur_thread->state == TH_ST_RUNNING); - assert(emu->cur_thread->task == task); - assert(emu->cur_thread == task->thread); + if(task == NULL) + die("cannot find task with id %d\n", taskid); + + if(task->state != TASK_ST_RUNNING) + die("task state is not running\n"); + + if(emu->cur_thread->state != TH_ST_RUNNING) + die("thread is not running\n"); + + if(emu->cur_thread->task != task) + die("thread has assigned a different task\n"); + + if(emu->cur_thread != task->thread) + die("task is assigned to a different thread\n"); task->state = TASK_ST_DEAD; task->thread = NULL; @@ -213,9 +244,14 @@ pre_task_running(struct ovni_emu *emu, struct nosv_task *task) th = emu->cur_thread; proc = emu->cur_proc; - assert(task->id > 0); - assert(task->type_id > 0); - assert(proc->appid > 0); + if(task->id <= 0) + die("task id must be positive\n"); + + if(task->type_id <= 0) + die("task type id must be positive\n"); + + if(proc->appid <= 0) + die("app id must be positive\n"); chan_set(&th->chan[CHAN_NOSV_TASKID], task->id); chan_set(&th->chan[CHAN_NOSV_TYPEID], task->type_id); @@ -440,7 +476,6 @@ pre_ss(struct ovni_emu *emu, int st) th = emu->cur_thread; chan_th = &th->chan[CHAN_NOSV_SUBSYSTEM]; - assert(chan_th->id == CHAN_NOSV_SUBSYSTEM); dbg("pre_ss chan id %d st=%d\n", chan_th->id, st); switch(emu->cur_ev->header.value) @@ -481,10 +516,13 @@ check_affinity(struct ovni_emu *emu) void hook_pre_nosv(struct ovni_emu *emu) { - assert(emu->cur_ev->header.model == 'V'); + if(emu->cur_ev->header.model != 'V') + die("hook_pre_nosv: unexpected event with model %c\n", + emu->cur_ev->header.model); - /* Ensure the thread is active */ - assert(emu->cur_thread->is_active); + if(!emu->cur_thread->is_active) + die("hook_pre_nosv: current thread %d not active\n", + emu->cur_thread->tid); switch(emu->cur_ev->header.category) { diff --git a/emu_openmp.c b/emu_openmp.c index 16ec43b..6780d02 100644 --- a/emu_openmp.c +++ b/emu_openmp.c @@ -15,7 +15,6 @@ * along with this program. If not, see . */ -#include #include "uthash.h" #include "ovni.h" @@ -90,8 +89,13 @@ pre_mode(struct ovni_emu *emu, int st) void hook_pre_openmp(struct ovni_emu *emu) { - assert(emu->cur_ev->header.model == 'M'); - assert(emu->cur_thread->is_active); + if(emu->cur_ev->header.model != 'M') + die("hook_pre_openmp: unexpected event with model %c\n", + emu->cur_ev->header.model); + + if(!emu->cur_thread->is_active) + die("hook_pre_openmp: current thread %d not active\n", + emu->cur_thread->tid); switch(emu->cur_ev->header.category) { diff --git a/emu_ovni.c b/emu_ovni.c index f06aa29..cd4a3b6 100644 --- a/emu_ovni.c +++ b/emu_ovni.c @@ -21,8 +21,6 @@ #include "chan.h" #include "utlist.h" -#include - /* The emulator ovni module provides the execution model by tracking the thread * state and which threads run in each CPU */ @@ -79,7 +77,8 @@ chan_tracking_update(struct ovni_chan *chan, struct ovni_ethread *th) { int enabled; - assert(th); + if(th == NULL) + die("chan_tracking_update: thread is NULL"); switch (chan->track) { @@ -112,7 +111,9 @@ thread_set_state(struct ovni_ethread *th, enum ethread_state state) int i; /* The state must be updated when a cpu is set */ - assert(th->cpu); + if(th->cpu == NULL) + die("thread_set_state: thread %d doesn't have a CPU\n", + th->tid); dbg("thread_set_state: setting thread %d state %d\n", th->tid, state); @@ -187,12 +188,11 @@ update_cpu(struct ovni_cpu *cpu) struct ovni_cpu * emu_get_cpu(struct ovni_loom *loom, int cpuid) { - assert(cpuid < (int) loom->ncpus); + if(cpuid >= (int) loom->ncpus) + die("emu_get_cpu: CPU index out of bounds\n"); if(cpuid < 0) - { return &loom->vcpu; - } return &loom->cpu[cpuid]; } @@ -265,7 +265,10 @@ cpu_migrate_thread(struct ovni_cpu *cpu, static void thread_set_cpu(struct ovni_ethread *th, struct ovni_cpu *cpu) { - assert(th->cpu == NULL); + if(th->cpu != NULL) + die("thread_set_cpu: thread %d already has a CPU\n", + th->tid); + dbg("thread_set_cpu: setting thread %d cpu to %s\n", th->tid, cpu->name); th->cpu = cpu; @@ -279,7 +282,10 @@ thread_set_cpu(struct ovni_ethread *th, struct ovni_cpu *cpu) static void thread_unset_cpu(struct ovni_ethread *th) { - assert(th->cpu != NULL); + if(th->cpu == NULL) + die("thread_unset_cpu: thread %d doesn't have a CPU\n", + th->tid); + th->cpu = NULL; chan_enable(&th->chan[CHAN_OVNI_CPU], 0); @@ -290,10 +296,11 @@ thread_unset_cpu(struct ovni_ethread *th) static void thread_migrate_cpu(struct ovni_ethread *th, struct ovni_cpu *cpu) { - assert(th->cpu != NULL); + if(th->cpu == NULL) + die("thread_migrate_cpu: thread %d doesn't have a CPU\n", + th->tid); th->cpu = cpu; - assert(chan_is_enabled(&th->chan[CHAN_OVNI_CPU])); chan_set(&th->chan[CHAN_OVNI_CPU], cpu->gindex + 1); } @@ -304,7 +311,9 @@ pre_thread_execute(struct ovni_emu *emu, struct ovni_ethread *th) int cpuid; /* The thread cannot be already running */ - assert(th->state != TH_ST_RUNNING); + if(th->state == TH_ST_RUNNING) + die("pre_thread_execute: thread %d already running\n", + th->tid); cpuid = emu->cur_ev->payload.i32[0]; @@ -325,8 +334,13 @@ pre_thread_execute(struct ovni_emu *emu, struct ovni_ethread *th) static void pre_thread_end(struct ovni_ethread *th) { - assert(th->state == TH_ST_RUNNING); - assert(th->cpu); + if(th->state != TH_ST_RUNNING) + die("pre_thread_end: thread %d not running\n", + th->tid); + + if(th->cpu == NULL) + die("pre_thread_end: thread %d doesn't have a CPU\n", + th->tid); /* First update the thread state */ thread_set_state(th, TH_ST_DEAD); @@ -340,8 +354,13 @@ pre_thread_end(struct ovni_ethread *th) static void pre_thread_pause(struct ovni_ethread *th) { - assert(th->state == TH_ST_RUNNING || th->state == TH_ST_COOLING); - assert(th->cpu); + if(th->state != TH_ST_RUNNING && th->state != TH_ST_COOLING) + die("pre_thread_pause: thread %d not running or cooling\n", + th->tid); + + if(th->cpu == NULL) + die("pre_thread_pause: thread %d doesn't have a CPU\n", + th->tid); thread_set_state(th, TH_ST_PAUSED); update_cpu(th->cpu); @@ -350,8 +369,13 @@ pre_thread_pause(struct ovni_ethread *th) static void pre_thread_resume(struct ovni_ethread *th) { - assert(th->state == TH_ST_PAUSED || th->state == TH_ST_WARMING); - assert(th->cpu); + if(th->state != TH_ST_PAUSED && th->state != TH_ST_WARMING) + die("pre_thread_resume: thread %d not paused or warming\n", + th->tid); + + if(th->cpu == NULL) + die("pre_thread_resume: thread %d doesn't have a CPU\n", + th->tid); thread_set_state(th, TH_ST_RUNNING); update_cpu(th->cpu); @@ -360,8 +384,13 @@ pre_thread_resume(struct ovni_ethread *th) static void pre_thread_cool(struct ovni_ethread *th) { - assert(th->state == TH_ST_RUNNING); - assert(th->cpu); + if(th->state != TH_ST_RUNNING) + die("pre_thread_cool: thread %d not running\n", + th->tid); + + if(th->cpu == NULL) + die("pre_thread_cool: thread %d doesn't have a CPU\n", + th->tid); thread_set_state(th, TH_ST_COOLING); update_cpu(th->cpu); @@ -370,8 +399,13 @@ pre_thread_cool(struct ovni_ethread *th) static void pre_thread_warm(struct ovni_ethread *th) { - assert(th->state == TH_ST_PAUSED); - assert(th->cpu); + if(th->state != TH_ST_PAUSED) + die("pre_thread_warm: thread %d not paused\n", + th->tid); + + if(th->cpu == NULL) + die("pre_thread_warm: thread %d doesn't have a CPU\n", + th->tid); thread_set_state(th, TH_ST_WARMING); update_cpu(th->cpu); @@ -421,10 +455,13 @@ pre_affinity_set(struct ovni_emu *emu) th = emu->cur_thread; cpuid = emu->cur_ev->payload.i32[0]; - assert(th->cpu); - assert(th->state == TH_ST_RUNNING - || th->state == TH_ST_COOLING - || th->state == TH_ST_WARMING); + if(th->cpu == NULL) + die("pre_affinity_set: thread %d doesn't have a CPU\n", + th->tid); + + if(!th->is_active) + die("pre_affinity_set: thread %d is not active\n", + th->tid); /* Migrate current cpu to the one at cpuid */ newcpu = emu_get_cpu(emu->cur_loom, cpuid); @@ -483,11 +520,18 @@ pre_affinity_remote(struct ovni_emu *emu) } /* The remote_th cannot be in states dead or unknown */ - assert(remote_th->state != TH_ST_DEAD - && remote_th->state != TH_ST_UNKNOWN); + if(remote_th->state == TH_ST_DEAD) + die("pre_affinity_remote: remote thread %d in state DEAD\n", + remote_th->tid); + + if(remote_th->state == TH_ST_UNKNOWN) + die("pre_affinity_remote: remote thread %d in state UNKNOWN\n", + remote_th->tid); /* It must have an assigned CPU */ - assert(remote_th->cpu); + if(remote_th->cpu == NULL) + die("pre_affinity_remote: remote thread %d has no CPU\n", + remote_th->tid); /* Migrate current cpu to the one at cpuid */ newcpu = emu_get_cpu(emu->cur_loom, cpuid); diff --git a/emu_tampi.c b/emu_tampi.c index a643611..3a2833f 100644 --- a/emu_tampi.c +++ b/emu_tampi.c @@ -15,7 +15,6 @@ * along with this program. If not, see . */ -#include #include "uthash.h" #include "ovni.h" @@ -86,7 +85,9 @@ pre_tampi_mode(struct ovni_emu *emu, int state) void hook_pre_tampi(struct ovni_emu *emu) { - assert(emu->cur_ev->header.model == 'T'); + if(emu->cur_ev->header.model != 'T') + die("hook_pre_tampi: unexpected event with model %c\n", + emu->cur_ev->header.model); switch(emu->cur_ev->header.category) { diff --git a/ovni.c b/ovni.c index bf5c1e2..a68f3e7 100644 --- a/ovni.c +++ b/ovni.c @@ -43,7 +43,6 @@ # define gettid() ((pid_t)syscall(SYS_gettid)) #endif -/* Prevent usage of assert() */ #pragma GCC poison assert /* Data per process */ diff --git a/ovnisync.c b/ovnisync.c index 589124f..01bf30c 100644 --- a/ovnisync.c +++ b/ovnisync.c @@ -24,7 +24,6 @@ #include #include #include -#include #include #include "ovni.h" diff --git a/prv.c b/prv.c index d7104f1..330895d 100644 --- a/prv.c +++ b/prv.c @@ -16,7 +16,6 @@ */ #include -#include #include "ovni.h" #include "emu.h" @@ -56,18 +55,20 @@ prv_ev_cpu(struct ovni_emu *emu, int row, int type, int val) void prv_ev_autocpu_raw(struct ovni_emu *emu, int64_t time, int type, int val) { - int row; - struct ovni_cpu *cpu; + if(emu->cur_thread == NULL) + die("prv_ev_autocpu_raw: current thread is NULL\n"); - assert(emu->cur_thread); + struct ovni_cpu *cpu = emu->cur_thread->cpu; - cpu = emu->cur_thread->cpu; + if(cpu == NULL) + die("prv_ev_autocpu_raw: current thread CPU is NULL\n"); - assert(cpu); - assert(cpu->i >= 0); + /* FIXME: Use the global index of the CPUs */ + if(cpu->i < 0) + die("prv_ev_autocpu_raw: CPU index is negative\n"); /* Begin at 1 */ - row = emu->cur_loom->offset_ncpus + cpu->i + 1; + int row = emu->cur_loom->offset_ncpus + cpu->i + 1; prv_ev_cpu_raw(emu, row, time, type, val); } diff --git a/sort.c b/sort.c index e9e0a2b..cff3544 100644 --- a/sort.c +++ b/sort.c @@ -162,16 +162,19 @@ sort_buf(uint8_t *src, uint8_t *buf, int64_t bufsize, if((uint8_t *) ev == srcnext) break; - assert((uint8_t *) ev < srcnext); + if((uint8_t *) ev > srcnext) + die("exceeded srcnext while sorting\n"); + + if(bufsize < evsize) + die("no space left in the sort buffer\n"); - assert(bufsize >= evsize); memcpy(buf, ev, evsize); buf += evsize; bufsize -= evsize; injected++; } - err("injected %ld events in the past\n", injected); + dbg("injected %ld events in the past\n", injected); } static int @@ -193,14 +196,12 @@ execute_sort_plan(struct sortplan *sp) /* Allocate a working buffer */ bufsize = ((int64_t) sp->next) - ((int64_t) sp->first); - assert(bufsize > 0); + if(bufsize <= 0) + die("bufsize is non-positive\n"); buf = malloc(bufsize); if(!buf) - { - perror("malloc failed"); - abort(); - } + die("malloc failed: %s\n", strerror(errno)); sort_buf((uint8_t *) sp->first, buf, bufsize, (uint8_t *) sp->bad0, (uint8_t *) sp->next); @@ -308,7 +309,8 @@ process_trace(struct ovni_trace *trace) ring.size = max_look_back; ring.ev = malloc(ring.size * sizeof(struct ovni_ev *)); - assert(ring.ev); + if(ring.ev == NULL) + die("malloc failed: %s\n", strerror(errno)); for(i=0; instreams; i++) { diff --git a/trace.c b/trace.c index ba0fd9a..3fce5b4 100644 --- a/trace.c +++ b/trace.c @@ -13,7 +13,6 @@ #include #include #include -#include #include #include #include @@ -103,7 +102,8 @@ load_proc_metadata(struct ovni_eproc *proc) JSON_Object *meta; meta = json_value_get_object(proc->meta); - assert(meta); + if(meta == NULL) + die("load_proc_metadata: json_value_get_object() failed\n"); proc->appid = (int) json_object_get_number(meta, "app_id"); } @@ -551,6 +551,10 @@ ovni_load_next_event(struct ovni_stream *stream) size = ovni_ev_size(stream->cur_ev); stream->offset += size; + /* It cannot overflow, otherwise we are reading garbage */ + if(stream->offset > stream->size) + die("ovni_load_next_event: stream offset exceeds size\n"); + /* We have reached the end */ if(stream->offset == stream->size) { @@ -559,9 +563,6 @@ ovni_load_next_event(struct ovni_stream *stream) return -1; } - /* It cannot overflow, otherwise we are reading garbage */ - assert(stream->offset < stream->size); - stream->cur_ev = (struct ovni_ev *) &stream->buf[stream->offset]; out: