File: ax_platform_node_textrangeprovider_win_fuzzer.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (243 lines) | stat: -rw-r--r-- 9,184 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ui/accessibility/platform/ax_platform_node_textrangeprovider_win.h"

#include <memory>
#include <tuple>
#include <utility>

#include "base/at_exit.h"
#include "base/i18n/icu_util.h"
#include "base/win/atl.h"
#include "base/win/scoped_bstr.h"
#include "base/win/scoped_variant.h"
#include "ui/accessibility/ax_enums.mojom.h"
#include "ui/accessibility/ax_node.h"
#include "ui/accessibility/ax_node_data.h"
#include "ui/accessibility/ax_node_position.h"
#include "ui/accessibility/ax_position.h"
#include "ui/accessibility/ax_range.h"
#include "ui/accessibility/ax_role_properties.h"
#include "ui/accessibility/ax_tree.h"
#include "ui/accessibility/ax_tree_data.h"
#include "ui/accessibility/ax_tree_fuzzer_util.h"
#include "ui/accessibility/ax_tree_id.h"
#include "ui/accessibility/ax_tree_update.h"
#include "ui/accessibility/platform/ax_fragment_root_delegate_win.h"
#include "ui/accessibility/platform/ax_fragment_root_win.h"
#include "ui/accessibility/platform/ax_platform_for_test.h"
#include "ui/accessibility/platform/test_ax_node_wrapper.h"

#include <UIAutomationClient.h>
#include <UIAutomationCore.h>
#include <UIAutomationCoreApi.h>

using Microsoft::WRL::ComPtr;

// We generate positions using fuzz data, this constant should be aligned
// with the amount of bytes needed to generate a new text range.
constexpr size_t kBytesNeededToGenerateTextRange = 4;
// This should be aligned with the amount of bytes needed to mutate a text range
// in ...MutateTextRangeProvider...
constexpr size_t kBytesNeededToMutateTextRange = 4;

// Min/Max node size for the initial tree.
constexpr size_t kMinNodeCount = 10;
constexpr size_t kMaxNodeCount = kMinNodeCount + 50;

// Min fuzz data needed for fuzzer to function.
// We need to ensure we have enough data to create a tree with text, as well as
// generate a couple of text ranges and mutate them.
constexpr size_t kMinFuzzDataSize =
    kMinNodeCount * AXTreeFuzzerGenerator::kMinimumNewNodeFuzzDataSize +
    kMinNodeCount * AXTreeFuzzerGenerator::kMinTextFuzzDataSize +
    2 * kBytesNeededToGenerateTextRange + 2 * kBytesNeededToMutateTextRange;

// Cap fuzz data to avoid slowness.
constexpr size_t kMaxFuzzDataSize = 3500;

ui::AXPlatformNode* AXPlatformNodeFromNode(ui::AXTree* tree, ui::AXNode* node) {
  const ui::TestAXNodeWrapper* wrapper =
      ui::TestAXNodeWrapper::GetOrCreate(tree, node);
  return wrapper ? wrapper->ax_platform_node() : nullptr;
}

template <typename T>
ComPtr<T> QueryInterfaceFromNode(ui::AXTree* tree, ui::AXNode* node) {
  ui::AXPlatformNode* ax_platform_node = AXPlatformNodeFromNode(tree, node);
  if (!ax_platform_node)
    return ComPtr<T>();
  ComPtr<T> result;
  ax_platform_node->GetNativeViewAccessible()->QueryInterface(__uuidof(T),
                                                              &result);
  return result;
}

// This method returns a text range scoped to this node.
void GetTextRangeProviderFromTextNode(
    ui::AXTree* tree,
    ui::AXNode* text_node,
    ComPtr<ITextRangeProvider>& text_range_provider) {
  ComPtr<IRawElementProviderSimple> provider_simple =
      QueryInterfaceFromNode<IRawElementProviderSimple>(tree, text_node);
  if (!provider_simple.Get())
    return;
  ComPtr<ITextProvider> text_provider;
  provider_simple->GetPatternProvider(UIA_TextPatternId, &text_provider);

  if (!text_provider.Get())
    return;

  text_provider->get_DocumentRange(&text_range_provider);

  ComPtr<ui::AXPlatformNodeTextRangeProviderWin> text_range_provider_interal;
  text_range_provider->QueryInterface(
      IID_PPV_ARGS(&text_range_provider_interal));
  ui::AXPlatformNode* ax_platform_node =
      AXPlatformNodeFromNode(tree, text_node);
  text_range_provider_interal->SetOwnerForTesting(
      static_cast<ui::AXPlatformNodeWin*>(ax_platform_node));
}

void CallComparisonAPIs(const ComPtr<ITextRangeProvider>& text_range,
                        const ComPtr<ITextRangeProvider>& other_text_range) {
  BOOL are_same;
  std::ignore = text_range->Compare(other_text_range.Get(), &are_same);
  int compare_endpoints_result;
  std::ignore = text_range->CompareEndpoints(
      TextPatternRangeEndpoint_Start, other_text_range.Get(),
      TextPatternRangeEndpoint_Start, &compare_endpoints_result);
  std::ignore = text_range->CompareEndpoints(
      TextPatternRangeEndpoint_End, other_text_range.Get(),
      TextPatternRangeEndpoint_Start, &compare_endpoints_result);
  std::ignore = text_range->CompareEndpoints(
      TextPatternRangeEndpoint_Start, other_text_range.Get(),
      TextPatternRangeEndpoint_End, &compare_endpoints_result);
  std::ignore = text_range->CompareEndpoints(
      TextPatternRangeEndpoint_End, other_text_range.Get(),
      TextPatternRangeEndpoint_End, &compare_endpoints_result);
}

TextPatternRangeEndpoint GenerateEndpoint(unsigned char byte) {
  return (byte % 2) ? TextPatternRangeEndpoint_Start
                    : TextPatternRangeEndpoint_End;
}

TextUnit GenerateTextUnit(unsigned char byte) {
  return static_cast<TextUnit>(byte % 7);
}

enum class TextRangeMutation {
  kMoveEndpointByRange,
  kExpandToEnclosingUnit,
  kMove,
  kMoveEndpointByUnit,
  kLast
};

TextRangeMutation GenerateTextRangeMutation(unsigned char byte) {
  constexpr unsigned char max =
      static_cast<unsigned char>(TextRangeMutation::kLast);
  return static_cast<TextRangeMutation>(byte % max);
}

void MutateTextRangeProvider(ComPtr<ITextRangeProvider>& text_range,
                             const ComPtr<ITextRangeProvider>& other_text_range,
                             FuzzerData& fuzz_data) {
  const int kMaxMoveCount = 20;
  TextUnit unit = GenerateTextUnit(fuzz_data.NextByte());
  int units_moved;

  TextRangeMutation mutation_type =
      GenerateTextRangeMutation(fuzz_data.NextByte());
  switch (mutation_type) {
    case TextRangeMutation::kMoveEndpointByRange:
      if (other_text_range.Get()) {
        text_range->MoveEndpointByRange(GenerateEndpoint(fuzz_data.NextByte()),
                                        other_text_range.Get(),
                                        GenerateEndpoint(fuzz_data.NextByte()));
        return;
      }
      [[fallthrough]];
    case TextRangeMutation::kExpandToEnclosingUnit:
      text_range->ExpandToEnclosingUnit(unit);
      return;
    case TextRangeMutation::kMove:
      text_range->Move(unit, fuzz_data.NextByte() % kMaxMoveCount,
                       &units_moved);
      return;
    case TextRangeMutation::kMoveEndpointByUnit:
      text_range->MoveEndpointByUnit(GenerateEndpoint(fuzz_data.NextByte()),
                                     unit, fuzz_data.NextByte() % kMaxMoveCount,
                                     &units_moved);
      return;
    case TextRangeMutation::kLast:
      NOTREACHED();
  }
}

struct Environment {
  Environment() { CHECK(base::i18n::InitializeICU()); }
  base::AtExitManager at_exit_manager;
  ui::AXPlatformForTest ax_platform_for_test;
};

// Entry point for LibFuzzer.
extern "C" int LLVMFuzzerTestOneInput(const unsigned char* data, size_t size) {
  if (size < kMinFuzzDataSize || size > kMaxFuzzDataSize)
    return 0;
  static Environment env;

  FuzzerData fuzz_data(data, size);
  AXTreeFuzzerGenerator generator;

  // Create initial tree.
  const size_t node_count =
      kMinNodeCount + fuzz_data.NextByte() % (kMaxNodeCount - kMinNodeCount);
  generator.GenerateInitialUpdate(fuzz_data, node_count);

  ui::AXTree* tree = generator.GetTree();

  // Run with --v=1 to aid in debugging a specific crash.
  VLOG(1) << tree->ToString();

  // Loop until data is expended.
  std::vector<ComPtr<ITextRangeProvider>> created_ranges;
  while (fuzz_data.RemainingBytes() > kBytesNeededToGenerateTextRange) {
    // Create a new position on a random node in the tree.
    {
      // To ensure that anchor_id is between |ui::kInvalidAXNodeID| and the max
      // ID of the tree (non-inclusive), get a number [0, max_id - 1) and then
      // shift by 1 to get [1, max_id)
      ui::AXNodeID anchor_id =
          (fuzz_data.NextByte() % (generator.GetMaxAssignedID() - 1)) + 1;
      ui::AXNode* anchor = tree->GetFromId(anchor_id);
      if (!anchor)
        continue;
      ComPtr<ITextRangeProvider> text_range_provider;
      GetTextRangeProviderFromTextNode(tree, anchor, text_range_provider);
      if (text_range_provider)
        created_ranges.push_back(std::move(text_range_provider));
    }
    for (size_t i = 0; i < created_ranges.size(); ++i) {
      ComPtr<ITextRangeProvider> text_range = created_ranges[i];
      ComPtr<ITextRangeProvider> other_range;
      if (i > 0)
        other_range = created_ranges[i - 1].Get();
      if (!other_range.Get())
        continue;

      CallComparisonAPIs(text_range.Get(), other_range);
      if (fuzz_data.RemainingBytes() > kBytesNeededToMutateTextRange)
        MutateTextRangeProvider(text_range, other_range, fuzz_data);
    }
    // Do a Tree Update.
    if (!generator.GenerateTreeUpdate(fuzz_data, 5))
      break;
  }
  tree->Destroy();
  ui::TestAXNodeWrapper::ResetGlobalState();
  return 0;
}