ejschwartz commited on
Commit
e98177c
·
1 Parent(s): 3db437f
Files changed (1) hide show
  1. main.py +31 -8
main.py CHANGED
@@ -2,25 +2,46 @@ import gradio as gr
2
  import subprocess
3
  import tempfile
4
 
 
 
 
 
 
 
 
 
 
5
  def compile(source):
6
  # Create a temporary file for the C source code
7
  with tempfile.NamedTemporaryFile(suffix=".c", delete=False) as temp_c_file:
8
  temp_c_file.write(source.encode())
9
  temp_c_file_name = temp_c_file.name
10
-
11
  # Create a temporary file for the object file
12
  with tempfile.NamedTemporaryFile(suffix=".o", delete=False) as temp_o_file:
13
  temp_o_file_name = temp_o_file.name
14
-
15
  # Compile the C file to an object file
16
  subprocess.run(["cc", "-c", temp_c_file_name, "-o", temp_o_file_name], check=True)
17
-
18
- # Read the compiled object file and load the bytes into a bytestring
19
- with open(temp_o_file_name, "rb") as f:
20
- compiled_bytes = f.read()
21
-
 
 
 
 
 
 
 
 
 
 
 
22
  return compiled_bytes
23
 
 
24
  def predict(source):
25
  bytes = compile(source)
26
  return bytes, 1.0
@@ -29,10 +50,12 @@ def predict(source):
29
  def run():
30
  demo = gr.Interface(
31
  fn=predict,
 
32
  inputs=gr.Textbox(lines=10, label="Decompiled C Source Code"),
33
  outputs=[gr.Textbox(), gr.Number()],
34
  )
35
 
36
  demo.launch(server_name="0.0.0.0", server_port=7860)
37
 
38
- run()
 
 
2
  import subprocess
3
  import tempfile
4
 
5
+ description = """This is a space testing a method for evaluating the quality of decompilation.
6
+
7
+ Currently unhandled features:
8
+ * Global references
9
+ * Function calls
10
+ * Compiler and flag selection
11
+ """
12
+
13
+
14
  def compile(source):
15
  # Create a temporary file for the C source code
16
  with tempfile.NamedTemporaryFile(suffix=".c", delete=False) as temp_c_file:
17
  temp_c_file.write(source.encode())
18
  temp_c_file_name = temp_c_file.name
19
+
20
  # Create a temporary file for the object file
21
  with tempfile.NamedTemporaryFile(suffix=".o", delete=False) as temp_o_file:
22
  temp_o_file_name = temp_o_file.name
23
+
24
  # Compile the C file to an object file
25
  subprocess.run(["cc", "-c", temp_c_file_name, "-o", temp_o_file_name], check=True)
26
+
27
+ # Create a temporary file for the raw bytes
28
+ with tempfile.NamedTemporaryFile(suffix=".raw", delete=True) as raw_bytes_file:
29
+ subprocess.run(
30
+ [
31
+ "objcopy",
32
+ "--only-section",
33
+ ".text",
34
+ "-O",
35
+ "binary",
36
+ temp_o_file_name,
37
+ raw_bytes_file.name,
38
+ ]
39
+ )
40
+ compiled_bytes = raw_bytes_file.read()
41
+
42
  return compiled_bytes
43
 
44
+
45
  def predict(source):
46
  bytes = compile(source)
47
  return bytes, 1.0
 
50
  def run():
51
  demo = gr.Interface(
52
  fn=predict,
53
+ description=description,
54
  inputs=gr.Textbox(lines=10, label="Decompiled C Source Code"),
55
  outputs=[gr.Textbox(), gr.Number()],
56
  )
57
 
58
  demo.launch(server_name="0.0.0.0", server_port=7860)
59
 
60
+
61
+ run()