From 68d53c01b0b8e9a007a6a30158c19e34b2d2a34e Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Bj=C3=B6rn=20Gustavsson?= This module provides an interface to files created by
+ the BEAM Compiler ("BEAM files"). The format used, a variant of
"EA IFF 1985" Standard for Interchange Format Files, divides data
into chunks. Chunk data can be returned as binaries or as compound terms.
Compound terms are returned when chunks are referenced by names
- (atoms) rather than identifiers (strings). The names recognized
- and the corresponding identifiers are:
The option
Option
Source code can be reconstructed from the debug information. - Use encrypted debug information (see below) to prevent this.
+ To prevent this, use encrypted debug information (see below).The debug information can also be removed from BEAM files
- using
Here is an example of how to reconstruct source code from
- the debug information in a BEAM file
- {ok,{_,[{abstract_code,{_,AC}}]}} = beam_lib:chunks(Beam,[abstract_code]).
- io:fwrite("~s~n", [erl_prettypr:format(erl_syntax:form_list(AC))]).
- The debug information can be encrypted in order to keep - the source code secret, but still being able to use tools such as - Xref or Debugger.
-To use encrypted debug information, a key must be provided to
- the compiler and
The default type -- and currently the only type -- of crypto
- algorithm is
As far as we know by the time of writing, it is
- infeasible to break
There are two ways to provide the key:
-Use the compiler option
If no such fun is registered,
Store the key in a text file named
In this case, the compiler option
The following example shows how to reconstruct source code from
+ the debug information in a BEAM file
+{ok,{_,[{abstract_code,{_,AC}}]}} = beam_lib:chunks(Beam,[abstract_code]).
+io:fwrite("~s~n", [erl_prettypr:format(erl_syntax:form_list(AC))]).
The
- {debug_info, Mode, Module, Key}
- The
Here is an example of an
+ Encrypted Debug Information
+ The debug information can be encrypted to keep
+ the source code secret, but still be able to use tools such as
+ Debugger or Xref.
+
+ To use encrypted debug information, a key must be provided to
+ the compiler and beam_lib . The key is specified as a string.
+ It is recommended that the string contains at least 32 characters and
+ that both upper and lower case letters as well as digits and
+ special characters are used.
+
+ The default type (and currently the only type) of crypto
+ algorithm is des3_cbc , three rounds of DES. The key string
+ is scrambled using
+ erlang:md5/1
+ to generate the keys used for des3_cbc .
+
+
+ As far as we know by the time of writing, it is
+ infeasible to break des3_cbc encryption without any
+ knowledge of the key. Therefore, as long as the key is kept
+ safe and is unguessable, the encrypted debug information
+ should be safe from intruders.
+
+
+ The key can be provided in the following two ways:
+
+
+ -
+
Use Compiler option {debug_info,Key} , see
+ compile(3)
+ and function
+ crypto_key_fun/1
+ to register a fun that returns the key whenever
+ beam_lib must decrypt the debug information.
+ If no such fun is registered, beam_lib instead
+ searches for an .erlang.crypt file, see the next section.
+
+ -
+
Store the key in a text file named .erlang.crypt .
+ In this case, Compiler option encrypt_debug_info
+ can be used, see
+ compile(3) .
+
+
+
+
File
+{debug_info, Mode, Module, Key}
+
+ The following is an example of an
7}|pc/DM6Cga*68$Mw]L#&_Gejr]G^"}].]]>
- And here is a slightly more complicated example of an
-
The following is a slightly more complicated example of an
+ .erlang.crypt providing one key for module
+ t and another key for all other modules:
+
+ 7}|pc/DM6Cga*68$Mw]L#&_Gejr]G^"}].]]>
-
- Do not use any of the keys in these examples. Use your own
- keys.
-
-
Do not use any of the keys in these examples. Use your own keys.
+Each of the functions described below accept either the - module name, the filename, or a binary containing the beam + module name, the filename, or a binary containing the BEAM module.
The list of attributes is sorted on
It is not checked that the forms conform to the abstract format
- indicated by
Reads chunk data for all chunks.
Builds a BEAM module (as a binary) from a list of chunks.
+Reads chunk data for selected chunks refs. The order of +
Reads chunk data for selected chunks references. The order of the returned list of chunk data is determined by the order of the list of chunks references.
Reads chunk data for selected chunks refs. The order of +
Reads chunk data for selected chunks references. The order of the returned list of chunk data is determined by the order of the list of chunks references.
-By default, if any requested chunk is missing in
By default, if any requested chunk is missing in
+
Builds a BEAM module (as a binary) from a list of chunks.
+Unregisters the crypto key fun and terminates the process
+ holding it, started by
+
Returns either
Returns the module version(s). A version is defined by
- the module attribute
-1> beam_lib:version(a). % -vsn(1). -{ok,{a,[1]}} -2> beam_lib:version(b). % -vsn([1]). -{ok,{b,[1]}} -3> beam_lib:version(c). % -vsn([1]). -vsn(2). -{ok,{c,[1,2]}} -4> beam_lib:version(d). % no -vsn attribute -{ok,{d,[275613208176997377698094100858909383631]}}+
Compares the contents of two BEAM files. If the module names
+ are the same, and all chunks except for chunk
Calculates an MD5 redundancy check for the code of the module - (compilation date and other attributes are not included).
+Compares the BEAM files in
+ two directories. Only files with extension
Registers an unary fun
+ that is called if
If a fun is already registered when attempting to
+ register a fun,
The fun must handle the following arguments:
+
+CryptoKeyFun(init) -> ok | {ok, NewCryptoKeyFun} | {error, Term}
+ Called when the fun is registered, in the process that holds
+ the fun. Here the crypto key fun can do any necessary
+ initializations. If
+CryptoKeyFun({debug_info, Mode, Module, Filename}) -> Key
+ Called when the key is needed for module
+CryptoKeyFun(clear) -> term()
+ Called before the fun is unregistered. Here any cleaning up
+ can be done. The return value is not important, but is passed
+ back to the caller of
Compares the BEAM files in two directories as
+
For a specified error returned by any function in this module,
+ this function returns a descriptive string
+ of the error in English. For file errors, function
+
Returns a list containing some information about a BEAM file
as tuples
The name (string) of the BEAM file, or the binary from which the information was extracted.
@@ -310,7 +417,8 @@The name (atom) of the module.
For each chunk, the identifier (string) and the position and size of the chunk data, in bytes.
@@ -318,135 +426,75 @@Compares the contents of two BEAM files. If the module names
- are the same, and all chunks except for the
The
The
Calculates an MD5 redundancy check for the code of the module + (compilation date and other attributes are not included).
The
Removes all chunks from a BEAM
file except those needed by the loader. In particular,
- the debug information (
The
Removes all chunks except
those needed by the loader from BEAM files. In particular,
- the debug information (
The
Removes all chunks
except those needed by the loader from the BEAM files of a
- release.
Given the error returned by any function in this module,
- the function
The
If there already is a fun registered when attempting to
- register a fun,
The fun must handle the following arguments:
-
- CryptoKeyFun(init) -> ok | {ok, NewCryptoKeyFun} | {error, Term}
- Called when the fun is registered, in the process that holds
- the fun. Here the crypto key fun can do any necessary
- initializations. If
- CryptoKeyFun({debug_info, Mode, Module, Filename}) -> Key
- Called when the key is needed for the module
- CryptoKeyFun(clear) -> term()
- Called before the fun is unregistered. Here any cleaning up
- can be done. The return value is not important, but is passed
- back to the caller of
Unregisters the crypto key fun and terminates the process
- holding it, started by
The
Returns the module version or versions. A version is defined by
+ module attribute
Examples:
++1> beam_lib:version(a). % -vsn(1). +{ok,{a,[1]}} +2> beam_lib:version(b). % -vsn([1]). +{ok,{b,[1]}} +3> beam_lib:version(c). % -vsn([1]). -vsn(2). +{ok,{c,[1,2]}} +4> beam_lib:version(d). % no -vsn attribute +{ok,{d,[275613208176997377698094100858909383631]}}