summaryrefslogtreecommitdiffstats
path: root/source/slang-record-replay/replay/recordFile-processor.cpp
blob: 4772a2711d8821954e4e78a934a9bfd53702ba24 (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
#include "recordFile-processor.h"

#include "../util/record-format.h"
#include "parameter-decoder.h"

namespace SlangRecord
{
RecordFileProcessor::RecordFileProcessor(const Slang::String& filePath)
{
    Slang::FileMode fileMode = Slang::FileMode::Open;
    Slang::FileAccess fileAccess = Slang::FileAccess::Read;
    Slang::FileShare fileShare = Slang::FileShare::None;

    // Open the record file with read-only access
    SlangResult res = m_inputStream.init(filePath, fileMode, fileAccess, fileShare);

    if (res != SLANG_OK)
    {
        SlangRecord::slangRecordLog(
            SlangRecord::LogLevel::Error,
            "Failed to open file %s\n",
            filePath.begin());
        std::abort();
    }

    // Enable log system
    setLogLevel();
}

bool RecordFileProcessor::processNextBlock()
{
    FunctionHeader header{};
    if (!processHeader(header))
    {
        return false;
    }

    ApiClassId classId = static_cast<ApiClassId>(getClassId(header.callId));

    // capacity comparison will be performed in the reserve call, so we can safely call reserve
    m_parameterBuffer.reserve(header.dataSizeInBytes);

    size_t readBytes = 0;
    SlangResult res = SLANG_OK;

    if (header.dataSizeInBytes)
    {
        res = m_inputStream.read(m_parameterBuffer.getBuffer(), header.dataSizeInBytes, readBytes);
    }

    if (res != SLANG_OK || readBytes != header.dataSizeInBytes)
    {
        return false;
    }

    FunctionTailer tailer{};
    if (processTailer(tailer) == ERROR_BLOCK)
    {
        return false;
    }

    if (tailer.dataSizeInBytes)
    {
        m_outputBuffer.reserve(tailer.dataSizeInBytes);
        res = m_inputStream.read(m_outputBuffer.getBuffer(), tailer.dataSizeInBytes, readBytes);

        if (res != SLANG_OK || readBytes != tailer.dataSizeInBytes)
        {
            return false;
        }
    }

    bool ret = false;
    SlangDecoder::ParameterBlock paramBlock{};
    paramBlock.parameterBuffer = m_parameterBuffer.getBuffer();
    paramBlock.parameterBufferSize = header.dataSizeInBytes;
    paramBlock.outputBuffer = m_outputBuffer.getBuffer();
    paramBlock.outputBufferSize = tailer.dataSizeInBytes;

    if (classId == ApiClassId::GlobalFunction)
    {
        ret = m_decoder->processFunctionCall(header, paramBlock);
    }
    else
    {
        ret = m_decoder->processMethodCall(header, paramBlock);
    }

    m_parameterBuffer.clear();
    m_outputBuffer.clear();
    return ret;
}

bool RecordFileProcessor::processHeader(FunctionHeader& header)
{
    size_t readBytes = 0;
    SlangResult res = m_inputStream.read(&header, sizeof(FunctionHeader), readBytes);

    if (res != SLANG_OK || readBytes != sizeof(FunctionHeader))
    {
        return false;
    }

    if (header.magic != MAGIC_HEADER || header.callId == ApiCallId::InvalidCallId)
    {
        return false;
    }

    return true;
}

RecordFileResultCode RecordFileProcessor::processTailer(FunctionTailer& tailer)
{
    size_t readBytes = 0;
    SlangResult res = m_inputStream.read(&tailer, sizeof(FunctionTailer), readBytes);

    if (res != SLANG_OK || readBytes != sizeof(FunctionTailer))
    {
        return ERROR_BLOCK;
    }

    // If we don't read a valid tailer, but the magic is bit is a header, it indicates that
    // there is no tailer for this block, but it's still a valid block.
    if (tailer.magic == MAGIC_HEADER)
    {
        // revert back to last read position, and clear tailer
        int64_t offset = -(int64_t)sizeof(FunctionTailer);
        m_inputStream.seek(Slang::SeekOrigin::Current, offset);
        memset(&tailer, 0, sizeof(FunctionTailer));
        return NOT_EXSIT;
    }

    if (tailer.magic != MAGIC_TAILER)
    {
        return ERROR_BLOCK;
    }

    return RESULT_OK;
}

bool RecordFileProcessor::processMethod(
    FunctionHeader const& header,
    const uint8_t* parameterBuffer,
    int64_t bufferSize)
{
    return false;
}

bool RecordFileProcessor::processFunction(
    FunctionHeader const& header,
    const uint8_t* parameterBuffer,
    int64_t bufferSize)
{
    return false;
}
}; // namespace SlangRecord