Bitdefender Hypervisor Memory Introspection
structs.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2020 Bitdefender
3  * SPDX-License-Identifier: Apache-2.0
4  */
5 #include "structs.h"
6 
9  _In_reads_bytes_(Size) const void *Buffer,
10  _In_ size_t Size,
11  _Inout_updates_(Count) INT_STRUCT_INVARIANT *Invariants,
12  _In_ size_t Count,
13  _In_ BOOLEAN LogErrors,
14  _Inout_opt_ void *Context
15  )
33 {
34  size_t inv = 0;
35 
36  if (NULL == Buffer)
37  {
39  }
40 
41  if (0 == Size || Size > INT_STRUCT_MAX_SEARCH_SIZE)
42  {
44  }
45 
46  if (NULL == Invariants)
47  {
49  }
50 
51  if (0 == Count || Count > INT_STRUCT_MAX_INVARIANT_CNT)
52  {
54  }
55 
56  for (size_t lastOffset = 0; inv < Count; inv++)
57  {
59 
60  if (Invariants[inv].Offset == INT_OFFSET_NOT_INITIALIZED)
61  {
62  for (size_t i = lastOffset; i < Size; )
63  {
64  lastOffset = i;
65  status = Invariants[inv].Getter(Buffer, Size, &i, Context);
66  if (INT_SUCCESS(status))
67  {
68  Invariants[inv].Offset = lastOffset;
69  lastOffset = i;
70  break;
71  }
72  }
73  }
74  else
75  {
76  lastOffset = Invariants[inv].Offset;
77  status = Invariants[inv].Getter(Buffer, Size, &lastOffset, Context);
78  }
79 
80  if (!INT_SUCCESS(status))
81  {
82  if (LogErrors)
83  {
84  ERROR("[ERROR] Failed applying invariant %zu @ 0x%zx\n", inv, lastOffset);
85  }
86 
87  return status;
88  }
89  }
90 
91  return inv == Count ? INT_STATUS_SUCCESS : INT_STATUS_NOT_INITIALIZED;
92 }
93 
_Bool BOOLEAN
Definition: intro_types.h:58
INTSTATUS IntStructFill(const void *Buffer, size_t Size, INT_STRUCT_INVARIANT *Invariants, size_t Count, BOOLEAN LogErrors, void *Context)
Fill an internal structure with information gathered from the guest by applying a list of invariants ...
Definition: structs.c:8
#define _In_
Definition: intro_sal.h:21
#define INT_STATUS_SUCCESS
Definition: introstatus.h:54
#define INT_SUCCESS(Status)
Definition: introstatus.h:42
#define ERROR(fmt,...)
Definition: glue.h:62
int INTSTATUS
The status data type.
Definition: introstatus.h:24
#define INT_STATUS_NOT_FOUND
Definition: introstatus.h:284
#define _Inout_opt_
Definition: intro_sal.h:31
#define INT_STATUS_NOT_INITIALIZED
Definition: introstatus.h:266
#define INT_STATUS_INVALID_PARAMETER_4
Definition: introstatus.h:71
#define INT_STRUCT_MAX_INVARIANT_CNT
Upper limit of the number of invariants to be applied to a bufffer.
Definition: structs.h:56
#define _Inout_updates_(expr)
Definition: intro_sal.h:32
#define _In_reads_bytes_(expr)
Definition: intro_sal.h:25
#define INT_STRUCT_MAX_SEARCH_SIZE
Maximum size of a buffer in which to search for fields/structures.
Definition: structs.h:53
#define INT_STATUS_INVALID_PARAMETER_1
Definition: introstatus.h:62
#define INT_STATUS_INVALID_PARAMETER_2
Definition: introstatus.h:65
#define INT_OFFSET_NOT_INITIALIZED
Specifies that an offset value is yet to be searched for.
Definition: structs.h:50
#define INT_STATUS_INVALID_PARAMETER_3
Definition: introstatus.h:68