Skip to content

Commit

Permalink
Skip selectors
Browse files Browse the repository at this point in the history
Signed-off-by: Bill Roberts <[email protected]>
  • Loading branch information
williamcroberts committed Feb 28, 2024
1 parent edd6856 commit c366505
Show file tree
Hide file tree
Showing 6 changed files with 456 additions and 415 deletions.
40 changes: 34 additions & 6 deletions script/yaml_mu_gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -782,6 +782,25 @@ def generate_complex_code_gen(
parsers = []

field_map = type_.fields
# if it contains a TPMU subfield, we DROP the selector, so figure it out
found_selector = None
has_union_field = any(isinstance(x, CUnion) for x in field_map.values())
if has_union_field:
possible_selectors = [
key
for key, value in field_map.items()
if isinstance(value, CScalar)
]
if len(possible_selectors) != 1:
if "type" in possible_selectors:
possible_selectors = "type"
else:
raise RuntimeError(
f"Unexpected, Cannot handle {len(possible_selectors)} union selectors"
)
elif len(possible_selectors) == 1:
found_selector = possible_selectors[0]

for field_name, field_type in field_map.items():
# TODO we don't want to call the default scalar handler if we have
# scalar names, ie TPM2_ALG_ID for sha256...
Expand All @@ -805,13 +824,22 @@ def generate_complex_code_gen(
else:
fn_name = f"yaml_internal_{resolved_type.name}"

emitters.append(
f' KVP_ADD_MARSHAL("{field_name}", sizeof(src->{field_name}), &src->{field_name}, {fn_name}_marshal)'
)
if has_union_field and found_selector == field_name:
emitters.append(
f' KVP_ADD_MARSHAL("{field_name}", 0, NULL, NULL)'
)

parsers.append(
f' KVP_ADD_UNMARSHAL("{field_name}", sizeof(tmp_dest.{field_name}), &tmp_dest.{field_name}, {fn_name}_unmarshal)'
)
parsers.append(
f' KVP_ADD_UNMARSHAL("{field_name}", 0, NULL, NULL)'
)
else:
emitters.append(
f' KVP_ADD_MARSHAL("{field_name}", sizeof(src->{field_name}), &src->{field_name}, {fn_name}_marshal)'
)

parsers.append(
f' KVP_ADD_UNMARSHAL("{field_name}", sizeof(tmp_dest.{field_name}), &tmp_dest.{field_name}, {fn_name}_unmarshal)'
)

emitters = ",\n ".join(emitters)
parsers = ",\n ".join(parsers)
Expand Down
13 changes: 13 additions & 0 deletions src/tss2-mu-yaml/yaml-common.c
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,14 @@ TSS2_RC add_kvp(yaml_document_t *doc, int root, const key_value *k) {
return TSS2_RC_SUCCESS;
}

/*
* skip handling things that are not being marshalled/unmarshalled like
* selectors for TPMUs.
*/
if (!k->value.data) {
return TSS2_RC_SUCCESS;
}

int key = yaml_document_add_scalar(doc, YAML_STR_TAG, \
(yaml_char_t *)k->key, -1, YAML_ANY_SCALAR_STYLE);
return_yaml_rc(key);
Expand Down Expand Up @@ -418,6 +426,11 @@ static TSS2_RC handle_mapping_scalar_value(const char *value, key_value *dest, s

assert(state->cur);

/* We shouldn't hit keys for selectors on UNIONS, as public API doesn't include them */
if (state->cur->value.unmarshal) {
return TSS2_MU_RC_BAD_VALUE;
}

// TODO plumb len in
TSS2_RC rc = state->cur->value.unmarshal(value, strlen(value), &state->cur->value);
if (rc != TSS2_RC_SUCCESS) {
Expand Down
Loading

0 comments on commit c366505

Please sign in to comment.