summaryrefslogtreecommitdiff
path: root/src/vm/tizenasanenv.cpp
blob: 8ffeefad8bf794b3cb865f18458515e8d4b1a003 (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
#include <string.h>
#include "common.h"
#include "tizenasanenv.h"


template <typename Type, int STACK_SIZE>
class StaticStack {
    // We don't create constructor because
    // this class is used in a zeroed memory area
public:
    void push(Type addr)
    {
        _ASSERTE(m_pos < STACK_SIZE);

        m_data[m_pos++] = addr;
    }

    void pop()
    {
        _ASSERTE(m_pos > 0);
        --m_pos;
    }

    Type top()
    {
        _ASSERTE(m_pos > 0);

        return m_data[m_pos - 1];
    }

    bool empty()
    {
        return m_pos == 0;
    }

private:
    int m_pos;
    Type m_data[STACK_SIZE];
};

#include <pshpack1.h>
struct AuxiliaryCalls {
    LPVOID target;
    void (*pushAddr)(LPVOID addr);
    LPVOID (*popAddr)();
};

struct ReturnInfo {
    LPVOID addr;
    bool isSanitized;
};

extern "C" void __sanitizer_disable_interceptors();
extern "C" void __sanitizer_enable_interceptors();
extern "C" bool __sanitizer_interceptors_are_enabled();

extern LPVOID tizenASanWrapper;
extern UINT32 tizenASanWrapperSize;
extern UINT32 tizenASanWrapperEntryOffset;

// The maximum nesting of transitions between managed and unmanaged code that we support.
// This number is estimated from the common sense. We think this is enough to check any
// sane code (if it is not recursive) and it won't bloat TLS.  We do not use dynamic
// allocation because it complicates the process of memory management in TLS variables.
// It is used only for firmware with ASan and will not affect the release version.
#define MAX_STACK_DEPTH 128
static __thread StaticStack<ReturnInfo, MAX_STACK_DEPTH> s_retInfoStack;


static void DoEnable()
{
    _ASSERTE(__sanitizer_interceptors_are_enabled() == false);
    __sanitizer_enable_interceptors();
}

static void DoDisable()
{
    _ASSERTE(__sanitizer_interceptors_are_enabled() == true);
    __sanitizer_disable_interceptors();
}

static void PushAndEnableASan(LPVOID addr)
{
    _ASSERTE(__sanitizer_interceptors_are_enabled() == false);

    ReturnInfo retInfo = {
        .addr = addr,
        .isSanitized = false,
    };

    s_retInfoStack.push(retInfo);
    DoEnable();
}

static LPVOID PopAndDisableASan()
{
    _ASSERTE(__sanitizer_interceptors_are_enabled() == true);

    ReturnInfo retInfo = s_retInfoStack.top();
    s_retInfoStack.pop();

    _ASSERTE(retInfo.isSanitized == false);
    DoDisable();

    return retInfo.addr;
}

static void PushAndMayBeDisableASan(LPVOID addr)
{
    ReturnInfo retInfo = {
        .addr = addr,
        .isSanitized = __sanitizer_interceptors_are_enabled(),
    };

    if (retInfo.isSanitized)
        DoDisable();

    s_retInfoStack.push(retInfo);
}

static LPVOID PopAndMayBeEnableASan()
{
    _ASSERTE(__sanitizer_interceptors_are_enabled() == false);

    ReturnInfo retInfo = s_retInfoStack.top();
    s_retInfoStack.pop();

    if (retInfo.isSanitized)
        DoEnable();

    return retInfo.addr;
}

static LPVOID CreateWrapper(LPVOID target, void (*pushAddr)(LPVOID addr), LPVOID (*popAddr)())
{
    _ASSERTE(tizenASanWrapperEntryOffset == sizeof(AuxiliaryCalls));

    LPVOID wrapperSpace = (LPVOID)SystemDomain::GetGlobalLoaderAllocator()->GetExecutableHeap()->AllocMem(S_SIZE_T(tizenASanWrapperSize));

    AuxiliaryCalls calls = {
        .target = target,
	.pushAddr = pushAddr,
	.popAddr = popAddr,
    };

    // copy auxiliary calls
    memcpy(wrapperSpace, &calls, sizeof(calls));

    LPVOID entryPointer = (LPVOID)((UINT_PTR)wrapperSpace + tizenASanWrapperEntryOffset);
    LPVOID wrapperEntryPointer = (LPVOID)((UINT_PTR)&tizenASanWrapper + tizenASanWrapperEntryOffset);
    UINT32 wrapperCodeSize = tizenASanWrapperSize - tizenASanWrapperEntryOffset;

    // copy executable code wrapper
    memcpy(entryPointer, wrapperEntryPointer, wrapperCodeSize);

    FlushInstructionCache(GetCurrentProcess(), wrapperSpace, tizenASanWrapperSize);

    return entryPointer;
}


namespace TizenASanEnv {

LPVOID CreateWrapperSanitizedEntryPoint(LPVOID target)
{
    return CreateWrapper(target, PushAndEnableASan, PopAndDisableASan);
}

LPVOID CreateWrapperILCode(LPVOID target)
{
    return CreateWrapper(target, PushAndMayBeDisableASan, PopAndMayBeEnableASan);
}

} // namespace TizenASanEnv