Coverage Report

Created: 2025-07-18 06:32

/src/fuzz_libunwind.c
Line
Count
Source (jump to first uncovered line)
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
693
void get_save_loc(int regnum) {
35
693
  unw_cursor_t cursor;
36
693
  unw_context_t uc;
37
693
  unw_word_t reference_reg;
38
39
693
  unw_getcontext(&uc);
40
693
  unw_init_local(&cursor, &uc);
41
693
  unw_save_loc_t loc;
42
6.23k
  while (unw_step(&cursor) > 0) {
43
5.54k
    unw_get_save_loc(&cursor, regnum, &loc);
44
5.54k
  }
45
693
}
46
47
1.69k
void get_random_reg(int regnum) {
48
1.69k
  unw_cursor_t cursor;
49
1.69k
  unw_context_t uc;
50
1.69k
  unw_word_t reference_reg;
51
52
1.69k
  unw_getcontext(&uc);
53
1.69k
  unw_init_local(&cursor, &uc);
54
15.2k
  while (unw_step(&cursor) > 0) {
55
13.5k
    unw_get_reg(&cursor, regnum, &reference_reg);
56
13.5k
  }
57
1.69k
}
58
59
228
void check_is_signal() {
60
228
  unw_cursor_t cursor;
61
228
  unw_context_t uc;
62
228
  unw_word_t reference_reg;
63
64
228
  unw_getcontext(&uc);
65
228
  unw_init_local(&cursor, &uc);
66
2.05k
  while (unw_step(&cursor) > 0) {
67
1.82k
    if (unw_is_signal_frame(&cursor)) {
68
0
      return;
69
0
    }
70
1.82k
  }
71
228
}
72
73
2.95k
void get_proc_name() {
74
2.95k
  unw_cursor_t cursor;
75
2.95k
  unw_context_t uc;
76
77
2.95k
  unw_getcontext(&uc);
78
2.95k
  unw_init_local(&cursor, &uc);
79
26.5k
  while (unw_step(&cursor) > 0) {
80
23.6k
    unw_word_t offset;
81
23.6k
    char buf[512];
82
23.6k
    unw_get_proc_name(&cursor, buf, sizeof(buf), &offset);
83
23.6k
  }
84
2.95k
}
85
86
7.12k
void dispatch(const uint8_t *data, size_t size) {
87
7.12k
  if (size < 8) {
88
327
    return;
89
327
  }
90
6.80k
  uint8_t decider = data[0] % 4;
91
6.80k
  data += 2;
92
6.80k
  size -= 2;
93
6.80k
  if (decider == 0) {
94
3.27k
    recurse1(data, size);
95
3.52k
  } else if (decider == 1) {
96
2.12k
    recurse2(data, size);
97
2.12k
  } else if (decider == 2) {
98
453
    recurse3(data, size);
99
943
  } else {
100
943
    recurse4(data, size);
101
943
  }
102
6.80k
}
103
104
3.27k
void recurse1(const uint8_t *data, size_t size) {
105
3.27k
  if (data[0] == 0x01) {
106
2.95k
    get_proc_name();
107
2.95k
  }
108
3.27k
  data += 2;
109
3.27k
  size -= 2;
110
111
3.27k
  dispatch(data, size);
112
3.27k
  return;
113
3.27k
}
114
115
2.12k
void recurse2(const uint8_t *data, size_t size) {
116
2.12k
  if (data[0] == 0x01) {
117
1.69k
    get_random_reg((int)data[1]);
118
1.69k
  }
119
2.12k
  data += 2;
120
2.12k
  size -= 2;
121
122
2.12k
  dispatch(data, size);
123
2.12k
  return;
124
2.12k
}
125
126
453
void recurse3(const uint8_t *data, size_t size) {
127
453
  if (data[0] == 0x01) {
128
228
    check_is_signal((int)data[1]);
129
228
  }
130
453
  data += 2;
131
453
  size -= 2;
132
133
453
  dispatch(data, size);
134
453
  return;
135
453
}
136
137
943
void recurse4(const uint8_t *data, size_t size) {
138
943
  if (data[0] == 0x01) {
139
693
    get_save_loc((int)data[1]);
140
693
  }
141
943
  data += 2;
142
943
  size -= 2;
143
144
943
  dispatch(data, size);
145
943
  return;
146
943
}
147
148
359
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
359
  if (size < 12 || size > 512) {
151
32
    return 0;
152
32
  }
153
154
327
  dispatch(data, size);
155
327
  return 0;
156
359
}