aboutsummaryrefslogtreecommitdiff
path: root/thirdparty/tourist/analysis/src/dispatcher.cpp
blob: a667b0c37036fd50edb810dd185ae074d858e66b (plain) (blame)
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
#include <analysis/dispatcher.h>
#include <foundation/scheduler.h>
#include <trace/trace.h>

#include <constants.h>

//------------------------------------------------------------------------------
void Dispatcher::add_analyzer(Analyzer& analyzer)
{
    analyzer.subscribe(pending_subs);
}

//------------------------------------------------------------------------------
void Dispatcher::on_new_type(const Type* type)
{
    auto [group, name] = type->get_name();
    uint32 type_hash = Hash(group) * Hash(name);

    for (Subscription& sub : pending_subs)
    {
        Outline* outline = sub.outline.get();
        if (outline->hash != type_hash)
            continue;

        // Sum of non-array field sizes — the byte length of event.data that
        // scalar field reads index into.
        uint32 event_size = 0;
        for (uint32 i = 0, n = type->get_field_count(); i < n; ++i)
            event_size += type->get_field(i).get_size();

        for (uint32 i = 0; i < type->get_field_count(); ++i)
        {
            auto [field_name, field] = type->get_field_info(i);
            uint32 field_hash = Hash(field_name);

            for (Outline::FieldBase* f = outline->fields(); f->hash; ++f)
            {
                if (f->hash != field_hash)
                    continue;

                uint32 type_info = field.get_type_info();
                uint32 offset = field.get_offset();

                // Scalar fields are read via event.data + offset. Validate the
                // offset and element size against event_size so a malicious
                // utrace can't make Outline::Field memcpy past the event
                // payload. Array/string fields are read from aux and ignore
                // offset.
                if ((type_info & TYPE_INFO_CAT_ARRAY) == 0)
                {
                    uint32 element_size = 1u << (type_info & TYPE_INFO_SIZE_MASK);
                    if (offset > event_size || element_size > event_size - offset)
                        break;
                }

                f->type_info = uint8(type_info);
                f->offset = int16(offset);
                f->set = 1;
                f->index = i;
                break;
            }
        }

        uint32 uid = type->get_uid();
        if (uid >= dispatchers.size())
        {
            uint32 new_size = (uid + 16) & ~15;
            dispatchers.resize(new_size);
        }

        std::swap(sub, pending_subs.back());
        dispatchers[uid] = std::move(pending_subs.back());
        pending_subs.pop_back();
        break;
    }
}

//------------------------------------------------------------------------------
void Dispatcher::on_parcel(const EventParcel& parcel)
{
    if (!pending_subs.empty())
        for (const Type* type : parcel.new_types)
            on_new_type(type);

    if (dispatchers.empty())
        return;

    for (const Event& event : parcel.events)
    {
        uint32 uid = event.uid;
        if (uid >= dispatchers.size())
            continue;

        const Subscription& sub = dispatchers[uid];
        Outline* outline = sub.outline.get();
        if (outline == nullptr)
            continue;

        outline->event = &event;
        auto* analyzer = (Analyzer*)(sub.analyzer);
        (analyzer->*(sub.sink))(*outline);
    }
}

//------------------------------------------------------------------------------
void Dispatcher::run(DataSource& data_source)
{
    Allocator allocator;

    Preamble preamble(data_source, allocator);
    Transport transport = preamble.get_transport();
    Protocol protocol = preamble.get_protocol();

#if 0
    Scheduler scheduler({
        .concurrency = 3
    });

    struct State {
        Bundle bundle;
        Packet packets[128];
        EventParcel parcel;
    };
    State states[3];

    Task transport_task;
    Task protocol_task;
    Task analysis_task;

    auto analysis_entry = [&, index=uint32(0)] () mutable {
        auto& parcel = states[index].parcel;
        on_parcel(parcel);
    };

    auto protocol_entry = [&, index=uint32(0)] () mutable {
        auto& parcel = states[index].parcel;
        auto& bundle = states[index].bundle;
        parcel.reset();
        protocol.read(parcel, bundle);
    };

    auto transport_entry = [&, index=uint32(0)] () mutable {
        auto& bundle = states[index].bundle;
        auto& packets = states[index].packets;
        bundle = transport.read_packets(packets);
    };

    while (true)
    {
        transport_task = scheduler.create("transport", transport_entry);
        protocol_task = scheduler.create("protocol", protocol_entry);
        analysis_task = scheduler.create("analysis", analysis_entry);

        scheduler.start_after(protocol_task, transport_task);
        scheduler.start_after(analysis_task, protocol_task);
        scheduler.submit(transport_task);

        scheduler.wait(analysis_task);
    };

#else
    Packet packets[128];
    EventParcel parcel;
    while (Bundle bundle = transport.read_packets(packets))
    {
        parcel.reset();
        protocol.read(parcel, bundle);
        on_parcel(parcel);
    }
#endif // 0
}