11 #include "snapshot-interface.h"
14 #include "threads-model.h"
16 #include "traceanalysis.h"
17 #include "execution.h"
19 #include "bugmessage.h"
23 ModelChecker *model = NULL;
26 void placeholder(void *) {
32 #define SIGSTACKSIZE 65536
33 static void mprot_handle_pf(int sig, siginfo_t *si, void *unused)
35 model_print("Segmentation fault at %p\n", si->si_addr);
36 model_print("For debugging, place breakpoint at: %s:%d\n",
38 print_trace(); // Trace printing may cause dynamic memory allocation
43 void install_handler() {
45 ss.ss_sp = model_malloc(SIGSTACKSIZE);
46 ss.ss_size = SIGSTACKSIZE;
48 sigaltstack(&ss, NULL);
50 sa.sa_flags = SA_SIGINFO | SA_NODEFER | SA_RESTART | SA_ONSTACK;
51 sigemptyset(&sa.sa_mask);
52 sa.sa_sigaction = mprot_handle_pf;
54 if (sigaction(SIGSEGV, &sa, NULL) == -1) {
55 perror("sigaction(SIGSEGV)");
60 void createModelIfNotExist() {
63 snapshot_system_init(100000);
64 model = new ModelChecker();
65 model->startChecker();
70 /** @brief Constructor */
71 ModelChecker::ModelChecker() :
72 /* Initialize default scheduler */
74 scheduler(new Scheduler()),
75 history(new ModelHistory()),
76 execution(new ModelExecution(this, scheduler)),
78 curr_thread_num(MAIN_THREAD_ID),
82 model_print("C11Tester\n"
83 "Copyright (c) 2013 and 2019 Regents of the University of California. All rights reserved.\n"
84 "Distributed under the GPLv2\n"
85 "Written by Weiyu Luo, Brian Norris, and Brian Demsky\n\n");
87 real_memset(&stats,0,sizeof(struct execution_stats));
88 init_thread = new Thread(execution->get_next_id(), (thrd_t *) model_malloc(sizeof(thrd_t)), &placeholder, NULL, NULL);
90 init_thread->setTLS((char *)get_tls_addr());
92 execution->add_thread(init_thread);
93 scheduler->set_current_thread(init_thread);
95 execution->setParams(¶ms);
96 param_defaults(¶ms);
97 parse_options(¶ms);
99 /* Configure output redirection for the model-checker */
103 /** @brief Destructor */
104 ModelChecker::~ModelChecker()
109 /** Method to set parameters */
110 model_params * ModelChecker::getParams() {
115 * Restores user program to initial state and resets all model-checker data
118 void ModelChecker::reset_to_initial_state()
122 * FIXME: if we utilize partial rollback, we will need to free only
123 * those pending actions which were NOT pending before the rollback
126 for (unsigned int i = 0;i < get_num_threads();i++)
127 delete get_thread(int_to_id(i))->get_pending();
129 snapshot_roll_back(snapshot);
132 /** @return the number of user threads created during this execution */
133 unsigned int ModelChecker::get_num_threads() const
135 return execution->get_num_threads();
139 * Must be called from user-thread context (e.g., through the global
140 * thread_current() interface)
142 * @return The currently executing Thread.
144 Thread * ModelChecker::get_current_thread() const
146 return scheduler->get_current_thread();
150 * Must be called from user-thread context (e.g., through the global
151 * thread_current_id() interface)
153 * @return The id of the currently executing Thread.
155 thread_id_t ModelChecker::get_current_thread_id() const
157 ASSERT(int_to_id(curr_thread_num) == get_current_thread()->get_id());
158 return int_to_id(curr_thread_num);
162 * @brief Choose the next thread to execute.
164 * This function chooses the next thread that should execute. It can enforce
165 * execution replay/backtracking or, if the model-checker has no preference
166 * regarding the next thread (i.e., when exploring a new execution ordering),
167 * we defer to the scheduler.
169 * @return The next chosen thread to run, if any exist. Or else if the current
170 * execution should terminate, return NULL.
172 Thread * ModelChecker::get_next_thread()
176 * Have we completed exploring the preselected path? Then let the
179 return scheduler->select_next_thread();
183 * @brief Assert a bug in the executing program.
185 * Use this function to assert any sort of bug in the user program. If the
186 * current trace is feasible (actually, a prefix of some feasible execution),
187 * then this execution will be aborted, printing the appropriate message. If
188 * the current trace is not yet feasible, the error message will be stashed and
189 * printed if the execution ever becomes feasible.
191 * @param msg Descriptive message for the bug (do not include newline char)
192 * @return True if bug is immediately-feasible
194 void ModelChecker::assert_bug(const char *msg, ...)
200 vsnprintf(str, sizeof(str), msg, ap);
203 execution->assert_bug(str);
207 * @brief Assert a bug in the executing program, asserted by a user thread
208 * @see ModelChecker::assert_bug
209 * @param msg Descriptive message for the bug (do not include newline char)
211 void ModelChecker::assert_user_bug(const char *msg)
213 /* If feasible bug, bail out now */
218 /** @brief Print bug report listing for this execution (if any bugs exist) */
219 void ModelChecker::print_bugs() const
221 SnapVector<bug_message *> *bugs = execution->get_bugs();
223 model_print("Bug report: %zu bug%s detected\n",
225 bugs->size() > 1 ? "s" : "");
226 for (unsigned int i = 0;i < bugs->size();i++)
227 (*bugs)[i] -> print();
231 * @brief Record end-of-execution stats
233 * Must be run when exiting an execution. Records various stats.
234 * @see struct execution_stats
236 void ModelChecker::record_stats()
239 if (execution->have_bug_reports())
240 stats.num_buggy_executions ++;
241 else if (execution->is_complete_execution())
242 stats.num_complete ++;
244 //All threads are sleeping
246 * @todo We can violate this ASSERT() when fairness/sleep sets
247 * conflict to cause an execution to terminate, e.g. with:
248 * Scheduler: [0: disabled][1: disabled][2: sleep][3: current, enabled]
250 //ASSERT(scheduler->all_threads_sleeping());
254 /** @brief Print execution stats */
255 void ModelChecker::print_stats() const
257 model_print("Number of complete, bug-free executions: %d\n", stats.num_complete);
258 model_print("Number of buggy executions: %d\n", stats.num_buggy_executions);
259 model_print("Total executions: %d\n", stats.num_total);
263 * @brief End-of-exeuction print
264 * @param printbugs Should any existing bugs be printed?
266 void ModelChecker::print_execution(bool printbugs) const
268 model_print("Program output from execution %d:\n",
269 get_execution_number());
270 print_program_output();
272 if (params.verbose >= 3) {
276 /* Don't print invalid bugs */
277 if (printbugs && execution->have_bug_reports()) {
283 execution->print_summary();
287 * Queries the model-checker for more executions to explore and, if one
288 * exists, resets the model-checker state to execute a new execution.
290 * @return If there are more executions to explore, return true. Otherwise,
293 void ModelChecker::finish_execution(bool more_executions)
296 /* Is this execution a feasible execution that's worth bug-checking? */
297 bool complete = (execution->is_complete_execution() ||
298 execution->have_bug_reports());
300 /* End-of-execution bug checks */
302 if (execution->is_deadlocked())
303 assert_bug("Deadlock detected");
305 run_trace_analyses();
310 if ( (complete && params.verbose) || params.verbose>1 || (complete && execution->have_bug_reports()))
311 print_execution(complete);
313 clear_program_output();
316 history->set_new_exec_flag();
319 reset_to_initial_state();
322 /** @brief Run trace analyses on complete trace */
323 void ModelChecker::run_trace_analyses() {
324 for (unsigned int i = 0;i < trace_analyses.size();i ++)
325 trace_analyses[i] -> analyze(execution->get_action_trace());
329 * @brief Get a Thread reference by its ID
330 * @param tid The Thread's ID
331 * @return A Thread reference
333 Thread * ModelChecker::get_thread(thread_id_t tid) const
335 return execution->get_thread(tid);
339 * @brief Get a reference to the Thread in which a ModelAction was executed
340 * @param act The ModelAction
341 * @return A Thread reference
343 Thread * ModelChecker::get_thread(const ModelAction *act) const
345 return execution->get_thread(act);
348 void ModelChecker::startRunExecution(Thread *old) {
350 if (params.traceminsize != 0 &&
351 execution->get_curr_seq_num() > checkfree) {
352 checkfree += params.checkthreshold;
353 execution->collectActions();
356 curr_thread_num = MAIN_THREAD_ID;
357 Thread *thr = getNextThread(old);
358 if (thr != nullptr) {
359 scheduler->set_current_thread(thr);
361 if (Thread::swap(old, thr) < 0) {
362 perror("swap threads");
368 if (!handleChosenThread(old)) {
374 Thread* ModelChecker::getNextThread(Thread *old)
376 Thread *nextThread = nullptr;
377 for (unsigned int i = curr_thread_num;i < get_num_threads();i++) {
378 thread_id_t tid = int_to_id(i);
379 Thread *thr = get_thread(tid);
381 if (!thr->is_complete()) {
382 if (!thr->get_pending()) {
387 } else if (thr != old && !thr->is_freed()) {
388 thr->freeResources();
391 ModelAction *act = thr->get_pending();
392 if (act && scheduler->is_enabled(tid)){
393 /* Don't schedule threads which should be disabled */
394 if (!execution->check_action_enabled(act)) {
395 scheduler->sleep(thr);
398 /* Allow pending relaxed/release stores or thread actions to perform first */
399 else if (!chosen_thread) {
400 if (act->is_write()) {
401 std::memory_order order = act->get_mo();
402 if (order == std::memory_order_relaxed || \
403 order == std::memory_order_release) {
406 } else if (act->get_type() == THREAD_CREATE || \
407 act->get_type() == PTHREAD_CREATE || \
408 act->get_type() == THREAD_START || \
409 act->get_type() == THREAD_FINISH) {
418 /* Swap back to system_context and terminate this execution */
419 void ModelChecker::finishRunExecution(Thread *old)
421 scheduler->set_current_thread(NULL);
423 /** Reset curr_thread_num to initial value for next execution. */
424 curr_thread_num = MAIN_THREAD_ID;
426 /** If we have more executions, we won't make it past this call. */
427 finish_execution(execution_number < params.maxexecutions);
430 /** We finished the final execution. Print stuff and exit. */
431 model_print("******* Model-checking complete: *******\n");
434 /* Have the trace analyses dump their output. */
435 for (unsigned int i = 0;i < trace_analyses.size();i++)
436 trace_analyses[i]->finish();
438 /* unlink tmp file created by last child process */
440 snprintf_(filename, sizeof(filename), "C11FuzzerTmp%d", getpid());
447 uint64_t ModelChecker::switch_thread(ModelAction *act)
450 static bool fork_message_printed = false;
452 if (!fork_message_printed) {
453 model_print("Fork handler or dead thread trying to call into model checker...\n");
454 fork_message_printed = true;
462 Thread *old = thread_current();
463 old->set_state(THREAD_READY);
465 ASSERT(!old->get_pending());
467 if (inspect_plugin != NULL) {
468 inspect_plugin->inspectModelAction(act);
471 old->set_pending(act);
473 if (old->is_waiting_on(old))
474 assert_bug("Deadlock detected (thread %u)", curr_thread_num);
476 Thread* next = getNextThread(old);
477 if (next != nullptr) {
478 scheduler->set_current_thread(next);
479 if (Thread::swap(old, next) < 0) {
480 perror("swap threads");
484 if (handleChosenThread(old)) {
485 startRunExecution(old);
488 return old->get_return_value();
491 bool ModelChecker::handleChosenThread(Thread *old)
493 if (execution->has_asserted()) {
494 finishRunExecution(old);
497 if (!chosen_thread) {
498 chosen_thread = get_next_thread();
500 if (!chosen_thread) {
501 finishRunExecution(old);
504 if (chosen_thread->just_woken_up()) {
505 chosen_thread->set_wakeup_state(false);
506 chosen_thread->set_pending(NULL);
507 chosen_thread = NULL;
508 // Allow this thread to stash the next pending action
512 // Consume the next action for a Thread
513 ModelAction *curr = chosen_thread->get_pending();
514 chosen_thread->set_pending(NULL);
515 chosen_thread = execution->take_step(curr);
517 if (should_terminate_execution()) {
518 finishRunExecution(old);
525 void ModelChecker::startChecker() {
527 //Need to initial random number generator state to avoid resets on rollback
528 initstate(423121, random_state, sizeof(random_state));
530 snapshot = take_snapshot();
532 //reset random number generator state
533 setstate(random_state);
535 install_trace_analyses(get_execution());
540 bool ModelChecker::should_terminate_execution()
542 if (execution->have_bug_reports()) {
543 execution->set_assert();
545 } else if (execution->isFinished()) {