hlbc 0.7.0

Hashlink bytecode disassembler and analyzer
Documentation
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
use std::ffi::CString;
use std::io::Write;

use byteorder::{LittleEndian, WriteBytesExt};

use crate::types::{RefField, RefFloat, RefFun, RefGlobal, RefInt, RefString, RefType, TypeFun};
use crate::{Bytecode, ConstantDef, Function, Native, ObjField, Str, Type, TypeObj};
use crate::{Error, Result};

impl Bytecode {
    /// Serialize the bytecode to any sink.
    /// Bytecode is serialized to the same format.
    pub fn serialize(&self, w: &mut impl Write) -> Result<()> {
        w.write_all(&[b'H', b'L', b'B'])?;
        w.write_u8(self.version)?;
        write_var(w, if self.debug_files.is_some() { 1 } else { 0 })?;
        write_var(w, self.ints.len() as i32)?;
        write_var(w, self.floats.len() as i32)?;
        write_var(w, self.strings.len() as i32)?;
        if let Some((_, pos)) = &self.bytes {
            write_var(w, pos.len() as i32)?;
        }
        write_var(w, self.types.len() as i32)?;
        write_var(w, self.globals.len() as i32)?;
        write_var(w, self.natives.len() as i32)?;
        write_var(w, self.functions.len() as i32)?;
        if let Some(constants) = &self.constants {
            write_var(w, constants.len() as i32)?;
        }
        self.entrypoint.write(w)?;
        for &i in &self.ints {
            w.write_i32::<LittleEndian>(i)?;
        }
        for &f in &self.floats {
            w.write_f64::<LittleEndian>(f)?;
        }
        write_strings(w, &self.strings)?;
        if let Some((bytes, pos)) = &self.bytes {
            w.write_i32::<LittleEndian>(bytes.len() as i32)?;
            w.write_all(bytes)?;
            for &p in pos {
                write_var(w, p as i32)?;
            }
        }
        if let Some(debug_files) = &self.debug_files {
            write_var(w, debug_files.len() as i32)?;
            write_strings(w, debug_files)?;
        }
        for t in &self.types {
            t.write(w)?;
        }
        for g in &self.globals {
            g.write(w)?;
        }
        for n in &self.natives {
            n.write(w)?;
        }
        for f in &self.functions {
            f.write(w)?;
        }
        if let Some(constants) = &self.constants {
            for c in constants {
                c.write(w)?;
            }
        }
        Ok(())
    }
}

impl RefInt {
    pub(crate) fn write(&self, w: &mut impl Write) -> Result<()> {
        write_var(w, self.0 as i32)
    }
}

impl RefFloat {
    pub(crate) fn write(&self, w: &mut impl Write) -> Result<()> {
        write_var(w, self.0 as i32)
    }
}

impl RefString {
    pub(crate) fn write(&self, w: &mut impl Write) -> Result<()> {
        write_var(w, self.0 as i32)
    }
}

impl RefGlobal {
    pub(crate) fn write(&self, w: &mut impl Write) -> Result<()> {
        write_var(w, self.0 as i32)
    }
}

impl RefFun {
    pub(crate) fn write(&self, w: &mut impl Write) -> Result<()> {
        write_var(w, self.0 as i32)
    }
}

impl RefType {
    pub(crate) fn write(&self, w: &mut impl Write) -> Result<()> {
        write_var(w, self.0 as i32)
    }
}

impl RefField {
    pub(crate) fn write(&self, w: &mut impl Write) -> Result<()> {
        write_var(w, self.0 as i32)
    }
}

impl ObjField {
    pub(crate) fn write(&self, w: &mut impl Write) -> Result<()> {
        self.name.write(w)?;
        self.t.write(w)
    }
}

impl TypeFun {
    pub(crate) fn write(&self, w: &mut impl Write) -> Result<()> {
        w.write_u8(self.args.len() as u8)?;
        for arg in &self.args {
            arg.write(w)?;
        }
        self.ret.write(w)
    }
}

impl TypeObj {
    pub(crate) fn write(&self, w: &mut impl Write) -> Result<()> {
        self.name.write(w)?;
        write_var(w, self.super_.map(|s| s.0 as i32).unwrap_or(-1))?;
        self.global.write(w)?;
        write_var(w, self.own_fields.len() as i32)?;
        write_var(w, self.protos.len() as i32)?;
        write_var(w, self.bindings.len() as i32)?;
        for f in &self.own_fields {
            f.write(w)?;
        }
        for p in &self.protos {
            p.name.write(w)?;
            p.findex.write(w)?;
            write_var(w, p.pindex)?;
        }
        for (fi, fun) in &self.bindings {
            fi.write(w)?;
            fun.write(w)?;
        }
        Ok(())
    }
}

impl Type {
    pub(crate) fn write(&self, w: &mut impl Write) -> Result<()> {
        match self {
            Type::Void => w.write_u8(0)?,
            Type::UI8 => w.write_u8(1)?,
            Type::UI16 => w.write_u8(2)?,
            Type::I32 => w.write_u8(3)?,
            Type::I64 => w.write_u8(4)?,
            Type::F32 => w.write_u8(5)?,
            Type::F64 => w.write_u8(6)?,
            Type::Bool => w.write_u8(7)?,
            Type::Bytes => w.write_u8(8)?,
            Type::Dyn => w.write_u8(9)?,
            Type::Fun(fun) => {
                w.write_u8(10)?;
                fun.write(w)?;
            }
            Type::Obj(obj) => {
                w.write_u8(11)?;
                obj.write(w)?;
            }
            Type::Array => w.write_u8(12)?,
            Type::Type => w.write_u8(13)?,
            Type::Ref(inner) => {
                w.write_u8(14)?;
                inner.write(w)?;
            }
            Type::Virtual { fields } => {
                w.write_u8(15)?;
                write_var(w, fields.len() as i32)?;
                for f in fields {
                    f.write(w)?;
                }
            }
            Type::DynObj => w.write_u8(16)?,
            Type::Abstract { name } => {
                w.write_u8(17)?;
                name.write(w)?;
            }
            Type::Enum {
                name,
                global,
                constructs,
            } => {
                w.write_u8(18)?;
                name.write(w)?;
                global.write(w)?;
                write_var(w, constructs.len() as i32)?;
                for c in constructs {
                    c.name.write(w)?;
                    write_var(w, c.params.len() as i32)?;
                    for p in &c.params {
                        p.write(w)?;
                    }
                }
            }
            Type::Null(inner) => {
                w.write_u8(19)?;
                inner.write(w)?;
            }
            Type::Method(fun) => {
                w.write_u8(20)?;
                fun.write(w)?;
            }
            Type::Struct(obj) => {
                w.write_u8(21)?;
                obj.write(w)?;
            }
            Type::Packed(inner) => {
                w.write_u8(22)?;
                inner.write(w)?;
            }
        }
        Ok(())
    }
}

impl Native {
    pub(crate) fn write(&self, w: &mut impl Write) -> Result<()> {
        self.lib.write(w)?;
        self.name.write(w)?;
        self.t.write(w)?;
        self.findex.write(w)
    }
}

impl Function {
    pub(crate) fn write(&self, w: &mut impl Write) -> Result<()> {
        self.t.write(w)?;
        self.findex.write(w)?;
        write_var(w, self.regs.len() as i32)?;
        write_var(w, self.ops.len() as i32)?;
        for r in &self.regs {
            r.write(w)?;
        }
        for o in &self.ops {
            o.write(w)?;
        }
        // https://github.com/HaxeFoundation/haxe/blob/ea57ab1ef60d212228c8657b7bc5b1085c62714e/src/generators/genhl.ml#L3910
        if let Some(debug_info) = &self.debug_info {
            let mut curfile: i32 = -1;
            let mut curpos = 0;
            let mut rcount = 0;
            for &(f, p) in debug_info {
                if f as i32 != curfile {
                    flush_repeat(w, &mut curpos, &mut rcount, p)?;
                    curfile = f as i32;
                    w.write_u8(((f >> 7) | 1) as u8)?;
                    w.write_u8((f & 0xFF) as u8)?;
                }
                if p != curpos {
                    flush_repeat(w, &mut curpos, &mut rcount, p)?;
                }
                if p == curpos {
                    rcount += 1;
                } else {
                    let delta = p as i32 - curpos as i32;
                    if delta > 0 && delta < 32 {
                        w.write_u8(((delta << 3) | 4) as u8)?;
                    } else {
                        w.write_u8((p << 3) as u8)?;
                        w.write_u8((p >> 5) as u8)?;
                        w.write_u8((p >> 13) as u8)?;
                    }
                    curpos = p;
                }
            }
            let old_curpos = curpos;
            flush_repeat(w, &mut curpos, &mut rcount, old_curpos)?;
        }
        if let Some(assigns) = &self.assigns {
            write_var(w, assigns.len() as i32)?;
            for (s, p) in assigns {
                s.write(w)?;
                write_var(w, *p as i32)?;
            }
        }
        Ok(())
    }
}

impl ConstantDef {
    pub(crate) fn write(&self, w: &mut impl Write) -> Result<()> {
        self.global.write(w)?;
        write_var(w, self.fields.len() as i32)?;
        for f in &self.fields {
            write_var(w, *f as i32)?;
        }
        Ok(())
    }
}

// https://github.com/HaxeFoundation/haxe/blob/613b0291c4976a8169aa643cdcc408c7d6b69da9/src/generators/genhl.ml#L3698
/// Write an index with a variable size encoding
pub(crate) fn write_var(w: &mut impl Write, value: i32) -> Result<()> {
    if value < 0 {
        let value = -value;
        if value < 0x2000 {
            w.write_u8(((value >> 8) | 0xA0) as u8)?;
            w.write_u8((value & 0xFF) as u8)?;
        } else if value < 20000000 {
            w.write_u8(((value >> 24) | 0xE0) as u8)?;
            w.write_u8(((value >> 16) & 0xFF) as u8)?;
            w.write_u8(((value >> 8) & 0xFF) as u8)?;
            w.write_u8((value & 0xFF) as u8)?;
        } else {
            return Err(Error::ValueOutOfBounds {
                value,
                limit: 20000000,
            });
        }
    } else if value < 0x80 {
        w.write_u8(value as u8)?;
    } else if value < 0x2000 {
        w.write_u8(((value >> 8) | 0x80) as u8)?;
        w.write_u8((value & 0xFF) as u8)?;
    } else if value < 0x20000000 {
        w.write_u8(((value >> 24) | 0xC0) as u8)?;
        w.write_u8(((value >> 16) & 0xFF) as u8)?;
        w.write_u8(((value >> 8) & 0xFF) as u8)?;
        w.write_u8((value & 0xFF) as u8)?;
    } else {
        return Err(Error::ValueOutOfBounds {
            value,
            limit: 20000000,
        });
    }
    Ok(())
}

pub(crate) fn write_strings(w: &mut impl Write, strings: &[Str]) -> Result<()> {
    let cstr: Vec<CString> = strings
        .iter()
        .map(|s| CString::new(s.as_bytes()).unwrap())
        .collect();
    let size = cstr
        .iter()
        .map(|s| s.as_bytes_with_nul().len())
        .reduce(|acc, len| acc + len)
        .unwrap_or(0);
    w.write_i32::<LittleEndian>(size as i32)?;
    for s in cstr.iter() {
        w.write_all(s.as_bytes_with_nul())?;
    }
    for s in cstr.iter() {
        write_var(w, s.as_bytes().len() as i32)?;
    }
    Ok(())
}

// Adapted from https://github.com/HaxeFoundation/haxe/blob/ea57ab1ef60d212228c8657b7bc5b1085c62714e/src/generators/genhl.ml#L3895
fn flush_repeat(
    w: &mut impl Write,
    curpos: &mut usize,
    rcount: &mut usize,
    pos: usize,
) -> Result<()> {
    if *rcount > 0 {
        if *rcount > 15 {
            w.write_u8((15 << 2) | 2)?;
            *rcount -= 15;
            flush_repeat(w, curpos, rcount, pos)?;
        } else {
            let mut delta = pos as i32 - *curpos as i32;
            delta = if delta > 0 && delta < 4 { delta } else { 0 };
            w.write_u8(((delta << 6) | ((*rcount as i32) << 2) | 2) as u8)?;
            *rcount = 0;
            *curpos += delta as usize;
        }
    }
    Ok(())
}

#[cfg(test)]
mod tests {
    use std::fs;

    use crate::Bytecode;

    #[test]
    fn ser_eq_deser() {
        let data = fs::read("../../data/Anonymous.hl").unwrap();
        // Deserialize
        let code = Bytecode::deserialize(&mut data.as_slice()).unwrap();

        let mut out = Vec::with_capacity(data.len());
        // Then serialize back again
        code.serialize(&mut out).unwrap();

        // Test bytes
        assert_eq!(data, out);
    }

    //#[test]
    fn ser_eq_deser_all() {
        for entry in fs::read_dir("../../data").unwrap() {
            let path = entry.unwrap().path();
            if let Some(ext) = path.extension() {
                if ext == "hl" {
                    let data = fs::read(&path).unwrap();
                    let code = Bytecode::deserialize(&mut data.as_slice()).unwrap();
                    let mut out = Vec::with_capacity(data.len());
                    code.serialize(&mut out).unwrap();
                    let new = Bytecode::deserialize(&mut out.as_slice()).unwrap();
                    fs::write("original.txt", format!("{:#?}", code)).unwrap();
                    fs::write("new.txt", format!("{:#?}", new)).unwrap();
                    assert_eq!(data, out);
                    //assert_eq!(code, new);
                    break;
                }
            }
        }
    }
}