Coverage Report

Created: 2026-01-07 06:50

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
659
void get_save_loc(int regnum) {
35
659
  unw_cursor_t cursor;
36
659
  unw_context_t uc;
37
659
  unw_word_t reference_reg;
38
39
659
  unw_getcontext(&uc);
40
659
  unw_init_local(&cursor, &uc);
41
659
  unw_save_loc_t loc;
42
5.93k
  while (unw_step(&cursor) > 0) {
43
5.27k
    unw_get_save_loc(&cursor, regnum, &loc);
44
5.27k
  }
45
659
}
46
47
1.67k
void get_random_reg(int regnum) {
48
1.67k
  unw_cursor_t cursor;
49
1.67k
  unw_context_t uc;
50
1.67k
  unw_word_t reference_reg;
51
52
1.67k
  unw_getcontext(&uc);
53
1.67k
  unw_init_local(&cursor, &uc);
54
15.0k
  while (unw_step(&cursor) > 0) {
55
13.4k
    unw_get_reg(&cursor, regnum, &reference_reg);
56
13.4k
  }
57
1.67k
}
58
59
231
void check_is_signal() {
60
231
  unw_cursor_t cursor;
61
231
  unw_context_t uc;
62
231
  unw_word_t reference_reg;
63
64
231
  unw_getcontext(&uc);
65
231
  unw_init_local(&cursor, &uc);
66
2.07k
  while (unw_step(&cursor) > 0) {
67
1.84k
    if (unw_is_signal_frame(&cursor)) {
68
0
      return;
69
0
    }
70
1.84k
  }
71
231
}
72
73
3.17k
void get_proc_name() {
74
3.17k
  unw_cursor_t cursor;
75
3.17k
  unw_context_t uc;
76
77
3.17k
  unw_getcontext(&uc);
78
3.17k
  unw_init_local(&cursor, &uc);
79
28.5k
  while (unw_step(&cursor) > 0) {
80
25.3k
    unw_word_t offset;
81
25.3k
    char buf[512];
82
25.3k
    unw_get_proc_name(&cursor, buf, sizeof(buf), &offset);
83
25.3k
  }
84
3.17k
}
85
86
7.49k
void dispatch(const uint8_t *data, size_t size) {
87
7.49k
  if (size < 8) {
88
296
    return;
89
296
  }
90
7.20k
  uint8_t decider = data[0] % 4;
91
7.20k
  data += 2;
92
7.20k
  size -= 2;
93
7.20k
  if (decider == 0) {
94
3.61k
    recurse1(data, size);
95
3.61k
  } else if (decider == 1) {
96
2.22k
    recurse2(data, size);
97
2.22k
  } else if (decider == 2) {
98
438
    recurse3(data, size);
99
917
  } else {
100
917
    recurse4(data, size);
101
917
  }
102
7.20k
}
103
104
3.61k
void recurse1(const uint8_t *data, size_t size) {
105
3.61k
  if (data[0] == 0x01) {
106
3.17k
    get_proc_name();
107
3.17k
  }
108
3.61k
  data += 2;
109
3.61k
  size -= 2;
110
111
3.61k
  dispatch(data, size);
112
3.61k
  return;
113
3.61k
}
114
115
2.22k
void recurse2(const uint8_t *data, size_t size) {
116
2.22k
  if (data[0] == 0x01) {
117
1.67k
    get_random_reg((int)data[1]);
118
1.67k
  }
119
2.22k
  data += 2;
120
2.22k
  size -= 2;
121
122
2.22k
  dispatch(data, size);
123
2.22k
  return;
124
2.22k
}
125
126
438
void recurse3(const uint8_t *data, size_t size) {
127
438
  if (data[0] == 0x01) {
128
231
    check_is_signal((int)data[1]);
129
231
  }
130
438
  data += 2;
131
438
  size -= 2;
132
133
438
  dispatch(data, size);
134
438
  return;
135
438
}
136
137
917
void recurse4(const uint8_t *data, size_t size) {
138
917
  if (data[0] == 0x01) {
139
659
    get_save_loc((int)data[1]);
140
659
  }
141
917
  data += 2;
142
917
  size -= 2;
143
144
917
  dispatch(data, size);
145
917
  return;
146
917
}
147
148
319
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
319
  if (size < 12 || size > 512) {
151
23
    return 0;
152
23
  }
153
154
296
  dispatch(data, size);
155
296
  return 0;
156
319
}