Skip to content

Commit

Permalink
Extract thread-local-value examples from silver searcher
Browse files Browse the repository at this point in the history
  • Loading branch information
sim642 committed Sep 20, 2023
1 parent dbb4e9e commit 16aa15f
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 0 deletions.
37 changes: 37 additions & 0 deletions tests/regression/03-practical/36-thread-local-value.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Thread-local variable flow-sensitive value analysis.
// Extracted from silver searcher.
#include <stdlib.h>
#include <pthread.h>
#include <goblint.h>
extern int __VERIFIER_nondet_int();

__thread int data = 0;

void *thread(void *arg) {
__goblint_check(data == 0); // NORACE
data = 1; // NORACE
__goblint_check(data == 1); // NORACE
__goblint_check(data != 0); // NORACE
return NULL;
}

int main() {
int threads_total = __VERIFIER_nondet_int();
__goblint_assume(threads_total >= 0);

pthread_t *tids = malloc(threads_total * sizeof(pthread_t));

// create threads
for (int i = 0; i < threads_total; i++) {
pthread_create(&tids[i], NULL, &thread, NULL); // may fail but doesn't matter
}

// join threads
for (int i = 0; i < threads_total; i++) {
pthread_join(tids[i], NULL);
}

free(tids);

return 0;
}
47 changes: 47 additions & 0 deletions tests/regression/03-practical/37-thread-local-value-dynamic.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Thread-local variable flow-sensitive value analysis with dynamic allocation.
// Extracted from silver searcher.
#include <stdlib.h>
#include <pthread.h>
#include <goblint.h>
extern int __VERIFIER_nondet_int();

__thread int* data = NULL;

void *thread(void *arg) {
int n = __VERIFIER_nondet_int();
__goblint_assume(n >= 0);

data = calloc(n, sizeof(int)); // NORACE

for (int i = 0; i < n; i++) {
__goblint_check(data[i] == 0); // NORACE
}

for (int i = 0; i < n; i++) {
data[i] = 1; // NORACE
}

free(data); // NORACE
return NULL;
}

int main() {
int threads_total = __VERIFIER_nondet_int();
__goblint_assume(threads_total >= 0);

pthread_t *tids = malloc(threads_total * sizeof(pthread_t));

// create threads
for (int i = 0; i < threads_total; i++) {
pthread_create(&tids[i], NULL, &thread, NULL); // may fail but doesn't matter
}

// join threads
for (int i = 0; i < threads_total; i++) {
pthread_join(tids[i], NULL);
}

free(tids);

return 0;
}

0 comments on commit 16aa15f

Please sign in to comment.