From 91e8ff01cfc234d72fbc38603914b0ae8f92bc0c Mon Sep 17 00:00:00 2001 From: tildearrow Date: Thu, 13 Jan 2022 19:36:02 -0500 Subject: [PATCH] add notifyInsDeletion prevent invalid memory access when deleting instruments --- src/engine/macroInt.cpp | 6 ++++++ src/engine/macroInt.h | 1 + src/engine/platform/ay.cpp | 6 ++++++ src/engine/platform/ay.h | 1 + src/engine/platform/c64.cpp | 6 ++++++ src/engine/platform/c64.h | 1 + src/engine/platform/gb.cpp | 6 ++++++ src/engine/platform/gb.h | 1 + src/engine/platform/genesis.cpp | 4 ++++ src/engine/platform/genesis.h | 1 + src/engine/platform/nes.cpp | 6 ++++++ src/engine/platform/nes.h | 1 + src/engine/platform/pce.cpp | 6 ++++++ src/engine/platform/pce.h | 1 + src/engine/platform/sms.cpp | 6 ++++++ src/engine/platform/sms.h | 1 + src/engine/platform/ym2610.cpp | 6 ++++++ src/engine/platform/ym2610.h | 1 + 18 files changed, 61 insertions(+) diff --git a/src/engine/macroInt.cpp b/src/engine/macroInt.cpp index a7abda2c6..42b1dce5b 100644 --- a/src/engine/macroInt.cpp +++ b/src/engine/macroInt.cpp @@ -215,3 +215,9 @@ void DivMacroInt::init(DivInstrument* which) { arpMode=true; } } + +void DivMacroInt::notifyInsDeletion(DivInstrument* which) { + if (ins==which) { + init(NULL); + } +} \ No newline at end of file diff --git a/src/engine/macroInt.h b/src/engine/macroInt.h index b486b4e3b..63b34471f 100644 --- a/src/engine/macroInt.h +++ b/src/engine/macroInt.h @@ -17,6 +17,7 @@ class DivMacroInt { bool arpMode; void next(); void init(DivInstrument* which); + void notifyInsDeletion(DivInstrument* which); DivMacroInt(): ins(NULL), volPos(0), diff --git a/src/engine/platform/ay.cpp b/src/engine/platform/ay.cpp index 5383aef60..328f5de48 100644 --- a/src/engine/platform/ay.cpp +++ b/src/engine/platform/ay.cpp @@ -331,6 +331,12 @@ bool DivPlatformAY8910::keyOffAffectsArp(int ch) { return true; } +void DivPlatformAY8910::notifyInsDeletion(void* ins) { + for (int i=0; i<3; i++) { + chan[i].std.notifyInsDeletion((DivInstrument*)ins); + } +} + int DivPlatformAY8910::init(DivEngine* p, int channels, int sugRate, bool pal) { parent=p; skipRegisterWrites=false; diff --git a/src/engine/platform/ay.h b/src/engine/platform/ay.h index 4f98379eb..e240c29c5 100644 --- a/src/engine/platform/ay.h +++ b/src/engine/platform/ay.h @@ -59,6 +59,7 @@ class DivPlatformAY8910: public DivDispatch { void muteChannel(int ch, bool mute); bool isStereo(); bool keyOffAffectsArp(int ch); + void notifyInsDeletion(void* ins); int init(DivEngine* parent, int channels, int sugRate, bool pal); void quit(); }; diff --git a/src/engine/platform/c64.cpp b/src/engine/platform/c64.cpp index bbda090d5..6265cacb7 100644 --- a/src/engine/platform/c64.cpp +++ b/src/engine/platform/c64.cpp @@ -311,6 +311,12 @@ void DivPlatformC64::forceIns() { updateFilter(); } +void DivPlatformC64::notifyInsDeletion(void* ins) { + for (int i=0; i<3; i++) { + chan[i].std.notifyInsDeletion((DivInstrument*)ins); + } +} + void DivPlatformC64::reset() { for (int i=0; i<3; i++) { chan[i]=DivPlatformC64::Channel(); diff --git a/src/engine/platform/c64.h b/src/engine/platform/c64.h index 9638bd57c..699a4159b 100644 --- a/src/engine/platform/c64.h +++ b/src/engine/platform/c64.h @@ -61,6 +61,7 @@ class DivPlatformC64: public DivDispatch { void tick(); void muteChannel(int ch, bool mute); void setPAL(bool pal); + void notifyInsDeletion(void* ins); int init(DivEngine* parent, int channels, int sugRate, bool pal); void setChipModel(bool is6581); void quit(); diff --git a/src/engine/platform/gb.cpp b/src/engine/platform/gb.cpp index 0a8c4fa8c..a1affa876 100644 --- a/src/engine/platform/gb.cpp +++ b/src/engine/platform/gb.cpp @@ -317,6 +317,12 @@ bool DivPlatformGB::isStereo() { return true; } +void DivPlatformGB::notifyInsDeletion(void* ins) { + for (int i=0; i<4; i++) { + chan[i].std.notifyInsDeletion((DivInstrument*)ins); + } +} + int DivPlatformGB::init(DivEngine* p, int channels, int sugRate, bool pal) { for (int i=0; i<4; i++) { isMuted[i]=false; diff --git a/src/engine/platform/gb.h b/src/engine/platform/gb.h index 7cb9624b7..55028597a 100644 --- a/src/engine/platform/gb.h +++ b/src/engine/platform/gb.h @@ -45,6 +45,7 @@ class DivPlatformGB: public DivDispatch { void tick(); void muteChannel(int ch, bool mute); bool isStereo(); + void notifyInsDeletion(void* ins); int init(DivEngine* parent, int channels, int sugRate, bool pal); void quit(); ~DivPlatformGB(); diff --git a/src/engine/platform/genesis.cpp b/src/engine/platform/genesis.cpp index 203a4c006..1cb1732fe 100644 --- a/src/engine/platform/genesis.cpp +++ b/src/engine/platform/genesis.cpp @@ -423,6 +423,10 @@ bool DivPlatformGenesis::keyOffAffectsPorta(int ch) { return (ch>5); } +void DivPlatformGenesis::notifyInsDeletion(void* ins) { + psg.notifyInsDeletion(ins); +} + void DivPlatformGenesis::setPAL(bool pal) { if (pal) { rate=211125; diff --git a/src/engine/platform/genesis.h b/src/engine/platform/genesis.h index 53d731c3f..a1ead27a6 100644 --- a/src/engine/platform/genesis.h +++ b/src/engine/platform/genesis.h @@ -60,6 +60,7 @@ class DivPlatformGenesis: public DivDispatch { bool keyOffAffectsArp(int ch); bool keyOffAffectsPorta(int ch); void setPAL(bool pal); + void notifyInsDeletion(void* ins); int init(DivEngine* parent, int channels, int sugRate, bool pal); void quit(); ~DivPlatformGenesis(); diff --git a/src/engine/platform/nes.cpp b/src/engine/platform/nes.cpp index d26326db9..0b70a6c88 100644 --- a/src/engine/platform/nes.cpp +++ b/src/engine/platform/nes.cpp @@ -324,6 +324,12 @@ void DivPlatformNES::setPAL(bool pal) { } } +void DivPlatformNES::notifyInsDeletion(void* ins) { + for (int i=0; i<5; i++) { + chan[i].std.notifyInsDeletion((DivInstrument*)ins); + } +} + int DivPlatformNES::init(DivEngine* p, int channels, int sugRate, bool pal) { parent=p; skipRegisterWrites=false; diff --git a/src/engine/platform/nes.h b/src/engine/platform/nes.h index 133218201..b3d3ce467 100644 --- a/src/engine/platform/nes.h +++ b/src/engine/platform/nes.h @@ -50,6 +50,7 @@ class DivPlatformNES: public DivDispatch { void muteChannel(int ch, bool mute); bool keyOffAffectsArp(int ch); void setPAL(bool pal); + void notifyInsDeletion(void* ins); int init(DivEngine* parent, int channels, int sugRate, bool pal); void quit(); ~DivPlatformNES(); diff --git a/src/engine/platform/pce.cpp b/src/engine/platform/pce.cpp index 1128622bc..73150ec01 100644 --- a/src/engine/platform/pce.cpp +++ b/src/engine/platform/pce.cpp @@ -313,6 +313,12 @@ bool DivPlatformPCE::keyOffAffectsArp(int ch) { return true; } +void DivPlatformPCE::notifyInsDeletion(void* ins) { + for (int i=0; i<6; i++) { + chan[i].std.notifyInsDeletion((DivInstrument*)ins); + } +} + void DivPlatformPCE::setPAL(bool pal) { if (pal) { // technically there is no PAL PC Engine but oh well... rate=1773448/6; diff --git a/src/engine/platform/pce.h b/src/engine/platform/pce.h index 930c9b59e..e67c5a847 100644 --- a/src/engine/platform/pce.h +++ b/src/engine/platform/pce.h @@ -65,6 +65,7 @@ class DivPlatformPCE: public DivDispatch { bool isStereo(); bool keyOffAffectsArp(int ch); void setPAL(bool pal); + void notifyInsDeletion(void* ins); int init(DivEngine* parent, int channels, int sugRate, bool pal); void quit(); ~DivPlatformPCE(); diff --git a/src/engine/platform/sms.cpp b/src/engine/platform/sms.cpp index 7b072b983..befd8add4 100644 --- a/src/engine/platform/sms.cpp +++ b/src/engine/platform/sms.cpp @@ -193,6 +193,12 @@ int DivPlatformSMS::getPortaFloor(int ch) { return 12; } +void DivPlatformSMS::notifyInsDeletion(void* ins) { + for (int i=0; i<4; i++) { + chan[i].std.notifyInsDeletion((DivInstrument*)ins); + } +} + void DivPlatformSMS::setPAL(bool pal) { if (pal) { rate=221681; diff --git a/src/engine/platform/sms.h b/src/engine/platform/sms.h index 3b5ac6403..c74d1a44a 100644 --- a/src/engine/platform/sms.h +++ b/src/engine/platform/sms.h @@ -42,6 +42,7 @@ class DivPlatformSMS: public DivDispatch { bool keyOffAffectsPorta(int ch); int getPortaFloor(int ch); void setPAL(bool pal); + void notifyInsDeletion(void* ins); int init(DivEngine* parent, int channels, int sugRate, bool pal); void quit(); ~DivPlatformSMS(); diff --git a/src/engine/platform/ym2610.cpp b/src/engine/platform/ym2610.cpp index 84c3e39f9..ac15dec76 100644 --- a/src/engine/platform/ym2610.cpp +++ b/src/engine/platform/ym2610.cpp @@ -589,6 +589,12 @@ bool DivPlatformYM2610::keyOffAffectsArp(int ch) { return (ch>3); } +void DivPlatformYM2610::notifyInsDeletion(void* ins) { + for (int i=4; i<7; i++) { + chan[i].std.notifyInsDeletion((DivInstrument*)ins); + } +} + int DivPlatformYM2610::init(DivEngine* p, int channels, int sugRate, bool pal) { parent=p; skipRegisterWrites=false; diff --git a/src/engine/platform/ym2610.h b/src/engine/platform/ym2610.h index 6a31632b8..b3a1c3e8d 100644 --- a/src/engine/platform/ym2610.h +++ b/src/engine/platform/ym2610.h @@ -71,6 +71,7 @@ class DivPlatformYM2610: public DivDispatch { void muteChannel(int ch, bool mute); bool isStereo(); bool keyOffAffectsArp(int ch); + void notifyInsDeletion(void* ins); int init(DivEngine* parent, int channels, int sugRate, bool pal); void quit(); ~DivPlatformYM2610();