binaryninja/
render_layer.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
//! Customize the presentation of Linear and Graph view output.

use crate::basic_block::{BasicBlock, BasicBlockType};
use crate::disassembly::DisassemblyTextLine;
use crate::flowgraph::FlowGraph;
use crate::function::{Function, NativeBlock};
use crate::linear_view::{LinearDisassemblyLine, LinearDisassemblyLineType, LinearViewObject};
use crate::rc::{Array, CoreArrayProvider, CoreArrayProviderInner};
use crate::string::BnStrCompatible;
use binaryninjacore_sys::*;
use std::ffi::{c_char, c_void};
use std::ptr::NonNull;

/// The state in which the [`RenderLayer`] will be registered with.
#[repr(u32)]
pub enum RenderLayerDefaultState {
    /// Register the [`RenderLayer`] as disabled, the user must then enable it via the UI.
    ///
    /// This is the default registration value.
    Disabled = 0,
    /// Register the [`RenderLayer`] as enabled, the user must then disable it via the UI.
    Enabled = 1,
    /// Use this if you do not want the render layer to be adjustable via the UI.
    AlwaysEnabled = 2,
}

impl From<BNRenderLayerDefaultEnableState> for RenderLayerDefaultState {
    fn from(value: BNRenderLayerDefaultEnableState) -> Self {
        match value {
            BNRenderLayerDefaultEnableState::DisabledByDefaultRenderLayerDefaultEnableState => {
                Self::Disabled
            }
            BNRenderLayerDefaultEnableState::EnabledByDefaultRenderLayerDefaultEnableState => {
                Self::Enabled
            }
            BNRenderLayerDefaultEnableState::AlwaysEnabledRenderLayerDefaultEnableState => {
                Self::AlwaysEnabled
            }
        }
    }
}

impl From<RenderLayerDefaultState> for BNRenderLayerDefaultEnableState {
    fn from(value: RenderLayerDefaultState) -> Self {
        match value {
            RenderLayerDefaultState::Disabled => {
                Self::DisabledByDefaultRenderLayerDefaultEnableState
            }
            RenderLayerDefaultState::Enabled => Self::EnabledByDefaultRenderLayerDefaultEnableState,
            RenderLayerDefaultState::AlwaysEnabled => {
                Self::AlwaysEnabledRenderLayerDefaultEnableState
            }
        }
    }
}

impl Default for RenderLayerDefaultState {
    fn default() -> Self {
        Self::Disabled
    }
}

/// Register a [`RenderLayer`] with the API.
pub fn register_render_layer<S: BnStrCompatible, T: RenderLayer>(
    name: S,
    render_layer: T,
    default_state: RenderLayerDefaultState,
) -> (&'static mut T, CoreRenderLayer) {
    let render_layer = Box::leak(Box::new(render_layer));
    let mut callback = BNRenderLayerCallbacks {
        context: render_layer as *mut _ as *mut c_void,
        applyToFlowGraph: Some(cb_apply_to_flow_graph::<T>),
        applyToLinearViewObject: Some(cb_apply_to_linear_view_object::<T>),
        freeLines: Some(cb_free_lines),
    };
    let result = unsafe {
        BNRegisterRenderLayer(
            name.into_bytes_with_nul().as_ref().as_ptr() as *const _,
            &mut callback,
            default_state.into(),
        )
    };
    let core = CoreRenderLayer::from_raw(NonNull::new(result).unwrap());
    (render_layer, core)
}

pub trait RenderLayer: Sized {
    /// Apply this Render Layer to a Flow Graph.
    fn apply_to_flow_graph(&self, graph: &mut FlowGraph) {
        for node in &graph.nodes() {
            if let Some(block) = node.basic_block(NativeBlock::new()) {
                let new_lines = self.apply_to_block(&block, node.lines().to_vec());
                node.set_lines(new_lines);
            }
        }
    }

    /// Apply this Render Layer to the lines produced by a LinearViewObject for rendering in Linear View.
    fn apply_to_linear_object(
        &self,
        object: &mut LinearViewObject,
        _prev_object: Option<&mut LinearViewObject>,
        _next_object: Option<&mut LinearViewObject>,
        lines: Vec<LinearDisassemblyLine>,
    ) -> Vec<LinearDisassemblyLine> {
        let text_to_lines =
            |function: &Function, block: &BasicBlock<NativeBlock>, text: DisassemblyTextLine| {
                LinearDisassemblyLine {
                    ty: LinearDisassemblyLineType::CodeDisassemblyLineType,
                    function: Some(function.to_owned()),
                    basic_block: Some(block.to_owned()),
                    contents: text,
                }
            };

        // Hack: HLIL bodies don't have basic blocks.
        let obj_ident = object.identifier();
        if !lines.is_empty()
            && (obj_ident.name.starts_with("HLIL") || obj_ident.name.starts_with("Language"))
        {
            // Apply to HLIL body.
            let function = lines[0]
                .function
                .to_owned()
                .expect("HLIL body has no function");
            return self.apply_to_hlil_body(&function, lines);
        }

        // Collect the "line blocks".
        // Line blocks are contiguous lines with the same backing basic block (or lack thereof).
        // Line blocks also group by line type.
        let mut line_blocks: Vec<Vec<LinearDisassemblyLine>> = Vec::new();
        for line in lines {
            let Some(last_block) = line_blocks.last_mut() else {
                // No last block, create the first block.
                line_blocks.push(vec![line]);
                continue;
            };

            let Some(last_line) = last_block.last() else {
                // No last line, create the first line.
                last_block.push(line);
                continue;
            };

            // TODO: If we want to allow a block with multiple line types we need to specifically check
            // TODO: If the last line type was Code, if it is and the last line is not we make a new block.
            if last_line.basic_block == line.basic_block && last_line.ty == line.ty {
                // Same basic block and line type, this is a part of the same line block.
                last_block.push(line);
            } else {
                // Not the same line block, create a new block.
                line_blocks.push(vec![line]);
            }
        }

        line_blocks
            .into_iter()
            .filter_map(|line_block| {
                let probe_line = line_block.first()?;
                Some((probe_line.ty, probe_line.basic_block.to_owned(), line_block))
            })
            .flat_map(|(line_ty, basic_block, lines)| {
                match (basic_block, line_ty) {
                    (Some(block), LinearDisassemblyLineType::CodeDisassemblyLineType) => {
                        // Dealing with code lines.
                        let function = block.function();
                        let text_lines = lines.into_iter().map(|line| line.contents).collect();
                        let new_text_lines = self.apply_to_block(&block, text_lines);
                        new_text_lines
                            .into_iter()
                            .map(|line| text_to_lines(&function, &block, line))
                            .collect()
                    }
                    _ => {
                        // Dealing with misc lines.
                        self.apply_to_misc_lines(
                            object,
                            _prev_object.as_deref(),
                            _next_object.as_deref(),
                            lines,
                        )
                    }
                }
            })
            .collect()
    }

    /// Apply this Render Layer to a single Basic Block of Disassembly lines.
    ///
    /// Modify the lines to change the presentation of the block.
    fn apply_to_disassembly_block(
        &self,
        _block: &BasicBlock<NativeBlock>,
        lines: Vec<DisassemblyTextLine>,
    ) -> Vec<DisassemblyTextLine> {
        lines
    }

    /// Apply this Render Layer to a single Basic Block of Low Level IL lines.
    ///
    /// Modify the lines to change the presentation of the block.
    fn apply_to_llil_block(
        &self,
        _block: &BasicBlock<NativeBlock>,
        lines: Vec<DisassemblyTextLine>,
    ) -> Vec<DisassemblyTextLine> {
        lines
    }

    /// Apply this Render Layer to a single Basic Block of Medium Level IL lines.
    ///
    /// Modify the lines to change the presentation of the block.
    fn apply_to_mlil_block(
        &self,
        _block: &BasicBlock<NativeBlock>,
        lines: Vec<DisassemblyTextLine>,
    ) -> Vec<DisassemblyTextLine> {
        lines
    }

    /// Apply this Render Layer to a single Basic Block of High Level IL lines.
    ///
    /// Modify the lines to change the presentation of the block.
    ///
    /// This function will NOT apply to High Level IL bodies as displayed in Linear View!
    /// Those are handled by [`RenderLayer::apply_to_hlil_body`] instead as they do not
    /// have a [`BasicBlock`] associated with them.
    fn apply_to_hlil_block(
        &self,
        _block: &BasicBlock<NativeBlock>,
        lines: Vec<DisassemblyTextLine>,
    ) -> Vec<DisassemblyTextLine> {
        lines
    }

    /// Apply this Render Layer to the entire body of a High Level IL function.
    ///
    /// Modify the lines to change the presentation of the block.
    ///
    /// This function only applies to Linear View, and not to Graph View! If you want to
    /// handle Graph View too, you will need to use [`RenderLayer::apply_to_hlil_block`] and handle
    /// the lines one block at a time.
    fn apply_to_hlil_body(
        &self,
        _function: &Function,
        lines: Vec<LinearDisassemblyLine>,
    ) -> Vec<LinearDisassemblyLine> {
        lines
    }

    // TODO: We might want to just go ahead and pass the line type.
    /// Apply to lines generated by Linear View that are not part of a function.
    ///
    /// Modify the lines to change the presentation of the block.
    fn apply_to_misc_lines(
        &self,
        _object: &mut LinearViewObject,
        _prev_object: Option<&LinearViewObject>,
        _next_object: Option<&LinearViewObject>,
        lines: Vec<LinearDisassemblyLine>,
    ) -> Vec<LinearDisassemblyLine> {
        lines
    }

    /// Apply this Render Layer to all IL blocks and disassembly blocks.
    ///
    /// If not implemented this will handle calling the view specific apply functions:
    ///
    /// - [`RenderLayer::apply_to_disassembly_block`]
    /// - [`RenderLayer::apply_to_llil_block`]
    /// - [`RenderLayer::apply_to_mlil_block`]
    /// - [`RenderLayer::apply_to_hlil_block`]
    ///
    /// Modify the lines to change the presentation of the block.
    fn apply_to_block(
        &self,
        block: &BasicBlock<NativeBlock>,
        lines: Vec<DisassemblyTextLine>,
    ) -> Vec<DisassemblyTextLine> {
        match block.block_type() {
            BasicBlockType::Native => self.apply_to_disassembly_block(block, lines),
            BasicBlockType::LowLevelIL => self.apply_to_llil_block(block, lines),
            BasicBlockType::MediumLevelIL => self.apply_to_mlil_block(block, lines),
            BasicBlockType::HighLevelIL => self.apply_to_hlil_block(block, lines),
        }
    }
}

#[repr(transparent)]
pub struct CoreRenderLayer {
    pub(crate) handle: NonNull<BNRenderLayer>,
}

impl CoreRenderLayer {
    pub fn from_raw(handle: NonNull<BNRenderLayer>) -> Self {
        Self { handle }
    }

    pub fn render_layers() -> Array<CoreRenderLayer> {
        let mut count = 0;
        let result = unsafe { BNGetRenderLayerList(&mut count) };
        unsafe { Array::new(result, count, ()) }
    }

    pub fn render_layer_by_name<S: BnStrCompatible>(name: S) -> Option<CoreRenderLayer> {
        let name_raw = name.into_bytes_with_nul();
        let result = unsafe { BNGetRenderLayerByName(name_raw.as_ref().as_ptr() as *const c_char) };
        NonNull::new(result).map(Self::from_raw)
    }

    pub fn default_state(&self) -> RenderLayerDefaultState {
        let raw = unsafe { BNGetRenderLayerDefaultEnableState(self.handle.as_ptr()) };
        RenderLayerDefaultState::from(raw)
    }

    pub fn apply_to_flow_graph(&self, graph: &FlowGraph) {
        unsafe { BNApplyRenderLayerToFlowGraph(self.handle.as_ptr(), graph.handle) }
    }

    pub fn apply_to_linear_view_object(
        &self,
        object: &LinearViewObject,
        prev_object: Option<&LinearViewObject>,
        next_object: Option<&LinearViewObject>,
        lines: Vec<LinearDisassemblyLine>,
    ) -> Vec<LinearDisassemblyLine> {
        let mut lines_raw: Vec<_> = lines
            .into_iter()
            // NOTE: Freed after the core call
            .map(LinearDisassemblyLine::into_raw)
            .collect();

        let prev_object_ptr = prev_object
            .map(|o| o.handle)
            .unwrap_or(std::ptr::null_mut());
        let next_object_ptr = next_object
            .map(|o| o.handle)
            .unwrap_or(std::ptr::null_mut());

        let mut new_lines = std::ptr::null_mut();
        let mut new_line_count = 0;

        unsafe {
            BNApplyRenderLayerToLinearViewObject(
                self.handle.as_ptr(),
                object.handle,
                prev_object_ptr,
                next_object_ptr,
                lines_raw.as_mut_ptr(),
                lines_raw.len(),
                &mut new_lines,
                &mut new_line_count,
            )
        };

        for line in lines_raw {
            LinearDisassemblyLine::free_raw(line);
        }

        let raw: Array<LinearDisassemblyLine> =
            unsafe { Array::new(new_lines, new_line_count, ()) };
        raw.to_vec()
    }
}

impl CoreArrayProvider for CoreRenderLayer {
    type Raw = *mut BNRenderLayer;
    type Context = ();
    type Wrapped<'a> = Self;
}

unsafe impl CoreArrayProviderInner for CoreRenderLayer {
    unsafe fn free(raw: *mut Self::Raw, _count: usize, _context: &Self::Context) {
        BNFreeRenderLayerList(raw)
    }

    unsafe fn wrap_raw<'a>(raw: &'a Self::Raw, _context: &'a Self::Context) -> Self::Wrapped<'a> {
        // TODO: Because handle is a NonNull we should prob make Self::Raw that as well...
        let handle = NonNull::new(*raw).unwrap();
        CoreRenderLayer::from_raw(handle)
    }
}

unsafe extern "C" fn cb_apply_to_flow_graph<T: RenderLayer>(
    ctxt: *mut c_void,
    graph: *mut BNFlowGraph,
) {
    let ctxt: &mut T = &mut *(ctxt as *mut T);
    // SAFETY: We do not own the flowgraph, do not take it as Ref.
    let mut flow_graph = FlowGraph::from_raw(graph);
    ctxt.apply_to_flow_graph(&mut flow_graph);
}

unsafe extern "C" fn cb_apply_to_linear_view_object<T: RenderLayer>(
    ctxt: *mut c_void,
    object: *mut BNLinearViewObject,
    prev: *mut BNLinearViewObject,
    next: *mut BNLinearViewObject,
    in_lines: *mut BNLinearDisassemblyLine,
    in_line_count: usize,
    out_lines: *mut *mut BNLinearDisassemblyLine,
    out_line_count: *mut usize,
) {
    let ctxt: &mut T = &mut *(ctxt as *mut T);
    // SAFETY: We do not own the flowgraph, do not take it as Ref.
    let mut object = LinearViewObject::from_raw(object);
    let mut prev_object = if !prev.is_null() {
        Some(LinearViewObject::from_raw(prev))
    } else {
        None
    };
    let mut next_object = if !next.is_null() {
        Some(LinearViewObject::from_raw(next))
    } else {
        None
    };

    let raw_lines = std::slice::from_raw_parts(in_lines, in_line_count);
    // NOTE: The caller is owned of the inLines.
    let lines: Vec<_> = raw_lines
        .iter()
        .map(|line| LinearDisassemblyLine::from_raw(line))
        .collect();

    let new_lines = ctxt.apply_to_linear_object(
        &mut object,
        prev_object.as_mut(),
        next_object.as_mut(),
        lines,
    );

    unsafe {
        *out_line_count = new_lines.len();
        let boxed_new_lines: Box<[_]> = new_lines
            .into_iter()
            // NOTE: Freed by cb_free_lines
            .map(LinearDisassemblyLine::into_raw)
            .collect();
        // NOTE: Dropped by cb_free_lines
        *out_lines = Box::leak(boxed_new_lines).as_mut_ptr();
    }
}

unsafe extern "C" fn cb_free_lines(
    _ctxt: *mut c_void,
    lines: *mut BNLinearDisassemblyLine,
    line_count: usize,
) {
    let lines_ptr = std::ptr::slice_from_raw_parts_mut(lines, line_count);
    let boxed_lines = Box::from_raw(lines_ptr);
    for line in boxed_lines {
        LinearDisassemblyLine::free_raw(line);
    }
}