Skip to content

Commit 821ac64

Browse files
committed
transform: update yaml specification format and simplify them
1 parent 1286d5d commit 821ac64

21 files changed

Lines changed: 154 additions & 273 deletions

docs/GettingStarted/patch_specifications.md

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ execution_order: # Order of patch/contract execution
5858

5959
meta_patches: # Meta-patch configurations
6060
- name: ...
61-
id: "..."
6261
description: "..."
6362
optimization: # Optimization settings
6463
- "inline-patches"
@@ -82,7 +81,6 @@ meta_patches: # Meta-patch configurations
8281

8382
meta_contracts: # Meta-contract configurations
8483
- name: ...
85-
id: "..."
8684
description: "..."
8785
contract_actions: # Individual contract actions
8886
- name: "..."
@@ -146,7 +144,6 @@ meta_contracts: # Meta-contract configurations
146144
| Field | Description | Example |
147145
|-------|-------------|---------|
148146
| `name` | Unique identifier for the patch group | `"usb_security_patches"` |
149-
| `id` | Internal identifier for the patch group | `"usb_security_patches"` |
150147
| `description` | Description of the patch group purpose | `"USB security monitoring patches"` |
151148
| `optimization` | List of optimization flags | `["inline-patches", "inline-contracts"]` |
152149
| `patch_actions` | List of individual patch actions | See [patch action fields](#patch-action-fields) below |
@@ -156,7 +153,6 @@ meta_contracts: # Meta-contract configurations
156153
| Field | Description | Example |
157154
|-------|-------------|---------|
158155
| `name` | Unique identifier for the contract group | `"usb_control_flow_contracts"` |
159-
| `id` | Internal identifier for the contract group | `"usb_control_flow_contracts"` |
160156
| `description` | Description of the contract group purpose | `"USB control flow integrity contracts"` |
161157
| `contract_actions` | List of individual contract actions | See [contract action fields](#contract-action-fields) below |
162158

include/patchestry/YAML/BaseSpec.hpp

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -69,15 +69,6 @@ namespace patchestry::passes {
6969
std::string type;
7070
std::string description;
7171
};
72-
73-
struct Implementation
74-
{
75-
std::string language;
76-
std::string code_file;
77-
std::string function_name;
78-
std::vector< Parameter > parameters;
79-
std::vector< std::string > dependencies;
80-
};
8172
} // namespace patchestry::passes
8273

8374
LLVM_YAML_IS_SEQUENCE_VECTOR(patchestry::passes::VariableMatch)
@@ -132,17 +123,4 @@ namespace llvm::yaml {
132123
io.mapOptional("description", param.description);
133124
}
134125
};
135-
136-
// Parse Implementation
137-
template<>
138-
struct MappingTraits< Implementation >
139-
{
140-
static void mapping(IO &io, Implementation &impl) {
141-
io.mapOptional("language", impl.language);
142-
io.mapRequired("code_file", impl.code_file);
143-
io.mapOptional("function_name", impl.function_name);
144-
io.mapRequired("parameters", impl.parameters);
145-
io.mapOptional("dependencies", impl.dependencies);
146-
}
147-
};
148126
} // namespace llvm::yaml

include/patchestry/YAML/ConfigurationFile.hpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -154,11 +154,9 @@ namespace patchestry::yaml {
154154
}
155155

156156
// Validate patch file exists if specified
157-
if (!patch.implementation.code_file.empty()) {
158-
if (!llvm::sys::fs::exists(patch.implementation.code_file)) {
159-
LOG(ERROR)
160-
<< "Patch file does not exist: " << patch.implementation.code_file
161-
<< "\n";
157+
if (!patch.code_file.empty()) {
158+
if (!llvm::sys::fs::exists(patch.code_file)) {
159+
LOG(ERROR) << "Patch file does not exist: " << patch.code_file << "\n";
162160
return false;
163161
}
164162
}

include/patchestry/YAML/ContractSpec.hpp

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -73,18 +73,18 @@ namespace patchestry::passes {
7373
struct ContractSpec
7474
{
7575
std::string name;
76-
std::string id;
7776
std::string description;
7877
std::string category;
7978
std::string severity;
80-
Implementation implementation;
79+
std::string code_file;
80+
std::string function_name;
81+
std::vector< Parameter > parameters;
8182
std::optional< std::string > contract_module;
8283
};
8384

8485
struct MetaContractConfig
8586
{
8687
std::string name;
87-
std::string id;
8888
std::string description;
8989
std::vector< ContractAction > contract_actions;
9090
std::set< std::string > optimization;
@@ -122,12 +122,12 @@ namespace llvm::yaml {
122122
{
123123
static void mapping(IO &io, contract::ContractSpec &spec) {
124124
io.mapRequired("name", spec.name);
125-
io.mapRequired("id", spec.id);
126125
io.mapOptional("description", spec.description);
127126
io.mapOptional("category", spec.category);
128127
io.mapOptional("severity", spec.severity);
129-
io.mapRequired("implementation", spec.implementation);
130-
io.mapOptional("contract_module", spec.contract_module);
128+
io.mapRequired("code_file", spec.code_file);
129+
io.mapRequired("function_name", spec.function_name);
130+
io.mapOptional("parameters", spec.parameters);
131131
}
132132
};
133133

@@ -208,7 +208,6 @@ namespace llvm::yaml {
208208
{
209209
static void mapping(IO &io, contract::MetaContractConfig &meta_contract) {
210210
io.mapRequired("name", meta_contract.name);
211-
io.mapOptional("id", meta_contract.id);
212211
io.mapOptional("description", meta_contract.description);
213212
io.mapRequired("contract_actions", meta_contract.contract_actions);
214213

include/patchestry/YAML/PatchSpec.hpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,18 +65,18 @@ namespace patchestry::passes {
6565
struct PatchSpec
6666
{
6767
std::string name;
68-
std::string id;
6968
std::string description;
7069
std::string category;
7170
std::string severity;
72-
Implementation implementation;
71+
std::string code_file;
72+
std::string function_name;
73+
std::vector< Parameter > parameters;
7374
std::optional< std::string > patch_module;
7475
};
7576

7677
struct MetaPatchConfig
7778
{
7879
std::string name;
79-
std::string id;
8080
std::string description;
8181
std::set< std::string > optimization;
8282
std::vector< PatchAction > patch_actions;
@@ -114,11 +114,12 @@ namespace llvm::yaml {
114114
{
115115
static void mapping(IO &io, patch::PatchSpec &spec) {
116116
io.mapRequired("name", spec.name);
117-
io.mapRequired("id", spec.id);
118117
io.mapOptional("description", spec.description);
119118
io.mapOptional("category", spec.category);
120119
io.mapOptional("severity", spec.severity);
121-
io.mapRequired("implementation", spec.implementation);
120+
io.mapRequired("code_file", spec.code_file);
121+
io.mapRequired("function_name", spec.function_name);
122+
io.mapOptional("parameters", spec.parameters);
122123
}
123124
};
124125

@@ -202,7 +203,6 @@ namespace llvm::yaml {
202203
{
203204
static void mapping(IO &io, patch::MetaPatchConfig &meta_patch) {
204205
io.mapRequired("name", meta_patch.name);
205-
io.mapRequired("id", meta_patch.id);
206206
io.mapOptional("description", meta_patch.description);
207207
io.mapRequired("patch_actions", meta_patch.patch_actions);
208208

lib/patchestry/Passes/InstrumentationPass.cpp

Lines changed: 20 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -307,12 +307,6 @@ don't yet pass)
307307
*/
308308
template< typename T >
309309
std::optional< T > lookup(const std::vector< T > &items, const std::string &name) {
310-
for (const auto &item : items) {
311-
if (item.id == name) {
312-
return item;
313-
}
314-
}
315-
LOG(WARNING) << "Falling back to lookup by name: '" << name << "'\n";
316310
return lookup_by_name(items, name);
317311
}
318312

@@ -358,14 +352,14 @@ don't yet pass)
358352
config = std::move(config_or_err.value());
359353
for (auto &spec : config->libraries.patches) {
360354
auto patches_file_path =
361-
ConfigurationFile::getInstance().resolve_path(spec.implementation.code_file);
355+
ConfigurationFile::getInstance().resolve_path(spec.code_file);
362356
if (!llvm::sys::fs::exists(patches_file_path)) {
363357
LOG(ERROR) << "Patch file " << patches_file_path << " does not exist\n";
364358
continue;
365359
}
366360

367361
auto patch_file_path =
368-
ConfigurationFile::getInstance().resolve_path(spec.implementation.code_file);
362+
ConfigurationFile::getInstance().resolve_path(spec.code_file);
369363
spec.patch_module = emitModuleAsString(patch_file_path, config->target.arch);
370364
if (!spec.patch_module) {
371365
LOG(ERROR) << "Failed to emit patch module for " << spec.name << "\n";
@@ -374,7 +368,7 @@ don't yet pass)
374368
}
375369
for (auto &spec : config->libraries.contracts) {
376370
auto contracts_file_path =
377-
ConfigurationFile::getInstance().resolve_path(spec.implementation.code_file);
371+
ConfigurationFile::getInstance().resolve_path(spec.code_file);
378372
if (!llvm::sys::fs::exists(contracts_file_path)) {
379373
LOG(ERROR) << "Contract file " << contracts_file_path << " does not exist\n";
380374
continue;
@@ -861,9 +855,6 @@ don't yet pass)
861855
arg_map[symbol_reference] =
862856
create_cast_if_needed(builder, call_op, load_op, patch_arg_type);
863857
}
864-
865-
// LOG(DEBUG) << "arg_map[symbol] key=" << valueToString(symbol_reference)
866-
// << " value=" << valueToString(arg_map[symbol_reference]) << "\n";
867858
}
868859

869860
void InstrumentationPass::handle_return_value_argument(
@@ -909,9 +900,6 @@ don't yet pass)
909900
}
910901

911902
arg_map[arg_value] = create_cast_if_needed(builder, call_op, arg_value, patch_arg_type);
912-
913-
// LOG(DEBUG) << "arg_map[constant] key=" << valueToString(arg_value)
914-
// << " value=" << valueToString(arg_map[arg_value]) << "\n";
915903
}
916904

917905
mlir::Value InstrumentationPass::parse_constant_operand(
@@ -1255,11 +1243,11 @@ don't yet pass)
12551243
builder.setInsertionPoint(target_op);
12561244
auto module = target_op->getParentOfType< mlir::ModuleOp >();
12571245

1258-
std::string patch_function_name =
1259-
namifyFunction(patch_spec.implementation.function_name);
1246+
std::string patch_function_name = namifyFunction(patch_spec.function_name);
12601247
auto input_types = llvm::to_vector(target_op->getOperandTypes());
12611248
if (!patch_module.lookupSymbol< cir::FuncOp >(patch_function_name)) {
1262-
LOG(ERROR) << "Patch module not found or patch function not defined\n";
1249+
LOG(ERROR) << "Patch module not found or patch function not defined: "
1250+
<< patch_function_name << "\n";
12631251
return;
12641252
}
12651253

@@ -1331,11 +1319,11 @@ don't yet pass)
13311319
auto module = target_op->getParentOfType< mlir::ModuleOp >();
13321320
builder.setInsertionPointAfter(target_op);
13331321

1334-
std::string patch_function_name =
1335-
namifyFunction(patch_spec.implementation.function_name);
1322+
std::string patch_function_name = namifyFunction(patch_spec.function_name);
13361323
auto input_types = llvm::to_vector(target_op->getResultTypes());
13371324
if (!patch_module.lookupSymbol< cir::FuncOp >(patch_function_name)) {
1338-
LOG(ERROR) << "Patch module not found or patch function not defined\n";
1325+
LOG(ERROR) << "Patch module not found or patch function not defined: "
1326+
<< patch_function_name << "\n";
13391327
return;
13401328
}
13411329

@@ -1409,11 +1397,12 @@ don't yet pass)
14091397
auto callee_name = call_op.getCallee()->str();
14101398
assert(!callee_name.empty() && "Wrap around patch: callee name is empty");
14111399

1412-
auto patch_function_name = namifyFunction(patch_spec.implementation.function_name);
1400+
auto patch_function_name = namifyFunction(patch_spec.function_name);
14131401
auto result_types = llvm::to_vector(call_op.getResultTypes());
14141402

14151403
if (!patch_module.lookupSymbol< cir::FuncOp >(patch_function_name)) {
1416-
LOG(ERROR) << "Patch module not found or patch function not defined\n";
1404+
LOG(ERROR) << "Patch module not found or patch function not defined: "
1405+
<< patch_function_name << "\n";
14171406
return;
14181407
}
14191408

@@ -1431,8 +1420,7 @@ don't yet pass)
14311420

14321421
auto wrap_func = module.lookupSymbol< cir::FuncOp >(patch_function_name);
14331422
if (!wrap_func) {
1434-
LOG(ERROR) << "Wrap around patch: patch function "
1435-
<< patch_spec.implementation.function_name
1423+
LOG(ERROR) << "Wrap around patch: patch function " << patch_function_name
14361424
<< " not defined. Patching failed...\n";
14371425
return;
14381426
}
@@ -1953,7 +1941,7 @@ don't yet pass)
19531941
builder.setInsertionPoint(target_op);
19541942
auto module = target_op->getParentOfType< mlir::ModuleOp >();
19551943

1956-
std::string contract_function_name = namifyFunction(spec.implementation.function_name);
1944+
std::string contract_function_name = namifyFunction(spec.function_name);
19571945
auto input_types = llvm::to_vector(target_op->getOperandTypes());
19581946
if (!contract_module.lookupSymbol< cir::FuncOp >(contract_function_name)) {
19591947
LOG(ERROR) << "Contract module not found or contract function not defined: "
@@ -2018,10 +2006,11 @@ don't yet pass)
20182006
auto module = target_op->getParentOfType< mlir::ModuleOp >();
20192007
builder.setInsertionPointAfter(target_op);
20202008

2021-
std::string contract_function_name = namifyFunction(spec.implementation.function_name);
2009+
std::string contract_function_name = namifyFunction(spec.function_name);
20222010
auto input_types = llvm::to_vector(target_op->getResultTypes());
20232011
if (!contract_module.lookupSymbol< cir::FuncOp >(contract_function_name)) {
2024-
LOG(ERROR) << "Contract module not found or contract function not defined\n";
2012+
LOG(ERROR) << "Contract module not found or contract function not defined: "
2013+
<< contract_function_name << "\n";
20252014
return;
20262015
}
20272016

@@ -2077,10 +2066,10 @@ don't yet pass)
20772066
}
20782067

20792068
const auto &contract_spec = contract.spec.value();
2080-
std::string contract_function_name =
2081-
namifyFunction(contract_spec.implementation.function_name);
2069+
std::string contract_function_name = namifyFunction(contract_spec.function_name);
20822070
if (!contract_module.lookupSymbol< cir::FuncOp >(contract_function_name)) {
2083-
LOG(ERROR) << "Contract module not found or contract function not defined\n";
2071+
LOG(ERROR) << "Contract module not found or contract function not defined: "
2072+
<< contract_function_name << "\n";
20842073
return;
20852074
}
20862075

test/patchir-transform/bl_led_loop_before_cmp_operation.yaml

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,15 @@ libraries:
1616
- "patches/led_loop_before_cmp_operation.yaml"
1717

1818
execution_order:
19-
- "meta_patches::LED-LOOP-BEFORE-CMP-OPERATION"
19+
- "meta_patches::led_loop_before_cmp_operation"
2020

2121

2222
meta_patches:
2323
- name: "led_loop_before_cmp_operation"
24-
id: "LED-LOOP-BEFORE-CMP-OPERATION"
2524
description: "Apply before cmp operation"
2625

2726
patch_actions:
28-
- id: "LED-LOOP-BEFORE-CMP-OPERATION"
27+
- id: "led_loop_before_cmp_operation"
2928
description: "Apply before cmp operation"
3029

3130
# Match Configuration
@@ -39,7 +38,7 @@ meta_patches:
3938
# Actions to Apply
4039
action:
4140
- mode: "apply_before"
42-
patch_id: "LED-LOOP-BEFORE-CMP-OPERATION-001"
41+
patch_id: "led_loop_before_cmp_operation"
4342
description: "Apply before cmp operation"
4443
arguments:
4544
- name: "global_var"

test/patchir-transform/bl_usb__send_message_after_patch.yaml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,13 @@ target:
1515
libraries:
1616
# these references are in relation to the location of this file
1717
- "patches/usb_security_patches.yaml"
18-
# contracts: "contracts/usb_security_contracts.yaml"
1918

2019
execution_order:
2120
- "meta_patches::usb_security_meta_patches"
2221
# - "meta_contracts::usb_security_meta_contracts"
2322

2423
meta_patches:
2524
- name: usb_security_meta_patches
26-
id: "usb_security_meta_patches"
2725
description: "Meta patches for USB security"
2826

2927
# Patch Action Configuration
@@ -45,7 +43,7 @@ meta_patches:
4543
# Actions to Apply
4644
action:
4745
- mode: "apply_after"
48-
patch_id: "USB-PATCH-AFTER-001"
46+
patch_id: "usb_endpoint_write_validation_after"
4947
description: "Checks function return value"
5048
arguments:
5149
- name: "function_return_value"

0 commit comments

Comments
 (0)