flip_value.py 7.29 KB
Newer Older
1
import os,signal
German Leon's avatar
German Leon committed
2
3
4
5
6
import gdb
import time
from classes.BitFlip import BitFlip
from classes.Logging import Logging
import common_parameters as cp
German Leon's avatar
German Leon committed
7
import common_functions as cf  # All common functions will be at common_functions module
German Leon's avatar
German Leon committed
8
9
10
"""
Handler attached to exit event
"""
German Leon's avatar
German Leon committed
11
12
def sendsignal (signal):
   os.kill(int(pid),signal)
German Leon's avatar
German Leon committed
13
14
def exit_handler(event):
    global global_logging
German Leon's avatar
German Leon committed
15
    sendsignal(cp.SIGNAL_EXIT)
German Leon's avatar
German Leon committed
16
    global_logging.info(str("event type: exit"))
17
  
German Leon's avatar
German Leon committed
18
19
20
21
22
23
24
25
26
27
28
29
30
    try:
        global_logging.info("exit code: {}".format(str(event.exit_code)))
    except Exception as err:
        err_str = "ERROR: {}".format(str(err))
        global_logging.exception(err_str)


"""
Handler that will put a breakpoint on the kernel after
signal
"""


German Leon's avatar
German Leon committed
31
32
33


def set_event_normal(event):
German Leon's avatar
German Leon committed
34
    # Accessing global vars
German Leon's avatar
German Leon committed
35
    global global_logging, was_hit, bit_flip,bp,t,primero
German Leon's avatar
German Leon committed
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75

    if (isinstance(event, gdb.SignalEvent)):
      gdb.events.stop.disconnect(set_event_normal)
      try:        
        # Just checking if it was hit
         if bit_flip.fault_injected is False:
            bit_flip.single_event()
            global_logging.info("BIT FLIP SET ON SIGNAL {}".format(event.stop_signal))
         #global_logging.info ("Enviado senal a "+ str(pid))
         #sendsignal(cp.SIGNAL_STOP)    
         gdb.events.stop.connect(set_event_normal)
      except Exception as err:
        global_logging.exception("EVENT DIFFERENT FROM STOP SIGNAL: {}".format(str(err)))    
    else:    
    	 global_logging.exception("EVENT DIFFERENT SignalEvent FROM STOP SIGNAL: {}".format(isinstance(event, gdb.BreakpointEvent))) 

def set_event_inst_adr(event):
    # Accessing global vars
    global global_logging, was_hit, bit_flip,bpia,t,primero,data_kernels
    if (isinstance(event, gdb.SignalEvent)):
      try:     
        # Just checking if it was hit
        #Generar una direccion aleatoria a un kernel y almacenarlo en checkpointkernel
        #checkpointkernel=" *0x555555c71ed0 "
        gdb.events.stop.disconnect(set_event_inst_adr)
        BitFlip.update_data_kernels(data_kernels)
       # global_logging.info(BitFlip.principiokernel("lud_diagonal"))
       #global_logging.info(BitFlip.principiokernel("lud_internal"))
       # global_logging.info(BitFlip.principiokernel("lud_perimeter"))
       # global_logging.info(BitFlip.principio2kernel("lud_diagonal"))
       # global_logging.info(BitFlip.principio2kernel("lud_internal"))
       # global_logging.info(BitFlip.principio2kernel("lud_perimeter"))
        checkpointkernel=BitFlip.address_random(data_kernels)
    
        global_logging.info("BREAK {} \n".format(checkpointkernel))
        bpia=gdb.Breakpoint(checkpointkernel)
        
        gdb.events.stop.connect(set_event_break_instr_adr)
      except Exception as err:
        global_logging.exception("EVENT DIFFERENT FROM STOP SIGNAL: {} IN set_event_inst_adr ".format(str(err)))      
German Leon's avatar
German Leon committed
76
        
German Leon's avatar
German Leon committed
77
78
79
80
81
82
83
84
def set_event_break_instr_adr(event):
    # Accessing global vars
    global global_logging, was_hit, bit_flip,bpia,t,primero
    if (isinstance(event, gdb.BreakpointEvent)):
      try: 
        gdb.events.stop.disconnect(set_event_break_instr_adr)
        global_logging.info("RUN BREAK {} \n")
        bpia.delete()
German Leon's avatar
German Leon committed
85
86
87
88
        # Just checking if it was hit
        if bit_flip.fault_injected is False:
            bit_flip.single_event()
            global_logging.info("BIT FLIP SET ON SIGNAL {}".format(event.stop_signal))
German Leon's avatar
German Leon committed
89
        #global_logging.info ("Enviado senal a "+ str(pid))
German Leon's avatar
German Leon committed
90
91
92
93
94
95
        #bpia.enabled=False
        
        
        gdb.events.stop.connect(set_event_inst_adr)
        #sendsignal(cp.SIGNAL_STOP)
        gdb.execute('c')     
German Leon's avatar
German Leon committed
96
      except Exception as err:
German Leon's avatar
German Leon committed
97
98
        global_logging.exception("EVENT DIFFERENT FROM STOP SIGNAL: {} IN set_event_break_instr_adr".format(str(err)))     
             
German Leon's avatar
German Leon committed
99

German Leon's avatar
German Leon committed
100
#De esta forma, si llega un event por nexti (event.stop), no realiza nada.
German Leon's avatar
German Leon committed
101
102
103
104
105
106
"""
Main function
"""


def main():
German Leon's avatar
German Leon committed
107
108
    global global_logging, register, injection_site, bits_to_flip, fault_model, was_hit, bit_flip, arg0,t,kernel,pid,bp,data_kernels,rsi,traza
   
German Leon's avatar
German Leon committed
109
    was_hit = False
German Leon's avatar
German Leon committed
110
111
    indirect= cp.INDIRECT_KERNEL 
    pruebaDebug=True
German Leon's avatar
German Leon committed
112
113
114
115
116
117
118
119
120
    # Initialize GDB to run the app
    gdb.execute("set confirm off")
    gdb.execute("set pagination off")
    gdb.execute("set target-async off")
    gdb.execute("set non-stop off")

    # Connecting to a exit handler event
    gdb.events.exited.connect(exit_handler)

German Leon's avatar
German Leon committed
121
   
German Leon's avatar
German Leon committed
122
                           
German Leon's avatar
German Leon committed
123
124
    # Get variables values from environment
    # Firsn parse line
German Leon's avatar
German Leon committed
125
    [kernel,pid,max_regs,file_connect,bits_to_flip, fault_model, flip_log_file,
German Leon's avatar
German Leon committed
126
     gdb_init_strings, injection_site] = arg0.split('|')
German Leon's avatar
German Leon committed
127
128
    
   
German Leon's avatar
German Leon committed
129
    pruebaDebug=(cp.INJECTION_SITES[injection_site]==cp.INST_OUT_V1)
German Leon's avatar
German Leon committed
130
131
    # Logging
    global_logging = Logging(log_file=flip_log_file)
German Leon's avatar
German Leon committed
132
    global_logging.info("Starting flip_value script "+" called by " + str(pid) + " for stop kernel " + str(kernel)+". This kernel has"+str(max_regs)+".");
German Leon's avatar
German Leon committed
133
134
135
136
137
138
139
140
141
142
143
    try:
        for init_str in gdb_init_strings.split(";"):
            gdb.execute(init_str)
            global_logging.info("initializing setup: " + str(init_str))

    except gdb.error as err:
        global_logging.exception("ERROR on initializing setup: {}".format(str(err)))

    # Set Breakpoint attributes to be use
    bits_to_flip = [i for i in bits_to_flip.split(",")]
    fault_model = int(fault_model)
leon@uji.es's avatar
leon@uji.es committed
144
    bit_flip = BitFlip(bits_to_flip=bits_to_flip, fault_model=fault_model,max_regs=max_regs,
German Leon's avatar
German Leon committed
145
                       logging=global_logging, injection_site=cp.INJECTION_SITES[injection_site])
German Leon's avatar
German Leon committed
146
147
    data_kernels=bit_flip.read_data_kernels(pruebaDebug)                    
           
German Leon's avatar
German Leon committed
148
149
    # Start app execution
    t=time.clock();
German Leon's avatar
German Leon committed
150
    gdb.Breakpoint('main')
German Leon's avatar
German Leon committed
151
    #gdb.execute("break "+kernel)
German Leon's avatar
German Leon committed
152
153
154
155
156
157
    
    
   
   
   
    global_logging.info("Put Break "+kernel+" "+ str(time.clock()-t))
German Leon's avatar
German Leon committed
158
159
    gdb.execute("r")

German Leon's avatar
German Leon committed
160
161
162
163
    try:
       pid_bench=gdb.execute ("info proc", to_string=True).splitlines()[0].split(' ')[1]
    except:
      global_logging.info("problema solictando info proc")
German Leon's avatar
German Leon committed
164
  
German Leon's avatar
German Leon committed
165
166
167
168
169
170
    global_logging.info("PID: {}".format(pid_bench)) 
       
    fp= open(file_connect,"w")
    fp.write(pid_bench)
    fp.close()
    # Connecting to a stop signal event
German Leon's avatar
German Leon committed
171
172
173
174
175
176
177
    if indirect:
      print ("Indirecto")
      gdb.execute("set cuda break_on_launch application")  
    else: 
      bp=gdb.Breakpoint(kernel)  
    gdb.execute('c')  
   
German Leon's avatar
German Leon committed
178

German Leon's avatar
German Leon committed
179
180
181
182
183
184
185
186
187
188
189
190
    #bp.enabled=False
    if indirect:
      gdb.execute("set cuda break_on_launch none")
      gdb.events.stop.connect(set_event_inst_adr)
    else:
      bp.delete()
      gdb.events.stop.connect(set_event_normal)
    global_logging.info("Before breakpoint"+ str(time.clock()-t))
    global_logging.info ("Enviado senal a "+ str(pid))
    sendsignal(cp.SIGNAL_STOP)
    gdb.execute('c')
    
German Leon's avatar
German Leon committed
191
    print("4")
German Leon's avatar
German Leon committed
192
193
194
195
196
197
198
199
200
201
202
203
204
    i = 0
    try:
        while 'The program' not in gdb.execute('c', to_string=True):
            i += 1
    except Exception as err:
        global_logging.info("CONTINUED {} times".format(i))
        err_str = str(err).rstrip()
        global_logging.exception("IGNORED CONTINUE ERROR: {}".format(err_str))

        # Make sure that it is going to finish
        if 'Failed' in err_str:
            gdb.execute('quit')
            global_logging.exception("QUIT REQUIRED")
German Leon's avatar
German Leon committed
205
    print("6")
German Leon's avatar
German Leon committed
206
207
208
209
210
211
212
213
214
215
216

# Call main execution
global_logging = None
register = None
bits_to_flip = None
fault_model = None
was_hit = False
injection_site = None
bit_flip = None

main()
German Leon's avatar
German Leon committed
217
218
219
220
221
222