Poke_Transporter_GB/include/libstd_replacements.h
Philippe Symons eef173b0d2 Fix crash + unrelated buffer overflow + some optimizations
There was a crash happening with ptgb::vector when you'd press A on the CONFIRM button of the box screen. It only occurred on actual gba hardware and
was a real heisenbug: as soon as you'd add code to display logs on screen, the problem would disappear. So it was very difficult to figure this one
out. We're not even entirely sure why, but it looks like the malloc/realloc/free use in ptgb::vector would cause issues.

Maybe it was alignment, but after messing with the code we also saw a warning appear in the terminal telling us that realloc wouldn't properly
deal with non-POD types. It complained about this very thing while referring to the add_track() function, which stores ptgb::vectors inside another
ptgb::vector. We also didn't have a custom copy constructor yet to actually copy the buffer instead of its pointer.
All of these could potentially have led to the crash. But debugging during the link cable flow was difficult, so we were never able to confirm it in
a debugger, log or dump.

Because I suspected the high IWRAM consumption (especially now with ZX0 decompression) for a while, I also did an optimization in mystery_gift_builder
to pass global_memory_buffer as its section_30_data buffer instead. This reduces IWRAM consumption by 4 KB.

There was another problem I discovered during my crash hunt: the out_array (now payload_buffer) was allocated as a 672 byte array, but the payloads
were actually 707 bytes. Therefore writing this to the buffer caused a buffer overflow, thereby corrupting the global variables appearing after it in
IWRAM. It turned out eventually that none of these variables were really critical, but it could explain some minor bugs GearsProgress has seen.

I also did a few performance optimizations:

- At various stages in the code, for loops were used to copy data from one buffer into another byte-by-byte. This was far from optimal because the gba
cpu can load/copy 4 bytes at a time if you ask it to. So I replaced those with memcpy(), which is a hand-optimized assembly function to copy data
using this principle.

- generate_payload was being called twice: once at start_link and once at continue_link, giving the exact same result, even though it was already
being stored in a global buffer allocated in IWRAM. This was also a fairly heavy function. So I optimized the code to only initialize it once in
the script chain and then just retrieve the buffer.

- generate_payload was constructing the eventual payload twice even within the same call. That's because it first merged z80_rng_seed, z80_payload
and z80_patchlist into a full_data ptgb::vector, after which it then copied the data again to out_array (now called payload_buffer). I eliminated the
full_data vector now.
2025-06-18 10:23:03 +02:00

225 lines
6.0 KiB
C++

#ifndef _LIBSTD_REPLACEMENTS_H
#define _LIBSTD_REPLACEMENTS_H
#include <inttypes.h>
#include <cstring>
#include <new>
#include <utility>
// To reduce the binary size, we need to get rid of libstdc++
// But we were happily using some functions that made life easier.
// so this file was created to provide some similar functions/functionalities
// (although stripped down to the bare minimum we're using)
namespace ptgb
{
/**
* This utility/convenience function converts the given value to a string.
* WARNING: it AND its other variants always use the same buffer.
* So calling it more than once will overwrite the previously returned string!
*
* Be careful!
* In case you want to convert more than one int to string within the same call,
* consider using npf_snprintf instead!
*
* Also: if you want to hold on to the converted value, consider copying the returned string
*/
const char* to_string(int intVal);
/**
* This utility/convenience function converts the given value to a string.
* WARNING: it AND its other variants always use the same buffer.
* So calling it more than once will overwrite the previously returned string!
*
* Be careful!
* In case you want to convert more than one int to string within the same call,
* consider using npf_snprintf instead!
*
* Also: if you want to hold on to the converted value, consider copying the returned string
*/
const char* to_string(unsigned int wordVal);
template <class ValueType>
class vector
{
public:
static constexpr uint16_t default_capacity = 2;
vector()
: buffer_()
, capacity_(default_capacity)
, count_(0)
{
buffer_ = static_cast<ValueType*>(::operator new(sizeof(ValueType) * capacity_));
if(!buffer_)
{
return; // allocation failed
}
}
vector(const ValueType* valueList, uint16_t listSize)
: buffer_()
, capacity_(listSize)
, count_(0)
{
buffer_ = static_cast<ValueType*>(::operator new(sizeof(ValueType) * capacity_));
if(!buffer_)
{
return; // allocation failed
}
insert(valueList, listSize);
}
vector(const vector<ValueType>& otherList)
: buffer_()
, capacity_(otherList.capacity_)
, count_(0)
{
buffer_ = static_cast<ValueType*>(::operator new(sizeof(ValueType) * capacity_));
if(!buffer_)
{
return; // allocation failed
}
insert(otherList);
}
~vector()
{
clear();
::operator delete(buffer_);
}
void reserve(uint16_t newSize)
{
if(newSize <= capacity_)
{
return;
}
ValueType* new_buffer = static_cast<ValueType*>(::operator new(sizeof(ValueType) * newSize));
if(!new_buffer)
{
return; // allocation failed, keep the old buffer
}
// copy data from the old buffer to the new one
for (uint16_t i = 0; i < count_; ++i)
{
new (new_buffer + i) ValueType(std::move(buffer_[i]));
buffer_[i].~ValueType();
}
::operator delete(buffer_);
buffer_ = new_buffer;
capacity_ = newSize;
}
void resize(uint16_t newSize)
{
resize(newSize, ValueType());
}
void resize(uint16_t newSize, const ValueType& value)
{
if(newSize < count_)
{
const uint16_t num_erase = (count_ - newSize);
for(uint16_t i=0; i < num_erase; ++i)
{
pop_back();
}
}
else if(newSize > count_)
{
reserve(newSize);
const uint16_t num_fill = (newSize - count_);
for(uint16_t i=0; i < num_fill; ++i)
{
push_back(value);
}
}
}
void push_back(const ValueType& value)
{
if(count_ == capacity_)
{
reserve(capacity_ * 2);
}
new(buffer_ + count_) ValueType(value);
++count_;
}
void pop_back()
{
if (count_ == 0) return;
ValueType *value = buffer_ + (count_ - 1);
value->~ValueType();
--count_;
}
void insert(const vector<ValueType>& otherList)
{
reserve(count_ + otherList.size());
for(uint16_t i=0; i < otherList.size(); ++i)
{
push_back(otherList.at(i));
}
}
void insert(const ValueType* list, uint16_t listSize)
{
reserve(count_ + listSize);
for(size_t i=0; i < listSize; ++i)
{
push_back(list[i]);
}
}
void clear()
{
resize(0);
}
uint16_t size() const { return count_; }
uint16_t capacity() const { return capacity_; }
ValueType* data() const
{
return buffer_;
}
ValueType* data()
{
return buffer_;
}
ValueType& at(uint16_t index)
{
return operator[](index);
}
const ValueType& at(uint16_t index) const
{
return operator[](index);
}
ValueType& operator[](uint16_t index)
{
return *(buffer_ + index);
}
const ValueType& operator[](uint16_t index) const
{
return *(buffer_ + index);
}
private:
ValueType *buffer_;
uint16_t capacity_;
uint16_t count_;
};
}
#endif