Coverage Report

Created: 2025-12-14 06:03

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