scripts/gcc-plugins/latent_entropy_plugin.c
Source file repositories/reference/linux-study-clean/scripts/gcc-plugins/latent_entropy_plugin.c
File Facts
- System
- Linux kernel
- Corpus path
scripts/gcc-plugins/latent_entropy_plugin.c- Extension
.c- Size
- 17100 bytes
- Lines
- 627
- Domain
- Support Tooling And Documentation
- Bucket
- scripts
- Inferred role
- Support Tooling And Documentation: implementation source
- Status
- source implementation candidate
Why This File Exists
Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.
- Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
gcc-common.hgcc-generate-gimple-pass.h
Detected Declarations
function get_random_constfunction tree_get_random_constfunction handle_latent_entropy_attributefunction register_attributesfunction latent_entropy_gatefunction create_varfunction get_opfunction create_assignfunction perturb_local_entropyfunction __perturb_latent_entropyfunction handle_tail_callsfunction perturb_latent_entropyfunction FOR_EACH_EDGEfunction init_local_entropyfunction create_latent_entropy_declfunction FOR_EACH_VARIABLEfunction latent_entropy_executefunction latent_entropy_start_unitfunction plugin_init
Annotated Snippet
* if (argc <= 1) {
* // perturb_local_entropy()
* local_entropy += 4623067384293424948;
* printf("%s: no command arguments :(\n", *argv);
* // perturb_local_entropy()
* } else {
* local_entropy ^= 3896280633962944730;
* printf("%s: %d command arguments!\n", *argv, argc - 1);
* }
*
* // latent_entropy_execute() 4.
* tmp_latent_entropy = rol(tmp_latent_entropy, local_entropy);
* latent_entropy = tmp_latent_entropy;
* }
*
* TODO:
* - add ipa pass to identify not explicitly marked candidate functions
* - mix in more program state (function arguments/return values,
* loop variables, etc)
* - more instrumentation control via attribute parameters
*
* BUGS:
* - none known
*
* Options:
* -fplugin-arg-latent_entropy_plugin-disable
*
* Attribute: __attribute__((latent_entropy))
* The latent_entropy gcc attribute can be only on functions and variables.
* If it is on a function then the plugin will instrument it. If the attribute
* is on a variable then the plugin will initialize it with a random value.
* The variable must be an integer, an integer array type or a structure
* with integer fields.
*/
#include "gcc-common.h"
__visible int plugin_is_GPL_compatible;
static GTY(()) tree latent_entropy_decl;
static struct plugin_info latent_entropy_plugin_info = {
.version = PLUGIN_VERSION,
.help = "disable\tturn off latent entropy instrumentation\n",
};
static unsigned HOST_WIDE_INT deterministic_seed;
static unsigned HOST_WIDE_INT rnd_buf[32];
static size_t rnd_idx = ARRAY_SIZE(rnd_buf);
static int urandom_fd = -1;
static unsigned HOST_WIDE_INT get_random_const(void)
{
if (deterministic_seed) {
unsigned HOST_WIDE_INT w = deterministic_seed;
w ^= w << 13;
w ^= w >> 7;
w ^= w << 17;
deterministic_seed = w;
return deterministic_seed;
}
if (urandom_fd < 0) {
urandom_fd = open("/dev/urandom", O_RDONLY);
gcc_assert(urandom_fd >= 0);
}
if (rnd_idx >= ARRAY_SIZE(rnd_buf)) {
gcc_assert(read(urandom_fd, rnd_buf, sizeof(rnd_buf)) == sizeof(rnd_buf));
rnd_idx = 0;
}
return rnd_buf[rnd_idx++];
}
static tree tree_get_random_const(tree type)
{
unsigned long long mask;
mask = 1ULL << (TREE_INT_CST_LOW(TYPE_SIZE(type)) - 1);
mask = 2 * (mask - 1) + 1;
if (TYPE_UNSIGNED(type))
return build_int_cstu(type, mask & get_random_const());
return build_int_cst(type, mask & get_random_const());
}
static tree handle_latent_entropy_attribute(tree *node, tree name,
tree args __unused,
int flags __unused,
bool *no_add_attrs)
{
Annotation
- Immediate include surface: `gcc-common.h`, `gcc-generate-gimple-pass.h`.
- Detected declarations: `function get_random_const`, `function tree_get_random_const`, `function handle_latent_entropy_attribute`, `function register_attributes`, `function latent_entropy_gate`, `function create_var`, `function get_op`, `function create_assign`, `function perturb_local_entropy`, `function __perturb_latent_entropy`.
- Atlas domain: Support Tooling And Documentation / scripts.
- Implementation status: source implementation candidate.
Implementation Notes
- This generated page is the file-by-file coverage layer; curated subsystem chapters should link here when they synthesize a multi-file control flow.
- Core OS pages should be promoted from atlas-only to deep-reviewed when they explain data structures, invariants, locking, lifecycle, and C implementation snippets.
- Driver-family pages are intentionally pattern-oriented unless they are part of the selected PCIe/NVMe representative device path.