Skip to content

Instantly share code, notes, and snippets.

@diazona
Last active August 29, 2015 14:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save diazona/ba7f270d3e1763e6abf8 to your computer and use it in GitHub Desktop.
Save diazona/ba7f270d3e1763e6abf8 to your computer and use it in GitHub Desktop.
Program producing error
#include <list>
#include <map>
#include <string>
#include <stdexcept>
#include <iostream>
using std::cerr;
using std::cout;
using std::endl;
using std::ostream;
using std::map;
using std::string;
using std::list;
using std::pair;
/* Code to print out something and the address it is stored at. Only for debugging */
template<class T>
struct val_loc_string {
const T& t;
const T* p_t;
val_loc_string(const T& t) : t(t), p_t(&t) {}
};
template<class T>
ostream& operator<<(ostream& out, const val_loc_string<T> t) {
return out << "@" << t.p_t << " [" << t.t << "]";
}
/* end debugging code */
/**
* A map of strings to ordered lists of (string, T).
*/
template<typename T>
class category_map : public std::map<const string, list<pair<const string, T> > > {
private:
typedef typename std::map<const string, list<pair<const string, T> > > super;
public:
typedef pair<const string, T> pair_type;
typedef list<pair_type> list_type;
void add(const string& name, const string& implementation, const T t) {
list_type& the_list = super::operator[](name);
cerr << "inserting under " << name << "." << implementation << endl;
// the real code checks for duplicates
pair_type the_pair(implementation, t);
the_list.push_back(the_pair);
cerr << "adding new entry: " << val_loc_string<T>(the_pair.second) << endl;
}
T& get(const string& name) const {
cerr << "searching for " << name << endl;
typename super::const_iterator map_iterator = super::find(name);
// the real code doesn't assume it will be found
list_type the_list = map_iterator->second;
T& p = the_list.front().second;
cerr << "found " << val_loc_string<T>(p) << endl;
return p;
}
};
int main(const int argc, const char** argv) {
category_map<int*> imap;
int a;
imap.add("Q1", "m", &a);
imap.add("Q1", "r", &a);
imap.add("Q2", "m", &a);
int* ip = imap.get("Q1");
cerr << "return value: " << val_loc_string<int*>(ip) << endl;
cout << *ip << endl;
}
Dump of assembler code from 0x7fff93188216 to 0x7fff931883b4:
0x00007fff93188216: push %rbp
0x00007fff93188217: mov %rsp,%rbp
0x00007fff9318821a: push %rbx
0x00007fff9318821b: mov %ecx,%r10d
0x00007fff9318821e: mov %r10d,%eax
0x00007fff93188221: dec %eax
0x00007fff93188223: movzwl %ax,%eax
0x00007fff93188226: mov $0x3f,%ecx
0x00007fff9318822b: cmp $0x3e,%eax
0x00007fff9318822e: ja 0x7fff93188236
0x00007fff93188230: movzwl %r10w,%ecx
0x00007fff93188234: dec %ecx
0x00007fff93188236: mov %ecx,%r8d
0x00007fff93188239: mov 0x28(%rsi,%r8,8),%r9
0x00007fff9318823e: mov %rdx,%r11
0x00007fff93188241: and $0xfffffffffff00000,%r11
0x00007fff93188248: mov %rdx,%rcx
0x00007fff9318824b: shr $0x4,%rcx
0x00007fff9318824f: mov %edx,%eax
0x00007fff93188251: shr $0x8,%eax
0x00007fff93188254: and $0xffe,%eax
0x00007fff93188259: mov $0x1,%ebx
0x00007fff9318825e: shl %cl,%ebx
0x00007fff93188260: or %ebx,0xfc0a0(%r11,%rax,4)
0x00007fff93188268: not %ebx
0x00007fff9318826a: or $0x1,%eax
0x00007fff9318826d: and %ebx,0xfc0a0(%r11,%rax,4)
0x00007fff93188275: movzwl %r10w,%eax
0x00007fff93188279: cmp $0x2,%eax
0x00007fff9318827c: jb 0x7fff9318828d
0x00007fff9318827e: shl $0x4,%rax
=> 0x00007fff93188282: mov %r10w,-0x2(%rax,%rdx,1)
0x00007fff93188288: mov %r10w,0x10(%rdx)
0x00007fff9318828d: test %r10w,%r10w
0x00007fff93188291: jne 0x7fff93188299
0x00007fff93188293: movw $0x0,0x10(%rdx)
0x00007fff93188299: test %r9,%r9
0x00007fff9318829c: je 0x7fff931882fd
0x00007fff9318829e: mov 0x1460(%rdi),%rax
0x00007fff931882a5: xor %rdx,%rax
0x00007fff931882a8: add $0x1460,%rdi
0x00007fff931882af: mov %rax,%r10
0x00007fff931882b2: shr $0x8,%r10
0x00007fff931882b6: add %eax,%r10d
0x00007fff931882b9: mov %rax,%rbx
0x00007fff931882bc: shr $0x10,%rbx
0x00007fff931882c0: add %r10d,%ebx
0x00007fff931882c3: mov %rax,%r10
0x00007fff931882c6: shr $0x18,%r10
0x00007fff931882ca: add %ebx,%r10d
0x00007fff931882cd: mov %rax,%rbx
0x00007fff931882d0: shr $0x20,%rbx
0x00007fff931882d4: add %r10d,%ebx
0x00007fff931882d7: mov %rax,%r10
0x00007fff931882da: shr $0x28,%r10
0x00007fff931882de: add %ebx,%r10d
0x00007fff931882e1: mov %rax,%rbx
0x00007fff931882e4: shr $0x30,%rbx
0x00007fff931882e8: add %r10d,%ebx
0x00007fff931882eb: shr $0x38,%rax
0x00007fff931882ef: add %ebx,%eax
0x00007fff931882f1: shl $0x3c,%rax
0x00007fff931882f5: or %rcx,%rax
0x00007fff931882f8: mov %rax,(%r9)
0x00007fff931882fb: jmp 0x7fff9318831b
0x00007fff931882fd: mov $0x1,%eax
0x00007fff93188302: mov %r8b,%cl
0x00007fff93188305: shl %cl,%eax
0x00007fff93188307: mov %r8d,%ecx
0x00007fff9318830a: shr $0x5,%ecx
0x00007fff9318830d: or %eax,0x828(%rsi,%rcx,4)
0x00007fff93188314: add $0x1460,%rdi
0x00007fff9318831b: mov (%rdi),%rax
0x00007fff9318831e: mov %rax,%rcx
0x00007fff93188321: shr $0x8,%rcx
0x00007fff93188325: add %eax,%ecx
0x00007fff93188327: mov %rax,%rbx
0x00007fff9318832a: shr $0x10,%rbx
0x00007fff9318832e: add %ecx,%ebx
0x00007fff93188330: mov %rax,%rcx
0x00007fff93188333: shr $0x18,%rcx
0x00007fff93188337: add %ebx,%ecx
0x00007fff93188339: mov %rax,%rbx
0x00007fff9318833c: shr $0x20,%rbx
0x00007fff93188340: add %ecx,%ebx
0x00007fff93188342: mov %rax,%rcx
0x00007fff93188345: shr $0x28,%rcx
0x00007fff93188349: add %ebx,%ecx
0x00007fff9318834b: mov %rax,%rbx
0x00007fff9318834e: shr $0x30,%rbx
0x00007fff93188352: add %ecx,%ebx
0x00007fff93188354: shr $0x38,%rax
0x00007fff93188358: add %ebx,%eax
0x00007fff9318835a: shl $0x3c,%rax
0x00007fff9318835e: mov %rax,(%rdx)
0x00007fff93188361: mov (%rdi),%rax
0x00007fff93188364: xor %r9,%rax
0x00007fff93188367: mov %rax,%rcx
0x00007fff9318836a: shr $0x8,%rcx
0x00007fff9318836e: add %eax,%ecx
0x00007fff93188370: mov %rax,%rdi
0x00007fff93188373: shr $0x10,%rdi
0x00007fff93188377: add %ecx,%edi
0x00007fff93188379: mov %rax,%rcx
0x00007fff9318837c: shr $0x18,%rcx
0x00007fff93188380: add %edi,%ecx
0x00007fff93188382: mov %rax,%rdi
0x00007fff93188385: shr $0x20,%rdi
0x00007fff93188389: add %ecx,%edi
0x00007fff9318838b: mov %rax,%rcx
0x00007fff9318838e: shr $0x28,%rcx
0x00007fff93188392: add %edi,%ecx
0x00007fff93188394: mov %rax,%rdi
0x00007fff93188397: shr $0x30,%rdi
0x00007fff9318839b: add %ecx,%edi
0x00007fff9318839d: shr $0x38,%rax
0x00007fff931883a1: add %edi,%eax
0x00007fff931883a3: shld $0x3c,%r9,%rax
0x00007fff931883a8: mov %rax,0x8(%rdx)
0x00007fff931883ac: mov %rdx,0x28(%rsi,%r8,8)
0x00007fff931883b1: pop %rbx
0x00007fff931883b2: pop %rbp
0x00007fff931883b3: retq
End of assembler dump.
rax 0x30 48
rbx 0xffffffbf 4294967231
rcx 0x100104c6 268502214
rdx 0x100104c60 4296035424
rsi 0x100016800 4295059456
rdi 0x100011000 4295036928
rbp 0x7fff5fbff300 0x7fff5fbff300
rsp 0x7fff5fbff2f8 0x7fff5fbff2f8
r8 0x2 2
r9 0x0 0
r10 0x3 3
r11 0x100100000 4296015872
r12 0x30 48
r13 0x3 3
r14 0x4c 76
r15 0x100016800 4295059456
rip 0x7fff93188282 0x7fff93188282
eflags 0x206 [ PF IF ]
cs 0x2b 43
ss <unavailable>
ds <unavailable>
es <unavailable>
fs 0x0 0
gs 0x0 0
#0 0x00007fff93188288 in ?? ()
#1 0x0000000100011000 in ?? ()
#2 0x00007fff5fbff3a0 in ?? ()
#3 0x00007fff931874ff in ?? ()
#4 0x00007fff5fbff384 in ?? ()
#5 0x00007fff92e4add4 in ?? ()
#6 0xffffffffffffffff in ?? ()
#7 0x0000000000000000 in ?? ()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment