Coverage Report

Created: 2025-11-09 06:54

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/fuzz_libunwind.c
Line
Count
Source
1
/* Copyright 2023 Google LLC
2
Licensed under the Apache License, Version 2.0 (the "License");
3
you may not use this file except in compliance with the License.
4
You may obtain a copy of the License at
5
      http://www.apache.org/licenses/LICENSE-2.0
6
Unless required by applicable law or agreed to in writing, software
7
distributed under the License is distributed on an "AS IS" BASIS,
8
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
9
See the License for the specific language governing permissions and
10
limitations under the License.
11
*/
12
13
/*
14
 * The main idea behind this fuzzer is the generate arbitrary stack traces
15
 * by way of recursive funcitons, and then using various calls to libunwind
16
 * apis arbitrarily.
17
 */
18
#define UNW_LOCAL_ONLY
19
#include <libunwind.h>
20
#include <stdint.h>
21
#include <stdlib.h>
22
#include <string.h>
23
24
void get_random_reg(int);
25
void get_proc_name();
26
void dispatch(const uint8_t *data, size_t size);
27
void check_is_signal();
28
void get_save_loc(int reg);
29
void recurse1(const uint8_t *data, size_t size);
30
void recurse2(const uint8_t *data, size_t size);
31
void recurse3(const uint8_t *data, size_t size);
32
void recurse4(const uint8_t *data, size_t size);
33
34
706
void get_save_loc(int regnum) {
35
706
  unw_cursor_t cursor;
36
706
  unw_context_t uc;
37
706
  unw_word_t reference_reg;
38
39
706
  unw_getcontext(&uc);
40
706
  unw_init_local(&cursor, &uc);
41
706
  unw_save_loc_t loc;
42
6.35k
  while (unw_step(&cursor) > 0) {
43
5.64k
    unw_get_save_loc(&cursor, regnum, &loc);
44
5.64k
  }
45
706
}
46
47
1.43k
void get_random_reg(int regnum) {
48
1.43k
  unw_cursor_t cursor;
49
1.43k
  unw_context_t uc;
50
1.43k
  unw_word_t reference_reg;
51
52
1.43k
  unw_getcontext(&uc);
53
1.43k
  unw_init_local(&cursor, &uc);
54
12.8k
  while (unw_step(&cursor) > 0) {
55
11.4k
    unw_get_reg(&cursor, regnum, &reference_reg);
56
11.4k
  }
57
1.43k
}
58
59
156
void check_is_signal() {
60
156
  unw_cursor_t cursor;
61
156
  unw_context_t uc;
62
156
  unw_word_t reference_reg;
63
64
156
  unw_getcontext(&uc);
65
156
  unw_init_local(&cursor, &uc);
66
1.40k
  while (unw_step(&cursor) > 0) {
67
1.24k
    if (unw_is_signal_frame(&cursor)) {
68
0
      return;
69
0
    }
70
1.24k
  }
71
156
}
72
73
3.81k
void get_proc_name() {
74
3.81k
  unw_cursor_t cursor;
75
3.81k
  unw_context_t uc;
76
77
3.81k
  unw_getcontext(&uc);
78
3.81k
  unw_init_local(&cursor, &uc);
79
34.3k
  while (unw_step(&cursor) > 0) {
80
30.5k
    unw_word_t offset;
81
30.5k
    char buf[512];
82
30.5k
    unw_get_proc_name(&cursor, buf, sizeof(buf), &offset);
83
30.5k
  }
84
3.81k
}
85
86
8.03k
void dispatch(const uint8_t *data, size_t size) {
87
8.03k
  if (size < 8) {
88
313
    return;
89
313
  }
90
7.72k
  uint8_t decider = data[0] % 4;
91
7.72k
  data += 2;
92
7.72k
  size -= 2;
93
7.72k
  if (decider == 0) {
94
4.32k
    recurse1(data, size);
95
4.32k
  } else if (decider == 1) {
96
1.99k
    recurse2(data, size);
97
1.99k
  } else if (decider == 2) {
98
349
    recurse3(data, size);
99
1.06k
  } else {
100
1.06k
    recurse4(data, size);
101
1.06k
  }
102
7.72k
}
103
104
4.32k
void recurse1(const uint8_t *data, size_t size) {
105
4.32k
  if (data[0] == 0x01) {
106
3.81k
    get_proc_name();
107
3.81k
  }
108
4.32k
  data += 2;
109
4.32k
  size -= 2;
110
111
4.32k
  dispatch(data, size);
112
4.32k
  return;
113
4.32k
}
114
115
1.99k
void recurse2(const uint8_t *data, size_t size) {
116
1.99k
  if (data[0] == 0x01) {
117
1.43k
    get_random_reg((int)data[1]);
118
1.43k
  }
119
1.99k
  data += 2;
120
1.99k
  size -= 2;
121
122
1.99k
  dispatch(data, size);
123
1.99k
  return;
124
1.99k
}
125
126
349
void recurse3(const uint8_t *data, size_t size) {
127
349
  if (data[0] == 0x01) {
128
156
    check_is_signal((int)data[1]);
129
156
  }
130
349
  data += 2;
131
349
  size -= 2;
132
133
349
  dispatch(data, size);
134
349
  return;
135
349
}
136
137
1.06k
void recurse4(const uint8_t *data, size_t size) {
138
1.06k
  if (data[0] == 0x01) {
139
706
    get_save_loc((int)data[1]);
140
706
  }
141
1.06k
  data += 2;
142
1.06k
  size -= 2;
143
144
1.06k
  dispatch(data, size);
145
1.06k
  return;
146
1.06k
}
147
148
342
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
149
  // Ensure we have a bit of data but not too much to cause stackoverflows.
150
342
  if (size < 12 || size > 512) {
151
29
    return 0;
152
29
  }
153
154
313
  dispatch(data, size);
155
313
  return 0;
156
342
}