soldb-ethdebug 0.4.5

ETHDebug artifact loading, legacy source-map parsing, and ABI/event decoding for SolDB
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
//! The legacy `srcmap` debug format.
//!
//! Solidity compilers can emit source mappings as a compact `s:l:f:j:m` string alongside
//! the bytecode, with fields inherited from the previous entry when omitted. This module
//! parses that format and builds the program-counter index for it, which requires walking
//! the bytecode so `PUSH` immediates are not mistaken for opcodes.
//!
//! This is a fallback path. ETHDebug is the primary source of debug information and
//! carries strictly more of it; prefer it whenever the compiler can produce it.

use std::borrow::Cow;
use std::collections::BTreeMap;
use std::fs;
use std::path::{Path, PathBuf};

use serde::{Deserialize, Serialize};
use serde_json::{json, Value};
use soldb_core::{SoldbError, SoldbResult};

use crate::opcodes;
use crate::storage_layout::StorageLayout;
use crate::{EthdebugInfo, Instruction, SourceLocation, SourceMapContext, SourceMapJump};

/// The bytecode environment described by a legacy source map.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SourceMapEnvironment {
    Creation,
    Runtime,
}

impl SourceMapEnvironment {
    fn fields(self) -> (&'static str, &'static str, &'static str) {
        match self {
            Self::Creation => ("srcmap", "bin", "create"),
            Self::Runtime => ("srcmap-runtime", "bin-runtime", "call"),
        }
    }
}

/// A legacy source-map program adapted to the common debug-info model.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SourceMapProgram {
    pub info: EthdebugInfo,
    pub resources: Value,
    pub source_contents: BTreeMap<u64, String>,
    /// The file each source was read from, which may be above the artifact directory.
    pub source_files: BTreeMap<u64, PathBuf>,
    /// The contract's `storage-layout` entry, when the artifact was compiled with it.
    pub storage_layout: Option<StorageLayout>,
    /// The contract's `transient-storage-layout` entry, when compiled with it.
    pub transient_storage_layout: Option<StorageLayout>,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct SourceMapEntry {
    pub offset: i64,
    pub length: i64,
    pub file_index: i64,
    pub jump_type: String,
    pub modifier_depth: i64,
}

impl SourceMapEntry {
    #[must_use]
    pub fn is_valid(&self) -> bool {
        self.file_index >= 0 && self.offset >= 0
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct SourceMapInfo {
    pub contract_name: String,
    pub sources: Vec<String>,
    pub bytecode: Vec<u8>,
    pub srcmap_entries: Vec<SourceMapEntry>,
    pub pc_to_instruction_index: BTreeMap<usize, usize>,
    pub compiler_version: Option<String>,
}

impl SourceMapInfo {
    #[must_use]
    pub fn source_entry_at_pc(&self, pc: usize) -> Option<&SourceMapEntry> {
        let instruction_index = self.pc_to_instruction_index.get(&pc)?;
        self.srcmap_entries.get(*instruction_index)
    }

    #[must_use]
    pub fn source_info(&self, pc: usize) -> Option<(&str, i64, i64)> {
        let entry = self.source_entry_at_pc(pc)?;
        if !entry.is_valid() {
            return None;
        }

        let source = self.sources.get(usize::try_from(entry.file_index).ok()?)?;
        Some((source, entry.offset, entry.length))
    }
}

pub fn build_pc_to_instruction_map(bytecode: &[u8]) -> BTreeMap<usize, usize> {
    let mut pc_to_index = BTreeMap::new();
    let mut pc = 0;
    let mut instruction_index = 0;

    while pc < bytecode.len() {
        pc_to_index.insert(pc, instruction_index);
        pc += 1 + opcodes::immediate_size(bytecode[pc]);
        instruction_index += 1;
    }

    pc_to_index
}

pub fn parse_srcmap(srcmap: &str) -> SoldbResult<Vec<SourceMapEntry>> {
    if srcmap.is_empty() {
        return Ok(Vec::new());
    }

    let mut entries = Vec::new();
    let mut previous = SourceMapEntry {
        offset: 0,
        length: 0,
        file_index: -1,
        jump_type: "-".to_owned(),
        modifier_depth: 0,
    };

    for part in srcmap.split(';') {
        if part.is_empty() {
            entries.push(previous.clone());
            continue;
        }

        let fields = part.split(':').collect::<Vec<_>>();
        let entry = SourceMapEntry {
            offset: parse_inherited_i64(fields.first().copied(), previous.offset, "offset")?,
            length: parse_inherited_i64(fields.get(1).copied(), previous.length, "length")?,
            file_index: parse_inherited_i64(
                fields.get(2).copied(),
                previous.file_index,
                "file index",
            )?,
            jump_type: parse_inherited_string(fields.get(3).copied(), &previous.jump_type),
            modifier_depth: parse_inherited_i64(
                fields.get(4).copied(),
                previous.modifier_depth,
                "modifier depth",
            )?,
        };

        previous = entry.clone();
        entries.push(entry);
    }

    Ok(entries)
}

/// Loads one program from `combined.json` when the requested source map exists.
///
/// `Ok(None)` means the directory has no legacy artifact, or the selected contract
/// does not contain that creation/runtime map. A present but malformed artifact is
/// always an error so callers cannot silently lose source attribution.
pub fn load_source_map_program(
    root: &Path,
    contract_name: &str,
    environment: SourceMapEnvironment,
) -> SoldbResult<Option<SourceMapProgram>> {
    load_source_map_program_with_sources(root, &[], contract_name, environment)
}

/// The same, also looking for the sources it names in `source_roots`.
pub fn load_source_map_program_with_sources(
    root: &Path,
    source_roots: &[PathBuf],
    contract_name: &str,
    environment: SourceMapEnvironment,
) -> SoldbResult<Option<SourceMapProgram>> {
    let path = root.join("combined.json");
    if !path.exists() {
        return Ok(None);
    }

    let input = fs::read_to_string(&path).map_err(|error| {
        SoldbError::Message(format!("failed to read `{}`: {error}", path.display()))
    })?;
    let combined = serde_json::from_str::<Value>(&input).map_err(|error| {
        SoldbError::Message(format!("invalid JSON in `{}`: {error}", path.display()))
    })?;
    source_map_program_from_combined(
        root,
        source_roots,
        &path,
        &combined,
        contract_name,
        environment,
    )
}

fn source_map_program_from_combined(
    root: &Path,
    source_roots: &[PathBuf],
    artifact_path: &Path,
    combined: &Value,
    contract_name: &str,
    environment: SourceMapEnvironment,
) -> SoldbResult<Option<SourceMapProgram>> {
    let contracts = combined
        .get("contracts")
        .and_then(Value::as_object)
        .ok_or_else(|| {
            SoldbError::Message(format!(
                "legacy artifact `{}` does not contain a contracts object",
                artifact_path.display()
            ))
        })?;
    let Some((contract_key, contract)) = find_contract(contracts, contract_name)? else {
        return Ok(None);
    };
    let (source_map_field, bytecode_field, environment_name) = environment.fields();
    let Some(source_map) = contract.get(source_map_field).and_then(Value::as_str) else {
        return Ok(None);
    };
    let bytecode = contract
        .get(bytecode_field)
        .and_then(Value::as_str)
        .ok_or_else(|| {
            SoldbError::Message(format!(
                "legacy artifact `{}` has `{source_map_field}` for `{contract_key}` but no \
                 `{bytecode_field}`; recompile with bytecode output enabled",
                artifact_path.display()
            ))
        })?;
    let bytecode = decode_bytecode(bytecode, artifact_path, contract_key, bytecode_field)?;
    let source_map = parse_srcmap(source_map)?;
    let source_list = combined
        .get("sourceList")
        .and_then(Value::as_array)
        .ok_or_else(|| {
            SoldbError::Message(format!(
                "legacy artifact `{}` does not contain a sourceList array",
                artifact_path.display()
            ))
        })?;

    let mut sources = BTreeMap::new();
    let mut source_contents = BTreeMap::new();
    let mut source_files = BTreeMap::new();
    let mut compilation_sources = Vec::with_capacity(source_list.len());
    for (source_id, source) in source_list.iter().enumerate() {
        let source_path = source.as_str().ok_or_else(|| {
            SoldbError::Message(format!(
                "legacy artifact `{}` has a non-string sourceList entry at index {source_id}",
                artifact_path.display()
            ))
        })?;
        let source_id = u64::try_from(source_id)
            .map_err(|_| SoldbError::Message("source index does not fit in `u64`".to_owned()))?;
        sources.insert(source_id, source_path.to_owned());

        let mut source = json!({
            "id": source_id,
            "path": source_path,
            "language": "Solidity",
        });
        if let Some((file, contents)) = read_source(root, source_roots, source_path)? {
            source_contents.insert(source_id, contents.clone());
            source_files.insert(source_id, file);
            source["contents"] = Value::String(contents);
        }
        compilation_sources.push(source);
    }

    let pc_to_instruction_index = build_pc_to_instruction_map(&bytecode);
    if source_map.len() > pc_to_instruction_index.len() {
        return Err(SoldbError::Message(format!(
            "legacy source map for `{contract_key}` has {} entries but `{bytecode_field}` has \
             only {} instructions",
            source_map.len(),
            pc_to_instruction_index.len()
        )));
    }

    let mut instructions = Vec::with_capacity(source_map.len());
    for (pc, instruction_index) in pc_to_instruction_index {
        let Some(entry) = source_map.get(instruction_index) else {
            break;
        };
        instructions.push(Instruction::from_source_map(
            u64::try_from(pc).map_err(|_| {
                SoldbError::Message("program counter does not fit in `u64`".to_owned())
            })?,
            opcode_mnemonic(bytecode[pc]),
            source_context(
                entry,
                sources.len(),
                &source_contents,
                instruction_index,
                contract_key,
            )?,
        ));
    }

    let compiler_version = combined
        .get("version")
        .and_then(Value::as_str)
        .unwrap_or("unknown");
    let compilation = json!({
        "id": format!("legacy-{compiler_version}"),
        "compiler": {
            "name": "Solidity compiler",
            "version": compiler_version,
        },
        "sources": compilation_sources,
    });
    let resources = json!({
        "compilation": compilation,
        "types": {},
        "pointers": {},
    });
    let resolved_contract_name = contract_key
        .rsplit_once(':')
        .map_or(contract_key, |(_, name)| name);
    let info = EthdebugInfo {
        compilation,
        contract_name: resolved_contract_name.to_owned(),
        environment: environment_name.to_owned(),
        instructions,
        sources,
        variable_locations: BTreeMap::new(),
    };

    let storage_layout = combined_layout(contract, "storage-layout", artifact_path, contract_key)?;
    let transient_storage_layout = combined_layout(
        contract,
        "transient-storage-layout",
        artifact_path,
        contract_key,
    )?;

    Ok(Some(SourceMapProgram {
        info,
        resources,
        source_contents,
        source_files,
        storage_layout,
        transient_storage_layout,
    }))
}

/// A contract's layout entry `key` in a `combined.json`, when it was compiled with it.
fn combined_layout(
    contract: &Value,
    key: &str,
    artifact_path: &Path,
    contract_key: &str,
) -> SoldbResult<Option<StorageLayout>> {
    contract
        .get(key)
        .map(StorageLayout::parse)
        .transpose()
        .map_err(|error| {
            SoldbError::Message(format!(
                "legacy artifact `{}` has an invalid `{key}` for `{contract_key}`: {error}",
                artifact_path.display()
            ))
        })
}

fn find_contract<'a>(
    contracts: &'a serde_json::Map<String, Value>,
    contract_name: &str,
) -> SoldbResult<Option<(&'a str, &'a Value)>> {
    if contract_name.is_empty() {
        let mut contracts = contracts.iter();
        let Some((name, contract)) = contracts.next() else {
            return Ok(None);
        };
        if contracts.next().is_some() {
            return Err(SoldbError::Message(
                "legacy combined JSON contains multiple contracts; provide `contractName`"
                    .to_owned(),
            ));
        }
        return Ok(Some((name, contract)));
    }
    if let Some((name, contract)) = contracts.get_key_value(contract_name) {
        return Ok(Some((name, contract)));
    }

    let suffix = format!(":{contract_name}");
    let mut matches = contracts.iter().filter(|(name, _)| name.ends_with(&suffix));
    let Some((name, contract)) = matches.next() else {
        return Ok(None);
    };
    if matches.next().is_some() {
        return Err(SoldbError::Message(format!(
            "contract name `{contract_name}` is ambiguous in legacy combined JSON"
        )));
    }
    Ok(Some((name, contract)))
}

fn decode_bytecode(
    bytecode: &str,
    artifact_path: &Path,
    contract_name: &str,
    bytecode_field: &str,
) -> SoldbResult<Vec<u8>> {
    let bytecode = bytecode.strip_prefix("0x").unwrap_or(bytecode);
    let (byte_pairs, remainder) = bytecode.as_bytes().as_chunks::<2>();
    if !remainder.is_empty() {
        return Err(SoldbError::Message(format!(
            "`{bytecode_field}` for `{contract_name}` in `{}` has an odd number of hex digits",
            artifact_path.display()
        )));
    }

    byte_pairs
        .iter()
        .map(|digits| {
            let high = decode_hex_digit(digits[0]);
            let low = decode_hex_digit(digits[1]);
            match (high, low) {
                (Some(high), Some(low)) => Ok(high << 4 | low),
                _ => Err(SoldbError::Message(format!(
                    "`{bytecode_field}` for `{contract_name}` in `{}` is not valid hex",
                    artifact_path.display()
                ))),
            }
        })
        .collect()
}

fn decode_hex_digit(digit: u8) -> Option<u8> {
    match digit {
        b'0'..=b'9' => Some(digit - b'0'),
        b'a'..=b'f' => Some(digit - b'a' + 10),
        b'A'..=b'F' => Some(digit - b'A' + 10),
        _ => None,
    }
}

fn opcode_mnemonic(opcode: u8) -> Cow<'static, str> {
    opcodes::mnemonic(opcode).map_or_else(
        || Cow::Owned(format!("UNKNOWN(0x{opcode:02x})")),
        Cow::Borrowed,
    )
}

/// The context of one legacy entry: its source range, and the jump markers ETHDebug
/// defines (`invoke` for a jump into a function, `return` for a jump out of one) when the
/// map's jump type carries them. Generated code has no range but may still jump.
fn source_context(
    entry: &SourceMapEntry,
    source_count: usize,
    source_contents: &BTreeMap<u64, String>,
    instruction_index: usize,
    contract_name: &str,
) -> SoldbResult<SourceMapContext> {
    Ok(SourceMapContext {
        code: source_code(
            entry,
            source_count,
            source_contents,
            instruction_index,
            contract_name,
        )?,
        jump: match entry.jump_type.as_str() {
            "i" => SourceMapJump::Into,
            "o" => SourceMapJump::Out,
            _ => SourceMapJump::Regular,
        },
        modifier_depth: entry.modifier_depth,
    })
}

/// The `code` part of an entry's context: the source range it maps to, if any.
fn source_code(
    entry: &SourceMapEntry,
    source_count: usize,
    source_contents: &BTreeMap<u64, String>,
    instruction_index: usize,
    contract_name: &str,
) -> SoldbResult<Option<SourceLocation>> {
    if entry.file_index < 0 {
        return Ok(None);
    }
    if entry.offset < 0 || entry.length < 0 {
        return Err(SoldbError::Message(format!(
            "legacy source-map entry {instruction_index} for `{contract_name}` has a negative \
             source range"
        )));
    }
    let source_id = u64::try_from(entry.file_index).map_err(|_| {
        SoldbError::Message(format!(
            "legacy source-map entry {instruction_index} for `{contract_name}` has an invalid \
             source index"
        ))
    })?;
    // solc attributes the Yul it generates (ABI decoding, checked arithmetic, panics) to a
    // utility source whose index is one past the last entry in `sourceList`, and that
    // source is never in the artifact. Such an instruction has no user source, the same
    // as an entry with index -1; treating it as an error would reject every map a modern
    // legacy-format compiler emits.
    if usize::try_from(source_id).map_or(true, |source_id| source_id >= source_count) {
        return Ok(None);
    }
    let offset = u64::try_from(entry.offset).map_err(|_| {
        SoldbError::Message(format!(
            "legacy source-map entry {instruction_index} for `{contract_name}` has an invalid \
             offset"
        ))
    })?;
    let length = u64::try_from(entry.length).map_err(|_| {
        SoldbError::Message(format!(
            "legacy source-map entry {instruction_index} for `{contract_name}` has an invalid \
             length"
        ))
    })?;
    let end = offset.checked_add(length).ok_or_else(|| {
        SoldbError::Message(format!(
            "legacy source-map entry {instruction_index} for `{contract_name}` overflows its \
             source range"
        ))
    })?;
    if let Some(source) = source_contents.get(&source_id) {
        let source_len = u64::try_from(source.len())
            .map_err(|_| SoldbError::Message("source length does not fit in `u64`".to_owned()))?;
        if end > source_len {
            return Err(SoldbError::Message(format!(
                "legacy source-map entry {instruction_index} for `{contract_name}` ends at byte \
                 {end}, beyond source {source_id} length {source_len}"
            )));
        }
    }

    Ok(Some(SourceLocation {
        source_id,
        offset,
        length,
    }))
}

fn read_source(
    root: &Path,
    source_roots: &[PathBuf],
    source_path: &str,
) -> SoldbResult<Option<(PathBuf, String)>> {
    let candidates = source_roots
        .iter()
        .flat_map(|extra| crate::artifacts::source_candidates(extra, source_path))
        .chain(crate::artifacts::source_candidates(root, source_path));
    for candidate in candidates {
        if !candidate.exists() {
            continue;
        }
        let contents = fs::read_to_string(&candidate).map_err(|error| {
            SoldbError::Message(format!(
                "failed to read source `{}`: {error}",
                candidate.display()
            ))
        })?;
        return Ok(Some((candidate, contents)));
    }
    Ok(None)
}

#[must_use]
pub fn is_legacy_compiler(version: &str) -> bool {
    if version.is_empty() {
        return true;
    }

    let version_core = version.split('+').next().unwrap_or(version);
    let parts = version_core.split('.').collect::<Vec<_>>();
    if parts.len() < 3 {
        return true;
    }

    let Ok(major) = parts[0].parse::<u64>() else {
        return true;
    };
    let Ok(minor) = parts[1].parse::<u64>() else {
        return true;
    };
    let Ok(patch) = parts[2].parse::<u64>() else {
        return true;
    };

    major == 0 && (minor < 8 || (minor == 8 && patch < 29))
}

fn parse_inherited_i64(field: Option<&str>, previous: i64, label: &str) -> SoldbResult<i64> {
    let Some(field) = field else {
        return Ok(previous);
    };

    let trimmed = field.trim();
    if trimmed.is_empty() {
        return Ok(previous);
    }

    trimmed.parse::<i64>().map_err(|error| {
        SoldbError::Message(format!("Invalid source map {label} '{trimmed}': {error}"))
    })
}

fn parse_inherited_string(field: Option<&str>, previous: &str) -> String {
    field
        .filter(|value| !value.trim().is_empty())
        .unwrap_or(previous)
        .to_owned()
}

#[cfg(test)]
mod tests {
    use crate::metadata::FunctionExit;
    use std::fs;
    use std::path::PathBuf;
    use std::time::{SystemTime, UNIX_EPOCH};

    use serde_json::json;

    use crate::Instruction;

    use super::{
        build_pc_to_instruction_map, is_legacy_compiler, load_source_map_program, opcode_mnemonic,
        parse_srcmap, SourceMapEnvironment, SourceMapInfo,
    };

    fn temp_dir(label: &str) -> PathBuf {
        let unique = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .expect("system time")
            .as_nanos();
        let dir = std::env::temp_dir().join(format!("soldb-source-map-{label}-{unique}"));
        fs::create_dir_all(&dir).expect("create temp dir");
        dir
    }

    #[test]
    fn names_instructions_and_marks_invalid_bytes() {
        // The names are what a trace records for the same steps, so a `JUMPDEST` in an
        // artifact is a `JUMPDEST` in a trace; a byte that is no instruction says so.
        assert_eq!(opcode_mnemonic(0x5b), "JUMPDEST");
        assert_eq!(opcode_mnemonic(0x60), "PUSH1");
        assert_eq!(opcode_mnemonic(0xf1), "CALL");
        assert_eq!(opcode_mnemonic(0x0c), "UNKNOWN(0x0c)");
    }

    #[test]
    fn maps_program_counters_around_push_data() {
        let bytecode = [0x60, 0xff, 0x61, 0x01, 0x02, 0x00];
        let pc_map = build_pc_to_instruction_map(&bytecode);

        assert_eq!(pc_map.get(&0), Some(&0));
        assert_eq!(pc_map.get(&2), Some(&1));
        assert_eq!(pc_map.get(&5), Some(&2));
        assert!(!pc_map.contains_key(&1));
        assert!(!pc_map.contains_key(&3));
    }

    #[test]
    fn parses_source_maps_with_inherited_fields() {
        let entries = parse_srcmap("0:10:0:-:0;:5::i:;20::1").expect("source map");

        assert_eq!(entries[0].offset, 0);
        assert_eq!(entries[1].offset, 0);
        assert_eq!(entries[1].length, 5);
        assert_eq!(entries[1].file_index, 0);
        assert_eq!(entries[1].jump_type, "i");
        assert_eq!(entries[2].offset, 20);
        assert_eq!(entries[2].length, 5);
        assert_eq!(entries[2].file_index, 1);
    }

    #[test]
    fn rejects_invalid_source_map_numbers() {
        let error = parse_srcmap("abc:10:0").expect_err("invalid number");
        assert!(error.to_string().contains("offset"));
    }

    #[test]
    fn reports_source_info_for_valid_pcs() {
        let bytecode = vec![0x60, 0xff, 0x00];
        let srcmap_entries = parse_srcmap("0:4:0;10:2:1").expect("source map");
        let info = SourceMapInfo {
            contract_name: "Counter".to_owned(),
            sources: vec!["A.sol".to_owned(), "B.sol".to_owned()],
            bytecode: bytecode.clone(),
            srcmap_entries,
            pc_to_instruction_index: build_pc_to_instruction_map(&bytecode),
            compiler_version: Some("0.8.16".to_owned()),
        };

        assert_eq!(info.source_info(0), Some(("A.sol", 0, 4)));
        assert_eq!(info.source_info(2), Some(("B.sol", 10, 2)));
        assert_eq!(info.source_info(1), None);
    }

    #[test]
    fn detects_legacy_compiler_versions() {
        assert!(is_legacy_compiler(""));
        assert!(is_legacy_compiler("0.8.16"));
        assert!(is_legacy_compiler("0.8.28+commit.deadbeef"));
        assert!(!is_legacy_compiler("0.8.29"));
        assert!(!is_legacy_compiler("0.9.0"));
    }

    #[test]
    fn loads_runtime_combined_json_as_debug_info() {
        let dir = temp_dir("runtime");
        let source = "contract Counter { function add() external {} }";
        fs::write(dir.join("Counter.sol"), source).expect("write source");
        fs::write(
            dir.join("combined.json"),
            json!({
                "version": "0.8.36+commit.test",
                "sourceList": ["Counter.sol"],
                "contracts": {
                    "Counter.sol:Counter": {
                        "bin-runtime": "600100",
                        "srcmap-runtime": "0:8:0:-:0;9:3:0",
                        "storage-layout": {
                            "storage": [{
                                "astId": 1,
                                "contract": "Counter.sol:Counter",
                                "label": "value",
                                "offset": 0,
                                "slot": "0",
                                "type": "t_uint256"
                            }],
                            "types": {
                                "t_uint256": {
                                    "encoding": "inplace",
                                    "label": "uint256",
                                    "numberOfBytes": "32"
                                }
                            }
                        },
                        "transient-storage-layout": {
                            "storage": [{
                                "astId": 2,
                                "contract": "Counter.sol:Counter",
                                "label": "locked",
                                "offset": 0,
                                "slot": "0",
                                "type": "t_bool"
                            }],
                            "types": {
                                "t_bool": {
                                    "encoding": "inplace",
                                    "label": "bool",
                                    "numberOfBytes": "1"
                                }
                            }
                        }
                    }
                }
            })
            .to_string(),
        )
        .expect("write combined JSON");

        let program = load_source_map_program(&dir, "Counter", SourceMapEnvironment::Runtime)
            .expect("load source map")
            .expect("runtime program");

        // `solc --combined-json ...,storage-layout` carries the layout per contract, so a
        // pre-ETHDebug compiler reads state variables by name just as a modern one does.
        let layout = program.storage_layout.as_ref().expect("storage layout");
        assert_eq!(layout.variables.len(), 1);
        assert_eq!(layout.variable("value").expect("value").slot, [0_u8; 32]);
        assert_eq!(
            layout.resolve("value").expect("resolve").type_id,
            "t_uint256"
        );
        // `transient-storage-layout` is the same shape, for the `transient` variables.
        let transient = program
            .transient_storage_layout
            .as_ref()
            .expect("transient layout");
        assert!(transient.variable("locked").is_some());
        assert!(transient.variable("value").is_none());

        assert_eq!(program.info.contract_name, "Counter");
        assert_eq!(program.info.environment, "call");
        assert_eq!(program.info.source_info(0), Some(("Counter.sol", 0, 8)));
        assert_eq!(program.info.source_info(2), Some(("Counter.sol", 9, 3)));
        assert_eq!(
            program
                .info
                .instruction_at_pc(0)
                .and_then(Instruction::mnemonic),
            Some("PUSH1")
        );
        assert_eq!(
            program
                .info
                .instruction_at_pc(2)
                .and_then(Instruction::mnemonic),
            Some("STOP")
        );
        assert_eq!(
            program.source_contents.get(&0).map(String::as_str),
            Some(source)
        );
        assert_eq!(
            program.resources["compilation"]["compiler"]["version"],
            "0.8.36+commit.test"
        );
    }

    #[test]
    fn jump_types_become_call_and_return_markers() {
        let dir = temp_dir("jump-types");
        let source = "contract Counter { function add() external {} }";
        fs::write(dir.join("Counter.sol"), source).expect("write source");
        fs::write(
            dir.join("combined.json"),
            json!({
                "version": "0.8.16+commit.test",
                "sourceList": ["Counter.sol"],
                "contracts": {
                    "Counter.sol:Counter": {
                        "bin-runtime": "6001600100",
                        "srcmap-runtime": "0:8:0:i;9:3:0:o;0:0:-1:-"
                    }
                }
            })
            .to_string(),
        )
        .expect("write combined JSON");

        let program = load_source_map_program(&dir, "Counter", SourceMapEnvironment::Runtime)
            .expect("load source map")
            .expect("runtime program");
        let at = |pc: u64| program.info.instruction_at_pc(pc).expect("instruction");
        assert_eq!(at(0).function_invocations().len(), 1);
        assert_eq!(at(0).modifier_depth(), Some(0));
        assert_eq!(at(0).function_exit(), None);
        assert!(at(2).function_invocations().is_empty());
        assert_eq!(at(2).function_exit(), Some(FunctionExit::Return));
        // Generated code without a range still retains its modifier depth.
        assert!(at(4).source_location().is_none());
        assert!(at(4).function_invocations().is_empty());
    }

    #[test]
    fn generated_code_past_the_source_list_has_no_source() {
        // solc 0.8.x maps its generated Yul to a source index one past `sourceList`;
        // those instructions are unmapped rather than a reason to reject the map.
        let dir = temp_dir("utility-source");
        let source = "contract Counter { function add() external {} }";
        fs::write(dir.join("Counter.sol"), source).expect("write source");
        fs::write(
            dir.join("combined.json"),
            json!({
                "version": "0.8.16+commit.test",
                "sourceList": ["Counter.sol"],
                "contracts": {
                    "Counter.sol:Counter": {
                        "bin-runtime": "600100",
                        "srcmap-runtime": "0:8:0:-:0;0:5:1"
                    }
                }
            })
            .to_string(),
        )
        .expect("write combined JSON");

        let program = load_source_map_program(&dir, "Counter", SourceMapEnvironment::Runtime)
            .expect("load source map")
            .expect("runtime program");
        assert_eq!(program.info.source_info(0), Some(("Counter.sol", 0, 8)));
        assert_eq!(program.info.source_info(2), None);
        assert!(program.info.instruction_at_pc(2).is_some());
    }

    #[test]
    fn loads_creation_map_and_infers_single_contract() {
        let dir = temp_dir("creation");
        fs::write(
            dir.join("combined.json"),
            json!({
                "sourceList": ["Missing.sol"],
                "contracts": {
                    "Missing.sol:Created": {
                        "bin": "00",
                        "srcmap": "0:1:0"
                    }
                }
            })
            .to_string(),
        )
        .expect("write combined JSON");

        let program = load_source_map_program(&dir, "", SourceMapEnvironment::Creation)
            .expect("load source map")
            .expect("creation program");

        assert_eq!(program.info.contract_name, "Created");
        assert_eq!(program.info.environment, "create");
        assert!(program.source_contents.is_empty());
    }

    #[test]
    fn requires_bytecode_for_program_counter_mapping() {
        let dir = temp_dir("missing-bytecode");
        fs::write(
            dir.join("combined.json"),
            json!({
                "sourceList": ["Counter.sol"],
                "contracts": {
                    "Counter.sol:Counter": {"srcmap-runtime": "0:1:0"}
                }
            })
            .to_string(),
        )
        .expect("write combined JSON");

        let error = load_source_map_program(&dir, "Counter", SourceMapEnvironment::Runtime)
            .expect_err("missing bytecode");
        assert!(error.to_string().contains("`bin-runtime`"));
        assert!(error.to_string().contains("bytecode output"));
    }
}