置换python2.7.13的opcode遇到的一些坑

主要有两个坑

1.XXXSLICE相关的opcode

#define SLICE      
#define SLICE_1    
#define SLICE_2    
#define SLICE_3    

#define STORE_SLICE
#define STORE_SLICE_1  
#define STORE_SLICE_2  
#define STORE_SLICE_3  

#define DELETE_SLICE   
#define DELETE_SLICE_1 
#define DELETE_SLICE_2 
#define DELETE_SLICE_3  

由于编译和运行的时候,这些宏定义是连续的,所以python源代码中会以SLICE+1,opcode - SLICE & 1等方式进行操作

如果置换的时候不连续,就会出问题

2.CALL_FUNCTIONXXX相关的opcode

/* The next 3 opcodes must be contiguous and satisfy
   (CALL_FUNCTION_VAR - CALL_FUNCTION) & 3 == 1  */
#define CALL_FUNCTION_VAR            /* #args + (#kwargs<<8) */
#define CALL_FUNCTION_KW           /* #args + (#kwargs<<8) */
#define CALL_FUNCTION_VAR_KW        /* #args + (#kwargs<<8) */

与1问题类似,ceval.c中出现(opcode - CALL_FUNCTION) & 3,所以也必须保证(CALL_FUNCTION_VAR - CALL_FUNCTION) & 3 == 1,且这三个宏连续

其他的参考https://zhuanlan.zhihu.com/p/25850970即可

原文地址:https://www.cnblogs.com/howmp/p/6754573.html