Core: Fix unitialized memory issues with graphics caches
Vicki Pfau vi@endrift.com
Sat, 16 Nov 2019 12:16:16 -0800
6 files changed,
39 insertions(+),
18 deletions(-)
M
CHANGES
→
CHANGES
@@ -94,6 +94,7 @@ - GBA Video: Fix OpenGL renderer 512x512 backgrounds (fixes mgba.io/i/1572)
- GBA Memory: Fix open bus from IWRAM (fixes mgba.io/i/1575) Other fixes: - 3DS: Fix screen darkening (fixes mgba.io/i/1562) + - Core: Fix uninitialized memory issues with graphics caches - Vita: Fix analog controls (fixes mgba.io/i/1554) - Qt: Fix fast forward mute being reset (fixes mgba.io/i/1574)
M
src/core/bitmap-cache.c
→
src/core/bitmap-cache.c
@@ -11,6 +11,7 @@ void mBitmapCacheInit(struct mBitmapCache* cache) {
// TODO: Reconfigurable cache for space savings cache->cache = NULL; cache->config = mBitmapCacheConfigurationFillShouldStore(0); + cache->sysConfig = 0; cache->status = NULL; cache->palette = NULL; cache->buffer = 0;@@ -18,14 +19,18 @@ }
static void _freeCache(struct mBitmapCache* cache) { size_t size = mBitmapCacheSystemInfoGetHeight(cache->sysConfig) * mBitmapCacheSystemInfoGetBuffers(cache->sysConfig); - mappedMemoryFree(cache->cache, mBitmapCacheSystemInfoGetWidth(cache->sysConfig) * size * sizeof(color_t)); - mappedMemoryFree(cache->status, size * sizeof(*cache->status)); + if (cache->cache) { + mappedMemoryFree(cache->cache, mBitmapCacheSystemInfoGetWidth(cache->sysConfig) * size * sizeof(color_t)); + cache->cache = NULL; + } + if (cache->status) { + mappedMemoryFree(cache->status, size * sizeof(*cache->status)); + cache->status = NULL; + } if (cache->palette) { free(cache->palette); + cache->palette = NULL; } - cache->cache = NULL; - cache->status = NULL; - cache->palette = NULL; } static void _redoCacheSize(struct mBitmapCache* cache) {
M
src/core/map-cache.c
→
src/core/map-cache.c
@@ -11,15 +11,20 @@ void mMapCacheInit(struct mMapCache* cache) {
// TODO: Reconfigurable cache for space savings cache->cache = NULL; cache->config = mMapCacheConfigurationFillShouldStore(0); + cache->sysConfig = 0; cache->status = NULL; } static void _freeCache(struct mMapCache* cache) { size_t tiles = (1 << mMapCacheSystemInfoGetTilesWide(cache->sysConfig)) * (1 << mMapCacheSystemInfoGetTilesHigh(cache->sysConfig)); - mappedMemoryFree(cache->cache, 8 * 8 * sizeof(color_t) * tiles); - mappedMemoryFree(cache->status, tiles * sizeof(*cache->status)); - cache->cache = NULL; - cache->status = NULL; + if (cache->cache) { + mappedMemoryFree(cache->cache, 8 * 8 * sizeof(color_t) * tiles); + cache->cache = NULL; + } + if (cache->status) { + mappedMemoryFree(cache->status, tiles * sizeof(*cache->status)); + cache->status = NULL; + } } static void _redoCacheSize(struct mMapCache* cache) {
M
src/core/tile-cache.c
→
src/core/tile-cache.c
@@ -51,6 +51,9 @@ cache->palette = malloc(size * bpp * sizeof(*cache->palette));
} void mTileCacheConfigure(struct mTileCache* cache, mTileCacheConfiguration config) { + if (cache->config == config) { + return; + } _freeCache(cache); cache->config = config; _redoCacheSize(cache);
M
src/gb/renderers/cache-set.c
→
src/gb/renderers/cache-set.c
@@ -12,11 +12,13 @@ #include <mgba/internal/gb/video.h>
void GBVideoCacheInit(struct mCacheSet* cache) { mCacheSetInit(cache, 2, 0, 1); - mTileCacheConfiguration config = 0; - config = mTileCacheSystemInfoSetPaletteBPP(config, 1); // 2^(2^1) = 4 entries - config = mTileCacheSystemInfoSetPaletteCount(config, 4); // 16 palettes - config = mTileCacheSystemInfoSetMaxTiles(config, 1024); - mTileCacheConfigureSystem(mTileCacheSetGetPointer(&cache->tiles, 0), config, 0, 0); + mTileCacheSystemInfo sysconfig = 0; + mTileCacheConfiguration config = mTileCacheConfigurationFillShouldStore(0); + sysconfig = mTileCacheSystemInfoSetPaletteBPP(sysconfig, 1); // 2^(2^1) = 4 entries + sysconfig = mTileCacheSystemInfoSetPaletteCount(sysconfig, 4); // 16 palettes + sysconfig = mTileCacheSystemInfoSetMaxTiles(sysconfig, 1024); + mTileCacheConfigureSystem(mTileCacheSetGetPointer(&cache->tiles, 0), sysconfig, 0, 0); + mTileCacheConfigure(mTileCacheSetGetPointer(&cache->tiles, 0), config); mMapCacheSetGetPointer(&cache->maps, 0)->tileCache = mTileCacheSetGetPointer(&cache->tiles, 0); mMapCacheSetGetPointer(&cache->maps, 1)->tileCache = mTileCacheSetGetPointer(&cache->tiles, 0);
M
src/gba/renderers/cache-set.c
→
src/gba/renderers/cache-set.c
@@ -17,20 +17,20 @@ mTileCacheConfiguration config = mTileCacheConfigurationFillShouldStore(0);
sysconfig = mTileCacheSystemInfoSetPaletteBPP(sysconfig, 2); // 2^(2^2) = 16 entries sysconfig = mTileCacheSystemInfoSetPaletteCount(sysconfig, 4); // 16 palettes sysconfig = mTileCacheSystemInfoSetMaxTiles(sysconfig, 2048); - mTileCacheConfigure(mTileCacheSetGetPointer(&cache->tiles, 0), config); mTileCacheConfigureSystem(mTileCacheSetGetPointer(&cache->tiles, 0), sysconfig, 0, 0); + mTileCacheConfigure(mTileCacheSetGetPointer(&cache->tiles, 0), config); sysconfig = mTileCacheSystemInfoSetMaxTiles(sysconfig, 1024); - mTileCacheConfigure(mTileCacheSetGetPointer(&cache->tiles, 2), config); mTileCacheConfigureSystem(mTileCacheSetGetPointer(&cache->tiles, 2), sysconfig, 0x10000, 0x100); + mTileCacheConfigure(mTileCacheSetGetPointer(&cache->tiles, 2), config); sysconfig = mTileCacheSystemInfoSetPaletteBPP(sysconfig, 3); // 2^(2^3) = 256 entries sysconfig = mTileCacheSystemInfoSetPaletteCount(sysconfig, 0); // 1 palettes sysconfig = mTileCacheSystemInfoSetMaxTiles(sysconfig, 2048); - mTileCacheConfigure(mTileCacheSetGetPointer(&cache->tiles, 1), config); mTileCacheConfigureSystem(mTileCacheSetGetPointer(&cache->tiles, 1), sysconfig, 0, 0); + mTileCacheConfigure(mTileCacheSetGetPointer(&cache->tiles, 1), config); sysconfig = mTileCacheSystemInfoSetMaxTiles(sysconfig, 1024); - mTileCacheConfigure(mTileCacheSetGetPointer(&cache->tiles, 3), config); mTileCacheConfigureSystem(mTileCacheSetGetPointer(&cache->tiles, 3), sysconfig, 0x10000, 0x100); + mTileCacheConfigure(mTileCacheSetGetPointer(&cache->tiles, 3), config); mBitmapCacheSystemInfo bitConfig; bitConfig = mBitmapCacheSystemInfoSetEntryBPP(0, 4);@@ -50,6 +50,11 @@ bitConfig = mBitmapCacheSystemInfoSetBuffers(bitConfig, 2);
mBitmapCacheConfigureSystem(mBitmapCacheSetGetPointer(&cache->bitmaps, 1), bitConfig); mBitmapCacheSetGetPointer(&cache->bitmaps, 1)->bitsStart[0] = 0; mBitmapCacheSetGetPointer(&cache->bitmaps, 1)->bitsStart[1] = 0xA000; + + mMapCacheSetGetPointer(&cache->maps, 0)->context = NULL; + mMapCacheSetGetPointer(&cache->maps, 1)->context = NULL; + mMapCacheSetGetPointer(&cache->maps, 2)->context = NULL; + mMapCacheSetGetPointer(&cache->maps, 3)->context = NULL; } void GBAVideoCacheAssociate(struct mCacheSet* cache, struct GBAVideo* video) {