Repository: Gallopsled/pwntools

Issue:
Weird asm/disasm functions 'arch' argument behavior when its prefixed with 'x86'
1. When we pass an incorrect arch we get a listing of all of them:
```
(Pdb) p disasm(b'\x90', arch='nonexistent')
*** AttributeError: AttributeError: arch must be one of ['aarch64', 'alpha', 'amd64', 'arm', 'avr', 'cris', 'i386', 'ia64', 'm68k', 'mips', 'mips64', 'msp430', 'none', 'powerpc', 'powerpc64', 's390', 'sparc', 'sparc64', 'thumb', 'vax']
```
2. However, it does not include any arch starting with "x86", but this works:
```
(Pdb) p disasm(b'\x90', arch='x86')
'   0:   90                      nop'
```
3. The above probably makes sense? However, this does not:
```
(Pdb) p disasm(b'\x90', arch='x86-lol-what-is-going-on')
'   0:   90                      nop'
```

It seems that when the `arch` starts with `"x86"` it is treated as `"i386"`.

This is not nice when we pass in a string like `"x86-64"` **as it is also treated as i386**:
```
(Pdb) p asm('mov rax, rbx', arch='amd64')
b'H\x89\xd8'
(Pdb) p asm('mov rax, rbx', arch='x86-64')
There was an error running ['/usr/bin/x86_64-linux-gnu-as', '-32', '-o', '/tmp/pwn-asm-78qaa8ef/step2', '/tmp/pwn-asm-78qaa8ef/step1']:
It had the exitcode 1.
It had this on stdout:
/tmp/pwn-asm-78qaa8ef/step1: Assembler messages:
/tmp/pwn-asm-78qaa8ef/step1:8: Error: too many memory references for `mov'


An error occurred while assembling:
   1: .section .shellcode,"awx"
   2: .global _start
   3: .global __start
   4: .p2align 2
   5: _start:
   6: __start:
   7: .intel_syntax noprefix
   8: mov rax, rbx
Traceback (most recent call last):
  File "/home/dc/src/pwndbg/pwndbg/commands/__init__.py", line 131, in __call__
    return self.function(*args, **kwargs)
  File "/home/dc/src/pwndbg/pwndbg/commands/__init__.py", line 225, in _OnlyWhenRunning
    return function(*a, **kw)
  File "/home/dc/src/pwndbg/pwndbg/commands/patch.py", line 24, in patch
    new_mem = asm(ins, arch=pwndbg.arch.current)
  File "/home/dc/.local/lib/python3.9/site-packages/pwnlib/context/__init__.py", line 1524, in setter
    return function(*a, **kw)
  File "/home/dc/.local/lib/python3.9/site-packages/pwnlib/asm.py", line 725, in asm
    log.exception("An error occurred while assembling:\n%s" % lines)
  File "/home/dc/.local/lib/python3.9/site-packages/pwnlib/asm.py", line 679, in asm
    _run(assembler + ['-o', step2, step1])
  File "/home/dc/.local/lib/python3.9/site-packages/pwnlib/asm.py", line 397, in _run
    log.error(msg, *args)
  File "/home/dc/.local/lib/python3.9/site-packages/pwnlib/log.py", line 424, in error
    raise PwnlibException(message % args)
pwnlib.exception.PwnlibException: There was an error running ['/usr/bin/x86_64-linux-gnu-as', '-32', '-o', '/tmp/pwn-asm-ed6_0_hn/step2', '/tmp/pwn-asm-ed6_0_hn/step1']:
It had the exitcode 1.
It had this on stdout:
/tmp/pwn-asm-ed6_0_hn/step1: Assembler messages:
/tmp/pwn-asm-ed6_0_hn/step1:8: Error: too many memory references for `mov'

(...) - output truncated
```

...and the `x86-64` is what Pwndbg may have in `pwndbg.arch.current` ;).

I don't mind having `"x86"` arch aliases but then it would be nice to make it so that:
* `x86` and `x86-32` aliases to `i386`
* `x86-64` aliases to `amd64`


Discussion:
There is some code responsible for arch transform here, I am investigating this.

https://github.com/Gallopsled/pwntools/blob/bd12d1874f17e1fd6a9b26411ccc7ccd6c31f4cb/pwnlib/context/__init__.py#L761-L774

Resolving patch:
diff --git a/pwnlib/context/__init__.py b/pwnlib/context/__init__.py
--- a/pwnlib/context/__init__.py
+++ b/pwnlib/context/__init__.py
@@ -762,6 +762,7 @@ def arch(self, arch):
         # We have to make sure that x86_64 appears before x86 for this to work correctly.
         transform = [('ppc64', 'powerpc64'),
                      ('ppc', 'powerpc'),
+                     ('x86-64', 'amd64'),
                      ('x86_64', 'amd64'),
                      ('x86', 'i386'),
                      ('i686', 'i386'),


Test patch: