Coverage Report

Created: 2025-11-06 07:11

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