Source file beam.ml

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
(*
 * Copyright yutopp 2017 - .
 *
 * Distributed under the Boost Software License, Version 1.0.
 * (See accompanying file LICENSE_1_0.txt or copy at
 * http://www.boost.org/LICENSE_1_0.txt)
 *)

(*
 * ref: http://rnyingma.synrc.com/publications/cat/Functional%20Languages/Erlang/BEAM.pdf
 * ref: http://www.erlang.se/~bjorn/beam_file_format.html
 *
 * Supported
 *
 * Atom
 * AtU8
 * Code
 * StrT
 * ImpT
 * ExpT
 *
 * FunT
 * LitT
 * Line
 *
 * Attr
 * CInf
 * Trac x
 * Abst
 * Dbgi
 * LocT
 *
 *)

type atom_chunk_layout_t = {
  atom_count : int32;
  atom_buf   : Bitstring.t;
}

let parse_atom_chunk_layout (_, buffer) =
  let open Aux in
  match%bitstring buffer with
  | {| "Atom" : 4*8 : string
     ; size   : 4*8 : bigendian
     ; count  : 4*8 : bigendian
     ; buf    : content_size_bits size 1 : bitstring
     ; rest   : -1 : bitstring
     |} ->
     let v = {
       atom_count = count;
       atom_buf = buf;
     } in
     Ok (v, rest |> skip_padding_for_size size 1)
  | {| _ |} ->
     Error ()


type atu8_chunk_layout_t = {
  atu8_count : int32;
  atu8_buf   : Bitstring.t;
}

let parse_atu8_chunk_layout (_, buffer) =
  let open Aux in
  match%bitstring buffer with
  | {| "AtU8" : 4*8 : string
     ; size   : 4*8 : bigendian
     ; count  : 4*8 : bigendian
     ; buf    : content_size_bits size 1 : bitstring
     ; rest   : -1 : bitstring
     |} ->
     let v = {
       atu8_count = count;
       atu8_buf = buf;
     } in
     Ok (v, rest |> skip_padding_for_size size 1)
  | {| _ |} ->
     Error ()


type code_chunk_layout_t = {
  code_info_size      : int32;
  code_version        : int32;
  code_opcode_max     : int32;
  code_labels         : int32;
  code_function_count : int32;
  code_buf            : Bitstring.t;
}

let parse_code_chunk_layout (_, buffer) =
  let open Aux in
  match%bitstring buffer with
  | {| "Code"         : 4*8 : string
     ; size           : 4*8 : bigendian
     ; info_size      : 4*8 : bigendian
     ; version        : 4*8 : bigendian
     ; opcode_max     : 4*8 : bigendian
     ; labels         : 4*8 : bigendian
     ; function_count : 4*8 : bigendian
     ; buf            : content_size_bits size 5 : bitstring
     ; rest           : -1 : bitstring
     |} ->
     let v = {
       code_info_size = info_size;
       code_version = version;
       code_opcode_max = opcode_max;
       code_labels = labels;
       code_function_count = function_count;
       code_buf = buf;
     } in
     Ok (v, rest |> skip_padding_for_size size 5)
  | {| _ |} ->
     Error "Failed to parse code_chunk"


type strt_chunk_layout_t = {
  strt_buf : Bitstring.t;
}

let parse_strt_chunk_layout (_, buffer) =
  let open Aux in
  match%bitstring buffer with
  | {| "StrT" : 4*8 : string
     ; size   : 4*8 : bigendian
     ; buf    : content_size_bits size 0 : bitstring
     ; rest   : -1 : bitstring
     |} ->
     let v = {
       strt_buf = buf;
     } in
     Ok (v, rest |> skip_padding_for_size size 0)
  | {| _ |} ->
     Error "Failed to parse str_chunk"


type impt_chunk_layout_t = {
  impt_buf : Bitstring.t;
}

let parse_impt_chunk_layout (_, buffer) =
  let open Aux in
  match%bitstring buffer with
  | {| "ImpT" : 4*8 : string
     ; size   : 4*8 : bigendian
     ; count  : 4*8 : bigendian
     ; buf    : content_size_bits size 1 : bitstring
     ; rest   : -1 : bitstring
     |} ->
     let v = {
       impt_buf = buf;
     } in
     Ok (v, rest |> skip_padding_for_size size 1)
  | {| _ |} ->
     Error "Failed to parse import_table_chunk"


type expt_chunk_layout_t = {
  expt_buf : Bitstring.t;
}

let parse_expt_chunk_layout (_, buffer) =
  let open Aux in
  match%bitstring buffer with
  | {| "ExpT" : 4*8 : string
     ; size   : 4*8 : bigendian
     ; count  : 4*8 : bigendian
     ; buf    : content_size_bits size 1 : bitstring
     ; rest   : -1 : bitstring
     |} ->
     let v = {
       expt_buf = buf;
     } in
     Ok (v, rest |> skip_padding_for_size size 0)
  | {| _ |} ->
     Error "Failed to parse export_table_chunk"


type funt_chunk_layout_t = {
  funt_buf : Bitstring.t;
}

let parse_funt_chunk_layout (_, buffer) =
  let open Aux in
  match%bitstring buffer with
  | {| "FunT" : 4*8 : string
     ; size   : 4*8 : bigendian
     ; count  : 4*8 : bigendian
     ; buf    : content_size_bits size 1 : bitstring
     ; rest   : -1 : bitstring
     |} ->
     let v = {
       funt_buf = buf;
     } in
     Ok (v, rest)
  | {| _ |} ->
     Error "Failed to parse function_table_chunk"


type litt_chunk_layout_t = {
  litt_buf : Bitstring.t;
}

let parse_litt_chunk_layout (_, buffer) =
  let open Aux in
  match%bitstring buffer with
  | {| "LitT" : 4*8 : string
     ; size   : 4*8 : bigendian
     ; buf    : content_size_bits size 0 : bitstring
     ; rest   : -1 : bitstring
     |} ->
     let v = {
       litt_buf = buf;
     } in
     Ok (v, rest |> skip_padding_for_size size 0)
  | {| _ |} ->
     Error "Failed to parse literal_table_chunk"


type line_chunk_layout_t = {
  line_buf : Bitstring.t;
}

let parse_line_chunk_layout (_, buffer) =
  let open Aux in
  match%bitstring buffer with
  | {| "Line" : 4*8 : string
     ; size   : 4*8 : bigendian
     ; buf    : content_size_bits size 0 : bitstring
     ; rest   : -1 : bitstring
     |} ->
     let v = {
       line_buf = buf;
     } in
     Ok (v, rest |> skip_padding_for_size size 0)
  | {| _ |} ->
     Error "Failed to parse line_table_chunk"


type attr_chunk_layout_t = {
  attr_buf : Bitstring.t;
}

let parse_attr_chunk_layout (_, buffer) =
  let open Aux in
  match%bitstring buffer with
  | {| "Attr" : 4*8 : string
     ; size   : 4*8 : bigendian
     ; buf    : content_size_bits size 0 : bitstring
     ; rest   : -1 : bitstring
     |} ->
     let v = {
       attr_buf = buf;
     } in
     Ok (v, rest |> skip_padding_for_size size 0)
  | {| _ |} ->
     Error "Failed to parse attr_table_chunk"


type cinf_chunk_layout_t = {
  cinf_buf : Bitstring.t;
}

let parse_cinf_chunk_layout (_, buffer) =
  let open Aux in
  match%bitstring buffer with
  | {| "CInf" : 4*8 : string
     ; size   : 4*8 : bigendian
     ; buf    : content_size_bits size 0 : bitstring
     ; rest   : -1 : bitstring
     |} ->
     let v = {
       cinf_buf = buf;
     } in
     Ok (v, rest |> skip_padding_for_size size 0)
  | {| _ |} ->
     Error "Failed to parse cinf_table_chunk"


type abst_chunk_layout_t = {
  abst_buf : Bitstring.t;
}

let parse_abst_chunk_layout (_, buffer) =
  let open Aux in
  match%bitstring buffer with
  | {| "Abst" : 4*8 : string
     ; size   : 4*8 : bigendian
     ; buf    : content_size_bits size 0 : bitstring
     ; rest   : -1 : bitstring
     |} ->
     let v = {
       abst_buf = buf;
     } in
     Ok (v, rest |> skip_padding_for_size size 0)
  | {| _ |} ->
     Error "Failed to parse abst_table_chunk"


type dbgi_chunk_layout_t = {
  dbgi_buf : Bitstring.t;
}

let parse_dbgi_chunk_layout (_, buffer) =
  let open Aux in
  match%bitstring buffer with
  | {| "Dbgi" : 4*8 : string
     ; size   : 4*8 : bigendian
     ; buf    : content_size_bits size 0 : bitstring
     ; rest   : -1 : bitstring
     |} ->
     let v = {
       dbgi_buf = buf;
     } in
     Ok (v, rest |> skip_padding_for_size size 0)
  | {| _ |} ->
     Error "Failed to parse dbgi_table_chunk"


type loct_chunk_layout_t = {
  loct_buf : Bitstring.t;
}

let parse_loct_chunk_layout (_, buffer) =
  let open Aux in
  match%bitstring buffer with
  | {| "LocT" : 4*8 : string
     ; size   : 4*8 : bigendian
     ; count  : 4*8 : bigendian
     ; buf    : content_size_bits size 1 : bitstring
     ; rest   : -1 : bitstring
     |} ->
     let v = {
       loct_buf = buf;
     } in
     Ok (v, rest |> skip_padding_for_size size 1)
  | {| _ |} ->
     Error "Failed to parse local_function_table_chunk"

type chunks_layout_table_t = {
  cl_atom : atom_chunk_layout_t option;
  cl_atu8 : atu8_chunk_layout_t option;
  cl_code : code_chunk_layout_t option;
  cl_strt : strt_chunk_layout_t option;
  cl_impt : impt_chunk_layout_t option;
  cl_expt : expt_chunk_layout_t option;

  cl_funt : funt_chunk_layout_t option;
  cl_litt : litt_chunk_layout_t option;
  cl_line : line_chunk_layout_t option;

  cl_attr : attr_chunk_layout_t option;
  cl_cinf : cinf_chunk_layout_t option;
  (* trac : trac_chunk_layout_t option; *)
  cl_abst : abst_chunk_layout_t option;
  cl_dbgi : dbgi_chunk_layout_t option;
  cl_loct : loct_chunk_layout_t option;
}

let empty_chunks_layout_table =
  {
    cl_atom = None;
    cl_atu8 = None;
    cl_code = None;
    cl_strt = None;
    cl_impt = None;
    cl_expt = None;

    cl_funt = None;
    cl_litt = None;
    cl_line = None;

    cl_attr = None;
    cl_cinf = None;
    (* trac = None; *)
    cl_abst = None;
    cl_dbgi = None;
    cl_loct = None;
  }

let parse_layout buffer =
  match%bitstring buffer with
  | {| "FOR1" : 4*8 : string
     ; length : 4*8 : bigendian
     ; "BEAM" : 4*8 : string
     ; buf    : ((Int32.to_int length)-4)*8 : bitstring
     |} ->
     let open Parser.Combinator in
     let chunks_layout_parser =
         act parse_atom_chunk_layout (fun n p -> {p with cl_atom = Some n})
       / act parse_atu8_chunk_layout (fun n p -> {p with cl_atu8 = Some n})
       / act parse_code_chunk_layout (fun n p -> {p with cl_code = Some n})
       / act parse_strt_chunk_layout (fun n p -> {p with cl_strt = Some n})
       / act parse_impt_chunk_layout (fun n p -> {p with cl_impt = Some n})
       / act parse_expt_chunk_layout (fun n p -> {p with cl_expt = Some n})

       / act parse_funt_chunk_layout (fun n p -> {p with cl_funt = Some n})
       / act parse_litt_chunk_layout (fun n p -> {p with cl_litt = Some n})
       / act parse_line_chunk_layout (fun n p -> {p with cl_line = Some n})

       / act parse_attr_chunk_layout (fun n p -> {p with cl_attr = Some n})
       / act parse_cinf_chunk_layout (fun n p -> {p with cl_cinf = Some n})
       (* / trac *)
       / act parse_abst_chunk_layout (fun n p -> {p with cl_abst = Some n})
       / act parse_dbgi_chunk_layout (fun n p -> {p with cl_dbgi = Some n})
       / act parse_loct_chunk_layout (fun n p -> {p with cl_loct = Some n})
     in
     let parser = repeat chunks_layout_parser >> Aux.eol in
     parser (empty_chunks_layout_table, buf)
  | {| _ |} ->
     Error ("failed to read header", buffer)