Home | History | Annotate | Download | only in sanitizer_common
      1 //===-- sanitizer_common_interceptors_ioctl.inc -----------------*- C++ -*-===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is distributed under the University of Illinois Open Source
      6 // License. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 //
     10 // Ioctl handling in common sanitizer interceptors.
     11 //===----------------------------------------------------------------------===//
     12 
     13 #include "sanitizer_flags.h"
     14 
     15 struct ioctl_desc {
     16   unsigned req;
     17   // FIXME: support read+write arguments. Those are currently marked as WRITE.
     18   enum {
     19     NONE,
     20     READ,
     21     WRITE,
     22     CUSTOM
     23   } type : 2;
     24   unsigned size : 30;
     25   const char* name;
     26 };
     27 
     28 const unsigned ioctl_table_max = 500;
     29 static ioctl_desc ioctl_table[ioctl_table_max];
     30 static unsigned ioctl_table_size = 0;
     31 
     32 // This can not be declared as a global, because references to struct_*_sz
     33 // require a global initializer. And this table must be available before global
     34 // initializers are run.
     35 static void ioctl_table_fill() {
     36 #define _(rq, tp, sz)                                    \
     37   if (IOCTL_##rq != IOCTL_NOT_PRESENT) {                 \
     38     CHECK(ioctl_table_size < ioctl_table_max);           \
     39     ioctl_table[ioctl_table_size].req = IOCTL_##rq;      \
     40     ioctl_table[ioctl_table_size].type = ioctl_desc::tp; \
     41     ioctl_table[ioctl_table_size].size = sz;             \
     42     ioctl_table[ioctl_table_size].name = #rq;            \
     43     ++ioctl_table_size;                                  \
     44   }
     45 
     46   _(FIOASYNC, READ, sizeof(int));
     47   _(FIOCLEX, NONE, 0);
     48   _(FIOGETOWN, WRITE, sizeof(int));
     49   _(FIONBIO, READ, sizeof(int));
     50   _(FIONCLEX, NONE, 0);
     51   _(FIOSETOWN, READ, sizeof(int));
     52   _(SIOCADDMULTI, READ, struct_ifreq_sz);
     53   _(SIOCATMARK, WRITE, sizeof(int));
     54   _(SIOCDELMULTI, READ, struct_ifreq_sz);
     55   _(SIOCGIFADDR, WRITE, struct_ifreq_sz);
     56   _(SIOCGIFBRDADDR, WRITE, struct_ifreq_sz);
     57   _(SIOCGIFCONF, CUSTOM, 0);
     58   _(SIOCGIFDSTADDR, WRITE, struct_ifreq_sz);
     59   _(SIOCGIFFLAGS, WRITE, struct_ifreq_sz);
     60   _(SIOCGIFMETRIC, WRITE, struct_ifreq_sz);
     61   _(SIOCGIFMTU, WRITE, struct_ifreq_sz);
     62   _(SIOCGIFNETMASK, WRITE, struct_ifreq_sz);
     63   _(SIOCGPGRP, WRITE, sizeof(int));
     64   _(SIOCSIFADDR, READ, struct_ifreq_sz);
     65   _(SIOCSIFBRDADDR, READ, struct_ifreq_sz);
     66   _(SIOCSIFDSTADDR, READ, struct_ifreq_sz);
     67   _(SIOCSIFFLAGS, READ, struct_ifreq_sz);
     68   _(SIOCSIFMETRIC, READ, struct_ifreq_sz);
     69   _(SIOCSIFMTU, READ, struct_ifreq_sz);
     70   _(SIOCSIFNETMASK, READ, struct_ifreq_sz);
     71   _(SIOCSPGRP, READ, sizeof(int));
     72   _(TIOCCONS, NONE, 0);
     73   _(TIOCEXCL, NONE, 0);
     74   _(TIOCGETD, WRITE, sizeof(int));
     75   _(TIOCGPGRP, WRITE, pid_t_sz);
     76   _(TIOCGWINSZ, WRITE, struct_winsize_sz);
     77   _(TIOCMBIC, READ, sizeof(int));
     78   _(TIOCMBIS, READ, sizeof(int));
     79   _(TIOCMGET, WRITE, sizeof(int));
     80   _(TIOCMSET, READ, sizeof(int));
     81   _(TIOCNOTTY, NONE, 0);
     82   _(TIOCNXCL, NONE, 0);
     83   _(TIOCOUTQ, WRITE, sizeof(int));
     84   _(TIOCPKT, READ, sizeof(int));
     85   _(TIOCSCTTY, NONE, 0);
     86   _(TIOCSETD, READ, sizeof(int));
     87   _(TIOCSPGRP, READ, pid_t_sz);
     88   _(TIOCSTI, READ, sizeof(char));
     89   _(TIOCSWINSZ, READ, struct_winsize_sz);
     90 
     91 #if (SANITIZER_LINUX && !SANITIZER_ANDROID) || SANITIZER_MAC
     92   _(SIOCGETSGCNT, WRITE, struct_sioc_sg_req_sz);
     93   _(SIOCGETVIFCNT, WRITE, struct_sioc_vif_req_sz);
     94 #endif
     95 
     96 #if SANITIZER_LINUX
     97   // Conflicting request ids.
     98   // _(CDROMAUDIOBUFSIZ, NONE, 0);
     99   // _(SNDCTL_TMR_CONTINUE, NONE, 0);
    100   // _(SNDCTL_TMR_START, NONE, 0);
    101   // _(SNDCTL_TMR_STOP, NONE, 0);
    102   // _(SOUND_MIXER_READ_LOUD, WRITE, sizeof(int)); // same as ...READ_ENHANCE
    103   // _(SOUND_MIXER_READ_MUTE, WRITE, sizeof(int)); // same as ...READ_ENHANCE
    104   // _(SOUND_MIXER_WRITE_LOUD, WRITE, sizeof(int)); // same as ...WRITE_ENHANCE
    105   // _(SOUND_MIXER_WRITE_MUTE, WRITE, sizeof(int)); // same as ...WRITE_ENHANCE
    106   _(BLKFLSBUF, NONE, 0);
    107   _(BLKGETSIZE, WRITE, sizeof(uptr));
    108   _(BLKRAGET, WRITE, sizeof(int));
    109   _(BLKRASET, NONE, 0);
    110   _(BLKROGET, WRITE, sizeof(int));
    111   _(BLKROSET, READ, sizeof(int));
    112   _(BLKRRPART, NONE, 0);
    113   _(CDROMEJECT, NONE, 0);
    114   _(CDROMEJECT_SW, NONE, 0);
    115   _(CDROMMULTISESSION, WRITE, struct_cdrom_multisession_sz);
    116   _(CDROMPAUSE, NONE, 0);
    117   _(CDROMPLAYMSF, READ, struct_cdrom_msf_sz);
    118   _(CDROMPLAYTRKIND, READ, struct_cdrom_ti_sz);
    119   _(CDROMREADAUDIO, READ, struct_cdrom_read_audio_sz);
    120   _(CDROMREADCOOKED, READ, struct_cdrom_msf_sz);
    121   _(CDROMREADMODE1, READ, struct_cdrom_msf_sz);
    122   _(CDROMREADMODE2, READ, struct_cdrom_msf_sz);
    123   _(CDROMREADRAW, READ, struct_cdrom_msf_sz);
    124   _(CDROMREADTOCENTRY, WRITE, struct_cdrom_tocentry_sz);
    125   _(CDROMREADTOCHDR, WRITE, struct_cdrom_tochdr_sz);
    126   _(CDROMRESET, NONE, 0);
    127   _(CDROMRESUME, NONE, 0);
    128   _(CDROMSEEK, READ, struct_cdrom_msf_sz);
    129   _(CDROMSTART, NONE, 0);
    130   _(CDROMSTOP, NONE, 0);
    131   _(CDROMSUBCHNL, WRITE, struct_cdrom_subchnl_sz);
    132   _(CDROMVOLCTRL, READ, struct_cdrom_volctrl_sz);
    133   _(CDROMVOLREAD, WRITE, struct_cdrom_volctrl_sz);
    134   _(CDROM_GET_UPC, WRITE, 8);
    135   _(EVIOCGABS, WRITE, struct_input_absinfo_sz); // fixup
    136   _(EVIOCGBIT, WRITE, struct_input_id_sz); // fixup
    137   _(EVIOCGEFFECTS, WRITE, sizeof(int));
    138   _(EVIOCGID, WRITE, struct_input_id_sz);
    139   _(EVIOCGKEY, WRITE, 0);
    140   _(EVIOCGKEYCODE, WRITE, sizeof(int) * 2);
    141   _(EVIOCGLED, WRITE, 0);
    142   _(EVIOCGNAME, WRITE, 0);
    143   _(EVIOCGPHYS, WRITE, 0);
    144   _(EVIOCGRAB, READ, sizeof(int));
    145   _(EVIOCGREP, WRITE, sizeof(int) * 2);
    146   _(EVIOCGSND, WRITE, 0);
    147   _(EVIOCGSW, WRITE, 0);
    148   _(EVIOCGUNIQ, WRITE, 0);
    149   _(EVIOCGVERSION, WRITE, sizeof(int));
    150   _(EVIOCRMFF, READ, sizeof(int));
    151   _(EVIOCSABS, READ, struct_input_absinfo_sz); // fixup
    152   _(EVIOCSFF, READ, struct_ff_effect_sz);
    153   _(EVIOCSKEYCODE, READ, sizeof(int) * 2);
    154   _(EVIOCSREP, READ, sizeof(int) * 2);
    155   _(FDCLRPRM, NONE, 0);
    156   _(FDDEFPRM, READ, struct_floppy_struct_sz);
    157   _(FDFLUSH, NONE, 0);
    158   _(FDFMTBEG, NONE, 0);
    159   _(FDFMTEND, NONE, 0);
    160   _(FDFMTTRK, READ, struct_format_descr_sz);
    161   _(FDGETDRVPRM, WRITE, struct_floppy_drive_params_sz);
    162   _(FDGETDRVSTAT, WRITE, struct_floppy_drive_struct_sz);
    163   _(FDGETDRVTYP, WRITE, 16);
    164   _(FDGETFDCSTAT, WRITE, struct_floppy_fdc_state_sz);
    165   _(FDGETMAXERRS, WRITE, struct_floppy_max_errors_sz);
    166   _(FDGETPRM, WRITE, struct_floppy_struct_sz);
    167   _(FDMSGOFF, NONE, 0);
    168   _(FDMSGON, NONE, 0);
    169   _(FDPOLLDRVSTAT, WRITE, struct_floppy_drive_struct_sz);
    170   _(FDRAWCMD, WRITE, struct_floppy_raw_cmd_sz);
    171   _(FDRESET, NONE, 0);
    172   _(FDSETDRVPRM, READ, struct_floppy_drive_params_sz);
    173   _(FDSETEMSGTRESH, NONE, 0);
    174   _(FDSETMAXERRS, READ, struct_floppy_max_errors_sz);
    175   _(FDSETPRM, READ, struct_floppy_struct_sz);
    176   _(FDTWADDLE, NONE, 0);
    177   _(FDWERRORCLR, NONE, 0);
    178   _(FDWERRORGET, WRITE, struct_floppy_write_errors_sz);
    179   _(HDIO_DRIVE_CMD, WRITE, sizeof(int));
    180   _(HDIO_GETGEO, WRITE, struct_hd_geometry_sz);
    181   _(HDIO_GET_32BIT, WRITE, sizeof(int));
    182   _(HDIO_GET_DMA, WRITE, sizeof(int));
    183   _(HDIO_GET_IDENTITY, WRITE, struct_hd_driveid_sz);
    184   _(HDIO_GET_KEEPSETTINGS, WRITE, sizeof(int));
    185   _(HDIO_GET_MULTCOUNT, WRITE, sizeof(int));
    186   _(HDIO_GET_NOWERR, WRITE, sizeof(int));
    187   _(HDIO_GET_UNMASKINTR, WRITE, sizeof(int));
    188   _(HDIO_SET_32BIT, NONE, 0);
    189   _(HDIO_SET_DMA, NONE, 0);
    190   _(HDIO_SET_KEEPSETTINGS, NONE, 0);
    191   _(HDIO_SET_MULTCOUNT, NONE, 0);
    192   _(HDIO_SET_NOWERR, NONE, 0);
    193   _(HDIO_SET_UNMASKINTR, NONE, 0);
    194   _(MTIOCGET, WRITE, struct_mtget_sz);
    195   _(MTIOCPOS, WRITE, struct_mtpos_sz);
    196   _(MTIOCTOP, READ, struct_mtop_sz);
    197   _(PPPIOCGASYNCMAP, WRITE, sizeof(int));
    198   _(PPPIOCGDEBUG, WRITE, sizeof(int));
    199   _(PPPIOCGFLAGS, WRITE, sizeof(int));
    200   _(PPPIOCGUNIT, WRITE, sizeof(int));
    201   _(PPPIOCGXASYNCMAP, WRITE, sizeof(int) * 8);
    202   _(PPPIOCSASYNCMAP, READ, sizeof(int));
    203   _(PPPIOCSDEBUG, READ, sizeof(int));
    204   _(PPPIOCSFLAGS, READ, sizeof(int));
    205   _(PPPIOCSMAXCID, READ, sizeof(int));
    206   _(PPPIOCSMRU, READ, sizeof(int));
    207   _(PPPIOCSXASYNCMAP, READ, sizeof(int) * 8);
    208   _(SIOCADDRT, READ, struct_rtentry_sz);
    209   _(SIOCDARP, READ, struct_arpreq_sz);
    210   _(SIOCDELRT, READ, struct_rtentry_sz);
    211   _(SIOCDRARP, READ, struct_arpreq_sz);
    212   _(SIOCGARP, WRITE, struct_arpreq_sz);
    213   _(SIOCGIFENCAP, WRITE, sizeof(int));
    214   _(SIOCGIFHWADDR, WRITE, struct_ifreq_sz);
    215   _(SIOCGIFMAP, WRITE, struct_ifreq_sz);
    216   _(SIOCGIFMEM, WRITE, struct_ifreq_sz);
    217   _(SIOCGIFNAME, NONE, 0);
    218   _(SIOCGIFSLAVE, NONE, 0);
    219   _(SIOCGRARP, WRITE, struct_arpreq_sz);
    220   _(SIOCGSTAMP, WRITE, timeval_sz);
    221   _(SIOCSARP, READ, struct_arpreq_sz);
    222   _(SIOCSIFENCAP, READ, sizeof(int));
    223   _(SIOCSIFHWADDR, READ, struct_ifreq_sz);
    224   _(SIOCSIFLINK, NONE, 0);
    225   _(SIOCSIFMAP, READ, struct_ifreq_sz);
    226   _(SIOCSIFMEM, READ, struct_ifreq_sz);
    227   _(SIOCSIFSLAVE, NONE, 0);
    228   _(SIOCSRARP, READ, struct_arpreq_sz);
    229   _(SNDCTL_COPR_HALT, WRITE, struct_copr_debug_buf_sz);
    230   _(SNDCTL_COPR_LOAD, READ, struct_copr_buffer_sz);
    231   _(SNDCTL_COPR_RCODE, WRITE, struct_copr_debug_buf_sz);
    232   _(SNDCTL_COPR_RCVMSG, WRITE, struct_copr_msg_sz);
    233   _(SNDCTL_COPR_RDATA, WRITE, struct_copr_debug_buf_sz);
    234   _(SNDCTL_COPR_RESET, NONE, 0);
    235   _(SNDCTL_COPR_RUN, WRITE, struct_copr_debug_buf_sz);
    236   _(SNDCTL_COPR_SENDMSG, READ, struct_copr_msg_sz);
    237   _(SNDCTL_COPR_WCODE, READ, struct_copr_debug_buf_sz);
    238   _(SNDCTL_COPR_WDATA, READ, struct_copr_debug_buf_sz);
    239   _(SNDCTL_DSP_GETBLKSIZE, WRITE, sizeof(int));
    240   _(SNDCTL_DSP_GETFMTS, WRITE, sizeof(int));
    241   _(SNDCTL_DSP_NONBLOCK, NONE, 0);
    242   _(SNDCTL_DSP_POST, NONE, 0);
    243   _(SNDCTL_DSP_RESET, NONE, 0);
    244   _(SNDCTL_DSP_SETFMT, WRITE, sizeof(int));
    245   _(SNDCTL_DSP_SETFRAGMENT, WRITE, sizeof(int));
    246   _(SNDCTL_DSP_SPEED, WRITE, sizeof(int));
    247   _(SNDCTL_DSP_STEREO, WRITE, sizeof(int));
    248   _(SNDCTL_DSP_SUBDIVIDE, WRITE, sizeof(int));
    249   _(SNDCTL_DSP_SYNC, NONE, 0);
    250   _(SNDCTL_FM_4OP_ENABLE, READ, sizeof(int));
    251   _(SNDCTL_FM_LOAD_INSTR, READ, struct_sbi_instrument_sz);
    252   _(SNDCTL_MIDI_INFO, WRITE, struct_midi_info_sz);
    253   _(SNDCTL_MIDI_PRETIME, WRITE, sizeof(int));
    254   _(SNDCTL_SEQ_CTRLRATE, WRITE, sizeof(int));
    255   _(SNDCTL_SEQ_GETINCOUNT, WRITE, sizeof(int));
    256   _(SNDCTL_SEQ_GETOUTCOUNT, WRITE, sizeof(int));
    257   _(SNDCTL_SEQ_NRMIDIS, WRITE, sizeof(int));
    258   _(SNDCTL_SEQ_NRSYNTHS, WRITE, sizeof(int));
    259   _(SNDCTL_SEQ_OUTOFBAND, READ, struct_seq_event_rec_sz);
    260   _(SNDCTL_SEQ_PANIC, NONE, 0);
    261   _(SNDCTL_SEQ_PERCMODE, NONE, 0);
    262   _(SNDCTL_SEQ_RESET, NONE, 0);
    263   _(SNDCTL_SEQ_RESETSAMPLES, READ, sizeof(int));
    264   _(SNDCTL_SEQ_SYNC, NONE, 0);
    265   _(SNDCTL_SEQ_TESTMIDI, READ, sizeof(int));
    266   _(SNDCTL_SEQ_THRESHOLD, READ, sizeof(int));
    267   _(SNDCTL_SYNTH_INFO, WRITE, struct_synth_info_sz);
    268   _(SNDCTL_SYNTH_MEMAVL, WRITE, sizeof(int));
    269   _(SNDCTL_TMR_METRONOME, READ, sizeof(int));
    270   _(SNDCTL_TMR_SELECT, WRITE, sizeof(int));
    271   _(SNDCTL_TMR_SOURCE, WRITE, sizeof(int));
    272   _(SNDCTL_TMR_TEMPO, WRITE, sizeof(int));
    273   _(SNDCTL_TMR_TIMEBASE, WRITE, sizeof(int));
    274   _(SOUND_MIXER_READ_ALTPCM, WRITE, sizeof(int));
    275   _(SOUND_MIXER_READ_BASS, WRITE, sizeof(int));
    276   _(SOUND_MIXER_READ_CAPS, WRITE, sizeof(int));
    277   _(SOUND_MIXER_READ_CD, WRITE, sizeof(int));
    278   _(SOUND_MIXER_READ_DEVMASK, WRITE, sizeof(int));
    279   _(SOUND_MIXER_READ_ENHANCE, WRITE, sizeof(int));
    280   _(SOUND_MIXER_READ_IGAIN, WRITE, sizeof(int));
    281   _(SOUND_MIXER_READ_IMIX, WRITE, sizeof(int));
    282   _(SOUND_MIXER_READ_LINE, WRITE, sizeof(int));
    283   _(SOUND_MIXER_READ_LINE1, WRITE, sizeof(int));
    284   _(SOUND_MIXER_READ_LINE2, WRITE, sizeof(int));
    285   _(SOUND_MIXER_READ_LINE3, WRITE, sizeof(int));
    286   _(SOUND_MIXER_READ_MIC, WRITE, sizeof(int));
    287   _(SOUND_MIXER_READ_OGAIN, WRITE, sizeof(int));
    288   _(SOUND_MIXER_READ_PCM, WRITE, sizeof(int));
    289   _(SOUND_MIXER_READ_RECLEV, WRITE, sizeof(int));
    290   _(SOUND_MIXER_READ_RECMASK, WRITE, sizeof(int));
    291   _(SOUND_MIXER_READ_RECSRC, WRITE, sizeof(int));
    292   _(SOUND_MIXER_READ_SPEAKER, WRITE, sizeof(int));
    293   _(SOUND_MIXER_READ_STEREODEVS, WRITE, sizeof(int));
    294   _(SOUND_MIXER_READ_SYNTH, WRITE, sizeof(int));
    295   _(SOUND_MIXER_READ_TREBLE, WRITE, sizeof(int));
    296   _(SOUND_MIXER_READ_VOLUME, WRITE, sizeof(int));
    297   _(SOUND_MIXER_WRITE_ALTPCM, WRITE, sizeof(int));
    298   _(SOUND_MIXER_WRITE_BASS, WRITE, sizeof(int));
    299   _(SOUND_MIXER_WRITE_CD, WRITE, sizeof(int));
    300   _(SOUND_MIXER_WRITE_ENHANCE, WRITE, sizeof(int));
    301   _(SOUND_MIXER_WRITE_IGAIN, WRITE, sizeof(int));
    302   _(SOUND_MIXER_WRITE_IMIX, WRITE, sizeof(int));
    303   _(SOUND_MIXER_WRITE_LINE, WRITE, sizeof(int));
    304   _(SOUND_MIXER_WRITE_LINE1, WRITE, sizeof(int));
    305   _(SOUND_MIXER_WRITE_LINE2, WRITE, sizeof(int));
    306   _(SOUND_MIXER_WRITE_LINE3, WRITE, sizeof(int));
    307   _(SOUND_MIXER_WRITE_MIC, WRITE, sizeof(int));
    308   _(SOUND_MIXER_WRITE_OGAIN, WRITE, sizeof(int));
    309   _(SOUND_MIXER_WRITE_PCM, WRITE, sizeof(int));
    310   _(SOUND_MIXER_WRITE_RECLEV, WRITE, sizeof(int));
    311   _(SOUND_MIXER_WRITE_RECSRC, WRITE, sizeof(int));
    312   _(SOUND_MIXER_WRITE_SPEAKER, WRITE, sizeof(int));
    313   _(SOUND_MIXER_WRITE_SYNTH, WRITE, sizeof(int));
    314   _(SOUND_MIXER_WRITE_TREBLE, WRITE, sizeof(int));
    315   _(SOUND_MIXER_WRITE_VOLUME, WRITE, sizeof(int));
    316   _(SOUND_PCM_READ_BITS, WRITE, sizeof(int));
    317   _(SOUND_PCM_READ_CHANNELS, WRITE, sizeof(int));
    318   _(SOUND_PCM_READ_FILTER, WRITE, sizeof(int));
    319   _(SOUND_PCM_READ_RATE, WRITE, sizeof(int));
    320   _(SOUND_PCM_WRITE_CHANNELS, WRITE, sizeof(int));
    321   _(SOUND_PCM_WRITE_FILTER, WRITE, sizeof(int));
    322   _(TCFLSH, NONE, 0);
    323   _(TCGETA, WRITE, struct_termio_sz);
    324   _(TCGETS, WRITE, struct_termios_sz);
    325   _(TCSBRK, NONE, 0);
    326   _(TCSBRKP, NONE, 0);
    327   _(TCSETA, READ, struct_termio_sz);
    328   _(TCSETAF, READ, struct_termio_sz);
    329   _(TCSETAW, READ, struct_termio_sz);
    330   _(TCSETS, READ, struct_termios_sz);
    331   _(TCSETSF, READ, struct_termios_sz);
    332   _(TCSETSW, READ, struct_termios_sz);
    333   _(TCXONC, NONE, 0);
    334   _(TIOCGLCKTRMIOS, WRITE, struct_termios_sz);
    335   _(TIOCGSOFTCAR, WRITE, sizeof(int));
    336   _(TIOCINQ, WRITE, sizeof(int));
    337   _(TIOCLINUX, READ, sizeof(char));
    338   _(TIOCSERCONFIG, NONE, 0);
    339   _(TIOCSERGETLSR, WRITE, sizeof(int));
    340   _(TIOCSERGWILD, WRITE, sizeof(int));
    341   _(TIOCSERSWILD, READ, sizeof(int));
    342   _(TIOCSLCKTRMIOS, READ, struct_termios_sz);
    343   _(TIOCSSOFTCAR, READ, sizeof(int));
    344   _(VT_ACTIVATE, NONE, 0);
    345   _(VT_DISALLOCATE, NONE, 0);
    346   _(VT_GETMODE, WRITE, struct_vt_mode_sz);
    347   _(VT_GETSTATE, WRITE, struct_vt_stat_sz);
    348   _(VT_OPENQRY, WRITE, sizeof(int));
    349   _(VT_RELDISP, NONE, 0);
    350   _(VT_RESIZE, READ, struct_vt_sizes_sz);
    351   _(VT_RESIZEX, READ, struct_vt_consize_sz);
    352   _(VT_SENDSIG, NONE, 0);
    353   _(VT_SETMODE, READ, struct_vt_mode_sz);
    354   _(VT_WAITACTIVE, NONE, 0);
    355 #endif
    356 
    357 #if SANITIZER_LINUX && !SANITIZER_ANDROID
    358   // _(SIOCDEVPLIP, WRITE, struct_ifreq_sz); // the same as EQL_ENSLAVE
    359   _(CYGETDEFTHRESH, WRITE, sizeof(int));
    360   _(CYGETDEFTIMEOUT, WRITE, sizeof(int));
    361   _(CYGETMON, WRITE, struct_cyclades_monitor_sz);
    362   _(CYGETTHRESH, WRITE, sizeof(int));
    363   _(CYGETTIMEOUT, WRITE, sizeof(int));
    364   _(CYSETDEFTHRESH, NONE, 0);
    365   _(CYSETDEFTIMEOUT, NONE, 0);
    366   _(CYSETTHRESH, NONE, 0);
    367   _(CYSETTIMEOUT, NONE, 0);
    368   _(EQL_EMANCIPATE, WRITE, struct_ifreq_sz);
    369   _(EQL_ENSLAVE, WRITE, struct_ifreq_sz);
    370   _(EQL_GETMASTRCFG, WRITE, struct_ifreq_sz);
    371   _(EQL_GETSLAVECFG, WRITE, struct_ifreq_sz);
    372   _(EQL_SETMASTRCFG, WRITE, struct_ifreq_sz);
    373   _(EQL_SETSLAVECFG, WRITE, struct_ifreq_sz);
    374   _(EVIOCGKEYCODE_V2, WRITE, struct_input_keymap_entry_sz);
    375   _(EVIOCGPROP, WRITE, 0);
    376   _(EVIOCSKEYCODE_V2, READ, struct_input_keymap_entry_sz);
    377   _(FS_IOC_GETFLAGS, WRITE, sizeof(int));
    378   _(FS_IOC_GETVERSION, WRITE, sizeof(int));
    379   _(FS_IOC_SETFLAGS, READ, sizeof(int));
    380   _(FS_IOC_SETVERSION, READ, sizeof(int));
    381   _(GIO_CMAP, WRITE, 48);
    382   _(GIO_FONT, WRITE, 8192);
    383   _(GIO_SCRNMAP, WRITE, e_tabsz);
    384   _(GIO_UNIMAP, WRITE, struct_unimapdesc_sz);
    385   _(GIO_UNISCRNMAP, WRITE, sizeof(short) * e_tabsz);
    386   _(KDADDIO, NONE, 0);
    387   _(KDDELIO, NONE, 0);
    388   _(KDDISABIO, NONE, 0);
    389   _(KDENABIO, NONE, 0);
    390   _(KDGETKEYCODE, WRITE, struct_kbkeycode_sz);
    391   _(KDGETLED, WRITE, 1);
    392   _(KDGETMODE, WRITE, sizeof(int));
    393   _(KDGKBDIACR, WRITE, struct_kbdiacrs_sz);
    394   _(KDGKBENT, WRITE, struct_kbentry_sz);
    395   _(KDGKBLED, WRITE, sizeof(int));
    396   _(KDGKBMETA, WRITE, sizeof(int));
    397   _(KDGKBMODE, WRITE, sizeof(int));
    398   _(KDGKBSENT, WRITE, struct_kbsentry_sz);
    399   _(KDGKBTYPE, WRITE, 1);
    400   _(KDMAPDISP, NONE, 0);
    401   _(KDMKTONE, NONE, 0);
    402   _(KDSETKEYCODE, READ, struct_kbkeycode_sz);
    403   _(KDSETLED, NONE, 0);
    404   _(KDSETMODE, NONE, 0);
    405   _(KDSIGACCEPT, NONE, 0);
    406   _(KDSKBDIACR, READ, struct_kbdiacrs_sz);
    407   _(KDSKBENT, READ, struct_kbentry_sz);
    408   _(KDSKBLED, NONE, 0);
    409   _(KDSKBMETA, NONE, 0);
    410   _(KDSKBMODE, NONE, 0);
    411   _(KDSKBSENT, READ, struct_kbsentry_sz);
    412   _(KDUNMAPDISP, NONE, 0);
    413   _(KIOCSOUND, NONE, 0);
    414   _(LPABORT, NONE, 0);
    415   _(LPABORTOPEN, NONE, 0);
    416   _(LPCAREFUL, NONE, 0);
    417   _(LPCHAR, NONE, 0);
    418   _(LPGETIRQ, WRITE, sizeof(int));
    419   _(LPGETSTATUS, WRITE, sizeof(int));
    420   _(LPRESET, NONE, 0);
    421   _(LPSETIRQ, NONE, 0);
    422   _(LPTIME, NONE, 0);
    423   _(LPWAIT, NONE, 0);
    424   _(MTIOCGETCONFIG, WRITE, struct_mtconfiginfo_sz);
    425   _(MTIOCSETCONFIG, READ, struct_mtconfiginfo_sz);
    426   _(PIO_CMAP, NONE, 0);
    427   _(PIO_FONT, READ, 8192);
    428   _(PIO_SCRNMAP, READ, e_tabsz);
    429   _(PIO_UNIMAP, READ, struct_unimapdesc_sz);
    430   _(PIO_UNIMAPCLR, READ, struct_unimapinit_sz);
    431   _(PIO_UNISCRNMAP, READ, sizeof(short) * e_tabsz);
    432   _(SCSI_IOCTL_PROBE_HOST, READ, sizeof(int));
    433   _(SCSI_IOCTL_TAGGED_DISABLE, NONE, 0);
    434   _(SCSI_IOCTL_TAGGED_ENABLE, NONE, 0);
    435   _(SNDCTL_DSP_GETISPACE, WRITE, struct_audio_buf_info_sz);
    436   _(SNDCTL_DSP_GETOSPACE, WRITE, struct_audio_buf_info_sz);
    437   _(TIOCGSERIAL, WRITE, struct_serial_struct_sz);
    438   _(TIOCSERGETMULTI, WRITE, struct_serial_multiport_struct_sz);
    439   _(TIOCSERSETMULTI, READ, struct_serial_multiport_struct_sz);
    440   _(TIOCSSERIAL, READ, struct_serial_struct_sz);
    441 
    442   // The following ioctl requests are shared between AX25, IPX, netrom and
    443   // mrouted.
    444   // _(SIOCAIPXITFCRT, READ, sizeof(char));
    445   // _(SIOCAX25GETUID, READ, struct_sockaddr_ax25_sz);
    446   // _(SIOCNRGETPARMS, WRITE, struct_nr_parms_struct_sz);
    447   // _(SIOCAIPXPRISLT, READ, sizeof(char));
    448   // _(SIOCNRSETPARMS, READ, struct_nr_parms_struct_sz);
    449   // _(SIOCAX25ADDUID, READ, struct_sockaddr_ax25_sz);
    450   // _(SIOCNRDECOBS, NONE, 0);
    451   // _(SIOCAX25DELUID, READ, struct_sockaddr_ax25_sz);
    452   // _(SIOCIPXCFGDATA, WRITE, struct_ipx_config_data_sz);
    453   // _(SIOCAX25NOUID, READ, sizeof(int));
    454   // _(SIOCNRRTCTL, READ, sizeof(int));
    455   // _(SIOCAX25DIGCTL, READ, sizeof(int));
    456   // _(SIOCAX25GETPARMS, WRITE, struct_ax25_parms_struct_sz);
    457   // _(SIOCAX25SETPARMS, READ, struct_ax25_parms_struct_sz);
    458 #endif
    459 #undef _
    460 }
    461 
    462 static bool ioctl_initialized = false;
    463 
    464 struct ioctl_desc_compare {
    465   bool operator()(const ioctl_desc& left, const ioctl_desc& right) const {
    466     return left.req < right.req;
    467   }
    468 };
    469 
    470 static void ioctl_init() {
    471   ioctl_table_fill();
    472   InternalSort(&ioctl_table, ioctl_table_size, ioctl_desc_compare());
    473 
    474   bool bad = false;
    475   for (unsigned i = 0; i < ioctl_table_size - 1; ++i) {
    476     if (ioctl_table[i].req >= ioctl_table[i + 1].req) {
    477       Printf("Duplicate or unsorted ioctl request id %x >= %x (%s vs %s)\n",
    478              ioctl_table[i].req, ioctl_table[i + 1].req, ioctl_table[i].name,
    479              ioctl_table[i + 1].name);
    480       bad = true;
    481     }
    482   }
    483 
    484   if (bad) Die();
    485 
    486   ioctl_initialized = true;
    487 }
    488 
    489 // Handle the most evil ioctls that encode argument value as part of request id.
    490 static unsigned ioctl_request_fixup(unsigned req) {
    491 #if SANITIZER_LINUX
    492   if ((req & ~0x3fff001fU) == IOCTL_EVIOCGBIT)
    493     return IOCTL_EVIOCGBIT;
    494   if ((req & ~0x3fU) == IOCTL_EVIOCGABS)
    495     return IOCTL_EVIOCGABS;
    496   if ((req & ~0x3fU) == IOCTL_EVIOCSABS)
    497     return IOCTL_EVIOCSABS;
    498 #endif
    499   return req;
    500 }
    501 
    502 static const ioctl_desc *ioctl_table_lookup(unsigned req) {
    503   int left = 0;
    504   int right = ioctl_table_size;
    505   while (left < right) {
    506     int mid = (left + right) / 2;
    507     if (ioctl_table[mid].req < req)
    508       left = mid + 1;
    509     else
    510       right = mid;
    511   }
    512   if (left == right && ioctl_table[left].req == req)
    513     return ioctl_table + left;
    514   else
    515     return 0;
    516 }
    517 
    518 static const ioctl_desc *ioctl_lookup(unsigned req) {
    519   req = ioctl_request_fixup(req);
    520   const ioctl_desc *desc = ioctl_table_lookup(req);
    521   if (desc) return desc;
    522 
    523   // Try stripping access size from the request id.
    524   desc = ioctl_table_lookup(req & ~0x3fff0000U);
    525   // Sanity check: requests that encode access size are either read or write and
    526   // have size of 0 in the table.
    527   if (desc && desc->size == 0 &&
    528       (desc->type == ioctl_desc::WRITE || desc->type == ioctl_desc::READ))
    529     return desc;
    530   return 0;
    531 }
    532 
    533 static void ioctl_common_pre(void *ctx, const ioctl_desc *desc, int d,
    534                              unsigned request, void *arg) {
    535   if (desc->type == ioctl_desc::READ) {
    536     unsigned size = desc->size ? desc->size : IOC_SIZE(request);
    537     COMMON_INTERCEPTOR_READ_RANGE(ctx, arg, size);
    538   }
    539   if (desc->type != ioctl_desc::CUSTOM)
    540     return;
    541   switch (request) {
    542     case 0x00008912: {  // SIOCGIFCONF
    543       struct __sanitizer_ifconf *ifc = (__sanitizer_ifconf *)arg;
    544       COMMON_INTERCEPTOR_READ_RANGE(ctx, &ifc->ifc_len, sizeof(ifc->ifc_len));
    545       break;
    546     }
    547   }
    548   return;
    549 }
    550 
    551 static void ioctl_common_post(void *ctx, const ioctl_desc *desc, int res, int d,
    552                               unsigned request, void *arg) {
    553   if (desc->type == ioctl_desc::WRITE) {
    554     // FIXME: add verbose output
    555     unsigned size = desc->size ? desc->size : IOC_SIZE(request);
    556     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, arg, size);
    557   }
    558   if (desc->type != ioctl_desc::CUSTOM)
    559     return;
    560   switch (request) {
    561     case 0x00008912: {  // SIOCGIFCONF
    562       struct __sanitizer_ifconf *ifc = (__sanitizer_ifconf *)arg;
    563       COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ifc->ifc_ifcu.ifcu_req, ifc->ifc_len);
    564       break;
    565     }
    566   }
    567   return;
    568 }
    569