Coverage Report

Created: 2026-11-12 06:48

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/brpc/test/fuzzing/fuzz_amf.cpp
Line
Count
Source
1
// Licensed to the Apache Software Foundation (ASF) under one
2
// or more contributor license agreements.  See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership.  The ASF licenses this file
5
// to you under the Apache License, Version 2.0 (the
6
// "License"); you may not use this file except in compliance
7
// with the License.  You may obtain a copy of the License at
8
//
9
//   http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing,
12
// software distributed under the License is distributed on an
13
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
// KIND, either express or implied.  See the License for the
15
// specific language governing permissions and limitations
16
// under the License.
17
18
#include "brpc/amf.h"
19
#include "butil/iobuf.h"
20
21
4.01k
#define kMinInputLength 5
22
2.00k
#define kMaxInputLength 4096
23
24
extern "C" int
25
LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
26
2.00k
{
27
2.00k
    if (size < kMinInputLength || size > kMaxInputLength){
28
15
        return 0;
29
15
    }
30
31
1.99k
    uint8_t mode = data[0] % 3;
32
1.99k
    const uint8_t *payload = data + 1;
33
1.99k
    size_t payload_size = size - 1;
34
35
1.99k
    butil::IOBuf buf;
36
1.99k
    buf.append(payload, payload_size);
37
38
1.99k
    switch (mode) {
39
1.77k
        case 0: {
40
            // Read AMF object
41
1.77k
            butil::IOBufAsZeroCopyInputStream zc_stream(buf);
42
1.77k
            brpc::AMFInputStream stream(&zc_stream);
43
1.77k
            brpc::AMFObject obj;
44
1.77k
            brpc::ReadAMFObject(&obj, &stream);
45
1.77k
            break;
46
0
        }
47
96
        case 1: {
48
            // Read AMF string
49
96
            butil::IOBufAsZeroCopyInputStream zc_stream(buf);
50
96
            brpc::AMFInputStream stream(&zc_stream);
51
96
            std::string val;
52
96
            brpc::ReadAMFString(&val, &stream);
53
96
            break;
54
0
        }
55
124
        case 2: {
56
            // Read raw AMF fields by consuming the stream directly
57
124
            butil::IOBufAsZeroCopyInputStream zc_stream(buf);
58
124
            brpc::AMFInputStream stream(&zc_stream);
59
124
            uint8_t marker;
60
7.91k
            while (stream.good() && stream.cut_u8(&marker) == 1) {
61
                // Try to identify marker type and read value
62
7.78k
                if (marker == brpc::AMF_MARKER_NUMBER) {
63
2.23k
                    uint64_t num;
64
2.23k
                    stream.cut_u64(&num);
65
5.54k
                } else if (marker == brpc::AMF_MARKER_BOOLEAN) {
66
668
                    uint8_t b;
67
668
                    stream.cut_u8(&b);
68
4.88k
                } else if (marker == brpc::AMF_MARKER_STRING) {
69
663
                    uint16_t len;
70
663
                    if (stream.cut_u16(&len) == 2 && len < 1024) {
71
341
                        char tmp[1024];
72
341
                        stream.cutn(tmp, len);
73
341
                    }
74
663
                }
75
7.78k
            }
76
124
            break;
77
0
        }
78
1.99k
    }
79
80
1.99k
    return 0;
81
1.99k
}