Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bad Command alignment #291

Open
Jan200101 opened this issue Oct 27, 2021 · 0 comments · May be fixed by #292
Open

Bad Command alignment #291

Jan200101 opened this issue Oct 27, 2021 · 0 comments · May be fixed by #292

Comments

@Jan200101
Copy link

lite/src/rencache.c

Lines 95 to 102 in 38bd9b3

static bool next_command(Command **prev) {
if (*prev == NULL) {
*prev = (Command*) command_buf;
} else {
*prev = (Command*) (((char*) *prev) + (*prev)->size);
}
return *prev != ((Command*) (command_buf + command_buf_idx));
}

This function moves prev size along which can and will cause unalignment and potential errors.

On ARM devices this causes a SIGBUS.

The people who helped me figure this out suggested this fix

static Command* push_command(int type, int size) {
+  size_t alignment = alignof(max_align_t) - 1;
+  size = (size + alignment) & ~alignment; // forward align to 4
  Command *cmd = (Command*) (command_buf + command_buf_idx);
  int n = command_buf_idx + size;
  if (n > COMMAND_BUF_SIZE) {
    fprintf(stderr, "Warning: (" __FILE__ "): exhausted command buffer\n");
    return NULL;
  }
  command_buf_idx = n;
  memset(cmd, 0, sizeof(Command));
  cmd->type = type;
  cmd->size = size;
  return cmd;
}

But alignof and max_align_t have been introduced with C11 so it might be of interest to replace the alignof with a static alignment of either 4 or 16 to be safe

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant