Synchronous and asynchronous scanning

All scanning functions support two scanning modes—synchronous and asynchronous. In asynchronous mode scanning function returns immediately after passing an object (a file or a memory block) to KAV Engine. In synchronous mode the function waits until the object is completely scanned by KAV Engine or possibly disinfected if the latter is possible.

Depending on the mode, you get the scan result right back to the scanning function, or result is returned through a custom callback handler function.

In synchronous mode scan result is returned to the Result variable. (This variable has a custom KAV_RESULT enumeration type defined within the kave8_def.h header file.)

The Result variable is not only critical for receiving results of scanning, but it is also the key that sets the scanning mode for scanning function.

When invoking the scanning function, you initialize the Result parameter to specify the mode to be used by the scanning function.

If the parameter does not have the zero (or NULL) value (and you pass the pointer to the variable's memory address), the scanning function will use synchronous mode: when the scanning function returns, the scan result is stored at this address. If the Result parameter is set to zero (no memory address is specified), the scanning function will run in asynchronous mode.

Synchronous mode

In synchronous mode, the scanning function is synchronized to the caller thread of the user application that invoked the scanning function. The called scanning function does not return until the file has been fully scanned and the scan result is available.

Working in synchronous mode has its specifics peculiar to the run mode. Whether your application works in the in-process mode, out-of-process mode, or hosts the KAV Engine as a service, it matters.

While in in-process mode (the dwScannerHostType parameter of the kaveInitialize function is set to KAV_SHT_INPROC), your application manages the scanning threads and your calling thread is re-used for scanning. In other words, scanning is performed in the context of your process so that the KAV Engine has the same security permissions as your application's process.

It is quite the contrary with the out-of-process mode (the dwScannerHostType parameter of the kaveInitialize function is set to KAV_SHT_OUTPROC). This mode assumes that your process runs in its own address space. As a result, the calling thread and the scanning thread are running in different contexts and thus may have different permissions. When your application requests scanning of a file by calling a scanning function from its thread, the calling thread enters the wait state. Scanning of files runs in a separate thread within the separate context of the kavehost process.

The service mode (the dwScannerHostType parameter of the kaveInitialize function is set to KAV_SHT_SERVICE) combines features specific to the two other modes. In this mode the KAV Engine is hosted by a dedicated service hosting process (such as svchost.exe in Windows environment). Because the hosting process may run under different permissions than your application, scanning is performed with the permissions assigned to the token of the user account used to run your application. In this mode scanning is performed in a separate thread similar to scanning behavior in the out-of-process mode.

Synchronous scanning mode can enabled for the scanning function by passing a non-zero value to the Result parameter of the scanning function. The following snippet demonstrates the way you can start scanning of a memory block in synchronous mode:

Example:

DWORD dwScanningMode = KAV_O_M_PACKED |

KAV_O_M_ARCHIVED |

KAV_O_M_MAILPLAIN |

KAV_O_M_HEURISTIC_LEVEL_SHALLOW;

unsigned long dwCleaningMode = KAV_CLEAN_SKIP;

char szEICAR[] = "CURE-X5O!P%@AP[4\\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*";

char szContextString[] = "DummyContext";

KAV_RESULT Result;

HRESULT hr = kaveScanMemory((BYTE*)szEICAR, (DWORD)strlen(szEICAR), 1, dwScanningMode, dwCleaningMode, INFINITE, (void*)szContextString, NULL, &Result);

Asynchronous mode

In asynchronous mode, the scanning function returns immediately. When the scanning function is invoked in asynchronous mode, scanning tasks are added to the task queue. The KAV SDK adds the scan task to the task queue and immediately releases the scanning function by returning control back to the user application's thread that called the scanning function.

You can enable the asynchronous mode for the scanning function by passing a zero (NULL) value to the Result parameter of the scanning function. The following snippet demonstrates the way you can start scanning of a file in asynchronous mode:

Example:

unsigned long dwScanningMode = KAV_O_M_PACKED |

KAV_O_M_ARCHIVED |

KAV_O_M_MAILBASES |

KAV_O_M_MAILPLAIN |

KAV_O_M_ICHECKER |

KAV_O_M_ISWIFT |

KAV_O_M_HEURISTIC_LEVEL_SHALLOW |

KAV_O_M_NTFS_ADS;

unsigned long dwCleaningMode = KAV_CLEAN_DELETE;

DWORD dwObjectId;

char szFileName[] = "path_to_a_file_to_scan";

HRESULT hr = kaveScanFile(szFileName, 4, dwScanningMode, dwCleaningMode, INFINITE, NULL, &dwObjectId, 0);

If adding the task to the task queue succeeds, scanning function returns the S_OK system code (see section "Return codes"). If adding fails, an error code such as E_FAIL is returned, or it may be some of the custom error codes, such as those from the KAV_E_LICENSE_* set. For more information about system and custom return codes, see section "Return codes".

When the scanning task is added to the task queue, it is then handled by the KAV Engine, and the file or memory block is scanned for malware. The result code, one of the scan results, is returned by KAV Engine only when the KAV Engine has finished processing the task.

Please make sure you ensure the data being scanned is preserved on a disk or in memory — depending on a scanning function — until you receive the EVEN_RESULT code (see section "Event notification codes") from callback function.

For example, when scanning a memory block with the kaveScanMemory scanning function, please make sure that you keep the contents of the memory block, which is to be scanned, preserved until the block is fully scanned and the KAV Engine returns the result scan code. The KAV SDK does not copy original memory block or file that have been passed for scanning. Do not call new()/delete() operators against the memory block that you pass for scanning until you receive the EVENT_RESULT code in the dwEvent parameter of the callback function.

Please do keep this in mind when passing files to the kaveScanFile function. No files passed for scanning from your application to the kaveScanFile function should be removed or modified before the KAV Engine finished scanning them and the EVENT_RESULT code is received by your application from the callback function. Please avoid writing to a file while it is being scanned. This also applies to working with the kaveScanForRootkitsInFolder function—do not delete or modify any file within the folder, which is being scanned for rootkits, or any of its subfolders.

When working with the kaveScanStream function, please be sure to not alter thread data while the file is being scanned. Never invoke new() or delete() functions for the memory segment allocated for the byte stream until you receive EVENT_RESULT. Avoid using functions declared in IStream interface for the thread in use, unless you provide for synchronization and manage pointers on your side. Trying to alter a thread using, for example, IStream::Write() or IStream::SetSize() method while scanning is active will likely cause a fault.

Follow these guidelines when working with every scanning function (see section "Scanning functions") that supports asynchronous mode.

Because in asynchronous mode scanning function runs asynchronously with the calling thread of your application, the scanning result is only available via the callback function—this function should be implemented during the KAV SDK integration process. Please use the provided prototype (see section "Callback function prototype") when implementing your callback function. For more information about callback functions, see section "Callback function" in the KAV SDK API Reference.

When the scanning function is released, and the KAV Engine finishes processing the added scanning task, the result code is retrieved by the KAV SDK from the KAV Engine. The KAV SDK calls the callback function and fills its fields with the result. The result is returned to the dwParam1 parameter of the callback function, and, depending on the callback event, to the dwParam2 field. The dwParam1 field gets filled with one of the callback recodes, which are different depending on the value of the dwEvent field. The dwParam2 field typically contains some additional information related to the information in the dwParam1 and the dwEvent fields. The dwEvent parameter of the callback function allows you to associate the retrieved scanning result with the type of action performed by the KAV Engine. For more information about callback events, see section "Event notification codes".

Please note that values returned to dwParam1 and dwParam2 fields are tied to the raised event, that is to the value of the dwEvent parameter returned by the KAV Engine together with the values for dwParam1 and dwParam2. To find what values you may expect to retrieve in dwParam1 and dwParam2 fields of the callback function for a specific value of the dwEvent field, see the "Event notification codes" table listed in section "Event notification codes".

When KAV Engine calls the specified callback function and sets the dwEvent field to EVENT_RESULT code, this action indicates processing the scanning task has been finished. If this is the case, results returned by KAV Engine to dwParam1 are called scanning results. Available scanning results are listed in section "Scan results". Additionally, you may look for the information provided for the EVENT_RESULT code in the "Event notification codes" table listed in section "Event notification codes".

When, for example, the callback function is called with the EVENT_SUSPECTED event code, or any other of the codes listed in section "Detection types and severity", the dwParam1 field is filled with the code specifying the type of malicious software found in the scanned file or memory block. The dwParam2 field at this time is set with a severity level of the found threat, which is listed in the "Severity level types" table.

Indeed, if dwEvent is set to EVENT_VERDICT_CURABILITY, which signalizes that KAV Engine has determined if it is possible to clean the infected file off of malware, values, returned to the dwParam1 field, specify whether the scanned file can be disinfected or not.

Summarizing, what is important is that values of dwParam1 and dwParam2 depend on the value of dwEvent.

The following example illustrates which fields can be filled as a result of a typical scanning procedure performed in asynchronous mode. This is an illustration of what could be a part of sequence you may see when the EVENT_RESULT code is returned to the dwEvent. Each scanning procedure is a sequence of calls made by the KAV Engine to the callback function. As a result, there is typically a series of events raised during the scanning procedure, each of the events leading to specific values of dwParam1, dwParam2 and other fields of the callback handler.

Example:

Consider the following example. A scanning function is called from a thread of your application in the asynchronous mode. The scanning task is successfully added to the task queue and the KAV Engine finishes scanning stating that a malware has been found in the file or memory block passed for scanning via the scanning function. No additional data is passed from your application to the scanning function.

The callback function is called by the KAV SDK with the event indicating that the KAV Engine has successfully finished scanning of a file or memory block in asynchronous mode, and a malware has been detected.

In the following example, the EVENT_RESULT event returned by the KAV Engine indicates that the KAV Engine has finished scanning and prepared the result. Should you have, say, the EVENT_CORRUPTED returned to your callback function, you would know that the KAV Engine has verified that the scanned file is corrupted.

 

Please note that the EVENT_RESULT event is always raised when the scanning procedure is finished. This is only valid for on-demand scanning (see section "Scanning") when scanning files or memory using a scanning function (see section "Scanning functions"). The event does not appear during real-time protection procedure (see section "Introducing Real Time Protection"), performed by any of the supported monitors (see section "Monitoring"). Typical scanning procedure is a sequence of events. Before the EVENT_RESULT is raised for a scanned file or memory object, several events may be raised, including the one referring to the EVENT_CORRUPTED event code.

 

  • The dwEvent field of your callback function is filled with the EVENT_RESULT event.
  • The dwParam1 field gets filled with the KAV_S_R_INFECTED return code indicating that processed file or memory block contains a malware.

    In cleaning mode (see section "Cleaning modes") the dwParam1 field may be filled by KAV Engine with the KAV_S_R_DISINFECTED code.

  • The variable pointed by the pObjectName is filled with the path to the scanned file passed from your application to the scanning function.
  • If you passed the path to the file that has been recognized by the KAV Engine as an archive, and malicious code has been found in a file contained within this archive, the KAV Engine returns a path relative to the parent file. For the intermediate events in a sequence of events, pObjectName parameter will be pointing to the path related to the scanned archive and not to the archive itself. When the dwEvent is set to EVENT_RESULT, the pObjectName points to the context associated with the archive.

     

  • Because the EVENT_RESULT does not support any additional data except for what is returned to the dwParam1 field, dwParam2 will contain 0.
  • The variable pointed by the pVirusName is filled with the name of the found malware as found in the anti-virus database that has been used by the KAV Engine for scanning of the file or memory block.
  • The dwObjectID field is filled with the scan task ID, and identifier of the scanning task associated with the scanned file or memory block.

    This identifier allows you to keep track of processing of the scanning task. The task ID is automatically generated by Kaspersky Anti-Virus Software Development Kit when the task gets added to the task queue.

    This parameter and is automatically generated by the KAV Engine when the task gets added to the task queue.

  • The dwMonitorID parameter will contain 0, certifying that scanning has been initiated by the scanning function.
  • The pUserContext parameter of the callback function will point to 0, because no additional data has been passed from your application and corresponding pUserContext argument of the scanning function has been initialized to zero. This user data is associated with the scanned context (that is with the file or memory block processed by the KAV Engine).

    In other words, we get a zero value for pUserContext on the output because we passed zero to the input.

Advantages and pitfalls

Each of the modes has its pros and cons that are worth keeping in mind when calling the scanning function with the pResult parameter set to 0 or a non-zero value.

The advantage of the synchronous mode is that you get the real scan result right from the scanning function. This effectively means that you can always be informed of the exact result right from the function and right after the KAV Engine finished processing of a file or a memory block passed to the scanning function from your application.

The negative side of this approach is that your process' thread has to wait all the time the KAV Engine processes the file until the scanning returns with the result. You cannot proceed with the following tasks by your thread until control is returned from the KAV SDK back to your application.

The advantage of using the asynchronous mode is seen in scanning parallelism. In this mode you can scan multiple files or memory objects at the same time without any extra effort. Your application initializes the Result to zero and multitasking is performed by the KAV SDK.

This possibility for multiprocessing lies on the fact that calling thread of your application is no longer synchronized to the scanning thread. That is, your application can return to processing another task as soon as the current task is added to the task queue.

You can do processing with the released thread that would wait for the KAV Engine's return if you used the synchronous mode, because the calling thread of your application would wait for the scanning function return, which, it its turn, would wait for the result from the KAV Engine. When in asynchronous mode, the scanning function returns immediately after the KAV Engine returned the task identifier, the value of the dwObjectID parameter that can be further used to cancel processing of the added task. For more information about how you can cancel processing of a scanning task, see kaveCancelScan.

What is the drawback of asynchronous mode compared to synchronous mode? It refers to the information that you receive from the scanning function.

In synchronous mode you get the return code (see section "Return codes"), which has the HRESULT type on Windows platform, certifying that execution of scanning function is finished. At the same time this function holds information about the scanning result (see section "Scan results"), which is returned at the address pointed by the pResult parameter of the scanning function and has a custom KAV_RESULT type. You get both the return code, a success code or an error, and the scan result immediately and this is done in exchange for the time which takes the KAV Engine to finish scanning.

In asynchronous mode the scanning function returns the result of execution, the return code (see section "Return codes") of the HRESULT type, immediately after the scanning task is added to the task queue. Similar to running in synchronous mode you get the return code once function is finished execution. On the other hand, this result is returned yet before scanning is done. The difference with asynchronous mode is how you get the scanning result.

Scanning result is returned only after KAV Engine finishes processing the task. The result is passed through callback function because by the time KAV Engine gets the scanning result, the function has already returned. You always get the S_OK return code from the scanning function when the scanning task is successfully added to the task queue. The problem here is that this return code only certifies that the task has been successfully added to the task queue, neither more nor less. By no means does this result code represent the actual scanning result code (see section "Scan results"), which is returned by the KAV Engine when the added task is processed by the KAV Engine. This scan result specifies the status of the processed task (that is a scanned file or a scanned memory block).

Page top