Spaces:
Sleeping
Sleeping
Commit ·
c2e30e3
1
Parent(s): 31b9410
Simplify the input form and highlight result code
Browse filesShow artifacts as radio buttons with a short description, collapse the SEFM
configuration unless uploading a binary, and hide the status box until a run
starts. Drop duplicate file headings and the file list for single-file tabs.
Highlight C, NASM and Isabelle results with Pygments.
Files changed (2)
- app.py+43-19
- requirements.txt+1
9 unmodified lines1011121314151 unmodified line1718192021222324252627282930313233343528 unmodified lines646566676869706 unmodified lines7778798081828384858687888914 unmodified lines10410510610710810911011111211311411511611711811912012112212328 unmodified lines15215315415515615715879 unmodified lines2382392402412422432442452462471 unmodified line2492502512522532542551 unmodified line2572582592602612622632642652662672682692702712729 unmodified linesfrom time import monotonicimport gradio as grfrom ccs import stages as ccs_stagesfrom runner import DEADLINE, MAX_UPLOAD, pldi, read_preview, stage_upload1 unmodified linefrom sefm import stages as sefm_stagesARTIFACTS = {"SEFM 2020 · C decompilation": ("wc", 120, sefm_stages),"PLDI 2022 · SSM Construction": ("simple_local_array", DEADLINE, pldi),"CCS 2024 · Verified lifting": ("wc", 480, ccs_stages),}EXAMPLE = "Built-in example"UPLOAD = "Upload a binary"SOURCE = "Compile C source"DISPLAY_LIMIT = 4 * 1024 * 1024logger = logging.getLogger(__name__)def prepare():return "Queued", "", gr.update(interactive=False)def display_results(work, files):28 unmodified linesdata = stream.read(DISPLAY_LIMIT + 1)truncated = len(data) > DISPLAY_LIMITdata = data[:DISPLAY_LIMIT]content = escape(data.decode(errors="replace")) or "No output."if truncated:content += "\n[Display limit reached for this file]"graph = ""6 unmodified linesexcept (OSError, subprocess.SubprocessError):graph = "<p>Graph rendering unavailable. DOT source is shown below.</p>"source = f'<pre>{content}</pre>' if not graph else f'<details><summary>DOT source</summary><pre>{content}</pre></details>'rendered_files.append(f'<section><h4>{escape(path.name)}</h4>{graph}{source}</section>')ident = f"file-{group_index}-{index}"choices.append(f'<input type="radio" name="files-{group_index}" id="{ident}"{selected}>'f'<label for="{ident}">{safe_name}</label>')contents.append(f'<article class="file-content file-{index}"><h3>{safe_name}</h3>{"".join(rendered_files)}</article>')rules = ''.join(f'.files-{group_index}:has(#file-{group_index}-{i}:checked) .file-{i}{{display:block}}' for i in range(len(entries)))panels.append(f'<div class="panel panel-{group_index}"><div class="files files-{group_index}">'f'<aside>{"".join(choices)}</aside><main>{"".join(contents)}</main></div><style>{rules}</style></div>')rules = ''.join(f'.workspace:has(#view-{i}:checked) .panel-{i}{{display:block}}' for i in range(len(panels)))return f'<div class="foxdec-results"><h2>Results</h2><div class="workspace"><nav>{"".join(tabs)}</nav>{"".join(panels)}<style>{rules}</style></div></div>'14 unmodified linesdef input_choices(artifact):example_name, _, workflow = ARTIFACTS[artifact]return [(f"{EXAMPLE} · {example_name}", EXAMPLE), *([(SOURCE, SOURCE)] if workflow is sefm_stages else []), (UPLOAD, UPLOAD)]def mode_controls(artifact, choice):if ARTIFACTS[artifact][2] is not sefm_stages:return gr.update(visible=choice == UPLOAD), *[gr.update(visible=False)] * 3config = {EXAMPLE: sefm.example_config, SOURCE: lambda: sefm.C_EXAMPLE_CONFIG, UPLOAD: lambda: sefm.CONFIG_TEMPLATE}[choice]()return (gr.update(visible=choice == UPLOAD), gr.update(visible=choice == SOURCE, value=sefm.C_EXAMPLE),gr.update(visible=choice == SOURCE, value=sefm.DEFAULT_CFLAGS), gr.update(visible=True, value=config))def input_controls(artifact):return (gr.update(choices=input_choices(artifact), value=EXAMPLE), None,gr.update(value="", visible=ARTIFACTS[artifact][2] is not sefm_stages), *mode_controls(artifact, EXAMPLE))28 unmodified linesreturnstarted = monotonic()_, budget, workflow = ARTIFACTS[artifact]deadline = started + budgetphases = []with TemporaryDirectory(prefix="foxdec-job-") as directory:79 unmodified lines.foxdec-results .files { display:grid; grid-template-columns:minmax(12rem, 22%) minmax(0, 1fr); gap:1rem; margin-top:1rem; }.foxdec-results aside { max-height:40rem; overflow:auto; border-right:1px solid var(--border-color-primary); }.foxdec-results main { min-width:0; }.foxdec-results h3 { overflow-wrap:anywhere; }.foxdec-results pre { max-height:40rem; overflow:auto; padding:1rem; border-radius:.5rem; background:var(--background-fill-secondary); white-space:pre; }.graph-scroll { overflow:auto; background:white; padding:1rem; border-radius:.5rem; }.graph-scroll img { max-width:none; height:auto; }@media(max-width:700px) { .foxdec-results .files { grid-template-columns:1fr; } .foxdec-results aside { max-height:12rem; border-right:0; border-bottom:1px solid var(--border-color-primary); } }""",analytics_enabled=False,1 unmodified line) as demo:gr.Markdown("# FoxDec research artifacts\nAnalyze a Linux x86-64 binary, inspect the generated assembly and graphs, or verify a lifting certificate.")first = next(iter(ARTIFACTS))artifact = gr.Dropdown(list(ARTIFACTS), value=first, label="Artifact")mode = gr.Radio(input_choices(first), value=EXAMPLE, label="Input")with gr.Column(visible=False) as upload_inputs:upload = gr.File(label="Linux x86-64 ELF binary", type="filepath")1 unmodified linesource = gr.Code(label="C source (compiled with gcc, never run)", language="c", value=sefm.C_EXAMPLE, visible=False)cflags = gr.Textbox(label="Compiler options", value=sefm.DEFAULT_CFLAGS, visible=False,info="Optimization, code generation, warning and macro options. The paper’s tool expects x86-64 and no PIE.")config = gr.Textbox(label="Decompiler configuration (editable)", lines=10, max_lines=24, show_copy_button=True,value=sefm.example_config(), visible=True)run = gr.Button("Run analysis", variant="primary")status = gr.Textbox(label="Status", lines=5, max_lines=12, interactive=False)results = gr.HTML(value="<p>Run an analysis to see its results here.</p>")with gr.Accordion("Artifact catalog", open=False):gr.Markdown(Path(__file__).with_name("catalog.md").read_text())artifact.input(input_controls, artifact, [mode, upload, addresses, upload_inputs, source, cflags, config], queue=False, api_name=False)mode.input(mode_controls, [artifact, mode], [upload_inputs, source, cflags, config], queue=False, api_name=False)outputs = [status, results, run]run.click(prepare, outputs=outputs, queue=False, api_name=False).then(run_example, [artifact, mode, upload, addresses, source, cflags, config], outputs, concurrency_limit=1, api_name="run",9 unmodified lines1011121314151617181 unmodified line20212223242526272829303132333435363738394041424344454628 unmodified lines757677787980818283846 unmodified lines91929394959697989910010110210310410514 unmodified lines12012112212312412512612712812913013113213313413513613713813914028 unmodified lines16917017117217317417579 unmodified lines2552562572582592602612622632642652662671 unmodified line2692702712722732742752761 unmodified line2782792802812822832842852862872882892902912922932942952969 unmodified linesfrom time import monotonicimport gradio as grfrom pygments import highlightfrom pygments.formatters import HtmlFormatterfrom pygments.lexers import CLexer, IsabelleLexer, NasmLexerfrom ccs import stages as ccs_stagesfrom runner import DEADLINE, MAX_UPLOAD, pldi, read_preview, stage_upload1 unmodified linefrom sefm import stages as sefm_stagesARTIFACTS = {"SEFM 2020 · C decompilation": ("wc", 120, sefm_stages,"Decompiles x86-64 functions to C with the paper’s decompiler, then recompiles the C with `gcc`."),"PLDI 2022 · SSM Construction": ("simple_local_array", DEADLINE, pldi,"Builds a symbolic state machine for a binary and shows its assembly, graphs and diagnostics."),"CCS 2024 · Verified lifting": ("wc", 480, ccs_stages,"Lifts a binary to symbolized assembly, recompiles it and checks an equivalence certificate in Isabelle. ""The example takes about four minutes."),}EXAMPLE = "Built-in example"UPLOAD = "Upload a binary"SOURCE = "Compile C source"DISPLAY_LIMIT = 4 * 1024 * 1024HIGHLIGHT_LIMIT = 1024 * 1024LEXERS = {".c": CLexer, ".h": CLexer, ".asm": NasmLexer, ".thy": IsabelleLexer}HIGHLIGHT_CSS = "\n".join(rule for style, scope in (("friendly", ".foxdec-results pre"), ("monokai", ".dark .foxdec-results pre"))for rule in HtmlFormatter(style=style).get_token_style_defs(scope))logger = logging.getLogger(__name__)def prepare():return gr.update(value="Queued", visible=True), "", gr.update(interactive=False)def display_results(work, files):28 unmodified linesdata = stream.read(DISPLAY_LIMIT + 1)truncated = len(data) > DISPLAY_LIMITdata = data[:DISPLAY_LIMIT]text = data.decode(errors="replace")lexer = LEXERS.get(path.suffix)content = ((highlight(text, lexer(), HtmlFormatter(nowrap=True))if lexer and len(data) <= HIGHLIGHT_LIMIT else escape(text)) or "No output.")if truncated:content += "\n[Display limit reached for this file]"graph = ""6 unmodified linesexcept (OSError, subprocess.SubprocessError):graph = "<p>Graph rendering unavailable. DOT source is shown below.</p>"source = f'<pre>{content}</pre>' if not graph else f'<details><summary>DOT source</summary><pre>{content}</pre></details>'heading = f'<h4>{escape(path.name)}</h4>' if len(paths) > 1 else ""rendered_files.append(f'<section>{heading}{graph}{source}</section>')ident = f"file-{group_index}-{index}"choices.append(f'<input type="radio" name="files-{group_index}" id="{ident}"{selected}>'f'<label for="{ident}">{safe_name}</label>')contents.append(f'<article class="file-content file-{index}"><h3>{safe_name}</h3>{"".join(rendered_files)}</article>')rules = ''.join(f'.files-{group_index}:has(#file-{group_index}-{i}:checked) .file-{i}{{display:block}}' for i in range(len(entries)))single = " single" if len(entries) == 1 else ""panels.append(f'<div class="panel panel-{group_index}"><div class="files files-{group_index}{single}">'f'<aside>{"".join(choices)}</aside><main>{"".join(contents)}</main></div><style>{rules}</style></div>')rules = ''.join(f'.workspace:has(#view-{i}:checked) .panel-{i}{{display:block}}' for i in range(len(panels)))return f'<div class="foxdec-results"><h2>Results</h2><div class="workspace"><nav>{"".join(tabs)}</nav>{"".join(panels)}<style>{rules}</style></div></div>'14 unmodified linesdef input_choices(artifact):example_name, _, workflow, _ = ARTIFACTS[artifact]return [(f"{EXAMPLE} · {example_name}", EXAMPLE), *([(SOURCE, SOURCE)] if workflow is sefm_stages else []), (UPLOAD, UPLOAD)]def mode_controls(artifact, choice):if ARTIFACTS[artifact][2] is not sefm_stages:return gr.update(visible=choice == UPLOAD), *[gr.update(visible=False)] * 3, gr.update()config = {EXAMPLE: sefm.example_config, SOURCE: lambda: sefm.C_EXAMPLE_CONFIG, UPLOAD: lambda: sefm.CONFIG_TEMPLATE}[choice]()return (gr.update(visible=choice == UPLOAD), gr.update(visible=choice == SOURCE, value=sefm.C_EXAMPLE),gr.update(visible=choice == SOURCE, value=sefm.DEFAULT_CFLAGS),gr.update(visible=True, open=choice == UPLOAD), gr.update(value=config))def input_controls(artifact):return (ARTIFACTS[artifact][3], gr.update(choices=input_choices(artifact), value=EXAMPLE), None,gr.update(value="", visible=ARTIFACTS[artifact][2] is not sefm_stages), *mode_controls(artifact, EXAMPLE))28 unmodified linesreturnstarted = monotonic()_, budget, workflow, _ = ARTIFACTS[artifact]deadline = started + budgetphases = []with TemporaryDirectory(prefix="foxdec-job-") as directory:79 unmodified lines.foxdec-results .files { display:grid; grid-template-columns:minmax(12rem, 22%) minmax(0, 1fr); gap:1rem; margin-top:1rem; }.foxdec-results aside { max-height:40rem; overflow:auto; border-right:1px solid var(--border-color-primary); }.foxdec-results main { min-width:0; }.foxdec-results .files.single { grid-template-columns:minmax(0, 1fr); }.foxdec-results .files.single aside { display:none; }.foxdec-results h3 { overflow-wrap:anywhere; }.foxdec-results pre { max-height:40rem; overflow:auto; padding:1rem; border-radius:.5rem; background:var(--background-fill-secondary); white-space:pre; }.graph-scroll { overflow:auto; background:white; padding:1rem; border-radius:.5rem; }.graph-scroll img { max-width:none; height:auto; }""" + HIGHLIGHT_CSS + """@media(max-width:700px) { .foxdec-results .files { grid-template-columns:1fr; } .foxdec-results aside { max-height:12rem; border-right:0; border-bottom:1px solid var(--border-color-primary); } }""",analytics_enabled=False,1 unmodified line) as demo:gr.Markdown("# FoxDec research artifacts\nAnalyze a Linux x86-64 binary, inspect the generated assembly and graphs, or verify a lifting certificate.")first = next(iter(ARTIFACTS))artifact = gr.Radio(list(ARTIFACTS), value=first, label="Artifact")about = gr.Markdown(ARTIFACTS[first][3])mode = gr.Radio(input_choices(first), value=EXAMPLE, label="Input")with gr.Column(visible=False) as upload_inputs:upload = gr.File(label="Linux x86-64 ELF binary", type="filepath")1 unmodified linesource = gr.Code(label="C source (compiled with gcc, never run)", language="c", value=sefm.C_EXAMPLE, visible=False)cflags = gr.Textbox(label="Compiler options", value=sefm.DEFAULT_CFLAGS, visible=False,info="Optimization, code generation, warning and macro options. The paper’s tool expects x86-64 and no PIE.")with gr.Accordion("Decompiler configuration", open=False) as config_panel:config = gr.Textbox(show_label=False, lines=10, max_lines=24, show_copy_button=True,value=sefm.example_config(),info="Functions to decompile, their argument registers, and the C preamble.")run = gr.Button("Run analysis", variant="primary", size="lg")status = gr.Textbox(label="Status", lines=4, max_lines=12, interactive=False, visible=False)results = gr.HTML(value="<p>Run an analysis to see its results here.</p>")with gr.Accordion("Artifact catalog", open=False):gr.Markdown(Path(__file__).with_name("catalog.md").read_text())mode_outputs = [upload_inputs, source, cflags, config_panel, config]artifact.input(input_controls, artifact, [about, mode, upload, addresses, *mode_outputs], queue=False, api_name=False)mode.input(mode_controls, [artifact, mode], mode_outputs, queue=False, api_name=False)outputs = [status, results, run]run.click(prepare, outputs=outputs, queue=False, api_name=False).then(run_example, [artifact, mode, upload, addresses, source, cflags, config], outputs, concurrency_limit=1, api_name="run",
1gradio==5.49.112gradio==5.49.1pygments==2.21.0
app.py+43-19
requirements.txt+1