Guest User

smart_ptr

a guest
Mar 23rd, 2020
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.20 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3.  
  4. // this can take a long time
  5. void doOneThing() {
  6.   // ...
  7. }
  8.  
  9. // this can take a long time too
  10. void doAnotherThing () {
  11.   //...
  12. }
  13.  
  14. int main(int argc, char** argv) {
  15.  
  16.   string mycool_str = "im a cool str\n"; // stl uses a smart pointers?
  17.  
  18.   doOneThing();
  19.  
  20.   doAnotherThing();
  21.  
  22.   return 0;
  23.  
  24. } // here the mycool_str will be deleted??
  25.  
  26.  
  27. // compiled with g++
  28. // decompiled with Radare2
  29.  
  30. /*
  31. [0x000010b0]> pdf @ main
  32. ┌ 129: int main (int argc, char **argv, char **envp);
  33. │           ; var char **var_60h @ rbp-0x60
  34. │           ; var int64_t var_54h @ rbp-0x54
  35. │           ; var int64_t var_41h @ rbp-0x41
  36. │           ; var int64_t var_40h @ rbp-0x40
  37. │           ; var int64_t var_18h @ rbp-0x18
  38. │           ; arg int argc @ rdi
  39. │           ; arg char **argv @ rsi
  40. │           ; DATA XREF from entry0 @ 0x10d1
  41. │           0x000011b7      55             push rbp
  42. │           0x000011b8      4889e5         mov rbp, rsp
  43. │           0x000011bb      53             push rbx
  44. │           0x000011bc      4883ec58       sub rsp, 0x58
  45. │           0x000011c0      897dac         mov dword [var_54h], edi    ; argc
  46. │           0x000011c3      488975a0       mov qword [var_60h], rsi    ; argv
  47. │           0x000011c7      64488b042528.  mov rax, qword fs:[0x28]
  48. │           0x000011d0      488945e8       mov qword [var_18h], rax
  49. │           0x000011d4      31c0           xor eax, eax
  50. │           0x000011d6      488d45bf       lea rax, [var_41h]
  51. │           0x000011da      4889c7         mov rdi, rax
  52. │           0x000011dd      e8befeffff     call sym std::allocator<char>::allocator() ; sym.std::allocator_char_::allocator
  53. │           0x000011e2      488d55bf       lea rdx, [var_41h]
  54. │           0x000011e6      488d45c0       lea rax, [var_40h]
  55. │           0x000011ea      488d35140e00.  lea rsi, str.im_a_cool_str  ; 0x2005 ; "im a cool str\n"
  56. │           0x000011f1      4889c7         mov rdi, rax
  57. │           0x000011f4      e877feffff     call sym std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(char const*, std::allocator<char> const&) ; sym.std::__cxx11::basic_string_char__std::char_traits_char___std::allocator_char___::basic_string_char_const___std::allocator_char__const
  58. │           0x000011f9      488d45bf       lea rax, [var_41h]
  59. │           0x000011fd      4889c7         mov rdi, rax
  60. │           0x00001200      e84bfeffff     call sym std::allocator<char>::~allocator() ; sym.std::allocator_char_::_allocator
  61. │           0x00001205      e89fffffff     call sym doOneThing()       ; sym.doOneThing
  62. │           0x0000120a      e8a1ffffff     call sym doAnotherThing()   ; sym.doAnotherThing
  63. │           0x0000120f      bb00000000     mov ebx, 0
  64. │           0x00001214      488d45c0       lea rax, [var_40h]
  65. │           0x00001218      4889c7         mov rdi, rax
  66. │           0x0000121b      e810feffff     call sym std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string() ; sym.std::__cxx11::basic_string_char__std::char_traits_char___std::allocator_char___::_basic_string
  67. │           0x00001220      89d8           mov eax, ebx
  68. │           0x00001222      488b4de8       mov rcx, qword [var_18h]
  69. │           0x00001226      6448330c2528.  xor rcx, qword fs:[0x28]
  70. │       ┌─< 0x0000122f      7421           je 0x1252
  71. └      ┌──< 0x00001231      eb1a           jmp loc.0000124d
  72. */
  73.  
  74. #include <iostream>
  75. #include <string>
  76.  
  77. void doOneThing() {
  78.   // ...
  79. }
  80.  
  81. void doAnotherThing () {
  82.   //...
  83. }
  84.  
  85. int main(int argc, char** argv) {
  86.  
  87.   string* mycool_str = new std::string("im a cool str\n"); // needs manual delete, write?
  88.  
  89.   delete mycool_str; // delete the mycool_str
  90.  
  91.   doOneThing();
  92.  
  93.   doAnotherThing();
  94.  
  95.   return 0;
  96.  
  97. }
  98.  
  99. // compiled with g++
  100. // decompiled with Radare2
  101.  
  102. /*
  103. [0x000010d0]> pdf @ main
  104. ┌ 172: int main (int argc, char **argv, char **envp);
  105. │           ; var char **var_40h @ rbp-0x40
  106. │           ; var int64_t var_34h @ rbp-0x34
  107. │           ; var int64_t var_21h @ rbp-0x21
  108. │           ; var int64_t var_20h @ rbp-0x20
  109. │           ; var int64_t canary @ rbp-0x18
  110. │           ; arg int argc @ rdi
  111. │           ; arg char **argv @ rsi
  112. │           ; DATA XREF from entry0 @ 0x10f1
  113. │           0x000011d7      55             push rbp
  114. │           0x000011d8      4889e5         mov rbp, rsp
  115. │           0x000011db      4154           push r12
  116. │           0x000011dd      53             push rbx
  117. │           0x000011de      4883ec30       sub rsp, 0x30
  118. │           0x000011e2      897dcc         mov dword [var_34h], edi    ; argc
  119. │           0x000011e5      488975c0       mov qword [var_40h], rsi    ; argv
  120. │           0x000011e9      64488b042528.  mov rax, qword fs:[0x28]
  121. │           0x000011f2      488945e8       mov qword [canary], rax
  122. │           0x000011f6      31c0           xor eax, eax
  123. │           0x000011f8      488d45df       lea rax, [var_21h]
  124. │           0x000011fc      4889c7         mov rdi, rax
  125. │           0x000011ff      e8bcfeffff     call sym std::allocator<char>::allocator() ; sym.std::allocator_char_::allocator
  126. │           0x00001204      4c8d65df       lea r12, [var_21h]
  127. │           0x00001208      bf20000000     mov edi, 0x20               ; "@"
  128. │           0x0000120d      e83efeffff     call sym operator new(unsigned long) ; sym.operator_new_unsigned_long
  129. │           0x00001212      4889c3         mov rbx, rax
  130. │           0x00001215      4c89e2         mov rdx, r12
  131. │           0x00001218      488d35e60d00.  lea rsi, str.im_a_cool_str  ; 0x2005 ; "im a cool str\n"
  132. │           0x0000121f      4889df         mov rdi, rbx
  133. │           0x00001222      e869feffff     call sym std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(char const*, std::allocator<char> const&) ; sym.std::__cxx11::basic_string_char__std::char_traits_char___std::allocator_char___::basic_string_char_const___std::allocator_char__const
  134. │           0x00001227      48895de0       mov qword [var_20h], rbx
  135. │           0x0000122b      488d45df       lea rax, [var_21h]
  136. │           0x0000122f      4889c7         mov rdi, rax
  137. │           0x00001232      e839feffff     call sym std::allocator<char>::~allocator() ; sym.std::allocator_char_::_allocator
  138. │           0x00001237      488b5de0       mov rbx, qword [var_20h]
  139. │           0x0000123b      4885db         test rbx, rbx
  140. │       ┌─< 0x0000123e      7415           je 0x1255
  141. │       │   0x00001240      4889df         mov rdi, rbx
  142. │       │   0x00001243      e8e8fdffff     call sym std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string() ; sym.std::__cxx11::basic_string_char__std::char_traits_char___std::allocator_char___::_basic_string
  143. │       │   0x00001248      be20000000     mov esi, 0x20               ; "@"
  144. │       │   0x0000124d      4889df         mov rdi, rbx
  145. │       │   0x00001250      e80bfeffff     call sym operator delete(void*, unsigned long) ; sym.operator_delete_void___unsigned_long
  146. │       │   ; CODE XREF from main @ 0x123e
  147. │       └─> 0x00001255      e86fffffff     call sym doOneThing()       ; sym.doOneThing
  148. │           0x0000125a      e871ffffff     call sym doAnotherThing()   ; sym.doAnotherThing
  149. │           0x0000125f      b800000000     mov eax, 0
  150. │           0x00001264      488b4de8       mov rcx, qword [canary]
  151. │           0x00001268      6448330c2528.  xor rcx, qword fs:[0x28]
  152. │       ┌─< 0x00001271      7436           je 0x12a9
  153. │      ┌──< 0x00001273      eb2f           jmp 0x12a4
  154. ..
  155. │      ││   ; CODE XREF from main @ 0x1273
  156. │      └──> 0x000012a4      e8d7fdffff     call sym.imp.__stack_chk_fail ; void __stack_chk_fail(void)
  157. │       │   ; CODE XREF from main @ 0x1271
  158. │       └─> 0x000012a9      4883c430       add rsp, 0x30
  159. │           0x000012ad      5b             pop rbx
  160. │           0x000012ae      415c           pop r12
  161. │           0x000012b0      5d             pop rbp
  162. └           0x000012b1      c3             ret
  163. [0x000010d0]>
  164. */
Advertisement
Add Comment
Please, Sign In to add comment