prog:gcc
Différences
Ci-dessous, les différences entre deux révisions de la page.
Les deux révisions précédentesRévision précédenteProchaine révision | Révision précédente | ||
prog:gcc [2020/02/24 15:23] – Ajout du lien "Nightly" root | prog:gcc [2024/11/12 12:48] (Version actuelle) – Déplacement des sanitizer vers une page dédiée root | ||
---|---|---|---|
Ligne 1: | Ligne 1: | ||
Nightly pour [[https:// | Nightly pour [[https:// | ||
- | =====Diagnostic===== | + | [[https:// |
- | ====-fdump-class-hierarchy==== | + | |
- | Taille des classes et tables virtuelles. | + | |
- | g++ -I… -fdump-class-hierarchy -c visitor1.cc | + | ====Ubuntu==== |
- | < | + | |
- | Vtable for pattern:: | + | |
- | pattern:: | + | |
- | 0 (int (*)(...))0 | + | |
- | 8 (int (*)(...))(& | + | |
- | 16 0 | + | |
- | 24 0 | + | |
- | 32 (int (*)(...))__cxa_pure_virtual | + | |
- | Class pattern:: | + | Ubuntu ne fournit pas de méthode 100% automatique. Il faut passer manuellement par '' |
- | size=8 align=8 | + | |
- | base size=8 base align=8 | + | <code bash> |
- | pattern:: | + | sudo update-alternatives --install / |
- | | + | |
</ | </ | ||
- | ====gprof==== | + | Utiliser pour switcher entre les versions : |
- | Génère un graphe de l' | + | |
- | [[https:// | + | <code bash> |
+ | sudo update-alternatives --config | ||
+ | </code> | ||
- | =====Commun avec clang===== | + | =====Diagnostic===== |
- | ====Sanitizer==== | + | ====Information |
- | Il existe '' | + | |
- | ===address=== | + | |
- | Il détecte des erreurs de type global-buffer-overflow, | + | |
- | [[https:// | + | |
- | * global-buffer-overflow | + | <code cpp> |
- | <file c main.c> | + | class VirtualBase |
- | int global_array[100] | + | { |
+ | public : | ||
+ | virtual | ||
+ | }; | ||
+ | |||
+ | class VirtualDerived : public VirtualBase | ||
+ | { | ||
+ | public : | ||
+ | int m_counter; | ||
+ | public : | ||
+ | VirtualDerived() : m_counter(0) | ||
+ | int tick(int n) { m_counter += n; return m_counter; } | ||
+ | }; | ||
+ | </ | ||
- | int main(int argc, char **argv) { | + | |
- | | + | |
- | } | + | |
- | </ | + | |
- | gcc main.c -g -fsanitize=address -o main && ./main | + | < |
+ | Vtable for VirtualBase | ||
+ | VirtualBase:: | ||
+ | 0 (int (*)(...))0 | ||
+ | 8 (int (*)(...))(& _ZTI11VirtualBase) | ||
+ | 16 (int (*)(...))__cxa_pure_virtual | ||
- | ================================================================= | + | Class VirtualBase |
- | ==2500==ERROR: | + | |
- | READ of size 4 at 0x000000740cf4 thread T0 | + | base size=8 base align=8 |
- | #0 0x50d9be in main / | + | VirtualBase |
- | #1 0x7f1208faa461 in __libc_start_main / | + | |
- | #2 0x419709 in _start | + | |
- | + | ||
- | 0x000000740cf4 is located 4 bytes to the right of global variable ' | + | |
- | SUMMARY: AddressSanitizer: | + | |
- | Shadow bytes around the buggy address: | + | |
- | | + | |
- | 0x0000800e0150: | + | |
- | 0x0000800e0160: | + | |
- | 0x0000800e0170: | + | |
- | 0x0000800e0180: | + | |
- | | + | |
- | 0x0000800e01a0: f9 f9 f9 f9 f9 f9 f9 f9 f9 f9 f9 f9 00 00 00 00 | + | |
- | 0x0000800e01b0: | + | |
- | 0x0000800e01c0: | + | |
- | 0x0000800e01d0: | + | |
- | 0x0000800e01e0: | + | |
- | Shadow byte legend (one shadow byte represents 8 application bytes): | + | |
- | Addressable: | + | |
- | Partially addressable: | + | |
- | Heap left redzone: | + | |
- | Freed heap region: | + | |
- | Stack left redzone: | + | |
- | Stack mid redzone: | + | |
- | Stack right redzone: | + | |
- | Stack after return: | + | |
- | Stack use after scope: | + | |
- | Global redzone: | + | |
- | Global init order: | + | |
- | Poisoned by user: f7 | + | |
- | Container overflow: | + | |
- | Array cookie: | + | |
- | Intra object redzone: | + | |
- | ASan internal: | + | |
- | Left alloca redzone: | + | |
- | Right alloca redzone: | + | |
- | ==2500==ABORTING | + | |
- | [[prog:valgrind|valgrind]] ne détecte pas l' | + | Vtable for VirtualDerived |
+ | VirtualDerived:: | ||
+ | 0 (int (*)(...))0 | ||
+ | 8 (int (*)(...))(& | ||
+ | 16 (int (*)(...))VirtualDerived:: | ||
- | | + | Class VirtualDerived |
- | <file c main2.cc> | + | |
- | int main(int argc, char **argv) { | + | base size=12 base align=8 |
- | int *array | + | VirtualDerived |
- | | + | vptr=((& VirtualDerived:: |
- | | + | |
- | } | + | |
- | </file> | + | </code> |
- | | + | |
- | ================================================================= | + | Ci-dessous, |
- | ==2765==ERROR: | + | |
- | READ of size 4 at 0x614000000044 thread T0 | + | |
- | #0 0x512444 in main / | + | |
- | #1 0x7fd8d0086461 in __libc_start_main / | + | |
- | #2 0x419d29 in _start (/ | + | |
- | + | ||
- | 0x614000000044 is located 4 bytes inside of 400-byte region [0x614000000040, | + | |
- | freed by thread T0 here: | + | |
- | #0 0x50ef30 in operator delete[](void*) / | + | |
- | #1 0x5123f6 in main / | + | |
- | #2 0x7fd8d0086461 in __libc_start_main / | + | |
- | #3 0x419d29 in _start (/ | + | |
- | + | ||
- | previously allocated by thread T0 here: | + | |
- | #0 0x50e1c8 in operator new[](unsigned long) / | + | |
- | #1 0x5123d4 in main / | + | |
- | #2 0x7fd8d0086461 in __libc_start_main / | + | |
- | #3 0x419d29 in _start (/ | + | |
- | + | ||
- | SUMMARY: AddressSanitizer: | + | |
- | Shadow bytes around the buggy address: | + | |
- | 0x0c287fff7fb0: | + | |
- | 0x0c287fff7fc0: | + | |
- | 0x0c287fff7fd0: | + | |
- | 0x0c287fff7fe0: | + | |
- | 0x0c287fff7ff0: | + | |
- | => | + | |
- | 0x0c287fff8010: | + | |
- | 0x0c287fff8020: | + | |
- | 0x0c287fff8030: | + | |
- | 0x0c287fff8040: | + | |
- | 0x0c287fff8050: | + | |
- | [[prog: | + | < |
+ | *** Dumping AST Record Layout | ||
+ | 0 | class VirtualBase | ||
+ | 0 | | ||
+ | | ||
+ | | | ||
- | ==2787== Command: ./main2 | + | *** Dumping AST Record Layout |
- | | + | 0 | class VirtualDerived |
- | | + | 0 | class VirtualBase |
- | ==2787== | + | 0 | (VirtualBase vtable pointer) |
- | | + | 8 | int m_counter |
- | ==2787== | + | | [sizeof=16, dsize=12, align=8, |
- | | + | | nvsize=12, nvalign=8] |
- | ==2787== Block was alloc' | + | </ |
- | ==2787== | + | |
- | | + | |
- | * stack-use-after-return | + | * Table virtuelle uniquement : '' |
- | <file c main3.c> | + | |
- | int *g; | + | |
- | void LeakLocal() { | + | < |
- | int local; | + | Vtable for ' |
- | g = &local; | + | 0 | offset_to_top (0) |
- | } | + | 1 | VirtualDerived RTTI |
+ | -- (VirtualBase, | ||
+ | -- (VirtualDerived, | ||
+ | 2 | int VirtualDerived:: | ||
- | int main(){ | + | VTable indices for ' |
- | | + | 0 | int VirtualDerived:: |
- | return *g; | + | |
- | } | + | |
- | </ | + | |
- | gcc main3.c -g -o main3 -fsanitize=address | + | Vtable for ' |
- | | + | 0 | offset_to_top (0) |
+ | 1 | VirtualBase RTTI | ||
+ | -- (VirtualBase, | ||
+ | 2 | int VirtualBase:: | ||
- | L'utilisation de '' | + | VTable indices for 'VirtualBase' |
+ | 0 | int VirtualBase:: | ||
+ | </ | ||
+ | ====gprof==== | ||
+ | Génère un graphe de l'utilisation des fonctions d'un programme (nécessite l' | ||
- | ================================================================= | + | [[https://www.thegeekstuff.com/2012/08/gprof-tutorial/|GPROF Tutorial – How to use Linux GNU GCC Profiling Tool]] {{ :prog:gcc:gprof_tutorial_how_to_use_linux_gnu_gcc_profiling_tool_2020-02-24_3_24_57_pm_.html |Archive du 20/08/2012 le 24/ |
- | ==2907==ERROR: AddressSanitizer: | + | |
- | READ of size 4 at 0x7f57b2a00020 thread T0 | + | |
- | #0 0x50db24 in main /tmp/main3.c:10:10 | + | |
- | #1 0x7f57b604d461 in __libc_start_main / | + | |
- | #2 0x419709 in _start (/tmp/ | + | |
- | + | ||
- | Address 0x7f57b2a00020 is located in stack of thread T0 at offset 32 in frame | + | |
- | #0 0x50d95f in LeakLocal / | + | |
- | + | ||
- | This frame has 1 object(s): | + | |
- | [32, 36) ' | + | |
- | HINT: this may be a false positive if your program uses some custom stack unwind mechanism or swapcontext | + | |
- | (longjmp and C++ exceptions *are* supported) | + | |
- | SUMMARY: AddressSanitizer: stack-use-after-return | + | |
- | Shadow bytes around the buggy address: | + | |
- | 0x0feb76537fb0: | + | |
- | 0x0feb76537fc0: | + | |
- | 0x0feb76537fd0: | + | |
- | 0x0feb76537fe0: | + | |
- | 0x0feb76537ff0: | + | |
- | => | + | |
- | 0x0feb76538010: | + | |
- | 0x0feb76538020: | + | |
- | 0x0feb76538030: | + | |
- | 0x0feb76538040: | + | |
- | 0x0feb76538050: | + | |
- | [[prog: | + | ====Couverture de code==== |
- | valgrind | + | Options à ajouter à la compilation : '' |
- | ==2961== Syscall param exit_group(status) contains uninitialised byte(s) | + | Options à ajouter au lieur : '' |
- | ==2961== | + | |
- | ==2961== | + | |
- | ==2961== | + | |
- | ==2961== | + | |
- | ==2961== | + | |
- | ==2961== | + | |
- | ===thread=== | + | Puis exécuter le ou les programmes. Attention, pas d' |
- | <file cpp main4.cc> | + | |
- | #include < | + | |
- | int main() { | + | <code bash> |
- | int x; | + | lcov --capture |
- | std::thread t([& | + | lcov --remove build/coverage.info "/usr/include/*" |
- | x = 43; | + | genhtml |
- | t.join(); | + | find build/coverage -name "*.html" -exec sed -i -r "s#headerValue"" |
- | + | </code> | |
- | return 0; | + | |
- | } | + | |
- | </file> | + | |
- | + | ||
- | g++ -std=c++11 main4.c | + | |
- | + | ||
- | <note important> | + | |
- | </ | + | |
- | + | ||
- | ================== | + | |
- | WARNING: ThreadSanitizer: | + | |
- | Write of size 4 at 0x7ffd04d5b144 by thread T1: | + | |
- | #0 operator() / | + | |
- | #1 __invoke_impl< | + | |
- | #2 __invoke< | + | |
- | #3 _M_invoke< | + | |
- | #4 operator() / | + | |
- | #5 _M_run / | + | |
- | #6 < | + | |
- | + | ||
- | Previous write of size 4 at 0x7ffd04d5b144 by main thread: | + | |
- | #0 main /tmp/main4.c:6 (main4+0x000000400d75) | + | |
- | + | ||
- | Location is stack of main thread. | + | |
- | + | ||
- | Thread T1 (tid=3228, running) created by main thread at: | + | |
- | | + | |
- | #1 std:: | + | |
- | #2 main / | + | |
- | + | ||
- | SUMMARY: ThreadSanitizer: | + | |
- | ================== | + | |
- | ThreadSanitizer: | + | |
- | + | ||
- | [[prog: | + | |
- | + | ||
- | ===undefined=== | + | |
- | <file c main5.c> | + | |
- | int main(int argc, char **argv) | + | |
- | int t = argc << 16; | + | |
- | return t*t; | + | |
- | } | + | |
- | </file> | + | |
- | + | ||
- | gcc -fsanitize=undefined main5.c -g -o main5 | + | |
- | main6.c:3:11: runtime error: signed integer overflow: 65536 * 65536 cannot be represented in type ' | + | Attention, la version de gcov doit correspondre à celle de gcc. |
- | [[prog: | + | En option, on supprime la date pour qu'un diff simple puisse se faire. |
prog/gcc.1582554219.txt.gz · Dernière modification : 2020/02/24 15:23 de root