Verified Commit 8248c889 authored by Jeremy Soller's avatar Jeremy Soller
Browse files

Require alignment of fmaps

parent 5163f962
Pipeline #2824 passed with stage
in 1 minute and 19 seconds
This diff is collapsed.
......@@ -7,6 +7,7 @@ version = "0.1.0"
[dependencies]
failure = "0.1.1"
libc = "0.2.48"
orbclient = "0.3"
orbimage = "0.1"
redox_syscall = "0.1"
......
use libc;
use orbclient::{Color, Mode, Renderer};
use orbimage;
use std::{cmp, mem, ptr};
use std::{cmp, mem, ptr, slice};
use std::cell::Cell;
use std::path::Path;
......@@ -257,3 +258,82 @@ impl Renderer for Image {
true
}
}
pub struct ImageAligned {
w: i32,
h: i32,
data: &'static mut [Color],
mode: Cell<Mode>,
}
impl Drop for ImageAligned {
fn drop(&mut self) {
unsafe { libc::free(self.data.as_mut_ptr() as *mut libc::c_void); }
}
}
impl ImageAligned {
pub unsafe fn new(width: i32, height: i32, align: usize) -> ImageAligned {
let size = (width * height) as usize;
let size_bytes = size * mem::size_of::<Color>();
let size_alignments = (size_bytes + align - 1) / align;
let size_aligned = size_alignments * align;
let ptr = libc::memalign(align, size_aligned);
libc::memset(ptr, 0, size_aligned);
ImageAligned {
w: width,
h: height,
data: slice::from_raw_parts_mut(
ptr as *mut Color,
size_aligned / mem::size_of::<Color>()
),
mode: Cell::new(Mode::Blend),
}
}
pub fn width(&self) -> i32 {
self.w
}
pub fn height(&self) -> i32 {
self.h
}
pub fn roi(&mut self, rect: &Rect) -> ImageRoi {
ImageRoi {
rect: *rect,
w: self.w,
data: &mut self.data
}
}
}
impl Renderer for ImageAligned {
/// Get the width of the image in pixels
fn width(&self) -> u32 {
self.w as u32
}
/// Get the height of the image in pixels
fn height(&self) -> u32 {
self.h as u32
}
/// Return a reference to a slice of colors making up the image
fn data(&self) -> &[Color] {
&self.data
}
/// Return a mutable reference to a slice of colors making up the image
fn data_mut(&mut self) -> &mut [Color] {
&mut self.data
}
fn mode(&self) -> &Cell<Mode> {
&self.mode
}
fn sync(&mut self) -> bool {
true
}
}
#[macro_use] extern crate failure;
extern crate event;
extern crate libc;
extern crate orbclient;
extern crate orbimage;
extern crate syscall;
......@@ -399,9 +400,15 @@ impl<H: Handler> SchemeMut for OrbitalHandler<H> {
.and(Ok(id))
}
fn fmap(&mut self, id: usize, map: &syscall::Map) -> syscall::Result<usize> {
let page_size = 4096;
let map_pages = (map.offset + map.size + page_size - 1)/page_size;
let data = self.handler.handle_window_map(&mut self.orb, id)?;
if map.offset + map.size <= data.len() * 4 {
Ok(data.as_mut_ptr() as usize + map.offset)
let data_addr = data.as_mut_ptr() as usize;
let data_size = data.len() * mem::size_of::<Color>();
// Do not allow leaking data before or after window to the user
println!("addr: {:X}, size: {}", data_addr, data_size);
if data_addr & (page_size - 1) == 0 && map_pages * page_size <= data_size {
Ok(data_addr + map.offset)
} else {
Err(syscall::Error::new(EINVAL))
}
......
......@@ -2,12 +2,13 @@ use orbclient::{Color, Event, Renderer};
use orbfont::Font;
use orbital_core::{
Properties,
image::{Image, ImageRef},
image::{Image, ImageAligned, ImageRef},
rect::Rect,
self
};
use std::cmp::{min, max};
use std::collections::VecDeque;
use std::mem;
use theme::{BAR_COLOR, BAR_HIGHLIGHT_COLOR, TEXT_COLOR, TEXT_HIGHLIGHT_COLOR};
......@@ -29,11 +30,10 @@ pub struct Window {
pub unclosable: bool,
pub zorder: WindowZOrder,
pub max_restore: Option<Rect>,
image: Image,
image: ImageAligned,
title_image: Image,
title_image_unfocused: Image,
pub events: VecDeque<Event>,
pub notified_read: bool
}
......@@ -50,11 +50,10 @@ impl Window {
unclosable: false,
zorder: WindowZOrder::Normal,
max_restore: None,
image: Image::new(w, h),
image: unsafe { ImageAligned::new(w, h, 4096) }, // Ensure that image data is page aligned at beginning and end
title_image: Image::new(0, 0),
title_image_unfocused: Image::new(0, 0),
events: VecDeque::new(),
notified_read: false
}
}
......@@ -228,7 +227,8 @@ impl Window {
}
pub fn set_size(&mut self, w: i32, h: i32) {
let mut new_image = Image::from_color(w, h, Color::rgba(0, 0, 0, 0));
//TODO: Invalidate old mappings
let mut new_image = unsafe { ImageAligned::new(w, h, 4096) };
let new_rect = Rect::new(0, 0, w, h);
let rect = Rect::new(0, 0, self.image.width(), self.image.height());
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment