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

Integrate Iroh for dynamic code analysis #44

Open
wants to merge 6 commits into
base: f24
Choose a base branch
from
Open
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
47 changes: 47 additions & 0 deletions .github/workflows/iroh.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
name: Iroh Dynamic Analysis

on:
push:
branches: [ f24 ]
pull_request:
branches: [ f24 ]

jobs:
analyze:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3

- name: Set up Node.js
uses: actions/setup-node@v3
with:
node-version: '18'

- name: Install dependencies
run: |
npm install
npm install iroh
- name: Run tests with Iroh analysis
run: |
# Start Iroh analysis
iroh start

# Run your test command here
npm test

# Generate Iroh report
iroh report
- name: Upload analysis results
uses: actions/upload-artifact@v3
with:
name: iroh-analysis
path: .iroh/report.json

- name: Check for issues
run: |
if [ -f .iroh/issues.json ]; then
echo "Issues found in dynamic analysis"
cat .iroh/issues.json
exit 1
fi
Binary file removed dump.rdb
Binary file not shown.
1 change: 1 addition & 0 deletions install/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
"html-to-text": "9.0.5",
"imagesloaded": "5.0.0",
"ipaddr.js": "2.2.0",
"iroh": "^0.3.0",
"jquery": "3.7.1",
"jquery-deserialize": "2.0.0",
"jquery-form": "4.3.0",
Expand Down
56 changes: 56 additions & 0 deletions iroh/iroh-listener.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
'use strict';

const Iroh = require('iroh');

// from copilot
function monitorFunction(fn, name) {
console.log(`Setting up Iroh monitoring for ${name}`);

const stage = new Iroh.Stage(`
function monitored() {
try {
return target();
} catch(e) {
console.error(e);
throw e;
}
}
`);

// Using Iroh's listeners
stage.addListener(Iroh.CALL)
.on('before', (e) => {
console.log(`[IROH] ${name} called`);
e.setData('time', process.hrtime());
})
.on('after', (e) => {
const diff = process.hrtime(e.getData('time'));
const time = ((diff[0] * 1e9) + diff[1]) / 1e6;
console.log(`[IROH] ${name} took ${time}ms`);
});

// Return the original function with timing
return async function (...args) {
const start = process.hrtime();
console.log(`[START] ${name}`);

try {
const result = await fn.apply(this, args);
const [s, ns] = process.hrtime(start);
const ms = (s * 1000) + (ns / 1e6);
console.log(`[TIME] ${name} took ${ms.toFixed(2)}ms`);
console.log(`[END] ${name}`);
return result;
} catch (error) {
console.error(`[ERROR] ${name}:`, error);
throw error;
}
};
}

module.exports = {
monitorFunction: monitorFunction,
CALL: Iroh.CALL,
FUNCTION: Iroh.FUNCTION,
TRY: Iroh.TRY,
};
20 changes: 20 additions & 0 deletions src/start.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,30 @@

const nconf = require('nconf');
const winston = require('winston');
const irohMonitor = require('../iroh/iroh-listener');

const start = module.exports;
const Topics = require('./topics');
const User = require('./user');
const Posts = require('./posts');


start.start = async function () {
console.log('Initializing Iroh monitoring...');
try {
// I chose to monitor core NodeBB functions such as creating new/replying to topics, posts, and users.
if (Topics.create) Topics.create = irohMonitor.monitorFunction(Topics.create, 'Topics.create');
if (Topics.post) Topics.post = irohMonitor.monitorFunction(Topics.post, 'Topics.post');
if (Topics.reply) Topics.reply = irohMonitor.monitorFunction(Topics.reply, 'Topics.reply');
if (Posts.create) Posts.create = irohMonitor.monitorFunction(Posts.create, 'Posts.create');
if (Posts.edit) Posts.edit = irohMonitor.monitorFunction(Posts.edit, 'Posts.edit');
if (User.create) User.create = irohMonitor.monitorFunction(User.create, 'User.create');

console.log('NodeBB core functions are now set up for monitoring!!');
} catch (err) {
console.error('Failed to initialize monitoring...', err);
}

printStartupInfo();

addProcessHandlers();
Expand Down
Loading