Skip to content

Commit

Permalink
[test/cilksan] Add some simple tests for Cilksan functionality.
Browse files Browse the repository at this point in the history
  • Loading branch information
neboat committed Feb 17, 2021
1 parent 1194b82 commit ead59bc
Show file tree
Hide file tree
Showing 13 changed files with 1,523 additions and 0 deletions.
29 changes: 29 additions & 0 deletions test/cilksan/alloctest.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include <stdlib.h>
#include <stdio.h>
#include <math.h>

#include <cilk/cilk.h>

int main(int argc, char *argv[]) {
long n = 0;
if (argc > 1)
n = atol(argv[1]);

// Check that instrumentation on this allocation is handled
// correctly, even when n == 0.
long x[n];

// Ensure that we have racing accesses on x, so that x is
// instrumented.
cilk_spawn {
cilk_for (long i = 0; i < n; ++i)
x[i] = sin(i);
}
cilk_for (long i = 0; i < n; ++i)
x[i] = cos(i);

for (long i = 0; i < n; ++i)
printf("sin(%ld) = %ld\n", i, x[i]);

return 0;
}
154 changes: 154 additions & 0 deletions test/cilksan/alloctypes.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
#include <iostream>
#include <cilk/cilk.h>

struct Foo_st {
int a = 0;
double b = 0.0;
};

class Foo {
int val = 0;
public:
Foo() {}
~Foo() {}
int &getVal() { return val; }
void incVal() { val++; }
};

class Bar {
int val[4] = {0,0,0,0};
public:
Bar() {}
~Bar() {}
int &getVal(int i) { return val[i]; }
void incVal(int i) { val[i]++; }
};

int global = 0;

__attribute__((noinline))
static void helper(int &x) {
x++;
}

static void arr_helper(int *x, int n) {
for (int i = 0; i < n; i++)
x[i]++;
}

void global_test() {
std::cout << "global_test\n";
cilk_for (int i = 0; i < 1000; i++)
helper(global);

cilk_for (int i = 0; i < 1000; i++)
global--;
std::cout << global << '\n';
}

void local_test() {
std::cout << "local_test\n";
int local = 1;
cilk_for (int i = 0; i < 1000; i++)
helper(local);
std::cout << local << '\n';
}

void param_test(int &param) {
std::cout << "param_test\n";
cilk_for (int i = 0; i < 1000; i++)
helper(param);
std::cout << param << '\n';
}

int *malloc_test(int size) {
std::cout << "malloc_test\n";
int *x = (int*)malloc(size * sizeof(int));
x[0] = 0;
cilk_for (int i = 0; i < 1000; i++)
arr_helper(x, size);
std::cout << x[0] << '\n';
return x;
}

void calloc_test(int size) {
std::cout << "calloc_test\n";
int *y = (int*)calloc(size, sizeof(int));
cilk_for (int i = 0; i < 1000; i++)
arr_helper(y, size);
std::cout << y[0] << '\n';
free(y);
}

int *realloc_test(int *x, int size) {
std::cout << "realloc_test\n";
x = (int*)realloc(x, size * sizeof(int));
cilk_for (int i = 0; i < 1000; i++)
arr_helper(x, size);
std::cout << x[0] << '\n';
return x;
}

void new_test() {
std::cout << "new_test\n";
Foo *x = new Foo();
cilk_for (int i = 0; i < 1000; i++)
x->getVal()--;

cilk_for (int i = 0; i < 1000; i++)
x->incVal();
std::cout << "x->getVal() = " << x->getVal() << '\n';
delete x;

Bar *y = new Bar();
cilk_for (int i = 0; i < 1000; i++)
y->getVal(i % 4)--;

cilk_for (int i = 0; i < 1000; i++)
y->incVal(i % 4);
std::cout << "y->getVal(0) = " << y->getVal(0) << '\n';
delete y;

Foo_st *z = new Foo_st();
cilk_for (int i = 0; i < 1000; i++) {
z->a++;
z->b += z->a * i;
}
std::cout << "z->b = " << z->b << '\n';
delete z;
}

int main(int argc, char** argv) {
int arrsize = 1;
if (argc == 2)
arrsize = atoi(argv[1]);

// Race 1
global_test();

// Race 2
local_test();

int parent_local = 2;
// Race 3
param_test(parent_local);

// Race 4
int *x = malloc_test(arrsize);

// Race 5
calloc_test(arrsize);

// Race 6
x = realloc_test(x, 5 * arrsize);
free(x);

// Redundant with race 4
x = malloc_test(arrsize);
free(x);

// Races 7-12
new_test();

return 0;
}
64 changes: 64 additions & 0 deletions test/cilksan/alpha-beta.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@

#include <stdio.h>
#include <stdint.h>

#include <cilk/cilk.h>
//#include "../cilktool/cilktool.h"

#define DEPTH_THRESHOLD 7

typedef struct node_s {
const struct node_s* parent;
uint32_t value;
uint32_t depth;
} node_t;


// Function declarations
void evaluateMove(const node_t *move);
void scout_search(node_t *move, uint32_t depth);


void initialize_move(node_t *move, uint32_t depth) {
move->depth = depth;
}

void evaluateMove(const node_t *move) {
node_t next_move;
next_move.parent = move;
next_move.value = 2 * move->value;

/* #if EXPOSE_BUG == 1 */
// NO RACE
uint32_t new_depth = move->depth + 1;
/* #else */
/* volatile uint32_t new_depth = move->depth + 1; */
/* #endif */

// Search further using `scout_search`
scout_search(&next_move, new_depth);
}

void scout_search(node_t *move, uint32_t depth) {
// Stop the recursion once our depth surpassed a threshold
if (depth > DEPTH_THRESHOLD) {
return;
}

initialize_move(move, depth);

cilk_for(uint32_t i = 0; i < move->value; i++) {
evaluateMove(move);
}
}

int main() {
node_t root_move;
root_move.parent = NULL;
root_move.value = 1;
root_move.depth = 1;

evaluateMove(&root_move);

return 0;
}
27 changes: 27 additions & 0 deletions test/cilksan/call-once.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include <cilk/cilk.h>
#include <iostream>
#include <mutex>

int x = 0;

int bar()
{
static std::once_flag initialized;

std::call_once(initialized, []() {
std::cout << "initializing x = 1 in bar once" << std::endl;
x = 1;
}
);
return x;
}

int main(int argc, char **argv)
{
int a = cilk_spawn bar();
int b = cilk_spawn bar();
cilk_sync;
std::cout << "a + b = " << a + b << std::endl;

return 0;
}
46 changes: 46 additions & 0 deletions test/cilksan/increment.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#include <iostream>
#include <cilk/cilk.h>
//#include <cilk/cilk_api.h>

int global = 0;

void increment(int *x, int n) {
for (int i = 0; i < n; i++)
x[i]++;
}

__attribute__((noinline))
void helper(int *x) {
(*x)++;
}

int main(int argc, char** argv) {
int n = 1;
if (argc == 2) n = atoi(argv[1]);

cilk_for (int i = 0; i < 1000; i++)
helper(&global);
std::cout << global << '\n';

int local = 1;
cilk_for (int i = 0; i < 1000; i++)
helper(&local);
std::cout << local << '\n';

int *x = (int*)malloc(n * sizeof(int));
cilk_for (int i = 0; i < 1000; i++)
increment(x, n);
std::cout << x[0] << '\n';

int *y = (int*)calloc(n, sizeof(int));
cilk_for (int i = 0; i < 1000; i++)
increment(y, n);
std::cout << y[0] << '\n';

int *z = (int*)realloc(x, 4 * n * sizeof(int));
cilk_for (int i = 0; i < 1000; i++)
increment(z, 2 * n);
std::cout << z[0] << '\n';

return 0;
}
Loading

0 comments on commit ead59bc

Please sign in to comment.