Embedded SDK
Embedded SDK
Loading...
Searching...
No Matches
fuzz_harness.h
Go to the documentation of this file.
1#pragma once
14#include <stddef.h>
15#include <stdint.h>
16#include <string.h>
17#include <setjmp.h>
18#include "fuzz_defs.h"
19#include "mocks.h"
20#include "parser.h"
21
23extern const size_t fuzz_n_commands;
24extern void fuzz_app_reset(void);
25extern void fuzz_app_dispatch(void *cmd);
26extern void fuzz_app_cleanup(
27 void);
28
30static inline int fuzz_use_structured_lane(void)
31{
32 return fuzz_ctrl[0] > FUZZ_STRUCTURED_LANE_THRESHOLD;
33}
34
36static inline uint8_t fuzz_clamp_p(uint8_t raw, uint8_t p_max)
37{
38 if (p_max == 0) {
39 return raw;
40 }
41 return raw % (p_max + 1);
42}
43
45#ifndef FUZZ_PICK_COMMAND_STRUCTURED
46#define FUZZ_PICK_COMMAND_STRUCTURED(data, size) (&fuzz_commands[fuzz_ctrl[1] % fuzz_n_commands])
47#endif
48
49#ifndef FUZZ_PICK_COMMAND_RAW
50#define FUZZ_PICK_COMMAND_RAW(data, size) (&fuzz_commands[(data)[1] % fuzz_n_commands])
51#endif
52
53static void fuzz_harness_cleanup(void)
54{
56 fuzz_tail_ptr = NULL;
57 fuzz_tail_len = 0;
58 try_context_set(NULL);
59 memset(&fuzz_exit_jump_ctx, 0, sizeof(fuzz_exit_jump_ctx));
60}
61
69static int fuzz_harness_entry(const uint8_t *data, size_t size)
70{
71 try_context_set(&fuzz_exit_jump_ctx);
72
73 if (sigsetjmp(fuzz_exit_jump_ctx.jmp_buf, 1)) {
75 return 0;
76 }
77
78 if (size < 4 || fuzz_n_commands == 0) {
79 return -1;
80 }
81
83
84 fuzz_tail_ptr = (size > 4) ? data + 4 : NULL;
85 fuzz_tail_len = (size > 4) ? size - 4 : 0;
86
87 const fuzz_command_spec_t *spec;
89 spec = FUZZ_PICK_COMMAND_STRUCTURED(data, size);
90 }
91 else {
92 spec = FUZZ_PICK_COMMAND_RAW(data, size);
93 }
94
95 command_t cmd;
96 memset(&cmd, 0, sizeof(cmd));
97
98 cmd.cla = spec->cla;
99 cmd.ins = spec->ins;
100 cmd.p1 = fuzz_clamp_p(data[2], spec->p1_max);
101 cmd.p2 = fuzz_clamp_p(data[3], spec->p2_max);
102
103 if (size > 4) {
104 size_t payload = size - 4;
105 cmd.lc = (uint8_t) (payload > 255 ? 255 : payload);
106 cmd.data = (uint8_t *) &data[4];
107 }
108
109 fuzz_app_dispatch(&cmd);
111 return 0;
112}
Shared constants and the command descriptor type for SDK fuzz harnesses.
#define FUZZ_STRUCTURED_LANE_THRESHOLD
Control byte 0 above this value selects the structured lane; at or below it the harness takes the raw...
Definition fuzz_defs.h:23
static int fuzz_harness_entry(const uint8_t *data, size_t size)
Default fuzz_entry() body: run one fuzzer input as one APDU.
void fuzz_app_reset(void)
Reset app state before each iteration (required).
void fuzz_app_cleanup(void)
Optional per-iteration teardown; a weak no-op default is provided.
#define FUZZ_PICK_COMMAND_RAW(data, size)
void fuzz_app_dispatch(void *cmd)
Dispatch one command_t to the app (required).
const size_t fuzz_n_commands
Number of entries in fuzz_commands.
static uint8_t fuzz_clamp_p(uint8_t raw, uint8_t p_max)
Clamp a raw P1/P2 byte to the command's declared maximum (0 = full range).
const fuzz_command_spec_t fuzz_commands[]
App command table.
static int fuzz_use_structured_lane(void)
Whether control byte 0 selects the structured lane.
#define FUZZ_PICK_COMMAND_STRUCTURED(data, size)
static void fuzz_harness_cleanup(void)
uint8_t ins
Instruction class.
Definition parser.h:12
uint8_t lc
Instruction parameter 2.
Definition parser.h:15
uint8_t cla
Definition parser.h:11
uint8_t p1
Instruction code.
Definition parser.h:13
uint8_t p2
Instruction parameter 1.
Definition parser.h:14
uint8_t * data
Length of command data.
Definition parser.h:16
Describes one APDU command the harness may synthesise and dispatch.
Definition fuzz_defs.h:29
uint8_t p2_max
Upper bound for P2 (0 = full range [0,255]).
Definition fuzz_defs.h:33
uint8_t p1_max
Upper bound for P1 (0 = full range [0,255]).
Definition fuzz_defs.h:32
uint8_t ins
APDU instruction byte.
Definition fuzz_defs.h:31
uint8_t cla
APDU class byte.
Definition fuzz_defs.h:30