Ilya_konstantinov

Untitled

Feb 27th, 2026
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.69 KB | None | 0 0
  1. =============================== solution.S
  2. .intel_syntax noprefix
  3. .section .note.GNU-stack
  4.  
  5. .text
  6.  
  7. .global print_tb
  8. print_tb:
  9. // TODO: your code here.
  10.  
  11. =============================== tests.cpp
  12. #include <cassert>
  13. #include <cstdint>
  14. #include <cstdio>
  15. #include <cstdlib>
  16.  
  17. extern "C" void print_tb();
  18.  
  19. static uint64_t CanaryValue(uint64_t seed) {
  20. for (size_t i = 0; i < 3; ++i) {
  21. seed ^= 424243;
  22. seed *= 12345;
  23. }
  24. return seed;
  25. }
  26.  
  27. #define BODY(n, self) \
  28. do { \
  29. volatile uint64_t canaries[n]; \
  30. for (size_t i = 0; i < n; ++i) { \
  31. canaries[i] = CanaryValue(i); \
  32. } \
  33. int retval; \
  34. if (seed == 1) { \
  35. print_tb(); \
  36. retval = 0; \
  37. } else if (seed % 2 != 0) { \
  38. retval = foo(3 * seed + 1, print); \
  39. } else { \
  40. retval = bar(seed / 2, print); \
  41. } \
  42. for (size_t i = 0; i < n; ++i) { \
  43. assert(canaries[i] == CanaryValue(i)); \
  44. } \
  45. if (print) { \
  46. printf("I am %s\n", self); \
  47. } \
  48. return retval; \
  49. } while (0)
  50.  
  51. extern "C" int foo(int seed, bool print);
  52. extern "C" int bar(int seed, bool print);
  53.  
  54. int main() {
  55. int seed;
  56. if (scanf("%d", &seed) != 1) {
  57. return 1;
  58. }
  59. bool print = std::getenv("PRINT_TB") != nullptr;
  60. BODY(2, "main");
  61. }
  62.  
  63. extern "C" __attribute__((noinline)) int bar(int seed, bool print) {
  64. BODY(3, "bar");
  65. }
  66.  
  67. extern "C" __attribute__((noinline)) int foo(int seed, bool print) {
  68. BODY(5, "foo");
  69. }
  70.  
Advertisement
Add Comment
Please, Sign In to add comment