Skip to content

Commit 2c67a82

Browse files
committed
[MERGE chakra-core#4429 @Cellule] OS#15089337 WASM: Do not use StatementReader in IRBuilder
Merge pull request chakra-core#4429 from Cellule:statement_reader Fix usage of Statement Reader in IRBuilderAsmJs to make sure we don't use uninitialized values. Don't even allocate a statement reader for wasm since there are no statements to use.
2 parents 27b5869 + 71822e6 commit 2c67a82

4 files changed

Lines changed: 22 additions & 18 deletions

File tree

lib/Backend/IRBuilderAsmJs.cpp

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ IRBuilderAsmJs::Build()
1616
m_tempAlloc = &localAlloc;
1717

1818
uint32 offset;
19-
uint32 statementIndex = m_statementReader.GetStatementIndex();
19+
uint32 statementIndex = m_statementReader ? m_statementReader->GetStatementIndex() : Js::Constants::NoStatementIndex;
2020

2121
m_argStack = JitAnew(m_tempAlloc, SListCounted<IR::Instr *>, m_tempAlloc);
2222
m_tempList = JitAnew(m_tempAlloc, SList<IR::Instr *>, m_tempAlloc);
@@ -124,7 +124,7 @@ IRBuilderAsmJs::Build()
124124
BuildImplicitArgIns();
125125
}
126126

127-
if (m_statementReader.AtStatementBoundary(&m_jnReader))
127+
if (m_statementReader && m_statementReader->AtStatementBoundary(&m_jnReader))
128128
{
129129
statementIndex = AddStatementBoundary(statementIndex, offset);
130130
}
@@ -164,14 +164,14 @@ IRBuilderAsmJs::Build()
164164
}
165165
offset = m_jnReader.GetCurrentOffset();
166166

167-
if (m_statementReader.AtStatementBoundary(&m_jnReader))
167+
if (m_statementReader && m_statementReader->AtStatementBoundary(&m_jnReader))
168168
{
169169
statementIndex = AddStatementBoundary(statementIndex, offset);
170170
}
171171

172172
}
173173

174-
if (Js::Constants::NoStatementIndex != statementIndex)
174+
if (m_statementReader && Js::Constants::NoStatementIndex != statementIndex)
175175
{
176176
statementIndex = AddStatementBoundary(statementIndex, Js::Constants::NoByteCodeOffset);
177177
}
@@ -422,14 +422,11 @@ IRBuilderAsmJs::BuildFieldSym(Js::RegSlot reg, Js::PropertyId propertyId, Proper
422422
uint
423423
IRBuilderAsmJs::AddStatementBoundary(uint statementIndex, uint offset)
424424
{
425-
if (m_func->GetJITFunctionBody()->IsWasmFunction())
426-
{
427-
return 0;
428-
}
425+
AssertOrFailFast(m_statementReader);
429426
IR::PragmaInstr* pragmaInstr = IR::PragmaInstr::New(Js::OpCode::StatementBoundary, statementIndex, m_func);
430427
this->AddInstr(pragmaInstr, offset);
431428

432-
return m_statementReader.MoveNextStatementBoundary();
429+
return m_statementReader->MoveNextStatementBoundary();
433430
}
434431

435432
uint32 IRBuilderAsmJs::GetTypedRegFromRegSlot(Js::RegSlot reg, WAsmJs::Types type)

lib/Backend/IRBuilderAsmJs.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,11 @@ class IRBuilderAsmJs
8181
, m_switchAdapter(this)
8282
, m_switchBuilder(&m_switchAdapter)
8383
{
84-
func->m_workItem->InitializeReader(&m_jnReader, &m_statementReader, func->m_alloc);
84+
if (!m_func->GetJITFunctionBody()->IsWasmFunction())
85+
{
86+
m_statementReader = Anew(func->m_alloc, Js::StatementReader<Js::FunctionBody::ArenaStatementMapList>);
87+
}
88+
func->m_workItem->InitializeReader(&m_jnReader, m_statementReader, func->m_alloc);
8589
m_asmFuncInfo = m_func->GetJITFunctionBody()->GetAsmJsInfo();
8690
#if 0
8791
// templatized JIT loop body
@@ -217,7 +221,7 @@ class IRBuilderAsmJs
217221
IR::Instr * m_lastInstr;
218222
IR::Instr ** m_offsetToInstruction;
219223
Js::ByteCodeReader m_jnReader;
220-
Js::StatementReader<Js::FunctionBody::ArenaStatementMapList> m_statementReader;
224+
Js::StatementReader<Js::FunctionBody::ArenaStatementMapList>* m_statementReader = nullptr;
221225
SListCounted<IR::Instr *> *m_argStack;
222226
SList<IR::Instr *> * m_tempList;
223227
SList<int32> * m_argOffsetStack;

lib/Backend/JITTimeWorkItem.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,10 @@ JITTimeWorkItem::InitializeReader(
114114
#endif
115115
bool hasSpanSequenceMap = m_jitBody.InitializeStatementMap(&m_statementMap, alloc);
116116
Js::SmallSpanSequence * spanSeq = hasSpanSequenceMap ? &m_statementMap : nullptr;
117-
statementReader->Create(m_jitBody.GetByteCodeBuffer(), startOffset, spanSeq, m_fullStatementList);
117+
if (statementReader)
118+
{
119+
statementReader->Create(m_jitBody.GetByteCodeBuffer(), startOffset, spanSeq, m_fullStatementList);
120+
}
118121
}
119122

120123
JITTimeFunctionBody *

lib/Runtime/ByteCode/StatementReader.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@ namespace Js
1010
class StatementReader
1111
{
1212
private:
13-
const byte* m_startLocation;
14-
SmallSpanSequence* m_statementMap;
13+
const byte* m_startLocation = nullptr;
14+
SmallSpanSequence* m_statementMap = nullptr;
1515
SmallSpanSequenceIter m_statementMapIter;
1616

17-
TStatementMapList * m_fullstatementMap;
18-
const byte* m_nextStatementBoundary;
19-
int m_statementIndex;
20-
bool m_startOfStatement;
17+
TStatementMapList * m_fullstatementMap = nullptr;
18+
const byte* m_nextStatementBoundary = nullptr;
19+
int m_statementIndex = 0;
20+
bool m_startOfStatement = true;
2121

2222
public:
2323
void Create(FunctionBody* functionRead, uint startOffset = 0);

0 commit comments

Comments
 (0)