IBM / nodejs-idb-connector

A JavaScript (Node.js) library for communicating with Db2 for IBM i, with support for queries, procedures, and much more. Uses traditional callback-style syntax
MIT License
38 stars 23 forks source link

Use of BINARY Parameter w/Stored Procedure Call Corrupts the Node Process #150

Open DavidRusso opened 2 years ago

DavidRusso commented 2 years ago

Describe the bug Calling a stored procedure with a binary parameter corrupts the Node process. The problem manifests itself in different ways depending on the program. For example, the process may crash with a segmentation fault.

To Reproduce Steps to reproduce the behavior:

  1. This simple example will reproduce the problem and crash the Node process with a segmentation fault.
  2. Create this SQL stored procedure on the IBM i system:
    create or replace procedure yourlib.testbin
    (
    in bin_data binary(16),
    out char_data char(1)
    )
    specific yourlib/testbin
    language sql
    set char_data = '1'
  3. Run this Node.js program:
    
    "use strict";

const db2i = require("idb-connector"); const util = require("util");

(async () => {

let dbconn; try { dbconn = new db2i.dbconn(); dbconn.conn("*LOCAL"); for (let i = 0; i < 1000; i++) console.log(i, await call()); } catch (error) { console.error(error); } finally { dbconn.disconn(); dbconn.close(); process.exit(0); }

async function call() { let dbstmt = new db2i.dbstmt(dbconn); try { await util.promisify(dbstmt.prepare).bind(dbstmt)("call yourlib.testbin(?,?)"); await util.promisify(dbstmt.bindParam).bind(dbstmt)([ [Buffer.alloc(16), db2i.IN, db2i.BINARY], [null, db2i.OUT, db2i.CHAR] ]); const output = await new Promise((resolve, reject) => { dbstmt.execute((outputParams, error) => { if (error) reject(error); else resolve(outputParams); }); }); return output; } finally { dbstmt.close(); }

}

})();

4. After usually 5-6 repetitions, the process will crash with output like this:

0 [ '1' ] 1 [ '1' ] 2 [ '1' ] 3 [ '1' ] 4 [ '1' ] 5 [ '1' ] 6 [ '1' ]

#

Fatal error in , line 0

Check failed: result.second.

# # #

FailureMessage Object: fffffffffffe510

Trace/BPT trap (core dumped)



* **Node.js version**:  v14.18.1
* **idb-connector version**: 1.2.13
* **IBM i version:**: 7.3

The same problem affects BLOB parameters.

As I mentioned above, the problem can manifest in different ways depending on the program. I originally encountered this issue in an HTTP/Express server application. In that case the process produces output like above, but doesn't crash. Instead it remains running, but appears to be stuck in a loop or something -- it runs with high CPU, stops responding to requests, and stays that way until killed with `ENDJOB`.

I'm guessing that something in `idb-connector` native code is writing into memory that doesn't belong to it, which corrupts the process in unpredictable ways.

I noticed this commit, which skips the blob/binary tests:

https://github.com/IBM/nodejs-idb-connector/commit/b02cd369e5ae45c601d127bb6ec7defd88b64547
github-actions[bot] commented 1 year ago

:wave: Hi! This issue has been marked stale due to inactivity. If no further activity occurs, it will automatically be closed.

DavidRusso commented 1 year ago

Please keep this active.

kadler commented 1 year ago

@abmusse can you please add keep-open rules to our stale bot config?