summary refs log tree commit diff
path: root/src/tools/miri/tests/native-lib/ptr_read_access.c
blob: 540845d53a744245e474ae62818c53278be1a686 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <stdio.h>

// See comments in build_native_lib()
#define EXPORT __attribute__((visibility("default")))

/* Test: test_pointer */

EXPORT void print_pointer(const int *ptr) {
  printf("printing pointer dereference from C: %d\n", *ptr);
}

/* Test: test_simple */

typedef struct Simple {
  int field;
} Simple;

EXPORT int access_simple(const Simple *s_ptr) {
  return s_ptr->field;
}

/* Test: test_nested */

typedef struct Nested {
  int value;
  struct Nested *next;
} Nested;

// Returns the innermost/last value of a Nested pointer chain.
EXPORT int access_nested(const Nested *n_ptr) {
  // Edge case: `n_ptr == NULL` (i.e. first Nested is None).
  if (!n_ptr) { return 0; }

  while (n_ptr->next) {
    n_ptr = n_ptr->next;
  }

  return n_ptr->value;
}

/* Test: test_static */

typedef struct Static {
    int value;
    struct Static *recurse;
} Static;

EXPORT int access_static(const Static *s_ptr) {
  return s_ptr->recurse->recurse->value;
}