Skip to content

Commit

Permalink
fix: dtx parsing exception when instrument service actively push data (
Browse files Browse the repository at this point in the history
  • Loading branch information
YueChen-C authored Aug 2, 2022
1 parent e0710ef commit 769face
Showing 1 changed file with 31 additions and 15 deletions.
46 changes: 31 additions & 15 deletions lib/instrument/headers.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ const DTX_MESSAGE_AUX_HEADER = 0x01F0;
const DTX_AUXILIARY_MAGIC = 0xa;

const FLAG_TYPES = Object.freeze({
push: 0,
recv: 1,
send: 2,
reply: 3
});

const AUX_TYPES = Object.freeze({
Text: 1,
NSKeyed: 2,
UInt32LE: 3,
BigUInt64LE: 4,
Expand Down Expand Up @@ -116,7 +118,7 @@ class DTXMessagePayloadHeader {

/**
* @param headerBuffer
* @returns {{flags: number, totalLength: bigint, auxLength: number}}
* @returns {DTXMessagePayloadHeaderObject}
*/
static parse (headerBuffer) {
return {
Expand Down Expand Up @@ -159,22 +161,33 @@ class DTXMessageAux {

/**
* Parses nskeyed Buffer into js array
* @param headerBuffer
* @param {Buffer} headerBuffer
* @param {DTXMessagePayloadHeaderObject} payloadHeader
* @returns {Array}
*/
static parse (headerBuffer) {
static parse (headerBuffer, payloadHeader) {
let cursor = 0;
const data = [];
const length = headerBuffer.readBigInt64LE(8);
cursor += 16;
while (cursor <= length) {
const m = headerBuffer.readUInt32LE(cursor);
if (m !== DTX_AUXILIARY_MAGIC) {
throw new Error(`incorrect auxiliary magic: ${m}`);
if (payloadHeader.flags !== FLAG_TYPES.push) {
const m = headerBuffer.readUInt32LE(cursor);
cursor += 4;
if (m !== DTX_AUXILIARY_MAGIC) {
throw new Error(`incorrect auxiliary magic: ${m}`);
}
}
const type = headerBuffer.readUInt32LE(cursor + 4);
cursor += 8;
const type = headerBuffer.readUInt32LE(cursor);
cursor += 4;
switch (type) {
case AUX_TYPES.Text: {
const strLen = headerBuffer.readUInt32LE(cursor);
cursor += 4;
data.push(headerBuffer.slice(cursor, cursor + strLen));
cursor += strLen;
break;
}
case AUX_TYPES.NSKeyed: {
const strLen = headerBuffer.readUInt32LE(cursor);
cursor += 4;
Expand Down Expand Up @@ -399,17 +412,20 @@ class DTXMessage {
return ret;
}
if (ret._payloadHeader.auxLength > 0) {
ret.auxiliaries = DTXMessageAux.parse(payloadBuf.slice(cursor, cursor + ret._payloadHeader.auxLength));
ret.auxiliaries = DTXMessageAux.parse(payloadBuf.slice(cursor, cursor + ret._payloadHeader.auxLength), ret._payloadHeader);
cursor += ret._payloadHeader.auxLength;
}

const data = payloadBuf.slice(cursor, cursor + payloadBuf.length);
for (const fun of [unarchive, plistlib.parseBuffer]) {
try {
ret.selector = fun(data);
break;
} catch (e) {
ret.selector = new InstrumentRPCParseError(data);
ret.selector = data;
if (data.length > 0) {
for (const fun of [unarchive, plistlib.parseBuffer]) {
try {
ret.selector = fun(data);
break;
} catch (e) {
ret.selector = new InstrumentRPCParseError(data);
}
}
}
return ret;
Expand Down

0 comments on commit 769face

Please sign in to comment.