Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[#!] #!/bin/sh -> #!/usr/bin/env sh
[simgrid.git] / tools / stack-cleaner / clean-stack-filter
1 #!/usr/bin/env perl
2 # Transform assembly in order to clean each stack frame for X86_64.
3
4 use strict;
5 use warnings;
6
7 $SIG{__WARN__} = sub { die @_ };
8
9 # Whether we are still scanning the content of a function:
10 our $scanproc = 0;
11
12 # Save lines of the function:
13 our $lines = "";
14
15 # Size of the stack for this function:
16 our $size = 0;
17
18 # Counter for assigning unique ids to labels:
19 our $id=0;
20
21 sub emit_code {
22     my $qsize = $size / 8;
23     my $offset = - $size - 8;
24
25     if($size != 0) {
26       # This is a crude hack to disable the stack cleaning on the main
27       # stack.  It relies on the fact that the main stack is high in
28       # the address space and the other stacks are in the heap (lower).
29       print("\tmovq \$0x7fff00000000, %r11\n");
30       print("\tcmpq %r11, %rsp\n");
31       print("\tjae .Lstack_cleaner_done$id\n");
32
33       # Loop over the stack frame quadwords and zero it:
34       print("\tmovabsq \$$qsize, %r11\n");
35       print(".Lstack_cleaner_loop$id:\n");
36       print("\tmovq    \$0, $offset(%rsp,%r11,8)\n");
37       print("\tsubq    \$1, %r11\n");
38       print("\tjne     .Lstack_cleaner_loop$id\n");
39       print(".Lstack_cleaner_done$id:\n");
40     }
41
42     print $lines;
43
44     $id = $id + 1;
45     $size = 0;
46     $lines = "";
47     $scanproc = 0;
48 }
49
50 while (<>) {
51   if ($scanproc) {
52       $lines = $lines . $_;
53       if (m/^[ \t]*.cfi_endproc$/) {
54         emit_code();
55       } elsif (m/^[ \t]*pushq/) {
56          $size += 8;
57       } elsif (m/^[ \t]*subq[\t *]\$([0-9]*),[ \t]*%rsp$/) {
58          my $val = $1;
59          $val = oct($val) if $val =~ /^0/;
60          $size += $val;
61          emit_code();
62       }
63   } elsif (m/^[ \t]*.cfi_startproc$/) {
64       print $_;
65
66       $scanproc = 1;
67   } else {
68       print $_;
69   }
70 }