Coverage Report

Created: 2026-12-01 07:08

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