Skip to content

Improve CopyObject handling. - #382

Merged
IsaacWoods merged 4 commits into
rust-osdev:mainfrom
martin-hughes:copy-object
Nov 26, 2026
Merged

IsaacWoods merged 4 commits into
rust-osdev:mainfrom
martin-hughes:copy-object

Conversation

@martin-hughes

Copy link
Copy Markdown
Contributor

Some relatively small tweaks to match the behaviour defined in the ACPI spec - see
https://uefi.org/specs/ACPI/6.6/19_ASL_Reference.html#object-storing-and-copying-rules

Tested against uACPI and Windows.

I'd like to add a few more tests, but @IsaacWoods would you take a look for any obvious holes? No rush, just when you have a chance

Some relatively small tweaks to match the behaviour defined in the ACPI
spec - see
https://uefi.org/specs/ACPI/6.6/19_ASL_Reference.html#object-storing-and-copying-rules

Tested against uACPI and Windows.
Specifically, CopyObject operations to an ArgX variable that isn't a
reference should not be visible to the caller.

This was tricky as the `inner` field for an Arg was originally a clone
of a `WrappedObject` stored in `MethodContext::args`. Thus, if the
pointed-to object is modified, the results are seen in the caller.

Also: if the `inner` field of a single Arg ref is rebound to point at a
different object (to prevent the caller seeing the result), then the
next time the same Arg is referenced by an `Arg` instruction the
original `WrappedObject` from the `args` array is cloned, and the result
of the `CopyObject` call is essentially lost.

The fix is to make the object stored in the `args` array be an `Arg`
reference itself, rather than creating a new `Arg` reference when the
`Arg` instruction is called. Then all uses of the same argument within
a callee function point at the same `inner` `WrappedObject`. At which
point, a `CopyObject` operation can simply rebind one `WrappedObject`
and have the result be visible in the caller, but not the callee.

There are a bunch of new unit tests to validate behaviour here - these
were AI-generated to cover most obvious cases.
Rather than expecting `do_copy_object` to deal with the specific rule
that Args that don't contain references are rebound before the
CopyObject operation, get `unwrap_ref_for_copyobject` to do it.

That keeps the logic all in one place and allows improved testability.

Also fix the actual tests - references can't be given to logical ops
directly.
Comment thread src/aml/mod.rs
operand.to_integer(self.integer_size)?
}
_ => Err(AmlError::ObjectNotOfExpectedType { expected: ObjectType::Integer, got: operand.typ() })?
Object::Integer(operand) => *operand,

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a formatting change that seems to have been missed previously

Comment thread src/aml/mod.rs
}
Object::FieldUnit(ref field_unit) => self.do_field_write(field_unit, object.clone())?,
_ => {
// TODO: This is definitively *unsound* if dst and object are one and the same...

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've put this as TODO, but I actually don't think it's a bit deal - I'd be very surprised if any Rust implementation did something stupid here.

@martin-hughes

Copy link
Copy Markdown
Contributor Author

During some playing I noticed that CopyObject to a "plain" Arg (i.e. one not containing a reference) wasn't working properly - within the callee function, the Arg essentially didn't store the copied object.

As such, I've changed MethodContext::args to contain Arg references, rather than the actual WrappedObjects provided by the caller method.

I think this commit message explains why, but do ask if you want a bit more info:

[...] the `inner` field for an Arg was originally a clone
of a `WrappedObject` stored in `MethodContext::args`. Thus, if the
pointed-to object is modified, the results are seen in the caller.

Also: if the `inner` field of a single Arg ref is rebound to point at a
different object (to prevent the caller seeing the result), then the
next time the same Arg is referenced by an `Arg` instruction the
original `WrappedObject` from the `args` array is cloned, and the result
of the `CopyObject` call is essentially lost.

The fix is to make the object stored in the `args` array be an `Arg`
reference itself, rather than creating a new `Arg` reference when the
`Arg` instruction is called. Then all uses of the same argument within
a callee function point at the same `inner` `WrappedObject`. At which
point, a `CopyObject` operation can simply rebind one `WrappedObject`
and have the result be visible in the caller, but not the callee.

Notes on testing: the ASL tests were tested against the Windows interpreter. The unit tests were LLM-generated to cover all the obvious cases.

There's also a formatting change in this PR that snuck into the main branch earlier and is corrected here.

@IsaacWoods would you mind taking a look?

@martin-hughes
martin-hughes marked this pull request as ready for review November 23, 2026 17:09
@IsaacWoods

Copy link
Copy Markdown
Member

Thanks @martin-hughes! This looks very sensible to me (albeit not with any further testing by myself), and handling arg references like this is definitely a little cleaner.

@IsaacWoods
IsaacWoods merged commit 65c203d into rust-osdev:main Nov 26, 2026
6 checks passed
@martin-hughes
martin-hughes deleted the copy-object branch November 27, 2026 20:39
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