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
// Copyright 2021 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

use bind::compiler::Symbol;
use bind::debugger::debug_dump::dump_bind_rules;
use bind::interpreter::match_bind::{match_bytecode, PropertyKey};
use std::collections::HashMap;
use std::ffi::{CStr, CString};

const BIND_PROTOCOL: u64 = 0x0001;
const BIND_AUTOBIND: u64 = 0x0002;

#[repr(u32)]
enum ValueType {
    NumberVal = 0,
    StringVal = 1,
    BoolVal = 2,
    EnumVal = 3,
}

#[repr(C)]
pub struct device_property_t {
    key: u32,
    value: u32,
}

#[repr(C)]
pub union value_t {
    num_value: u32,
    str_value: *const libc::c_char,
    bool_value: bool,
    enum_value: *const libc::c_char,
}

#[repr(C)]
struct property_value_t {
    val_type: ValueType,
    value: value_t,
}

#[repr(C)]
pub struct device_str_property_t {
    key: *const libc::c_char,
    value: property_value_t,
}

fn convert_str(c_str: *const libc::c_char) -> Option<String> {
    let str = unsafe { CStr::from_ptr(c_str) };
    match str.to_str() {
        Ok(value) => Some(value.to_string()),
        Err(_) => None,
    }
}

fn convert_to_symbol(prop_value: &property_value_t) -> Option<Symbol> {
    unsafe {
        match prop_value {
            property_value_t {
                val_type: ValueType::NumberVal,
                value: value_t { num_value: val },
            } => Some(Symbol::NumberValue(*val as u64)),
            property_value_t {
                val_type: ValueType::BoolVal,
                value: value_t { bool_value: val },
            } => Some(Symbol::BoolValue(*val)),
            property_value_t {
                val_type: ValueType::StringVal,
                value: value_t { str_value: val },
            } => convert_str(*val).map(|str_val| Symbol::StringValue(str_val)),
            property_value_t {
                val_type: ValueType::EnumVal,
                value: value_t { enum_value: val },
            } => convert_str(*val).map(|enum_val| Symbol::EnumValue(enum_val)),
        }
    }
}

#[no_mangle]
pub extern "C" fn str_property_with_string(
    key: *const libc::c_char,
    value: *const libc::c_char,
) -> device_str_property_t {
    device_str_property_t {
        key: key,
        value: property_value_t {
            val_type: ValueType::StringVal,
            value: value_t { str_value: value },
        },
    }
}

#[no_mangle]
pub extern "C" fn str_property_with_int(
    key: *const libc::c_char,
    value: u32,
) -> device_str_property_t {
    device_str_property_t {
        key: key,
        value: property_value_t {
            val_type: ValueType::NumberVal,
            value: value_t { num_value: value },
        },
    }
}

#[no_mangle]
pub extern "C" fn str_property_with_bool(
    key: *const libc::c_char,
    value: bool,
) -> device_str_property_t {
    device_str_property_t {
        key: key,
        value: property_value_t {
            val_type: ValueType::BoolVal,
            value: value_t { bool_value: value },
        },
    }
}

#[no_mangle]
pub extern "C" fn str_property_with_enum(
    key: *const libc::c_char,
    value: *const libc::c_char,
) -> device_str_property_t {
    device_str_property_t {
        key: key,
        value: property_value_t {
            val_type: ValueType::EnumVal,
            value: value_t { enum_value: value },
        },
    }
}

// |bytecode_sz| must match the size of |bytecode_c|. |properties_sz| must
// match the size o |properties_c|.
#[no_mangle]
#[allow(clippy::not_unsafe_ptr_arg_deref)]
pub extern "C" fn match_bind_rules(
    bytecode_c: *const u8,
    bytecode_sz: libc::size_t,
    properties_c: *const device_property_t,
    properties_sz: libc::size_t,
    str_properties_c: *const device_str_property_t,
    str_properties_sz: libc::size_t,
    protocol_id: u32,
    autobind: bool,
) -> bool {
    if bytecode_c.is_null() {
        return false;
    }

    let mut device_properties = HashMap::new();
    if !properties_c.is_null() {
        let properties = unsafe { std::slice::from_raw_parts(properties_c, properties_sz) };
        for property in properties.iter() {
            device_properties.insert(
                PropertyKey::NumberKey(property.key as u64),
                Symbol::NumberValue(property.value as u64),
            );
        }
    }

    // Insert the protocol ID property if it's missing.
    device_properties
        .entry(PropertyKey::NumberKey(BIND_PROTOCOL))
        .or_insert(Symbol::NumberValue(protocol_id as u64));
    device_properties
        .entry(PropertyKey::NumberKey(BIND_AUTOBIND))
        .or_insert(Symbol::NumberValue(if autobind { 1 } else { 0 }));

    if !str_properties_c.is_null() {
        let str_properties =
            unsafe { std::slice::from_raw_parts(str_properties_c, str_properties_sz) };
        for str_property in str_properties.iter() {
            let key = convert_str(str_property.key);
            let symbol = convert_to_symbol(&str_property.value);

            assert!(key.is_some() && symbol.is_some());
            device_properties.insert(PropertyKey::StringKey(key.unwrap()), symbol.unwrap());
        }
    }

    let bytecode = unsafe { std::slice::from_raw_parts(bytecode_c, bytecode_sz).to_vec() };

    // TODO(fxb/73943): Return the error instead of returning false.
    match_bytecode(bytecode, &device_properties).unwrap_or_else(|e| {
        println!("Error evaluating the bytecode: {}", e);
        false
    })
}

#[allow(clippy::missing_safety_doc)] // TODO(fxbug.dev/99060)
#[no_mangle]
pub unsafe extern "C" fn dump_bytecode(
    bytecode_c: *const u8,
    bytecode_sz: libc::size_t,
) -> *const libc::c_char {
    let bytecode = std::slice::from_raw_parts(bytecode_c, bytecode_sz).to_vec();
    let dump_str =
        CString::new(dump_bind_rules(bytecode).unwrap_or_else(|e| e.to_string())).unwrap();
    dump_str.into_raw()
}