From 62b7655a1c77f85f9cec02ba706f8e1affb20634 Mon Sep 17 00:00:00 2001 From: Andy McFadden Date: Sat, 14 Sep 2019 11:40:32 -0700 Subject: [PATCH] Fix handling of data formatting that overlaps with code If you play games with code hints you can create a data operand that overlaps with code. This causes problems (see issue #45). We now check for that situation and ignore overlapping data descriptors. Added a regression test to 2011-hinting. --- SourceGen/DisasmProject.cs | 25 +++++++++++++++++++ SourceGen/RuntimeData/ExportTemplate.html | 4 ++- SourceGen/SGTestData/2011-hinting | 2 +- SourceGen/SGTestData/2011-hinting.dis65 | 12 ++++++--- .../SGTestData/Expected/2011-hinting_64tass.S | 14 ++++++++--- .../Expected/2011-hinting_Merlin32.S | 14 ++++++++--- .../SGTestData/Expected/2011-hinting_acme.S | 14 ++++++++--- .../SGTestData/Expected/2011-hinting_cc65.S | 14 ++++++++--- .../SGTestData/Expected/2011-hinting_cc65.cfg | 2 +- SourceGen/SGTestData/Source/2011-hinting.S | 7 ++++++ 10 files changed, 89 insertions(+), 19 deletions(-) diff --git a/SourceGen/DisasmProject.cs b/SourceGen/DisasmProject.cs index f265fc5..9e74dda 100644 --- a/SourceGen/DisasmProject.cs +++ b/SourceGen/DisasmProject.cs @@ -858,6 +858,31 @@ namespace SourceGen { genLog.LogW("+" + offset.ToString("x6") + ": unexpected mid-instruction format descriptor"); continue; // ignore this one + } else { + // Data or inline data. The data analyzer hasn't run yet. We want to + // confirm that the descriptor doesn't overlap with code. + // + // Data descriptors that overlap code are problematic, for two reasons. + // First, we end up with references to hidden labels, because the code that + // tries to prevent it sees an Anattrib with code at the target address and + // assumes all is well. Second, if the overlap ends partway into an + // instruction, an Anattrib-walker will move from a data region to the middle + // of an instruction, which should never happen. + // + // All instruction bytes have been marked, so we just need to confirm that + // none of the bytes spanned by this descriptor are instructions. + bool overlap = false; + for (int i = offset; i < offset + kvp.Value.Length; i++) { + if (mAnattribs[i].IsInstruction) { + genLog.LogW("+" + offset.ToString("x6") + + ": data format descriptor overlaps code at +" + i.ToString("x6")); + overlap = true; + break; + } + } + if (overlap) { + continue; + } } mAnattribs[offset].DataDescriptor = kvp.Value; diff --git a/SourceGen/RuntimeData/ExportTemplate.html b/SourceGen/RuntimeData/ExportTemplate.html index 7cf1edd..9b52098 100644 --- a/SourceGen/RuntimeData/ExportTemplate.html +++ b/SourceGen/RuntimeData/ExportTemplate.html @@ -16,11 +16,13 @@

$ProjectName$ Disassembly

+ $CodeLines$
-

Symbol Table

+
+

Symbol Table

$SymbolTable$
diff --git a/SourceGen/SGTestData/2011-hinting b/SourceGen/SGTestData/2011-hinting index dd7fdf5..07cb179 100644 --- a/SourceGen/SGTestData/2011-hinting +++ b/SourceGen/SGTestData/2011-hinting @@ -1,2 +1,2 @@ ,,, -,  V$"  ( V$3D 7 :V$` V$Uf` \ No newline at end of file +,  V$"  ( V$3D : =V$ E` V$Uf`` \ No newline at end of file diff --git a/SourceGen/SGTestData/2011-hinting.dis65 b/SourceGen/SGTestData/2011-hinting.dis65 index 55bf376..6b7b8f1 100644 --- a/SourceGen/SGTestData/2011-hinting.dis65 +++ b/SourceGen/SGTestData/2011-hinting.dis65 @@ -1,8 +1,8 @@ ### 6502bench SourceGen dis65 v1.0 ### { -"_ContentVersion":1,"FileDataLength":63,"FileDataCrc32":977869194,"ProjectProps":{ -"CpuName":"6502","IncludeUndocumentedInstr":false,"EntryFlags":33489103,"AnalysisParams":{ -"AnalyzeUncategorizedData":true,"MinCharsForString":4,"SeekNearbyTargets":true}, +"_ContentVersion":2,"FileDataLength":72,"FileDataCrc32":-800253778,"ProjectProps":{ +"CpuName":"6502","IncludeUndocumentedInstr":false,"EntryFlags":33489103,"AutoLabelStyle":"Simple","AnalysisParams":{ +"AnalyzeUncategorizedData":true,"DefaultTextScanMode":"LowHighAscii","MinCharsForString":4,"SeekNearbyTargets":true,"SmartPlpHandling":true}, "PlatformSymbolFileIdentifiers":[],"ExtensionScriptFileIdentifiers":["PROJ:2011-hinting.cs"],"ProjectSyms":{ }}, "AddressMap":[{ @@ -27,4 +27,8 @@ }, "OperandFormats":{ "51":{ -"Length":3,"Format":"Dense","SubFormat":"None","SymbolRef":null}}} +"Length":3,"Format":"Dense","SubFormat":"None","SymbolRef":null}, +"66":{ +"Length":4,"Format":"NumericLE","SubFormat":"Hex","SymbolRef":null}}, +"LvTables":{ +}} diff --git a/SourceGen/SGTestData/Expected/2011-hinting_64tass.S b/SourceGen/SGTestData/Expected/2011-hinting_64tass.S index 16c6d08..6e47d1e 100644 --- a/SourceGen/SGTestData/Expected/2011-hinting_64tass.S +++ b/SourceGen/SGTestData/Expected/2011-hinting_64tass.S @@ -22,14 +22,22 @@ L101B .dword $22a211a9 jsr L1028 jsr $2456 L1028 .dword $44a233a9 - jsr L1037 jsr L103A + jsr L103D nop lda $2456 + jsr L1045 rts -L1037 jsr $2456 -L103A lda #$55 +L103A jsr $2456 +L103D lda #$55 ldx #$66 rts + .byte $81 + .byte $82 + .byte $83 + +L1045 lda #$99 + rts + diff --git a/SourceGen/SGTestData/Expected/2011-hinting_Merlin32.S b/SourceGen/SGTestData/Expected/2011-hinting_Merlin32.S index 7c466a6..02824ca 100644 --- a/SourceGen/SGTestData/Expected/2011-hinting_Merlin32.S +++ b/SourceGen/SGTestData/Expected/2011-hinting_Merlin32.S @@ -21,14 +21,22 @@ L101B adrl $22a211a9 jsr L1028 jsr $2456 L1028 adrl $44a233a9 - jsr L1037 jsr L103A + jsr L103D nop lda $2456 + jsr L1045 rts -L1037 jsr $2456 -L103A lda #$55 +L103A jsr $2456 +L103D lda #$55 ldx #$66 rts + dfb $81 + dfb $82 + dfb $83 + +L1045 lda #$99 + rts + diff --git a/SourceGen/SGTestData/Expected/2011-hinting_acme.S b/SourceGen/SGTestData/Expected/2011-hinting_acme.S index 9091181..bc19c05 100644 --- a/SourceGen/SGTestData/Expected/2011-hinting_acme.S +++ b/SourceGen/SGTestData/Expected/2011-hinting_acme.S @@ -22,14 +22,22 @@ L101B !32 $22a211a9 jsr L1028 jsr $2456 L1028 !32 $44a233a9 - jsr L1037 jsr L103A + jsr L103D nop lda $2456 + jsr L1045 rts -L1037 jsr $2456 -L103A lda #$55 +L103A jsr $2456 +L103D lda #$55 ldx #$66 rts + !byte $81 + !byte $82 + !byte $83 + +L1045 lda #$99 + rts + diff --git a/SourceGen/SGTestData/Expected/2011-hinting_cc65.S b/SourceGen/SGTestData/Expected/2011-hinting_cc65.S index 24036cd..394f242 100644 --- a/SourceGen/SGTestData/Expected/2011-hinting_cc65.S +++ b/SourceGen/SGTestData/Expected/2011-hinting_cc65.S @@ -23,14 +23,22 @@ L101B: .dword $22a211a9 jsr L1028 jsr $2456 L1028: .dword $44a233a9 - jsr L1037 jsr L103A + jsr L103D nop lda $2456 + jsr L1045 rts -L1037: jsr $2456 -L103A: lda #$55 +L103A: jsr $2456 +L103D: lda #$55 ldx #$66 rts + .byte $81 + .byte $82 + .byte $83 + +L1045: lda #$99 + rts + diff --git a/SourceGen/SGTestData/Expected/2011-hinting_cc65.cfg b/SourceGen/SGTestData/Expected/2011-hinting_cc65.cfg index 408483e..e856535 100644 --- a/SourceGen/SGTestData/Expected/2011-hinting_cc65.cfg +++ b/SourceGen/SGTestData/Expected/2011-hinting_cc65.cfg @@ -1,7 +1,7 @@ # 6502bench SourceGen generated linker script for 2011-hinting MEMORY { MAIN: file=%O, start=%S, size=65536; -# MEM000: file=%O, start=$1000, size=63; +# MEM000: file=%O, start=$1000, size=72; } SEGMENTS { CODE: load=MAIN, type=rw; diff --git a/SourceGen/SGTestData/Source/2011-hinting.S b/SourceGen/SGTestData/Source/2011-hinting.S index 8da7cb1..6f5dd0a 100644 --- a/SourceGen/SGTestData/Source/2011-hinting.S +++ b/SourceGen/SGTestData/Source/2011-hinting.S @@ -51,6 +51,8 @@ magic33 lda #$33 lda MAGIC ;EDIT: hint as data, format as dense hex, remove hint + jsr dataolap + rts @@ -59,3 +61,8 @@ part2 lda #$55 ldx #$66 rts +; Make sure that data descriptors that overlap with code are ignored. + hex 818283 ;EDIT: format as 4-byte int, so it overlaps with dataolap +dataolap lda #$99 + rts +