Studio:Integrating the Intercept Module (IM): Difference between revisions

From STRIDE Wiki
Jump to navigation Jump to search
Line 26: Line 26:


=== Windows Implementation ===
=== Windows Implementation ===
This code snippet for Windows is based on using the Win32 API calls. This code is currently mainly for use in [http://en.wikipedia.org/wiki/Windows_ce Windows CE] and [http://en.wikipedia.org/wiki/Windows_Mobile Windows Mobile]. For building STRIDE enabled applications on standard desktop Windows, please refer to [Windows Off-Target SDK].
This code snippet for Windows is based on using the Win32 API calls. This code is currently mainly for use in [http://en.wikipedia.org/wiki/Windows_ce Windows CE] and [http://en.wikipedia.org/wiki/Windows_Mobile Windows Mobile]. For building STRIDE enabled applications on standard desktop Windows, please refer to [[Windows Off-Target SDK]].


<source lang="c">
<source lang="c">

Revision as of 20:41, 15 October 2008

About Intercept Modules

Intercept Modules allow remote function calls across hardware (e.g., processor or machine) or software (operating system) platform boundaries. Intercept Modules are target-based components that are created as a step in the STRIDE build process. The Intercept Module, or IM, is created based on selected interfaces or test units that have been "captured", or identified through a subset of SCL pragmas that tag interfaces as candidates for remote function calls. Once created, the generated code is then compiled, linked and run along with the target application.


Activating the Intercept Module

Compiling the IM

To compile the IM, you must define the macro srIMON when you compile the IM.c file or any file that includes one of the headers. This "turns on" the intercept feature. For example:

 cc -c -IC:\STRIDE\inc -DsrIMON MyProjectIM.c

Notice that the IM.c and IMEntry.h files reference header files in the STRIDE installation directory. The build must include this directory when compiling either of these files. The Delegate (IM.h) header file does not have any dependencies on this directory.

IM Resource Requirements

The Intercept Module is usually run as a separate task, and therefore has some resource requirements that are in addition to those of the Runtime and the PAL. One of these is the task resource itself. Another is space for the task stack.

The required task stack size if difficult to predict since it is dependent on the underlying system and on the data that is passed through function arguments. A stack size between 2-4K would be a good starting point.

Starting the srThread and IMStubRead threads

The IM must be started after the Runtime and Transport have been initialized, but before any intercepted calls can be made. Failure to do this before making an intercepted call can crash or hang the target.

This code snippet for Linux, based on using the POSIX call pthread_create() to start a task (your OS may use a different call), demonstrates how to initialize the PAL and the Runtime, start the IM, then becomes the Runtime message processing loop. Note that this code assumes that it will become the Runtime thread's message processing loop (because srThread() will never return unless thread is stopped or killed). The comments note that this could instead create a second task for the Runtime and return.

Linux Implementation

See Linux PAL for examples of integrating your linux application with the STRIDE Runtime.

Windows Implementation

This code snippet for Windows is based on using the Win32 API calls. This code is currently mainly for use in Windows CE and Windows Mobile. For building STRIDE enabled applications on standard desktop Windows, please refer to Windows Off-Target SDK.

#include "stdafx.h"
#include <windows.h>
#include <commctrl.h>

#include <sr.h>
#include <palcfg.h>
#include <grs.h>
#include "MyProjectIMEntry.h"

static bool bUseSerial = false;

struct ThreadInfo
{
    HANDLE handle;
    DWORD id;
};

typedef void (*_ThreadFunc_t)( void );

static DWORD WINAPI _threadFunc(LPVOID param)
{
    if (param)
    {
        _ThreadFunc_t proc = (_ThreadFunc_t)param;
        proc();
    }

    return 0;
}

int _tmain(int argc, _TCHAR* argv[])
{
    ThreadInfo threads[15] = {{NULL, 0}};
    int i = 0;

    // Initialize the PAL
    if (palOSInit() != palTRUE) {
        palLog(palLOG_LEVEL_ERROR, "palOSInit FAILED.");
        return -1;
    }

    pal_io_config_t cfg;
    pal_prot_t protocol;
    if (bUseSerial) {
        protocol = PAL_PROT_SERIAL;
        cfg.u.serial.BaudRate = PAL_SERIAL_BAUDRATE;
        cfg.u.serial.ByteSize = PAL_SERIAL_BYTESIZE;
        cfg.u.serial.ComPort = PAL_SERIAL_PORT;
        cfg.u.serial.Parity = PAL_SERIAL_PARITY;
        cfg.u.serial.StopBits = PAL_SERIAL_STOPBITS;
    }
    else {
        protocol = PAL_PROT_TCP;
        cfg.u.tcp.Port = PAL_DEFAULT_TCP_PORT;
    }

    // Initialize tcp transport 
    if (palIOInit(protocol, &cfg) != palTRUE) {
        palLog(palLOG_LEVEL_ERROR, "palIOInit FAILED.");
        return -1;
    }

    // Initialize Runtime 
    if (srInit() != srOK) {
        palLog(palLOG_LEVEL_ERROR, "srInit FAILED.");
        return -1;
    }

    // Initialize GRS 
    if (grsInit() != grsTRUE) {
        palLog(palLOG_LEVEL_ERROR, "grsInit FAILED.");
        return -1;
    }

    // Start the Runtime thread first
    threads[i].handle = CreateThread(NULL, 0, _threadFunc, srThread, 0, &threads[i].id);
    if (!threads[i++].handle) {
        palLog(palLOG_LEVEL_ERROR, "CreateThread FAILED for Runtime Thread.");
        return -1;
    }

    // Start the IM stub read thread
    threads[i].handle = CreateThread(NULL, 0, _threadFunc, MyProjectIMStubThread, 0, &threads[i].id);
    if (!threads[i++].handle) {
        palLog(palLOG_LEVEL_ERROR, "CreateThread FAILED for IM Thread.");
        return -1;
    }

    // Wait for Runtime thread
    WaitForSingleObject(threads[0].handle, INFINITE);

    fprintf (stdout, "TestAgent exiting.\n");
    for (i = sizeof(threads)/sizeof(threads[0]); i > 0; i--)
    {
        if (threads[i-1].handle != NULL)
        {
            palNotify(threads[i-1].id, palSTOP_EVENT);
            WaitForSingleObject(threads[i-1].handle, INFINITE);
            CloseHandle(threads[i-1].handle);
        }
    }

    grsUninit();
    srUninit();
    palOSUninit();
    return 0;
}