Skip to content

Pr/datatype editing - #688

Open
egore wants to merge 5 commits into
bethington:devfrom
egore:pr/datatype-editing
Open

egore wants to merge 5 commits into
bethington:devfrom
egore:pr/datatype-editing

Conversation

@egore

@egore egore commented Nov 21, 2026

Copy link
Copy Markdown
Contributor

What this changes

The data type editing tools no longer silently lose data or reshape structures. Type creators take a category_path, struct and union members keep their comments, add_struct_field and remove_struct_field keep the layout intact, and delete_data_type refuses to delete a type that is still in use unless force=true. It also adds a kind filter to search_data_types and lets set_comment read and write a structure's description.

Fixes #

Why

Several of these tools did something surprising and unrecoverable:

  • Field parsing: struct fields were parsed by splitting on commas and colons, so a comment containing punctuation was mangled, and a union member with an unknown type was silently skipped.
  • Removing a field: remove_struct_field shifted every later field down, which moves all the offsets in code that already references the struct.
  • Placement: adding a field at an explicit offset could overwrite a defined field without warning.
  • Deleting a type: DataTypeManager.remove() succeeds on a type that is still referenced and takes its users with it. Applied data is cleared and parent types lose their fields. The old code assumed remove() would refuse.
  • Type resolution: an unknown pointer target silently became void *. Qualified /Category/Name paths didn't resolve, and ABI sizes were assumed.
  • Function signatures: parameter names were dropped and conventions couldn't be set. Updating an existing definition replaced it instead of editing it in place, so the existing object was lost.

What changed, one commit per item:

  1. Creation:
    • Optional category_path.
    • Member comments are kept, with an explicit size that must match.
    • Array and pointer member types, and a {name,value} array for enums.
    • Gson parsing and fail-on-bad-type.
    • The 1-byte placeholder is replaced inside the creation transaction.
    • Function signatures keep names and conventions and update in place.
    • Identical typedefs are a no-op, and conflicting ones are rejected.
  2. Layout:
    • add_struct_field accepts a bulk fields array applied in one transaction, with rollback.
    • A field placed at an explicit offset reuses undefined padding and refuses to overwrite a defined field unless overwrite=true.
    • remove_struct_field clears to undefined bytes and only shifts with shrink=true.
    • Field lookup accepts a hex offset, offset:N, or a default name like field0_0x0.
    • Growing a field consumes padding instead of moving its neighbours.
  3. Deletion: users are collected first (composites including through pointer and array wrappers, defined data through arrays, typedefs and pointers, and function returns and variables). The call is refused and lists them unless force=true.
  4. Inspection:
    • search_data_types takes an optional kind and an empty pattern, with query as an alias for pattern.
    • get_type_size also reports members, signatures and base types.
  5. Descriptions: set_comment(type="struct", struct_name=..., comment=...) sets, replaces or clears a structure's description transactionally. get_struct_layout returns it.

Behaviour changes callers will notice:

  • remove_struct_field no longer shifts later fields by default.
  • A bad type in a union or struct member now fails the call.
  • delete_data_type on a type in use is refused without force.

No tool is added or removed, so the catalog stays at 220.

Tests you ran

  • uv run pytest tests/unit/ — Python unit tier, no Ghidra needed
  • ./gradlew test --tests 'com.xebyte.offline.*' "-PGHIDRA_INSTALL_DIR=<dir>"
    — offline Java tier, needs a Ghidra install but no running server and no
    Maven (mvn test -Dtest='com.xebyte.offline.*Test' is the Maven equivalent)
  • powershell -File tests\pester\Run-Tests.ps1 — only if you touched
    ghidra-mcp-setup.ps1
  • Live tier (pytest tests/ -m readonly, ./gradlew test) — needs Ghidra
    running on port 8089 with a program loaded
  • Could not run the live tier
Rebased on upstream/dev (8dbbb1f6), Ghidra 12.2, macOS.

pytest tests/unit  (excluding test_release_evidence.py and the gradlew
toolchain tests, which need gpg signing / a Windows-style environment)
-> 0 failed

./gradlew test --tests 'com.xebyte.offline.*' --tests 'com.xebyte.core.DataTypeEditingGhidraTest'
-> 993 tests completed, 3 failed:
   SharedProjectLocatorTest.testResolveProjectDirOverrideIsContainmentChecked
   TreeRegistryTest.createIsIdempotentHoweverTheDefaultRootIsExpressed
   TreeRootTest.defaultsConfigRoundTripThroughExplicitRoot
   The same 3 fail on a clean upstream/dev checkout; this PR does not touch them.
   DataTypeEditingGhidraTest (new, real Ghidra ProgramBuilder) passes.

If you touched an @mcptool or @PARAM annotation

New parameters: fields and overwrite on add_struct_field, category_path on the type creators, calling_convention on create_function_signature, force on delete_data_type, shrink on remove_struct_field, kind on search_data_types, and struct_name on set_comment.

  • mvn test -Dtest=RegenerateEndpointsJson -Dregenerate=true — Maven
    only
    ; Gradle cannot pass the -Dregenerate flag into the forked test
    JVM and exits BUILD SUCCESSFUL having regenerated nothing. No Maven? Say
    so here and leave the catalog alone — a maintainer will regenerate it.
  • python -m tools.audit_server_scope --write
  • python -m tools.gen_readme_api_reference --write
  • Not applicable

I did not run RegenerateEndpointsJson (no Maven here). tests/endpoints.json was edited to add the new parameter names and the set_comment description. The parity tests in the offline Java tier and tests/unit (including test_project_consistency.py and test_audit_server_scope.py) pass against it. A maintainer should still regenerate it with Maven to confirm there is no drift.

Checklist

  • Branched from dev and targeting dev
  • CHANGELOG.md updated, if this is user-visible
  • No unrelated reformatting mixed into the diff

…tion

Type creators gain an optional category_path. Struct and union members
keep their comments, accept an explicit size that must match a fixed
type's natural size, and accept array and pointer types. Enum creation
also accepts an array of {name,value} objects. Field JSON is parsed with
Gson instead of by splitting on commas and colons, so a comment with
punctuation survives, and a member with a bad type fails the call
instead of being skipped. Replacing a 1-byte placeholder happens inside
the creation transaction, so a failed replacement leaves it intact.

Function signatures keep parameter names, take an optional calling
convention and update an existing definition in place instead of
replacing it; an invalid parameter rolls the whole update back.
Creating an identical typedef is a no-op and a conflicting one is
rejected instead of silently replacing the existing type.

Type resolution resolves qualified /Category/Name paths, sizes pointers
and well-known C types for the target ABI, handles arrays of pointers and
multi-dimensional arrays, and no longer turns an unknown pointer target
into void *.
add_struct_field accepts a bulk "fields" array that is applied in one
transaction and rolled back if any field fails. Placing a field at an
explicit offset reuses undefined padding but refuses to overwrite a
defined field unless overwrite=true; padding is cleared in reverse
ordinal order because clearing a component renumbers the ones after it.

remove_struct_field now clears the field to undefined bytes so later
offsets stay put, and only shifts the rest of the structure with
shrink=true; packed structures still repack. Both it and
modify_struct_field accept a hex offset or offset:N in place of a name,
and default field names such as field0_0x0 resolve.

Growing a field through modify_struct_field consumes padding after it
instead of moving its neighbours, and keeps the field's name and comment.
delete_data_type relied on DataTypeManager.remove() to refuse a type that
is still referenced, but remove() succeeds and takes the type's users
with it: applied data is cleared and parent types lose their fields.

Collect the users first: composite types that reference it, including
through pointer and array wrappers; defined data whose type resolves to
it through arrays, typedefs or pointers; and function return types and
variables. Refuse the deletion and list them unless force=true.
search_data_types takes an optional kind (struct, union, enum, typedef,
pointer, array, function_definition or primitive) and an empty pattern,
so it can list every type of one kind. "query" is accepted as an alias
for "pattern".

get_type_size additionally reports the kind and, depending on it, the
members of a composite, the return type, convention and parameters of a
function definition, or the base type and element count of a typedef,
pointer or array.
Add type=struct and a struct_name selector to the existing comment tool,
preserving the tool count and listing-comment behavior. Store
descriptions transactionally and expose them through get_struct_layout.

Cover setting, replacing, clearing, qualified lookup, input validation,
schema defaults, and bridge dispatch. Gradle extension build and Java
tests pass; Python unit suite reports 1190 passed, 13 skipped, and one
unrelated default-branch mismatch (dev versus main).
@egore
egore requested a review from bethington as a code owner November 21, 2026 18:04
@codecov-commenter

Copy link
Copy Markdown

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants