summaryrefslogtreecommitdiff
path: root/src/jit/jitconfig.cpp
blob: 94a94305d147a012389a470de2af9093cd6b6591 (plain)
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

#include "jitpch.h"
#ifdef _MSC_VER
#pragma hdrstop
#endif

#include "jitconfig.h"

JitConfigValues JitConfig;

void JitConfigValues::MethodSet::initialize(const wchar_t* list, ICorJitHost* host)
{
    assert(m_list == nullptr);
    assert(m_names == nullptr);

    enum State
    {
        NO_NAME,
        CLS_NAME,
        FUNC_NAME,
        ARG_LIST
    }; // parsing state machine

    const char SEP_CHAR = ' '; // current character use to separate each entry

    wchar_t      lastChar  = '?';     // dummy
    int          nameStart = -1;      // Index of the start of the current class or method name
    MethodName   currentName;         // Buffer used while parsing the current entry
    MethodName** lastName = &m_names; // Last entry inserted into the list
    bool         isQuoted = false;

    currentName.m_next            = nullptr;
    currentName.m_methodNameStart = -1;
    currentName.m_methodNameLen   = -1;
    currentName.m_classNameStart  = -1;
    currentName.m_classNameLen    = -1;
    currentName.m_numArgs         = -1;

    // Convert the input list to UTF-8
    int utf8ListLen = WszWideCharToMultiByte(CP_UTF8, 0, list, -1, nullptr, 0, nullptr, nullptr);
    m_list          = (char*)host->allocateMemory(utf8ListLen);
    if (WszWideCharToMultiByte(CP_UTF8, 0, list, -1, const_cast<LPSTR>(m_list), utf8ListLen, nullptr, nullptr) == 0)
    {
        // Failed to convert the list. Free the memory and ignore the list.
        host->freeMemory(reinterpret_cast<void*>(const_cast<char*>(m_list)));
        m_list = nullptr;
        return;
    }

    State state = NO_NAME;
    for (int i = 0; lastChar != '\0'; i++)
    {
        lastChar = m_list[i];

        switch (state)
        {
            case NO_NAME:
                if (m_list[i] != SEP_CHAR)
                {
                    nameStart = i;
                    state     = CLS_NAME; // we have found the start of the next entry
                }
                break;

            case CLS_NAME:
                if (m_list[nameStart] == '"')
                {
                    for (; m_list[i] != '\0' && m_list[i] != '"'; i++)
                    {
                        ;
                    }

                    nameStart++;
                    isQuoted = true;
                }

                if (m_list[i] == ':')
                {
                    if (m_list[nameStart] == '*' && !isQuoted)
                    {
                        // The class name is a wildcard; mark it invalid.
                        currentName.m_classNameStart = -1;
                        currentName.m_classNameLen   = -1;
                    }
                    else
                    {
                        currentName.m_classNameStart = nameStart;
                        currentName.m_classNameLen   = i - nameStart;

                        // Remove the trailing quote, if any
                        if (isQuoted)
                        {
                            currentName.m_classNameLen--;
                            isQuoted = false;
                        }
                    }

                    // Accept class::name syntax as well
                    if (m_list[i + 1] == ':')
                    {
                        i++;
                    }

                    nameStart = i + 1;
                    state     = FUNC_NAME;
                }
                else if (m_list[i] == '\0' || m_list[i] == SEP_CHAR || m_list[i] == '(')
                {
                    // Treat this as a method name without a class name.
                    currentName.m_classNameStart = -1;
                    currentName.m_classNameLen   = -1;
                    goto DONE_FUNC_NAME;
                }
                break;

            case FUNC_NAME:
                if (m_list[nameStart] == '"')
                {
                    // The first half of the outer contdition handles the case where the
                    // class name is valid.
                    for (; nameStart == i || (m_list[i] != '\0' && m_list[i] != '"'); i++)
                    {
                        ;
                    }

                    nameStart++;
                    isQuoted = true;
                }

                if (m_list[i] == '\0' || m_list[i] == SEP_CHAR || m_list[i] == '(')
                {
                DONE_FUNC_NAME:
                    assert(m_list[i] == '\0' || m_list[i] == SEP_CHAR || m_list[i] == '(');

                    if (m_list[nameStart] == '*' && !isQuoted)
                    {
                        // The method name is a wildcard; mark it invalid.
                        currentName.m_methodNameStart = -1;
                        currentName.m_methodNameLen   = -1;
                    }
                    else
                    {
                        currentName.m_methodNameStart = nameStart;
                        currentName.m_methodNameLen   = i - nameStart;

                        // Remove the trailing quote, if any
                        if (isQuoted)
                        {
                            currentName.m_classNameLen--;
                            isQuoted = false;
                        }
                    }

                    if (m_list[i] == '\0' || m_list[i] == SEP_CHAR)
                    {
                        currentName.m_numArgs = -1;
                        goto DONE_ARG_LIST;
                    }
                    else
                    {
                        assert(m_list[i] == '(');
                        currentName.m_numArgs = -1;
                        state                 = ARG_LIST;
                    }
                }
                break;

            case ARG_LIST:
                if (m_list[i] == '\0' || m_list[i] == ')')
                {
                    if (currentName.m_numArgs == -1)
                    {
                        currentName.m_numArgs = 0;
                    }

                DONE_ARG_LIST:
                    assert(m_list[i] == '\0' || m_list[i] == SEP_CHAR || m_list[i] == ')');

                    // We have parsed an entire method name; create a new entry in the list for it.
                    MethodName* name = (MethodName*)host->allocateMemory(sizeof(MethodName));
                    *name            = currentName;

                    assert(name->m_next == nullptr);
                    *lastName = name;
                    lastName  = &name->m_next;

                    state = NO_NAME;

                    // Skip anything after the argument list until we find the next
                    // separator character. Otherwise if we see "func(a,b):foo" we
                    // create entries for "func(a,b)" as well as ":foo".
                    if (m_list[i] == ')')
                    {
                        for (; m_list[i] && m_list[i] != SEP_CHAR; i++)
                        {
                            ;
                        }

                        lastChar = m_list[i];
                    }
                }
                else
                {
                    if (m_list[i] != SEP_CHAR && currentName.m_numArgs == -1)
                    {
                        currentName.m_numArgs = 1;
                    }

                    if (m_list[i] == ',')
                    {
                        currentName.m_numArgs++;
                    }
                }
                break;

            default:
                assert(!"Bad state");
                break;
        }
    }
}

void JitConfigValues::MethodSet::destroy(ICorJitHost* host)
{
    // Free method names, free the list string, and reset our state
    for (MethodName *name = m_names, *next = nullptr; name != nullptr; name = next)
    {
        next = name->m_next;
        host->freeMemory(reinterpret_cast<void*>(const_cast<MethodName*>(name)));
    }
    if (m_list != nullptr)
    {
        host->freeMemory(reinterpret_cast<void*>(const_cast<char*>(m_list)));
        m_list = nullptr;
    }
    m_names = nullptr;
}

static bool matchesName(const char* const name, int nameLen, const char* const s2)
{
    return strncmp(name, s2, nameLen) == 0 && s2[nameLen] == '\0';
}

bool JitConfigValues::MethodSet::contains(const char*       methodName,
                                          const char*       className,
                                          CORINFO_SIG_INFO* sigInfo) const
{
    int numArgs = sigInfo != nullptr ? sigInfo->numArgs : -1;

    // Try to match any the entries in the list.
    for (MethodName* name = m_names; name != nullptr; name = name->m_next)
    {
        // If m_numArgs is valid, check for a mismatch
        if (name->m_numArgs != -1 && name->m_numArgs != numArgs)
        {
            continue;
        }

        // If m_methodNameStart is valid, check for a mismatch
        if (name->m_methodNameStart != -1)
        {
            const char* expectedMethodName = &m_list[name->m_methodNameStart];
            if (!matchesName(expectedMethodName, name->m_methodNameLen, methodName))
            {
                // C++ embeds the class name into the method name; deal with that here.
                const char* colon = strchr(methodName, ':');
                if (colon != nullptr && colon[1] == ':' &&
                    matchesName(expectedMethodName, name->m_methodNameLen, methodName))
                {
                    int classLen = (int)(colon - methodName);
                    if (name->m_classNameStart == -1 ||
                        (classLen == name->m_classNameLen &&
                         strncmp(&m_list[name->m_classNameStart], methodName, classLen) == 0))
                    {
                        return true;
                    }
                }
                continue;
            }
        }

        // If m_classNameStart is valid, check for a mismatch
        if (className == nullptr || name->m_classNameStart == -1 ||
            matchesName(&m_list[name->m_classNameStart], name->m_classNameLen, className))
        {
            return true;
        }

        // Check for suffix wildcard like System.*
        if (name->m_classNameLen > 0 && m_list[name->m_classNameStart + name->m_classNameLen - 1] == '*' &&
            strncmp(&m_list[name->m_classNameStart], className, name->m_classNameLen - 1) == 0)
        {
            return true;
        }

#ifdef _DEBUG
        // Maybe className doesn't include the namespace. Try to match that
        const char* nsSep = strrchr(className, '.');
        if (nsSep != nullptr && nsSep != className)
        {
            const char* onlyClass = nsSep[-1] == '.' ? nsSep : &nsSep[1];
            if (matchesName(&m_list[name->m_classNameStart], name->m_classNameLen, onlyClass))
            {
                return true;
            }
        }
#endif
    }

    return false;
}

void JitConfigValues::initialize(ICorJitHost* host)
{
    assert(!m_isInitialized);

#define CONFIG_INTEGER(name, key, defaultValue) m_##name = host->getIntConfigValue(key, defaultValue);
#define CONFIG_STRING(name, key) m_##name = host->getStringConfigValue(key);
#define CONFIG_METHODSET(name, key)                                                                                    \
    const wchar_t* name##value = host->getStringConfigValue(key);                                                      \
    m_##name.initialize(name##value, host);                                                                            \
    host->freeStringConfigValue(name##value);

#include "jitconfigvalues.h"

    m_isInitialized = true;
}

void JitConfigValues::destroy(ICorJitHost* host)
{
    if (!m_isInitialized)
    {
        return;
    }

#define CONFIG_INTEGER(name, key, defaultValue)
#define CONFIG_STRING(name, key) host->freeStringConfigValue(m_##name);
#define CONFIG_METHODSET(name, key) m_##name.destroy(host);

#include "jitconfigvalues.h"

    m_isInitialized = false;
}