diamond-detect 0.1.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/dist/cli.js +79 -1
  2. package/package.json +1 -1
package/dist/cli.js CHANGED
@@ -217,6 +217,7 @@ function collectSlotConstants(ctx) {
217
217
  if (!isBytes32Constant(node)) return;
218
218
  const namespace = extractKeccakStringArg(node.value);
219
219
  if (namespace === null) return;
220
+ const declarationId = typeof node.id === "number" ? node.id : -1;
220
221
  const variableName = node.name ?? "<anon>";
221
222
  const contract = declaringContract(parents) ?? artifact.contractName;
222
223
  const src = node.src;
@@ -224,6 +225,7 @@ function collectSlotConstants(ctx) {
224
225
  if (seen.has(dedupeKey)) return;
225
226
  seen.add(dedupeKey);
226
227
  out.push({
228
+ declarationId,
227
229
  variableName,
228
230
  namespace,
229
231
  slot: keccak256Hex(namespace),
@@ -235,12 +237,88 @@ function collectSlotConstants(ctx) {
235
237
  }
236
238
  return out;
237
239
  }
240
+ function collectSlotAssignmentValueSrcs(yulNode, srcs) {
241
+ if (!yulNode || typeof yulNode !== "object") return;
242
+ const node = yulNode;
243
+ if (node.nodeType === "YulAssignment") {
244
+ const targets = node.variableNames;
245
+ const targetsSlot = Array.isArray(targets) && targets.some((t) => typeof t.name === "string" && t.name.endsWith(".slot"));
246
+ if (targetsSlot) {
247
+ const value = node.value;
248
+ if (value?.nodeType === "YulIdentifier" && typeof value.src === "string") {
249
+ srcs.add(value.src);
250
+ }
251
+ }
252
+ }
253
+ for (const key of Object.keys(node)) {
254
+ const v = node[key];
255
+ if (Array.isArray(v)) {
256
+ for (const child of v) collectSlotAssignmentValueSrcs(child, srcs);
257
+ } else if (v && typeof v === "object") {
258
+ collectSlotAssignmentValueSrcs(v, srcs);
259
+ }
260
+ }
261
+ }
262
+ function collectSlotUsedDeclarationIds(artifacts) {
263
+ const ids = /* @__PURE__ */ new Set();
264
+ for (const artifact of artifacts) {
265
+ if (!artifact.ast) continue;
266
+ walkAst(artifact.ast, (node) => {
267
+ if (node.nodeType !== "InlineAssembly") return;
268
+ const yulAst = node.AST;
269
+ if (!yulAst) return;
270
+ const slotValueSrcs = /* @__PURE__ */ new Set();
271
+ collectSlotAssignmentValueSrcs(yulAst, slotValueSrcs);
272
+ if (slotValueSrcs.size === 0) return;
273
+ const refs = node.externalReferences;
274
+ if (!Array.isArray(refs)) return;
275
+ for (const ref of refs) {
276
+ if (typeof ref.src === "string" && slotValueSrcs.has(ref.src) && typeof ref.declaration === "number") {
277
+ ids.add(ref.declaration);
278
+ }
279
+ }
280
+ });
281
+ }
282
+ return ids;
283
+ }
284
+ function collectAliases(artifacts) {
285
+ const aliases = /* @__PURE__ */ new Map();
286
+ for (const artifact of artifacts) {
287
+ if (!artifact.ast) continue;
288
+ walkAst(artifact.ast, (node) => {
289
+ if (node.nodeType !== "VariableDeclarationStatement") return;
290
+ const initialValue = node.initialValue;
291
+ if (initialValue?.nodeType !== "Identifier") return;
292
+ const referenced = initialValue.referencedDeclaration;
293
+ if (typeof referenced !== "number") return;
294
+ const decls = node.declarations;
295
+ if (!Array.isArray(decls)) return;
296
+ for (const d of decls) {
297
+ if (typeof d?.id === "number") aliases.set(d.id, referenced);
298
+ }
299
+ });
300
+ }
301
+ return aliases;
302
+ }
303
+ function isUsedAsSlot(constantId, slotUsedIds, aliases) {
304
+ if (constantId < 0) return false;
305
+ if (slotUsedIds.has(constantId)) return true;
306
+ for (const [aliasId, refId] of aliases) {
307
+ if (refId === constantId && slotUsedIds.has(aliasId)) return true;
308
+ }
309
+ return false;
310
+ }
238
311
  var diamondStorageAnalyzer = {
239
312
  name: "diamond-storage-namespace",
240
313
  run(ctx) {
241
314
  const constants = collectSlotConstants(ctx);
315
+ const slotUsedIds = collectSlotUsedDeclarationIds(ctx.artifacts);
316
+ const aliases = collectAliases(ctx.artifacts);
317
+ const slotConstants = constants.filter(
318
+ (c) => isUsedAsSlot(c.declarationId, slotUsedIds, aliases)
319
+ );
242
320
  const bySlot = /* @__PURE__ */ new Map();
243
- for (const c of constants) {
321
+ for (const c of slotConstants) {
244
322
  const list = bySlot.get(c.slot) ?? [];
245
323
  list.push(c);
246
324
  bySlot.set(c.slot, list);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "diamond-detect",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
4
4
  "description": "Static analyzer for EIP-2535 Diamond storage-slot collisions across facets",
5
5
  "homepage": "https://github.com/jayeshy14/Diamond-Storage-Detector#readme",
6
6
  "repository": {