// SPDX-License-Identifier: GPL-2.0 #define _GNU_SOURCE #include #include #include #include #include #include #include #include #include static int write_free(const char *mod) { char path[256]; int fd; snprintf(path, sizeof(path), "/sys/module/%s/parameters/free", mod); fd = open(path, O_WRONLY); if (fd < 0) { fprintf(stderr, "open %s: %s\n", path, strerror(errno)); return -1; } if (write(fd, "1\n", 2) != 2) fprintf(stderr, "write %s: %s\n", path, strerror(errno)); close(fd); return 0; } static int do_hwdep(void) { struct snd_hwdep_dsp_image img; int fd; fd = open("/dev/snd/hwC0D0", O_RDWR); if (fd < 0) { fprintf(stderr, "open hwdep: %s\n", strerror(errno)); return 1; } memset(&img, 0, sizeof(img)); img.index = 0; img.length = 0; img.image = 0; for (int i = 0; i < 4; i++) { if (ioctl(fd, SNDRV_HWDEP_IOCTL_DSP_LOAD, &img) < 0) fprintf(stderr, "ioctl dsp_load: %s\n", strerror(errno)); } close(fd); printf("HWDEP: ioctl done (max_active should be >= 2)\n"); sleep(1); return 0; } static void *hwdep_thread(void *arg) { int fd = *(int *)arg; struct snd_hwdep_dsp_image img; memset(&img, 0, sizeof(img)); img.index = 0; ioctl(fd, SNDRV_HWDEP_IOCTL_DSP_LOAD, &img); return NULL; } static int do_hwdep_race(void) { pthread_t th[4]; int fd; fd = open("/dev/snd/hwC0D0", O_RDWR); if (fd < 0) { fprintf(stderr, "open hwdep: %s\n", strerror(errno)); return 1; } for (int i = 0; i < 4; i++) pthread_create(&th[i], NULL, hwdep_thread, &fd); for (int i = 0; i < 4; i++) pthread_join(th[i], NULL); close(fd); printf("HWDEP: 4 concurrent DSP_LOAD issued\n"); sleep(1); return 0; } static int do_compress(void) { int fd; fd = open("/dev/snd/comprC0D0", O_WRONLY); if (fd < 0) { fprintf(stderr, "open compress: %s\n", strerror(errno)); return 1; } printf("COMPRESS: fd open, freeing card\n"); write_free("sndcore_compress"); printf("COMPRESS: card free returned, issuing ioctl on stale fd\n"); if (ioctl(fd, 0x4300, 0) < 0) fprintf(stderr, "ioctl stale fd: %s\n", strerror(errno)); printf("COMPRESS: ioctl returned without KASAN report\n"); close(fd); sleep(1); return 0; } static int do_rawmidi(void) { int fd; fd = open("/dev/snd/midiC0D0", O_RDONLY); if (fd < 0) { fprintf(stderr, "open rawmidi: %s\n", strerror(errno)); return 1; } printf("RAWMIDI: fd open, letting flood thread run\n"); sleep(2); printf("RAWMIDI: closing (trigger/cancel/free window)\n"); close(fd); sleep(3); printf("RAWMIDI: close returned, freeing card\n"); write_free("sndcore_rawmidi"); sleep(1); return 0; } int main(int argc, char **argv) { if (argc < 2) { fprintf(stderr, "usage: %s jackfree|compress|rawmidi|hwdep|hwdep-race\n", argv[0]); return 2; } if (!strcmp(argv[1], "jackfree")) return write_free("sndcore_jack"); if (!strcmp(argv[1], "compress")) return do_compress(); if (!strcmp(argv[1], "rawmidi")) return do_rawmidi(); if (!strcmp(argv[1], "hwdep")) return do_hwdep(); if (!strcmp(argv[1], "hwdep-race")) return do_hwdep_race(); fprintf(stderr, "unknown mode %s\n", argv[1]); return 2; }