Skip to content

Commit cbc66f9

Browse files
committed
Fix operand list iterators being validated when appending new instructions
The iterators now store an offset into the operand storage, rather than a pointer. Deferencing the iterator retrieves the value at that offset from the IL function. This issue existed prior to the operand list storage refactor, but became easier to hit after that change. The separate operand list vector is smaller and thus more likely to reallocate when a new instruction is appended.
1 parent eeceddf commit cbc66f9

7 files changed

Lines changed: 47 additions & 35 deletions

binaryninjacore.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,14 @@
3737
// Current ABI version for linking to the core. This is incremented any time
3838
// there are changes to the API that affect linking, including new functions,
3939
// new types, or modifications to existing functions or types.
40-
#define BN_CURRENT_CORE_ABI_VERSION 160
40+
#define BN_CURRENT_CORE_ABI_VERSION 161
4141

4242
// Minimum ABI version that is supported for loading of plugins. Plugins that
4343
// are linked to an ABI version less than this will not be able to load and
4444
// will require rebuilding. The minimum version is increased when there are
4545
// incompatible changes that break binary compatibility, such as changes to
4646
// existing types or functions.
47-
#define BN_MINIMUM_CORE_ABI_VERSION 160
47+
#define BN_MINIMUM_CORE_ABI_VERSION 161
4848

4949
#ifdef __GNUC__
5050
#ifdef BINARYNINJACORE_LIBRARY
@@ -6445,7 +6445,7 @@ extern "C"
64456445
BINARYNINJACOREAPI uint64_t* BNLowLevelILGetOperandList(
64466446
BNLowLevelILFunction* func, size_t expr, size_t operand, size_t* count);
64476447
BINARYNINJACOREAPI void BNLowLevelILFreeOperandList(uint64_t* operands);
6448-
BINARYNINJACOREAPI const uint64_t* BNLowLevelILGetOperandPointer(
6448+
BINARYNINJACOREAPI uint64_t BNLowLevelILGetOperand(
64496449
BNLowLevelILFunction* func, size_t offset);
64506450

64516451
BINARYNINJACOREAPI size_t BNCacheLowLevelILPossibleValueSet(BNLowLevelILFunction* func, BNPossibleValueSet* pvs);
@@ -6604,7 +6604,7 @@ extern "C"
66046604
BINARYNINJACOREAPI uint64_t* BNMediumLevelILGetOperandList(
66056605
BNMediumLevelILFunction* func, size_t expr, size_t operand, size_t* count);
66066606
BINARYNINJACOREAPI void BNMediumLevelILFreeOperandList(uint64_t* operands);
6607-
BINARYNINJACOREAPI const uint64_t* BNMediumLevelILGetOperandPointer(
6607+
BINARYNINJACOREAPI uint64_t BNMediumLevelILGetOperand(
66086608
BNMediumLevelILFunction* func, size_t offset);
66096609

66106610
BINARYNINJACOREAPI size_t BNCacheMediumLevelILPossibleValueSet(BNMediumLevelILFunction* func, BNPossibleValueSet* pvs);
@@ -6767,7 +6767,7 @@ extern "C"
67676767
BINARYNINJACOREAPI uint64_t* BNHighLevelILGetOperandList(
67686768
BNHighLevelILFunction* func, size_t expr, size_t operand, size_t* count);
67696769
BINARYNINJACOREAPI void BNHighLevelILFreeOperandList(uint64_t* operands);
6770-
BINARYNINJACOREAPI const uint64_t* BNHighLevelILGetOperandPointer(
6770+
BINARYNINJACOREAPI uint64_t BNHighLevelILGetOperand(
67716771
BNHighLevelILFunction* func, size_t offset);
67726772

67736773
BINARYNINJACOREAPI size_t BNCacheHighLevelILPossibleValueSet(BNHighLevelILFunction* func, BNPossibleValueSet* pvs);

highlevelilinstruction.cpp

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -288,26 +288,26 @@ bool HighLevelILIntegerList::ListIterator::operator<(const ListIterator& a) cons
288288
HighLevelILIntegerList::ListIterator& HighLevelILIntegerList::ListIterator::operator++()
289289
{
290290
count--;
291-
cur++;
291+
offset++;
292292
return *this;
293293
}
294294

295295

296296
uint64_t HighLevelILIntegerList::ListIterator::operator*()
297297
{
298-
return *cur;
298+
#ifdef BINARYNINJACORE_LIBRARY
299+
return function->GetOperand(offset);
300+
#else
301+
return BNHighLevelILGetOperand(function->GetObject(), offset);
302+
#endif
299303
}
300304

301305

302306
HighLevelILIntegerList::HighLevelILIntegerList(
303307
HighLevelILFunction* func, size_t offset, size_t count)
304308
{
305309
m_start.function = func;
306-
#ifdef BINARYNINJACORE_LIBRARY
307-
m_start.cur = func->GetOperandPointer(offset);
308-
#else
309-
m_start.cur = BNHighLevelILGetOperandPointer(func->GetObject(), offset);
310-
#endif
310+
m_start.offset = offset;
311311
m_start.count = count;
312312
}
313313

@@ -322,7 +322,7 @@ HighLevelILIntegerList::const_iterator HighLevelILIntegerList::end() const
322322
{
323323
const_iterator result;
324324
result.function = m_start.function;
325-
result.cur = m_start.cur + m_start.count;
325+
result.offset = m_start.offset + m_start.count;
326326
result.count = 0;
327327
return result;
328328
}
@@ -338,7 +338,11 @@ uint64_t HighLevelILIntegerList::operator[](size_t i) const
338338
{
339339
if (i >= size())
340340
throw HighLevelILInstructionAccessException();
341-
return m_start.cur[i];
341+
#ifdef BINARYNINJACORE_LIBRARY
342+
return m_start.function->GetOperand(m_start.offset + i);
343+
#else
344+
return BNHighLevelILGetOperand(m_start.function->GetObject(), m_start.offset + i);
345+
#endif
342346
}
343347

344348

highlevelilinstruction.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ namespace BinaryNinja
196196
#else
197197
Ref<HighLevelILFunction> function;
198198
#endif
199-
const uint64_t* cur;
199+
size_t offset;
200200
size_t count;
201201

202202
bool operator==(const ListIterator& a) const;

lowlevelilinstruction.cpp

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -597,26 +597,26 @@ bool LowLevelILIntegerList::ListIterator::operator<(const ListIterator& a) const
597597
LowLevelILIntegerList::ListIterator& LowLevelILIntegerList::ListIterator::operator++()
598598
{
599599
count--;
600-
cur++;
600+
offset++;
601601
return *this;
602602
}
603603

604604

605605
uint64_t LowLevelILIntegerList::ListIterator::operator*()
606606
{
607-
return *cur;
607+
#ifdef BINARYNINJACORE_LIBRARY
608+
return function->GetOperand(offset);
609+
#else
610+
return BNLowLevelILGetOperand(function->GetObject(), offset);
611+
#endif
608612
}
609613

610614

611615
LowLevelILIntegerList::LowLevelILIntegerList(
612616
LowLevelILFunction* func, size_t offset, size_t count)
613617
{
614618
m_start.function = func;
615-
#ifdef BINARYNINJACORE_LIBRARY
616-
m_start.cur = func->GetOperandPointer(offset);
617-
#else
618-
m_start.cur = BNLowLevelILGetOperandPointer(func->GetObject(), offset);
619-
#endif
619+
m_start.offset = offset;
620620
m_start.count = count;
621621
}
622622

@@ -631,7 +631,7 @@ LowLevelILIntegerList::const_iterator LowLevelILIntegerList::end() const
631631
{
632632
const_iterator result;
633633
result.function = m_start.function;
634-
result.cur = m_start.cur + m_start.count;
634+
result.offset = m_start.offset + m_start.count;
635635
result.count = 0;
636636
return result;
637637
}
@@ -647,7 +647,11 @@ uint64_t LowLevelILIntegerList::operator[](size_t i) const
647647
{
648648
if (i >= size())
649649
throw LowLevelILInstructionAccessException();
650-
return m_start.cur[i];
650+
#ifdef BINARYNINJACORE_LIBRARY
651+
return m_start.function->GetOperand(m_start.offset + i);
652+
#else
653+
return BNLowLevelILGetOperand(m_start.function->GetObject(), m_start.offset + i);
654+
#endif
651655
}
652656

653657

lowlevelilinstruction.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,7 @@ namespace BinaryNinja
385385
#else
386386
Ref<LowLevelILFunction> function;
387387
#endif
388-
const uint64_t* cur;
388+
size_t offset;
389389
size_t count;
390390

391391
bool operator==(const ListIterator& a) const;

mediumlevelilinstruction.cpp

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -320,26 +320,26 @@ bool MediumLevelILIntegerList::ListIterator::operator<(const ListIterator& a) co
320320
MediumLevelILIntegerList::ListIterator& MediumLevelILIntegerList::ListIterator::operator++()
321321
{
322322
count--;
323-
cur++;
323+
offset++;
324324
return *this;
325325
}
326326

327327

328328
uint64_t MediumLevelILIntegerList::ListIterator::operator*()
329329
{
330-
return *cur;
330+
#ifdef BINARYNINJACORE_LIBRARY
331+
return function->GetOperand(offset);
332+
#else
333+
return BNMediumLevelILGetOperand(function->GetObject(), offset);
334+
#endif
331335
}
332336

333337

334338
MediumLevelILIntegerList::MediumLevelILIntegerList(
335339
MediumLevelILFunction* func, size_t offset, size_t count)
336340
{
337341
m_start.function = func;
338-
#ifdef BINARYNINJACORE_LIBRARY
339-
m_start.cur = func->GetOperandPointer(offset);
340-
#else
341-
m_start.cur = BNMediumLevelILGetOperandPointer(func->GetObject(), offset);
342-
#endif
342+
m_start.offset = offset;
343343
m_start.count = count;
344344
}
345345

@@ -354,7 +354,7 @@ MediumLevelILIntegerList::const_iterator MediumLevelILIntegerList::end() const
354354
{
355355
const_iterator result;
356356
result.function = m_start.function;
357-
result.cur = m_start.cur + m_start.count;
357+
result.offset = m_start.offset + m_start.count;
358358
result.count = 0;
359359
return result;
360360
}
@@ -370,7 +370,11 @@ uint64_t MediumLevelILIntegerList::operator[](size_t i) const
370370
{
371371
if (i >= size())
372372
throw MediumLevelILInstructionAccessException();
373-
return m_start.cur[i];
373+
#ifdef BINARYNINJACORE_LIBRARY
374+
return m_start.function->GetOperand(m_start.offset + i);
375+
#else
376+
return BNMediumLevelILGetOperand(m_start.function->GetObject(), m_start.offset + i);
377+
#endif
374378
}
375379

376380

mediumlevelilinstruction.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ namespace BinaryNinja
247247
#else
248248
Ref<MediumLevelILFunction> function;
249249
#endif
250-
const uint64_t* cur;
250+
size_t offset;
251251
size_t count;
252252

253253
bool operator==(const ListIterator& a) const;

0 commit comments

Comments
 (0)