11package it .unipr .analysis ;
22
3+ import it .unipr .utils .BitManager ;
34import it .unive .lisa .analysis .BaseLattice ;
45import it .unive .lisa .analysis .Lattice ;
56import it .unive .lisa .analysis .SemanticException ;
@@ -451,11 +452,14 @@ else if (isTop() || other.isTop())
451452 return top ();
452453 else if (isTopNotJumpdest () || other .isTopNotJumpdest ())
453454 return NOT_JUMPDEST_TOP ;
454- else if (n .compareTo (new Number (256 )) >= 0 )
455- return ZERO ;
455+ else if (n .compareTo (new Number (Number . MAX_INT )) > 0 )
456+ return bottom (); // fake path
456457
457- return new StackElement (other .n .shiftLeft (this .n .getInt ()));
458+ int [] bits = BitManager .toBitArray (Number .toBigInteger (other .n ));
459+ int [] shiftedBits = BitManager .shiftLeft (bits , n .getInt ());
460+ BigInteger result = BitManager .fromBitArray (shiftedBits );
458461
462+ return new StackElement (new Number (result ));
459463 }
460464
461465 public StackElement shr (StackElement other ) {
@@ -465,10 +469,14 @@ else if (isTop() || other.isTop())
465469 return top ();
466470 else if (isTopNotJumpdest () || other .isTopNotJumpdest ())
467471 return NOT_JUMPDEST_TOP ;
468- else if (n .compareTo (new Number (256 )) >= 0 )
469- return ZERO ;
472+ else if (n .compareTo (new Number (Number .MAX_INT )) > 0 )
473+ return bottom (); // fake path
474+
475+ int [] bits = BitManager .toBitArray (Number .toBigInteger (other .n ));
476+ int [] shiftedBits = BitManager .shiftRight (bits , n .getInt ());
477+ BigInteger result = BitManager .fromBitArray (shiftedBits );
470478
471- return new StackElement (other . n . shiftRight ( this . n . getInt () ));
479+ return new StackElement (new Number ( result ));
472480 }
473481
474482 public StackElement sar (StackElement other ) {
@@ -478,126 +486,14 @@ else if (isTop() || other.isTop())
478486 return top ();
479487 else if (isTopNotJumpdest () || other .isTopNotJumpdest ())
480488 return NOT_JUMPDEST_TOP ;
481- else if (n .compareTo (new Number (256 )) > 0 )
482- return ZERO ;
483-
484- return new StackElement (
485- new Number (
486- new BigInteger (
487- StackElement .shiftArithmeticRight (
488- other .n .toByteArray (),
489- this .n .getInt ()))));
490- }
491-
492- /**
493- * Shifts the given byte array to the left by the specified number of bits.
494- *
495- * @param byteArray The byte array to be left-shifted.
496- * @param shiftBitCount The number of bits by which to shift the byte array
497- * to the left.
498- *
499- * @return The resulting byte array after left-shifting by the specified bit
500- * count.
501- * <p>
502- * This method performs a left shift on the provided byte array,
503- * where each byte is shifted to the left by the given number of
504- * bits. The shift operation is performed in a bitwise manner,
505- * and the bits shifted beyond the byte boundary are wrapped
506- * around to the opposite end. The shift is done in place, and
507- * the modified byte array is returned as the result.
508- * </p>
509- * <p>
510- * The {@code shiftBitCount} parameter determines the number of
511- * bits to shift.
512- * </p>
513- *
514- * @throws IllegalArgumentException If the input {@code byteArray} is
515- * {@code null}.
516- */
517- public static byte [] shiftLeft (byte [] byteArray , int shiftBitCount ) {
518- final int shiftMod = shiftBitCount % 8 ;
519- final byte carryMask = (byte ) ((1 << shiftMod ) - 1 );
520- final int offsetBytes = (shiftBitCount / 8 );
489+ else if (n .compareTo (new Number (Number .MAX_INT )) > 0 )
490+ return bottom (); // fake path
521491
522- int start ;
492+ int [] bits = BitManager .toBitArray (Number .toBigInteger (other .n ));
493+ int [] shiftedBits = BitManager .arithmeticShiftRight (bits , n .getInt ());
494+ BigInteger result = BitManager .fromBitArray (shiftedBits );
523495
524- if (byteArray .length > 32 )
525- start = 1 ;
526- else
527- start = 0 ;
528-
529- int sourceIndex ;
530- for (int i = start ; i < byteArray .length ; i ++) {
531- sourceIndex = i + offsetBytes ;
532- if (sourceIndex >= byteArray .length ) {
533- byteArray [i ] = 0 ;
534- } else {
535- byte src = byteArray [sourceIndex ];
536- byte dst = (byte ) (src << shiftMod );
537- if (sourceIndex + 1 < byteArray .length ) {
538- dst |= byteArray [sourceIndex + 1 ] >>> (8 - shiftMod ) & carryMask ;
539- }
540- byteArray [i ] = dst ;
541- }
542- }
543- return byteArray ;
544- }
545-
546- /**
547- * Shifts the bits of the given byte array towards the least significant bit
548- * (SAR - Shift Arithmetic Right). The bits moved before the first one are
549- * discarded, and the new bits are set to 0 if the previous most significant
550- * bit was 0; otherwise, the new bits are set to 1.
551- *
552- * @param byteArray The byte array to be right-shifted.
553- * @param shiftBitCount The number of bits by which to shift the byte array
554- * to the right.
555- *
556- * @return The resulting byte array after right-shifting by the specified
557- * bit count.
558- * <p>
559- * This method performs a right shift on the provided byte array
560- * (SAR operation), where each byte is shifted to the right by
561- * the given number of bits. The shift operation is performed in
562- * a bitwise manner, and the bits shifted beyond the byte
563- * boundary are discarded. The new bits introduced during the
564- * shift are set based on the value of the previous most
565- * significant bit (0 or 1).
566- * </p>
567- * <p>
568- * The {@code shiftBitCount} parameter determines the number of
569- * bits to shift.
570- * </p>
571- *
572- * @throws IllegalArgumentException If the input {@code byteArray} is
573- * {@code null}.
574- */
575- public static byte [] shiftArithmeticRight (byte [] byteArray , int shiftBitCount ) {
576- final int shiftMod = shiftBitCount % 8 ;
577- final byte carryMask = (byte ) (0xFF << (8 - shiftMod ));
578- final int offsetBytes = (shiftBitCount / 8 );
579-
580- int sourceIndex ;
581- int start ;
582-
583- if (byteArray .length > 32 )
584- start = 1 ;
585- else
586- start = 0 ;
587- for (int i = start ; i < byteArray .length ; i ++) {
588- sourceIndex = i + offsetBytes ;
589- if (sourceIndex >= byteArray .length ) {
590- byteArray [i ] = (byte ) (byteArray [i ] < 0 ? 0xFF : 0 );
591- } else {
592- byte src = byteArray [sourceIndex ];
593- byte dst = (byte ) (src >>> shiftMod );
594- if (sourceIndex + 1 < byteArray .length ) {
595- dst |= byteArray [sourceIndex + 1 ] << (8 - shiftMod ) & carryMask ;
596- }
597- byteArray [i ] = dst ;
598- }
599- }
600- return byteArray ;
496+ return new StackElement (new Number (result ));
601497 }
602498
603499 /**
0 commit comments