Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #1344, UT assert stub return values #1352

Merged
merged 1 commit into from
Feb 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Fix #1344, UT assert stub return values
Improves default support for return values of sizes other than 32 bits.
Previously this required a custom handler for any value that was not a
32 bit integer.  With this update any integer value can be used, and a
value translation will occur if the size is differnt.  Thus boolean
values, 8,16, and 64-bit ints can be configured through the same basic
API without needing to add a handler.
  • Loading branch information
jphickey committed Jan 19, 2023
commit c11a4f3d32749b8c42c49ed29bd48e05f26b9b87
4 changes: 2 additions & 2 deletions ut_assert/inc/utgenstub.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
* \param ReturnType The type of return value
*/
#define UT_GenStub_SetupReturnBuffer(FuncName, ReturnType) \
UT_Stub_RegisterReturnType(UT_KEY(FuncName), sizeof(ReturnType))
UT_Stub_RegisterReturnType(UT_KEY(FuncName), sizeof(ReturnType), #ReturnType)

/**
* Helper macro to get the return value from the handler
Expand All @@ -51,7 +51,7 @@
* \param ReturnType The type of return value
*/
#define UT_GenStub_GetReturnValue(FuncName, ReturnType) \
(*(ReturnType *)UT_Stub_GetReturnValuePtr(UT_KEY(FuncName), sizeof(ReturnType)))
(*(ReturnType *)UT_Stub_GetReturnValuePtr(UT_KEY(FuncName), sizeof(ReturnType), #ReturnType))

/**
* Helper macro to add a local parameter to the current context
Expand Down
73 changes: 35 additions & 38 deletions ut_assert/inc/utstubs.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,15 @@
*/
typedef cpuaddr UT_EntryKey_t;

/**
* Type for generic integer value return codes
*
* By using the C99 "ptrdiff_t" type, this should be large enough to also
* store pointer values on the target system, in addition to all normal
* integer values.
*/
typedef ptrdiff_t UT_IntReturn_t;

/**
* Macro to obtain a UT_EntryKey_t value from any function name
*/
Expand All @@ -70,6 +79,18 @@ typedef enum
UT_STUBCONTEXT_ARG_TYPE_INDIRECT /**< Indicates "ArgPtr" is a pointer to the argument value on the stack */
} UT_StubContext_Arg_Type_t;

/**
* Identifies different genres of return values. This serves as a hint to determine how to adapt
* or convert a return value if the stub has a different return size.
*/
typedef enum UT_ValueGenre
{
UT_ValueGenre_OPAQUE = 0, /**< The nature of the value is opaque, reference is stored directly (NOT copied!) */
UT_ValueGenre_INTEGER = 1, /**< The value is an integer and may be converted to integers of other sizes */
UT_ValueGenre_FLOAT = 2, /**< The value is a floating point and may be converted to floats of other sizes */
UT_ValueGenre_POINTER = 3 /**< The value is a pointer and should only be used to fulfill a pointer return */
} UT_ValueGenre_t;

/**
* Complete Metadata associated with a context argument
*/
Expand Down Expand Up @@ -170,9 +191,15 @@ void UT_ResetState(UT_EntryKey_t FuncKey);
*
* \param FuncKey The stub function to add the return code to.
* \param Count The number of times after which the Retcode should be triggered
* \param Retcode The code to return after Count calls.
* \param Retcode The signed integer value to return after Count calls.
*/
void UT_SetDeferredRetcode(UT_EntryKey_t FuncKey, int32 Count, UT_IntReturn_t Retcode);

/**
* \param FuncKey The stub function to add the return code to.
*/
void UT_SetDeferredRetcode(UT_EntryKey_t FuncKey, int32 Count, int32 Retcode);
void UT_ConfigureGenericStubReturnValue(UT_EntryKey_t FuncKey, const void *ValuePtr, size_t ValueSize,
UT_ValueGenre_t ValueGenre, int32 DeferCount, const char *TypeName);

/**
* Add a data buffer for a given stub function
Expand Down Expand Up @@ -220,9 +247,9 @@ void UT_GetDataBuffer(UT_EntryKey_t FuncKey, void **DataBuffer, size_t *MaxSize,
* User needs to use UT_ClearDefaultReturnValue to clear the value.
*
* \param FuncKey The stub function to add the return code to.
* \param Value Arbitrary return value (may or may not be used by the stub)
* \param Value Arbitrary signed integer return value (may or may not be used by the stub)
*/
void UT_SetDefaultReturnValue(UT_EntryKey_t FuncKey, int32 Value);
void UT_SetDefaultReturnValue(UT_EntryKey_t FuncKey, UT_IntReturn_t Value);

/**
* Disable the default return for the given stub function
Expand Down Expand Up @@ -344,38 +371,6 @@ uint32 UT_GetStubCount(UT_EntryKey_t FuncKey);
*/
void UT_Stub_CallOnce(void (*Func)(void));

/**
* Check for a deferred return code entry for the given stub function
*
* This is a default implementation for deferred retcodes and can be used
* by stub functions as a common implementation. If a deferred retcode
* for the given function is present, this will decrement the associated
* count. If the count becomes zero, this function returns true and
* the Retcode parameter is assigned the originally requested code.
* Otherwise this function returns false which indicates the default
* stub implementation should be used.
*
* Once the counter reaches zero, this clears the entry so that if a
* second deferred code is recorded it will be found next.
*
* \param FuncKey The stub function to check the return code.
* \param Retcode Buffer to store deferred return code, if available.
* \returns true if deferred code is present and counter reached zero
*/
bool UT_Stub_CheckDeferredRetcode(UT_EntryKey_t FuncKey, int32 *Retcode);

/**
* Check for a default return value entry for the given stub function
*
* If a UT_SetDefaultReturnValue() option is in place for the given function this
* will return true and increment the internal usage counter.
*
* \param FuncKey The stub function to check the return code.
* \param Value Set to the value supplied to UT_SetDefaultReturnValue()
* \returns true if force fail mode is active
*/
bool UT_Stub_CheckDefaultReturnValue(UT_EntryKey_t FuncKey, int32 *Value);

/**
* Copies data from a test-supplied buffer to the local buffer
*
Expand Down Expand Up @@ -460,8 +455,9 @@ void UT_Stub_SetReturnValue(UT_EntryKey_t FuncKey, const void *BufferPtr, size_t
*
* \param FuncKey The stub function associated with the buffer
* \param ReturnSize Size of the return value
* \param TypeName Expected return value type, as a string
*/
void UT_Stub_RegisterReturnType(UT_EntryKey_t FuncKey, size_t ReturnSize);
void UT_Stub_RegisterReturnType(UT_EntryKey_t FuncKey, size_t ReturnSize, const char *TypeName);

/**
* Obtains direct pointer to buffer for stub return value
Expand All @@ -474,8 +470,9 @@ void UT_Stub_RegisterReturnType(UT_EntryKey_t FuncKey, size_t ReturnSize);
*
* \param FuncKey The stub function associated with the buffer
* \param ReturnSize Size of the return value
* \param TypeName Expected return value type, as a string
*/
void *UT_Stub_GetReturnValuePtr(UT_EntryKey_t FuncKey, size_t ReturnSize);
void *UT_Stub_GetReturnValuePtr(UT_EntryKey_t FuncKey, size_t ReturnSize, const char *TypeName);

/**
* Exports a value from a hook/handler and stages it to be returned to the caller.
Expand Down
Loading